1212DEFAULT_TEST_NETWORK = Network .REGTEST
1313DEFAULT_BITCOIN_CLI_BIN = "bitcoin-cli"
1414
15- DEFAULT_MAX_ATTEMPS = 25
16- SLEEP_TIME = 0.5
17-
1815def bitcoin_cli (cmd ):
1916 args = []
2017
@@ -54,9 +51,9 @@ def mine_and_wait(esplora_endpoint, blocks):
5451def wait_for_block (esplora_endpoint , block_hash ):
5552 url = esplora_endpoint + "/block/" + block_hash + "/status"
5653 attempts = 0
54+ max_attempts = 30
5755
58-
59- while attempts < DEFAULT_MAX_ATTEMPS :
56+ while attempts < max_attempts :
6057 try :
6158 res = requests .get (url , timeout = 10 )
6259 json = res .json ()
@@ -67,16 +64,16 @@ def wait_for_block(esplora_endpoint, block_hash):
6764 print (f"Error: { e } " )
6865
6966 attempts += 1
70- time .sleep (SLEEP_TIME )
67+ time .sleep (0.5 )
7168
72- raise Exception (f"Failed to confirm block { block_hash } after { DEFAULT_MAX_ATTEMPS } attempts" )
69+ raise Exception (f"Failed to confirm block { block_hash } after { max_attempts } attempts" )
7370
7471def wait_for_tx (esplora_endpoint , txid ):
7572 url = esplora_endpoint + "/tx/" + txid
7673 attempts = 0
77-
74+ max_attempts = 30
7875
79- while attempts < DEFAULT_MAX_ATTEMPS :
76+ while attempts < max_attempts :
8077 try :
8178 res = requests .get (url , timeout = 10 )
8279 json = res .json ()
@@ -87,9 +84,9 @@ def wait_for_tx(esplora_endpoint, txid):
8784 print (f"Error: { e } " )
8885
8986 attempts += 1
90- time .sleep (SLEEP_TIME )
87+ time .sleep (0.5 )
9188
92- raise Exception (f"Failed to confirm transaction { txid } after { DEFAULT_MAX_ATTEMPS } attempts" )
89+ raise Exception (f"Failed to confirm transaction { txid } after { max_attempts } attempts" )
9390
9491def send_to_address (address , amount_sats ):
9592 amount_btc = amount_sats / 100000000.0
@@ -115,24 +112,32 @@ def get_esplora_endpoint():
115112 return str (os .environ ['ESPLORA_ENDPOINT' ])
116113 return DEFAULT_ESPLORA_SERVER_URL
117114
118- # handling expect events
115+ # handling expected event
119116
120117def expect_event (node , expected_event_type ):
121118 event = node .wait_next_event ()
122119 assert isinstance (event , expected_event_type )
123120 print ("EVENT:" , event )
124121 node .event_handled ()
125- #According to the event type, we may want to return some data from the event for further processing
126- match expected_event_type :
127- case Event .CHANNEL_PENDING :
128- return event .funding_txo .txid
129- case Event .PAYMENT_RECEIVED :
130- return event .payment_hash
131- case Event .CHANNEL_READY :
132- if node .name == "node_2" :
133- return event .user .channel_id
134- case _:
135- return None
122+
123+ # handling channel ready event
124+
125+ def expect_channel_pending_event (node ):
126+ event = node .wait_next_event ()
127+ assert isinstance (event , Event .CHANNEL_PENDING )
128+ print ("EVENT:" , event )
129+ node .event_handled ()
130+ return event
131+
132+ # handling channel ready event
133+
134+ def expect_channel_ready_event (node ):
135+ event = node .wait_next_event ()
136+ assert isinstance (event , Event .CHANNEL_READY )
137+ print ("EVENT:" , event )
138+ node .event_handled ()
139+ return event
140+
136141
137142class TestLdkNode (unittest .TestCase ):
138143 def setUp (self ):
@@ -197,41 +202,39 @@ def test_channel_full_cycle(self):
197202
198203 node_1 .open_channel (node_id_2 , listening_addresses_2 [0 ], 50000 , None , None )
199204
200- # expect the channel pending event on the node 1 then get the funding txid from the event
205+ channel_pending_event_1 = expect_channel_pending_event ( node_1 )
201206
202- funding_txid = expect_event (node_1 , Event .CHANNEL_PENDING )
203-
204- # expect channel pending on node 2
207+ # expect channel pending event on node 2 but without return value since it doesn't contain the funding_txo
205208 expect_event (node_2 , Event .CHANNEL_PENDING )
206209
210+ funding_txid = channel_pending_event_1 .funding_txo .txid
207211 wait_for_tx (esplora_endpoint , funding_txid )
208212 mine_and_wait (esplora_endpoint , 6 )
209213
210214 node_1 .sync_wallets ()
211215 node_2 .sync_wallets ()
212216
213- expect_event (node_1 , Event .CHANNEL_READY )
214- print (f"Node 1 channel ready with node 2, funding txid: { funding_txid } " )
217+ # expect generic channel ready event on node 1
218+ channel_ready_event_1 = expect_event (node_1 , Event .CHANNEL_READY )
219+ print ("funding_txo:" , funding_txid )
215220
216- node_2_channel_id = expect_event (node_2 , Event . CHANNEL_READY )
221+ channel_ready_event_2 = expect_channel_ready_event (node_2 )
217222
218223 description = Bolt11InvoiceDescription .DIRECT ("asdf" )
219224 invoice = node_2 .bolt11_payment ().receive (2500000 , description , 9217 )
220225 node_1 .bolt11_payment ().send (invoice , None )
221226
222- # expect payment successful on node 1
227+ # expect payment successful event on node 1 and payment received event on node 2
223228 expect_event (node_1 , Event .PAYMENT_SUCCESSFUL )
224229
225- # expect payment received on node_2
226230 expect_event (node_2 , Event .PAYMENT_RECEIVED )
227-
231+
228232
229- node_2 .close_channel (node_2_channel_id , node_id_1 )
233+ node_2 .close_channel (channel_ready_event_2 . user_channel_id , node_id_1 )
230234
231- #Expecting node 1 channel closed
235+ # expect channel closed event on both nodes
232236 expect_event (node_1 , Event .CHANNEL_CLOSED )
233237
234- #expecting channel close
235238 expect_event (node_2 , Event .CHANNEL_CLOSED )
236239
237240 mine_and_wait (esplora_endpoint , 1 )
@@ -254,8 +257,5 @@ def test_channel_full_cycle(self):
254257 tmp_dir_1 .cleanup ()
255258 tmp_dir_2 .cleanup ()
256259
257-
258-
259260if __name__ == '__main__' :
260261 unittest .main ()
261-
0 commit comments