From ce34d7e2ef9a40d4f35242859a3a2e0b4dd5adbf Mon Sep 17 00:00:00 2001 From: alexanderwiederin Date: Fri, 28 Mar 2025 21:02:46 +0100 Subject: [PATCH] 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. --- bindings/python/src/ldk_node/test_ldk_node.py | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/bindings/python/src/ldk_node/test_ldk_node.py b/bindings/python/src/ldk_node/test_ldk_node.py index 4c5cdd828..f71e89df8 100644 --- a/bindings/python/src/ldk_node/test_ldk_node.py +++ b/bindings/python/src/ldk_node/test_ldk_node.py @@ -50,27 +50,43 @@ def mine_and_wait(esplora_endpoint, blocks): def wait_for_block(esplora_endpoint, block_hash): url = esplora_endpoint + "/block/" + block_hash + "/status" - esplora_picked_up_block = False - while not esplora_picked_up_block: - res = requests.get(url) + attempts = 0 + max_attempts = 30 + + while attempts < max_attempts: try: + res = requests.get(url, timeout=10) json = res.json() - esplora_picked_up_block = json['in_best_chain'] - except: - pass - time.sleep(1) + if json.get('in_best_chain'): + return + + except Exception as e: + print(f"Error: {e}") + + attempts += 1 + time.sleep(0.5) + + raise Exception(f"Failed to confirm block {block_hash} after {max_attempts} attempts") def wait_for_tx(esplora_endpoint, txid): url = esplora_endpoint + "/tx/" + txid - esplora_picked_up_tx = False - while not esplora_picked_up_tx: - res = requests.get(url) + attempts = 0 + max_attempts = 30 + + while attempts < max_attempts: try: + res = requests.get(url, timeout=10) json = res.json() - esplora_picked_up_tx = json['txid'] == txid - except: - pass - time.sleep(1) + if json.get('txid') == txid: + return + + except Exception as e: + print(f"Error: {e}") + + attempts += 1 + time.sleep(0.5) + + raise Exception(f"Failed to confirm transaction {txid} after {max_attempts} attempts") def send_to_address(address, amount_sats): amount_btc = amount_sats/100000000.0