10
10
from decimal import Decimal
11
11
from pyln .client import LightningRpc
12
12
from pyln .client import Millisatoshi
13
+ from pyln .testing import grpc
13
14
14
15
import ephemeral_port_reserve # type: ignore
15
16
import json
@@ -538,7 +539,15 @@ def getnewaddress(self):
538
539
539
540
540
541
class LightningD (TailableProc ):
541
- def __init__ (self , lightning_dir , bitcoindproxy , port = 9735 , random_hsm = False , node_id = 0 ):
542
+ def __init__ (
543
+ self ,
544
+ lightning_dir ,
545
+ bitcoindproxy ,
546
+ port = 9735 ,
547
+ random_hsm = False ,
548
+ node_id = 0 ,
549
+ grpc_port = None
550
+ ):
542
551
# We handle our own version of verbose, below.
543
552
TailableProc .__init__ (self , lightning_dir , verbose = False )
544
553
self .executable = 'lightningd'
@@ -564,6 +573,9 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
564
573
'bitcoin-datadir' : lightning_dir ,
565
574
}
566
575
576
+ if grpc_port is not None :
577
+ opts ['grpc-port' ] = grpc_port
578
+
567
579
for k , v in opts .items ():
568
580
self .opts [k ] = v
569
581
@@ -693,19 +705,22 @@ def __init__(self, node_id, lightning_dir, bitcoind, executor, valgrind, may_fai
693
705
self .allow_bad_gossip = allow_bad_gossip
694
706
self .allow_warning = allow_warning
695
707
self .db = db
708
+ self .lightning_dir = Path (lightning_dir )
696
709
697
710
# Assume successful exit
698
711
self .rc = 0
699
712
700
- socket_path = os . path . join ( lightning_dir , TEST_NETWORK , "lightning-rpc" ). format ( node_id )
701
- self .rpc = PrettyPrintingLightningRpc ( socket_path , self . executor , jsonschemas = jsonschemas )
713
+ # Ensure we have an RPC we can use to talk to the node
714
+ self ._create_rpc ( jsonschemas )
702
715
703
716
self .gossip_store = GossipStore (Path (lightning_dir , TEST_NETWORK , "gossip_store" ))
704
717
705
718
self .daemon = LightningD (
706
719
lightning_dir , bitcoindproxy = bitcoind .get_proxy (),
707
- port = port , random_hsm = random_hsm , node_id = node_id
720
+ port = port , random_hsm = random_hsm , node_id = node_id ,
721
+ grpc_port = self .grpc_port ,
708
722
)
723
+
709
724
# If we have a disconnect string, dump it to a file for daemon.
710
725
if disconnect :
711
726
self .daemon .disconnect_file = os .path .join (lightning_dir , TEST_NETWORK , "dev_disconnect" )
@@ -751,6 +766,47 @@ def __init__(self, node_id, lightning_dir, bitcoind, executor, valgrind, may_fai
751
766
if SLOW_MACHINE :
752
767
self .daemon .cmd_prefix += ['--read-inline-info=no' ]
753
768
769
+ def _create_rpc (self , jsonschemas ):
770
+ """Prepares anything related to the RPC.
771
+ """
772
+ if os .environ .get ('CLN_TEST_GRPC' ) == '1' :
773
+ logging .info ("Switching to GRPC based RPC for tests" )
774
+ self ._create_grpc_rpc ()
775
+ else :
776
+ self ._create_jsonrpc_rpc (jsonschemas )
777
+
778
+ def _create_grpc_rpc (self ):
779
+ self .grpc_port = reserve_unused_port ()
780
+ d = self .lightning_dir / TEST_NETWORK
781
+ d .mkdir (parents = True , exist_ok = True )
782
+
783
+ # Copy all the certificates and keys into place:
784
+ with (d / "ca.pem" ).open (mode = 'wb' ) as f :
785
+ f .write (grpc .DUMMY_CA_PEM )
786
+
787
+ with (d / "ca-key.pem" ).open (mode = 'wb' ) as f :
788
+ f .write (grpc .DUMMY_CA_KEY_PEM )
789
+
790
+ # Now the node will actually start up and use them, so we can
791
+ # create the RPC instance.
792
+ self .rpc = grpc .LightningGrpc (
793
+ host = 'localhost' ,
794
+ port = self .grpc_port ,
795
+ root_certificates = grpc .DUMMY_CA_PEM ,
796
+ private_key = grpc .DUMMY_CLIENT_KEY_PEM ,
797
+ certificate_chain = grpc .DUMMY_CLIENT_PEM
798
+ )
799
+
800
+ def _create_jsonrpc_rpc (self , jsonschemas ):
801
+ socket_path = self .lightning_dir / TEST_NETWORK / "lightning-rpc"
802
+ self .grpc_port = None
803
+
804
+ self .rpc = PrettyPrintingLightningRpc (
805
+ str (socket_path ),
806
+ self .executor ,
807
+ jsonschemas = jsonschemas
808
+ )
809
+
754
810
def connect (self , remote_node ):
755
811
self .rpc .connect (remote_node .info ['id' ], '127.0.0.1' , remote_node .daemon .port )
756
812
0 commit comments