Skip to content

Commit abdcc6a

Browse files
authored
Merge pull request #513 from alexanderwiederin/fix-flaky-python-test
Fix: add retry logic to Esplora requests to handle connection issues
2 parents 095ba89 + ce34d7e commit abdcc6a

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

bindings/python/src/ldk_node/test_ldk_node.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,43 @@ def mine_and_wait(esplora_endpoint, blocks):
5050

5151
def wait_for_block(esplora_endpoint, block_hash):
5252
url = esplora_endpoint + "/block/" + block_hash + "/status"
53-
esplora_picked_up_block = False
54-
while not esplora_picked_up_block:
55-
res = requests.get(url)
53+
attempts = 0
54+
max_attempts = 30
55+
56+
while attempts < max_attempts:
5657
try:
58+
res = requests.get(url, timeout=10)
5759
json = res.json()
58-
esplora_picked_up_block = json['in_best_chain']
59-
except:
60-
pass
61-
time.sleep(1)
60+
if json.get('in_best_chain'):
61+
return
62+
63+
except Exception as e:
64+
print(f"Error: {e}")
65+
66+
attempts += 1
67+
time.sleep(0.5)
68+
69+
raise Exception(f"Failed to confirm block {block_hash} after {max_attempts} attempts")
6270

6371
def wait_for_tx(esplora_endpoint, txid):
6472
url = esplora_endpoint + "/tx/" + txid
65-
esplora_picked_up_tx = False
66-
while not esplora_picked_up_tx:
67-
res = requests.get(url)
73+
attempts = 0
74+
max_attempts = 30
75+
76+
while attempts < max_attempts:
6877
try:
78+
res = requests.get(url, timeout=10)
6979
json = res.json()
70-
esplora_picked_up_tx = json['txid'] == txid
71-
except:
72-
pass
73-
time.sleep(1)
80+
if json.get('txid') == txid:
81+
return
82+
83+
except Exception as e:
84+
print(f"Error: {e}")
85+
86+
attempts += 1
87+
time.sleep(0.5)
88+
89+
raise Exception(f"Failed to confirm transaction {txid} after {max_attempts} attempts")
7490

7591
def send_to_address(address, amount_sats):
7692
amount_btc = amount_sats/100000000.0

0 commit comments

Comments
 (0)