Skip to content

Commit 708fb17

Browse files
cdeckerShahanaFarooqui
authored andcommitted
pytest: Add helper to get a grpc stub and test decode
1 parent acc3bb2 commit 708fb17

File tree

2 files changed

+56
-41
lines changed

2 files changed

+56
-41
lines changed

contrib/pyln-testing/pyln/testing/utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,39 @@ def _create_jsonrpc_rpc(self, jsonschemas):
851851
jsonschemas=jsonschemas
852852
)
853853

854+
@property
855+
def grpc(self):
856+
"""Tiny helper to return a grpc stub if grpc was configured.
857+
"""
858+
# Before doing anything let's see if we have a grpc-port at all
859+
try:
860+
grpc_port = int(filter(
861+
lambda v: v[0] == 'grpc-port',
862+
self.daemon.opts.items()
863+
).__next__()[1])
864+
except Exception as e:
865+
raise ValueError("grpc-port is not specified, can't connect over grpc")
866+
867+
import grpc
868+
p = Path(self.daemon.lightning_dir) / TEST_NETWORK
869+
cert, key, ca = [f.open('rb').read() for f in [
870+
p / 'client.pem',
871+
p / 'client-key.pem',
872+
p / "ca.pem"]]
873+
creds = grpc.ssl_channel_credentials(
874+
root_certificates=ca,
875+
private_key=key,
876+
certificate_chain=cert,
877+
)
878+
879+
channel = grpc.secure_channel(
880+
f"localhost:{grpc_port}",
881+
creds,
882+
options=(('grpc.ssl_target_name_override', 'cln'),)
883+
)
884+
from pyln.testing import node_pb2_grpc as nodegrpc
885+
return nodegrpc.NodeStub(channel)
886+
854887
def connect(self, remote_node):
855888
self.rpc.connect(remote_node.info['id'], '127.0.0.1', remote_node.port)
856889

tests/test_cln_rs.py

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -303,27 +303,7 @@ def test_grpc_keysend_routehint(bitcoind, node_factory):
303303
bitcoind.generate_block(3)
304304
sync_blockheight(bitcoind, [l1, l2, l3])
305305

306-
def connect(node):
307-
p = Path(node.daemon.lightning_dir) / TEST_NETWORK
308-
cert, key, ca = [f.open('rb').read() for f in [
309-
p / 'client.pem',
310-
p / 'client-key.pem',
311-
p / "ca.pem"]]
312-
313-
creds = grpc.ssl_channel_credentials(
314-
root_certificates=ca,
315-
private_key=key,
316-
certificate_chain=cert,
317-
)
318-
319-
channel = grpc.secure_channel(
320-
f"localhost:{grpc_port}",
321-
creds,
322-
options=(('grpc.ssl_target_name_override', 'cln'),)
323-
)
324-
return nodegrpc.NodeStub(channel)
325-
326-
stub = connect(l1)
306+
stub = l1.grpc
327307
chan = l2.rpc.listpeerchannels(l3.info['id'])
328308

329309
routehint = primitivespb.RoutehintList(hints=[
@@ -362,26 +342,7 @@ def test_grpc_listpeerchannels(bitcoind, node_factory):
362342
announce_channels=True, # Do not enforce scid-alias
363343
)
364344

365-
def connect(node):
366-
p = Path(node.daemon.lightning_dir) / TEST_NETWORK
367-
cert, key, ca = [f.open('rb').read() for f in [
368-
p / 'client.pem',
369-
p / 'client-key.pem',
370-
p / "ca.pem"]]
371-
372-
creds = grpc.ssl_channel_credentials(
373-
root_certificates=ca,
374-
private_key=key,
375-
certificate_chain=cert,
376-
)
377-
378-
channel = grpc.secure_channel(
379-
f"localhost:{grpc_port}",
380-
creds,
381-
options=(('grpc.ssl_target_name_override', 'cln'),)
382-
)
383-
return nodegrpc.NodeStub(channel)
384-
stub = connect(l1)
345+
stub = l1.grpc
385346
res = stub.ListPeerChannels(nodepb.ListpeerchannelsRequest(id=None))
386347

387348
# Way too many fields to check, so just do a couple
@@ -399,3 +360,24 @@ def connect(node):
399360
l1.daemon.wait_for_log(r'onchaind complete, forgetting peer')
400361

401362
stub.ListClosedChannels(nodepb.ListclosedchannelsRequest())
363+
364+
365+
def test_grpc_decode(node_factory):
366+
grpc_port = reserve()
367+
l1 = node_factory.get_node(options={'grpc-port': str(grpc_port)})
368+
inv = l1.grpc.Invoice(nodepb.InvoiceRequest(
369+
amount_msat=primitivespb.AmountOrAny(any=True),
370+
description="desc",
371+
label="label",
372+
))
373+
374+
res = l1.grpc.DecodePay(nodepb.DecodepayRequest(
375+
bolt11=inv.bolt11
376+
))
377+
# If we get here we're good, conversions work
378+
print(res)
379+
380+
res = l1.grpc.Decode(nodepb.DecodeRequest(
381+
string=inv.bolt11
382+
))
383+
print(res)

0 commit comments

Comments
 (0)