Skip to content

Commit 9225712

Browse files
Fix: add retry logic to Esplora requests to handle connection issues
- Add timeout, max retries, and error handling to wait_for_block and wait_for_tx functions in Python tests - Catch and log connection errors instead of failing immediately - Use shorter sleep interval (0.5s) to match Kotlin implementation - Add error messages during retries This addresses the "Connection reset by peer" errors in CI by making the Python tests more resilient to temporary network issues when connecting to the Esplora API.
1 parent 9c02f23 commit 9225712

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

bindings/python/src/ldk_node/test_ldk_node.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,44 @@ 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+
max_retries = 30
54+
retry_count = 0
55+
56+
while retry_count < max_retries:
5657
try:
57-
json = res.json()
58-
esplora_picked_up_block = json['in_best_chain']
59-
except:
60-
pass
61-
time.sleep(1)
58+
res = requests.get(url, timeout=10)
59+
data = res.json()
60+
if data['in_best_chain']:
61+
return
62+
63+
except Exception as e:
64+
print(f"Error: {e}, retrying ({retry_count+1}/{max_retries})...")
65+
retry_count += 1
66+
67+
time.sleep(0.5)
68+
69+
raise TimeoutError(f"Block {block_hash} not found after {max_retries} attempts")
70+
6271

6372
def wait_for_tx(esplora_endpoint, txid):
6473
url = esplora_endpoint + "/tx/" + txid
65-
esplora_picked_up_tx = False
66-
while not esplora_picked_up_tx:
67-
res = requests.get(url)
74+
max_retries = 30
75+
retry_count = 0
76+
77+
while retry_count < max_retries:
6878
try:
69-
json = res.json()
70-
esplora_picked_up_tx = json['txid'] == txid
71-
except:
72-
pass
73-
time.sleep(1)
79+
res = requests.get(url, timeout=10)
80+
data = res.json()
81+
if data['txid'] == txid:
82+
return
83+
84+
except Exception as e:
85+
print(f"Error: {e}, retrying ({retry_count+1}/{max_retries})...")
86+
retry_count += 1
87+
88+
time.sleep(0.5)
89+
90+
raise TimeoutError(f"Transaction {txid} not found after {max_retries} attempts")
7491

7592
def send_to_address(address, amount_sats):
7693
amount_btc = amount_sats/100000000.0

0 commit comments

Comments
 (0)