1111import ssl
1212import sys
1313import tempfile
14+ import time
1415from typing import Dict
1516
1617from kubernetes import client , config
5556 "rpc_port" : int (pod .metadata .labels ["RPCPort" ]),
5657 "rpc_user" : "user" ,
5758 "rpc_password" : pod .metadata .labels ["rpcpassword" ],
59+ "init_peers" : pod .metadata .annotations ["init_peers" ],
5860 }
5961 )
6062
@@ -82,41 +84,87 @@ def auth_proxy_request(self, method, path, postdata):
8284
8385class LND :
8486 def __init__ (self , pod_name ):
87+ self .name = pod_name
8588 self .conn = http .client .HTTPSConnection (
8689 host = pod_name , port = 8080 , timeout = 5 , context = INSECURE_CONTEXT
8790 )
8891
8992 def get (self , uri ):
90- self .conn .request (
91- method = "GET" , url = uri , headers = {"Grpc-Metadata-macaroon" : ADMIN_MACAROON_HEX }
92- )
93- return self .conn .getresponse ().read ().decode ("utf8" )
93+ while True :
94+ try :
95+ self .conn .request (
96+ method = "GET" ,
97+ url = uri ,
98+ headers = {"Grpc-Metadata-macaroon" : ADMIN_MACAROON_HEX , "Connection" : "close" },
99+ )
100+ return self .conn .getresponse ().read ().decode ("utf8" )
101+ except Exception :
102+ time .sleep (1 )
94103
95104 def post (self , uri , data ):
96105 body = json .dumps (data )
97- self .conn .request (
98- method = "POST" ,
99- url = uri ,
100- body = body ,
101- headers = {
102- "Content-Type" : "application/json" ,
103- "Content-Length" : str (len (body )),
104- "Grpc-Metadata-macaroon" : ADMIN_MACAROON_HEX ,
105- },
106- )
107- # Stream output, otherwise we get a timeout error
108- res = self .conn .getresponse ()
109- stream = ""
106+ attempt = 0
110107 while True :
108+ attempt += 1
111109 try :
112- data = res .read (1 )
113- if len (data ) == 0 :
114- break
115- else :
116- stream += data .decode ("utf8" )
110+ self .conn .request (
111+ method = "POST" ,
112+ url = uri ,
113+ body = body ,
114+ headers = {
115+ "Content-Type" : "application/json" ,
116+ "Content-Length" : str (len (body )),
117+ "Grpc-Metadata-macaroon" : ADMIN_MACAROON_HEX ,
118+ "Connection" : "close" ,
119+ },
120+ )
121+ # Stream output, otherwise we get a timeout error
122+ res = self .conn .getresponse ()
123+ stream = ""
124+ while True :
125+ try :
126+ data = res .read (1 )
127+ if len (data ) == 0 :
128+ break
129+ else :
130+ stream += data .decode ("utf8" )
131+ except Exception :
132+ break
133+ return stream
117134 except Exception :
118- break
119- return stream
135+ time .sleep (1 )
136+
137+ def newaddress (self ):
138+ res = self .get ("/v1/newaddress" )
139+ return json .loads (res )
140+
141+ def walletbalance (self ):
142+ res = self .get ("/v1/balance/blockchain" )
143+ return int (json .loads (res )["confirmed_balance" ])
144+
145+ def uri (self ):
146+ res = self .get ("/v1/getinfo" )
147+ info = json .loads (res )
148+ if "uris" not in info or len (info ["uris" ]) == 0 :
149+ return None
150+ return info ["uris" ][0 ]
151+
152+ def connect (self , target_uri ):
153+ pk , host = target_uri .split ("@" )
154+ res = self .post ("/v1/peers" , data = {"addr" : {"pubkey" : pk , "host" : host }})
155+ return json .loads (res )
156+
157+ def channel (self , pk , local_amt , push_amt , fee_rate ):
158+ res = self .post (
159+ "/v1/channels/stream" ,
160+ data = {
161+ "local_funding_amount" : local_amt ,
162+ "push_sat" : push_amt ,
163+ "node_pubkey" : pk ,
164+ "sat_per_vbyte" : fee_rate ,
165+ },
166+ )
167+ return json .loads (res )
120168
121169
122170class Commander (BitcoinTestFramework ):
@@ -139,6 +187,13 @@ def ensure_miner(node):
139187 def hex_to_b64 (hex ):
140188 return base64 .b64encode (bytes .fromhex (hex )).decode ()
141189
190+ @staticmethod
191+ def b64_to_hex (b64 , reverse = False ):
192+ if reverse :
193+ return base64 .b64decode (b64 )[::- 1 ].hex ()
194+ else :
195+ return base64 .b64decode (b64 ).hex ()
196+
142197 def handle_sigterm (self , signum , frame ):
143198 print ("SIGTERM received, stopping..." )
144199 self .shutdown ()
@@ -193,6 +248,7 @@ def setup(self):
193248 coveragedir = self .options .coveragedir ,
194249 )
195250 node .rpc_connected = True
251+ node .init_peers = int (tank ["init_peers" ])
196252
197253 self .nodes .append (node )
198254 self .tanks [tank ["tank" ]] = node
0 commit comments