Skip to content

Commit 4f79eca

Browse files
committed
ln_init: never fail
1 parent ea66a0b commit 4f79eca

File tree

1 file changed

+92
-74
lines changed

1 file changed

+92
-74
lines changed

resources/scenarios/ln_init.py

Lines changed: 92 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@ def gen(n):
4242
ln_addrs = []
4343

4444
def get_ln_addr(self, ln):
45-
address = None
4645
while True:
47-
success, address = ln.newaddress()
48-
if success:
46+
try:
47+
address = ln.newaddress()
4948
ln_addrs.append(address)
5049
self.log.info(f"Got wallet address {address} from {ln.name}")
5150
break
52-
else:
51+
except Exception as e:
5352
self.log.info(
54-
f"Couldn't get wallet address from {ln.name}, retrying in 5 seconds..."
53+
f"Couldn't get wallet address from {ln.name} because {e}, retrying in 5 seconds..."
5554
)
5655
sleep(5)
5756

@@ -86,13 +85,18 @@ def get_ln_addr(self, ln):
8685
)
8786

8887
def confirm_ln_balance(self, ln):
89-
bal = 0
9088
while True:
91-
bal = ln.walletbalance()
92-
if bal >= (split * 100000000):
93-
self.log.info(f"LN node {ln.name} confirmed funds")
94-
break
95-
sleep(1)
89+
try:
90+
bal = ln.walletbalance()
91+
if bal >= (split * 100000000):
92+
self.log.info(f"LN node {ln.name} confirmed funds")
93+
break
94+
else:
95+
self.log.info(f"Got balance from {ln.name} but less than expected, retrying in 5 seconds...")
96+
sleep(5)
97+
except Exception as e:
98+
self.log.info(f"Couldn't get balance from {ln.name} because {e}, retrying in 5 seconds...")
99+
sleep(5)
96100

97101
fund_threads = [
98102
threading.Thread(target=confirm_ln_balance, args=(self, ln)) for ln in self.lns.values()
@@ -110,14 +114,15 @@ def confirm_ln_balance(self, ln):
110114
ln_uris = {}
111115

112116
def get_ln_uri(self, ln):
113-
uri = None
114117
while True:
115-
uri = ln.uri()
116-
if uri:
118+
try:
119+
uri = ln.uri()
117120
ln_uris[ln.name] = uri
118121
self.log.info(f"LN node {ln.name} has URI {uri}")
119122
break
120-
sleep(1)
123+
except Exception as e:
124+
self.log.info(f"Couldn't get URI from {ln.name} because {e}, retrying in 5 seconds...")
125+
sleep(5)
121126

122127
uri_threads = [
123128
threading.Thread(target=get_ln_uri, args=(self, ln)) for ln in self.lns.values()
@@ -150,28 +155,27 @@ def get_ln_uri(self, ln):
150155

151156
def connect_ln(self, pair):
152157
while True:
153-
res = pair[0].connect(ln_uris[pair[1].name])
154-
if res == {}:
155-
self.log.info(f"Connected LN nodes {pair[0].name} -> {pair[1].name}")
156-
break
157-
if "message" in res:
158-
if "already connected" in res["message"]:
159-
self.log.info(
160-
f"Already connected LN nodes {pair[0].name} -> {pair[1].name}"
161-
)
158+
try:
159+
res = pair[0].connect(ln_uris[pair[1].name])
160+
if res == {}:
161+
self.log.info(f"Connected LN nodes {pair[0].name} -> {pair[1].name}")
162162
break
163-
if "process of starting" in res["message"]:
164-
self.log.info(
165-
f"{pair[0].name} not ready for connections yet, wait and retry..."
166-
)
167-
sleep(1)
168-
else:
169-
self.log.error(
170-
f"Unexpected response attempting to connect {pair[0].name} -> {pair[1].name}:\n {res}\n ABORTING"
171-
)
172-
raise Exception(
173-
f"Unable to connect {pair[0].name} -> {pair[1].name}:\n {res}"
174-
)
163+
if "message" in res:
164+
if "already connected" in res["message"]:
165+
self.log.info(
166+
f"Already connected LN nodes {pair[0].name} -> {pair[1].name}"
167+
)
168+
break
169+
if "process of starting" in res["message"]:
170+
self.log.info(
171+
f"{pair[0].name} not ready for connections yet, wait and retry..."
172+
)
173+
sleep(5)
174+
else:
175+
raise Exception(res)
176+
except Exception as e:
177+
self.log.info(f"Couldn't connect {pair[0].name} -> {pair[1].name} because {e}, retrying in 5 seconds...")
178+
sleep(5)
175179

176180
p2p_threads = [
177181
threading.Thread(target=connect_ln, args=(self, pair)) for pair in connections
@@ -217,26 +221,30 @@ def open_channel(self, ch, fee_rate):
217221
self.log.info(
218222
f"Sending channel open from {ch['source']} -> {ch['target']} with fee_rate={fee_rate}"
219223
)
220-
res = src.channel(
221-
pk=tgt_pk,
222-
capacity=ch["capacity"],
223-
push_amt=ch["push_amt"],
224-
fee_rate=fee_rate,
225-
)
226-
if res and "txid" in res:
227-
ch["txid"] = res["txid"]
228-
self.log.info(
229-
f"Channel open {ch['source']} -> {ch['target']}\n "
230-
+ f"outpoint={res['outpoint']}\n "
231-
+ f"expected channel id: {ch['id']}"
232-
)
233-
else:
234-
ch["txid"] = "N/A"
235-
self.log.info(
236-
"Unexpected channel open response:\n "
237-
+ f"From {ch['source']} -> {ch['target']} fee_rate={fee_rate}\n "
238-
+ f"{res}"
239-
)
224+
while True:
225+
try:
226+
res = src.channel(
227+
pk=tgt_pk,
228+
capacity=ch["capacity"],
229+
push_amt=ch["push_amt"],
230+
fee_rate=fee_rate,
231+
)
232+
ch["txid"] = res["txid"]
233+
self.log.info(
234+
f"Channel open {ch['source']} -> {ch['target']}\n "
235+
+ f"outpoint={res['outpoint']}\n "
236+
+ f"expected channel id: {ch['id']}"
237+
)
238+
break
239+
except Exception as e:
240+
ch["txid"] = "N/A"
241+
self.log.info(
242+
"Couldn't open channel:\n "
243+
+ f"From {ch['source']} -> {ch['target']} fee_rate={fee_rate}\n "
244+
+ f"{e}\n"
245+
+ "Retrying in 5 seconds..."
246+
)
247+
sleep(5)
240248

241249
channels = sorted(ch_by_block[target_block], key=lambda ch: ch["id"]["index"])
242250
index = 0
@@ -279,20 +287,21 @@ def open_channel(self, ch, fee_rate):
279287

280288
def ln_all_chs(self, ln):
281289
expected = len(self.channels)
282-
attempts = 0
283290
actual = 0
284291
while actual != expected:
285-
actual = len(ln.graph()["edges"])
286-
if attempts > 10:
287-
break
288-
attempts += 1
289-
sleep(5)
290-
if actual == expected:
291-
self.log.info(f"LN {ln.name} has graph with all {expected} channels")
292-
else:
293-
self.log.error(
294-
f"LN {ln.name} graph is INCOMPLETE - {actual} of {expected} channels"
295-
)
292+
try:
293+
actual = len(ln.graph()["edges"])
294+
if actual == expected:
295+
self.log.info(f"LN {ln.name} has graph with all {expected} channels")
296+
break
297+
else:
298+
self.log.info(
299+
f"LN {ln.name} graph is incomplete - {actual} of {expected} channels, checking again in 5 seconds..."
300+
)
301+
sleep(5)
302+
except Exception as e:
303+
self.log.info(f"Couldn't check graph from {ln.name} because {e}, retrying in 5 seconds...")
304+
sleep(5)
296305

297306
ch_ann_threads = [
298307
threading.Thread(target=ln_all_chs, args=(self, ln)) for ln in self.lns.values()
@@ -315,12 +324,15 @@ def update_policy(self, ln, txid_hex, policy, capacity):
315324
while res is None:
316325
try:
317326
res = ln.update(txid_hex, policy, capacity)
327+
if len(res["failed_updates"]) != 0:
328+
self.log.info(
329+
f" Failed updates: {res['failed_updates']}\n txid: {txid_hex}\n policy:{policy}\n retrying in 5 seconds..."
330+
)
331+
sleep(5)
318332
break
319-
except Exception:
320-
sleep(1)
321-
assert len(res["failed_updates"]) == 0, (
322-
f" Failed updates: {res['failed_updates']}\n txid: {txid_hex}\n policy:{policy}"
323-
)
333+
except Exception as e:
334+
self.log.info(f"Couldn't update channel policy for {ln.name} because {e}, retrying in 5 seconds...")
335+
sleep(5)
324336

325337
update_threads = []
326338
for ch in self.channels:
@@ -365,7 +377,13 @@ def policy_equal(pol1, pol2, capacity):
365377
def matching_graph(self, expected, ln):
366378
done = False
367379
while not done:
368-
actual = ln.graph()["edges"]
380+
try:
381+
actual = ln.graph()["edges"]
382+
except Exception as e:
383+
self.log.info(f"Couldn't get graph from {ln.name} because {e}, retrying in 5 seconds...")
384+
sleep(5)
385+
continue
386+
369387
self.log.debug(f"LN {ln.name} channel graph edges: {actual}")
370388
if len(actual) > 0:
371389
done = True

0 commit comments

Comments
 (0)