Skip to content

Commit

Permalink
pytest: Separating new lightningd and legacy lightningd RPC client
Browse files Browse the repository at this point in the history
We intend to ultimately no longer use the legacy `daemon/lightningd`
and instead use `lightningd/lightningd`, so I grouped the new RPC and
the legacy RPC and implemented stubs for the new daemon.
  • Loading branch information
cdecker committed Jan 23, 2017
1 parent 091c2fc commit 5f61b3a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion contrib/pylightning/lightning/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .lightning import LightningRpc
from .lightning import LightningRpc, LegacyLightningRpc
70 changes: 53 additions & 17 deletions contrib/pylightning/lightning/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,12 @@
import sys
import threading

class LightningRpc(object):
"""RPC client for the `lightningd` daemon.
This RPC client connects to the `lightningd` daemon through a unix
domain socket and passes calls through. Since some of the calls
are blocking, the corresponding python methods include an `async`
keyword argument. If `async` is set to true then the method
returns a future immediately, instead of blocking indefinitely.
This implementation is thread safe in that it locks the socket
between calls, but it does not (yet) support concurrent calls.
"""
class UnixDomainSocketRpc(object):
def __init__(self, socket_path, executor=None):
self.socket_path = socket_path
self.socket = None
self.buff = b''
self.decoder = json.JSONDecoder()
self.executor = executor

def connect_rpc(self):
pass

def _writeobj(self, sock, obj):
s = json.dumps(obj)
sock.sendall(bytearray(s, 'UTF-8'))
Expand Down Expand Up @@ -66,6 +50,58 @@ def _call(self, method, args):
raise ValueError("Malformed response, 'result' missing.")
return resp['result']


class LightningRpc(UnixDomainSocketRpc):
"""RPC client for the `lightningd` daemon.
This RPC client connects to the `lightningd` daemon through a unix
domain socket and passes calls through. Since some of the calls
are blocking, the corresponding python methods include an `async`
keyword argument. If `async` is set to true then the method
returns a future immediately, instead of blocking indefinitely.
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.
"""
peers = self.getpeers()['peers']
for p in peers:
if p['peerid'] == peer_id:
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
"""
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", [])

class LegacyLightningRpc(UnixDomainSocketRpc):
def getchannels(self):
"""List all known channels.
"""
Expand Down

0 comments on commit 5f61b3a

Please sign in to comment.