pytest: Added dynamic RPC call dispatch

Instead of having to define every single method we can now just call
any method, it'll fail upstream if it does not exist.
This commit is contained in:
Christian Decker 2017-04-12 11:08:48 -07:00 committed by Rusty Russell
parent 6a072c4c6e
commit 4e0be7a48f
2 changed files with 16 additions and 28 deletions

View File

@ -33,6 +33,17 @@ class UnixDomainSocketRpc(object):
# Probably didn't read enough
pass
def __getattr__(self, name):
"""Intercept any call that is not explicitly defined and call _call
We might still want to define the actual methods in the subclasses for
documentation purposes.
"""
name = name.replace('_', '-')
def wrapper(*args, **kwargs):
return self._call(name, args)
return wrapper
def _call(self, method, args):
logging.debug("Calling %s with arguments %r", method, args)
@ -66,11 +77,6 @@ class LightningRpc(UnixDomainSocketRpc):
This implementation is thread safe in that it locks the socket
between calls, but it does not (yet) support concurrent calls.
"""
def connect(self, hostname, port, remote_id):
return self._call("connect", [hostname, port, remote_id])
def getpeers(self):
return self._call("getpeers", [])
def getpeer(self, peer_id):
"""Get info about a specific peer.
@ -81,38 +87,20 @@ class LightningRpc(UnixDomainSocketRpc):
return p
return None
def stop(self):
return self._call("stop", [])
def getlog(self, level=None):
args = []
if level is not None:
args.append(level)
return self._call("getlog", args)
def getinfo(self):
return self._call("getinfo", [])
def dev_add_route(self, src, dst, base, var, delay, minblocks):
"""Add route from {src} to {dst}, {base} rate in msatoshi, {var} rate in msatoshi, {delay} blocks delay and {minblocks} minimum timeout
""" Add a route from src to dst using the given parameters.
Add route from {src} to {dst}, {base} rate in msatoshi, {var} rate in
msatoshi, {delay} blocks delay and {minblocks} minimum timeout
"""
return self._call("dev-add-route", [src, dst, base, var, delay, minblocks])
def getchannels(self):
return self._call("getchannels", [])
def getnodes(self):
return self._call("getnodes", [])
def newaddr(self):
return self._call("newaddr", [])['address']
def addfunds(self, tx):
return self._call("addfunds", [tx])
def fundchannel(self, node_id, capacity):
return self._call("fundchannel", [node_id, capacity])
class LegacyLightningRpc(UnixDomainSocketRpc):
def getchannels(self):

View File

@ -133,7 +133,7 @@ class LightningDTests(BaseLightningDTests):
assert ret['id'] == l2.info['id']
addr = l1.rpc.newaddr()
addr = l1.rpc.newaddr()['address']
txid = l1.bitcoin.rpc.sendtoaddress(addr, 0.02)
tx = l1.bitcoin.rpc.getrawtransaction(txid)