11
11
import ssl
12
12
import sys
13
13
import tempfile
14
+ import time
14
15
from typing import Dict
15
16
16
17
from kubernetes import client , config
55
56
"rpc_port" : int (pod .metadata .labels ["RPCPort" ]),
56
57
"rpc_user" : "user" ,
57
58
"rpc_password" : pod .metadata .labels ["rpcpassword" ],
59
+ "init_peers" : pod .metadata .annotations ["init_peers" ],
58
60
}
59
61
)
60
62
@@ -82,41 +84,87 @@ def auth_proxy_request(self, method, path, postdata):
82
84
83
85
class LND :
84
86
def __init__ (self , pod_name ):
87
+ self .name = pod_name
85
88
self .conn = http .client .HTTPSConnection (
86
89
host = pod_name , port = 8080 , timeout = 5 , context = INSECURE_CONTEXT
87
90
)
88
91
89
92
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 )
94
103
95
104
def post (self , uri , data ):
96
105
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
110
107
while True :
108
+ attempt += 1
111
109
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
117
134
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 )
120
168
121
169
122
170
class Commander (BitcoinTestFramework ):
@@ -139,6 +187,13 @@ def ensure_miner(node):
139
187
def hex_to_b64 (hex ):
140
188
return base64 .b64encode (bytes .fromhex (hex )).decode ()
141
189
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
+
142
197
def handle_sigterm (self , signum , frame ):
143
198
print ("SIGTERM received, stopping..." )
144
199
self .shutdown ()
@@ -193,6 +248,7 @@ def setup(self):
193
248
coveragedir = self .options .coveragedir ,
194
249
)
195
250
node .rpc_connected = True
251
+ node .init_peers = int (tank ["init_peers" ])
196
252
197
253
self .nodes .append (node )
198
254
self .tanks [tank ["tank" ]] = node
0 commit comments