@@ -50,27 +50,43 @@ def mine_and_wait(esplora_endpoint, blocks):
50
50
51
51
def wait_for_block (esplora_endpoint , block_hash ):
52
52
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 :
56
57
try :
58
+ res = requests .get (url , timeout = 10 )
57
59
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" )
62
70
63
71
def wait_for_tx (esplora_endpoint , txid ):
64
72
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 :
68
77
try :
78
+ res = requests .get (url , timeout = 10 )
69
79
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" )
74
90
75
91
def send_to_address (address , amount_sats ):
76
92
amount_btc = amount_sats / 100000000.0
0 commit comments