From 708fb17fa2817440fa37764c881ea32d3ecb17c0 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 5 May 2023 16:31:16 +0200 Subject: [PATCH] pytest: Add helper to get a grpc stub and test decode --- contrib/pyln-testing/pyln/testing/utils.py | 33 +++++++++++ tests/test_cln_rs.py | 64 ++++++++-------------- 2 files changed, 56 insertions(+), 41 deletions(-) diff --git a/contrib/pyln-testing/pyln/testing/utils.py b/contrib/pyln-testing/pyln/testing/utils.py index 2d28535ef..15365cf93 100644 --- a/contrib/pyln-testing/pyln/testing/utils.py +++ b/contrib/pyln-testing/pyln/testing/utils.py @@ -851,6 +851,39 @@ class LightningNode(object): jsonschemas=jsonschemas ) + @property + def grpc(self): + """Tiny helper to return a grpc stub if grpc was configured. + """ + # Before doing anything let's see if we have a grpc-port at all + try: + grpc_port = int(filter( + lambda v: v[0] == 'grpc-port', + self.daemon.opts.items() + ).__next__()[1]) + except Exception as e: + raise ValueError("grpc-port is not specified, can't connect over grpc") + + import grpc + p = Path(self.daemon.lightning_dir) / TEST_NETWORK + cert, key, ca = [f.open('rb').read() for f in [ + p / 'client.pem', + p / 'client-key.pem', + p / "ca.pem"]] + creds = grpc.ssl_channel_credentials( + root_certificates=ca, + private_key=key, + certificate_chain=cert, + ) + + channel = grpc.secure_channel( + f"localhost:{grpc_port}", + creds, + options=(('grpc.ssl_target_name_override', 'cln'),) + ) + from pyln.testing import node_pb2_grpc as nodegrpc + return nodegrpc.NodeStub(channel) + def connect(self, remote_node): self.rpc.connect(remote_node.info['id'], '127.0.0.1', remote_node.port) diff --git a/tests/test_cln_rs.py b/tests/test_cln_rs.py index 694c6c8c6..19ccbba0e 100644 --- a/tests/test_cln_rs.py +++ b/tests/test_cln_rs.py @@ -303,27 +303,7 @@ def test_grpc_keysend_routehint(bitcoind, node_factory): bitcoind.generate_block(3) sync_blockheight(bitcoind, [l1, l2, l3]) - def connect(node): - p = Path(node.daemon.lightning_dir) / TEST_NETWORK - cert, key, ca = [f.open('rb').read() for f in [ - p / 'client.pem', - p / 'client-key.pem', - p / "ca.pem"]] - - creds = grpc.ssl_channel_credentials( - root_certificates=ca, - private_key=key, - certificate_chain=cert, - ) - - channel = grpc.secure_channel( - f"localhost:{grpc_port}", - creds, - options=(('grpc.ssl_target_name_override', 'cln'),) - ) - return nodegrpc.NodeStub(channel) - - stub = connect(l1) + stub = l1.grpc chan = l2.rpc.listpeerchannels(l3.info['id']) routehint = primitivespb.RoutehintList(hints=[ @@ -362,26 +342,7 @@ def test_grpc_listpeerchannels(bitcoind, node_factory): announce_channels=True, # Do not enforce scid-alias ) - def connect(node): - p = Path(node.daemon.lightning_dir) / TEST_NETWORK - cert, key, ca = [f.open('rb').read() for f in [ - p / 'client.pem', - p / 'client-key.pem', - p / "ca.pem"]] - - creds = grpc.ssl_channel_credentials( - root_certificates=ca, - private_key=key, - certificate_chain=cert, - ) - - channel = grpc.secure_channel( - f"localhost:{grpc_port}", - creds, - options=(('grpc.ssl_target_name_override', 'cln'),) - ) - return nodegrpc.NodeStub(channel) - stub = connect(l1) + stub = l1.grpc res = stub.ListPeerChannels(nodepb.ListpeerchannelsRequest(id=None)) # Way too many fields to check, so just do a couple @@ -399,3 +360,24 @@ def test_grpc_listpeerchannels(bitcoind, node_factory): l1.daemon.wait_for_log(r'onchaind complete, forgetting peer') stub.ListClosedChannels(nodepb.ListclosedchannelsRequest()) + + +def test_grpc_decode(node_factory): + grpc_port = reserve() + l1 = node_factory.get_node(options={'grpc-port': str(grpc_port)}) + inv = l1.grpc.Invoice(nodepb.InvoiceRequest( + amount_msat=primitivespb.AmountOrAny(any=True), + description="desc", + label="label", + )) + + res = l1.grpc.DecodePay(nodepb.DecodepayRequest( + bolt11=inv.bolt11 + )) + # If we get here we're good, conversions work + print(res) + + res = l1.grpc.Decode(nodepb.DecodeRequest( + string=inv.bolt11 + )) + print(res)