1212DEFAULT_TEST_NETWORK = Network .REGTEST
1313DEFAULT_BITCOIN_CLI_BIN = "bitcoin-cli"
1414
15+ DEFAULT_MAX_ATTEMPS = 25
16+ SLEEP_TIME = 0.5
17+
1518def bitcoin_cli (cmd ):
1619 args = []
1720
@@ -51,9 +54,9 @@ def mine_and_wait(esplora_endpoint, blocks):
5154def wait_for_block (esplora_endpoint , block_hash ):
5255 url = esplora_endpoint + "/block/" + block_hash + "/status"
5356 attempts = 0
54- max_attempts = 30
5557
56- while attempts < max_attempts :
58+
59+ while attempts < DEFAULT_MAX_ATTEMPS :
5760 try :
5861 res = requests .get (url , timeout = 10 )
5962 json = res .json ()
@@ -64,16 +67,16 @@ def wait_for_block(esplora_endpoint, block_hash):
6467 print (f"Error: { e } " )
6568
6669 attempts += 1
67- time .sleep (0.5 )
70+ time .sleep (SLEEP_TIME )
6871
69- raise Exception (f"Failed to confirm block { block_hash } after { max_attempts } attempts" )
72+ raise Exception (f"Failed to confirm block { block_hash } after { DEFAULT_MAX_ATTEMPS } attempts" )
7073
7174def wait_for_tx (esplora_endpoint , txid ):
7275 url = esplora_endpoint + "/tx/" + txid
7376 attempts = 0
74- max_attempts = 30
77+
7578
76- while attempts < max_attempts :
79+ while attempts < DEFAULT_MAX_ATTEMPS :
7780 try :
7881 res = requests .get (url , timeout = 10 )
7982 json = res .json ()
@@ -84,9 +87,9 @@ def wait_for_tx(esplora_endpoint, txid):
8487 print (f"Error: { e } " )
8588
8689 attempts += 1
87- time .sleep (0.5 )
90+ time .sleep (SLEEP_TIME )
8891
89- raise Exception (f"Failed to confirm transaction { txid } after { max_attempts } attempts" )
92+ raise Exception (f"Failed to confirm transaction { txid } after { DEFAULT_MAX_ATTEMPS } attempts" )
9093
9194def send_to_address (address , amount_sats ):
9295 amount_btc = amount_sats / 100000000.0
@@ -112,6 +115,14 @@ def get_esplora_endpoint():
112115 return str (os .environ ['ESPLORA_ENDPOINT' ])
113116 return DEFAULT_ESPLORA_SERVER_URL
114117
118+ # handling events
119+
120+ def event_handling (node , expected_event_type ):
121+ event = node .wait_next_event ()
122+ assert isinstance (event , expected_event_type )
123+ print ("EVENT:" , event )
124+ node .event_handled ()
125+
115126class TestLdkNode (unittest .TestCase ):
116127 def setUp (self ):
117128 bitcoin_cli ("createwallet ldk_node_test" )
@@ -123,26 +134,33 @@ def setUp(self):
123134 def test_channel_full_cycle (self ):
124135 esplora_endpoint = get_esplora_endpoint ()
125136
126- ## Setup Node 1
137+ ## Setup NodeS 1 & 2 in paralel
127138 tmp_dir_1 = tempfile .TemporaryDirectory ("_ldk_node_1" )
139+ tmp_dir_2 = tempfile .TemporaryDirectory ("_ldk_node_2" )
128140 print ("TMP DIR 1:" , tmp_dir_1 .name )
141+ print ("TMP DIR 2:" , tmp_dir_2 .name )
129142
143+ # listening addresses
130144 listening_addresses_1 = ["127.0.0.1:2323" ]
131- node_1 = setup_node (tmp_dir_1 .name , esplora_endpoint , listening_addresses_1 )
132- node_1 .start ()
133- node_id_1 = node_1 .node_id ()
134- print ("Node ID 1:" , node_id_1 )
145+ listening_addresses_2 = ["127.0.0.1:2324" ]
135146
136- # Setup Node 2
137- tmp_dir_2 = tempfile .TemporaryDirectory ("_ldk_node_2" )
138- print ("TMP DIR 2:" , tmp_dir_2 .name )
147+ # Start both node
139148
140- listening_addresses_2 = ["127.0.0.1:2324" ]
141- node_2 = setup_node (tmp_dir_2 .name , esplora_endpoint , listening_addresses_2 )
149+ node_1 = setup_node (tmp_dir = tmp_dir_1 , esplora_endpoint = esplora_endpoint , listening_addresses = listening_addresses_1 )
150+ node_2 = setup_node (tmp_dir = tmp_dir_2 , esplora_endpoint = esplora_endpoint , listening_addresses = listening_addresses_2 )
151+
152+ node_1 .start ()
142153 node_2 .start ()
154+
155+ # get Nodes IDs
156+ node_id_1 = node_1 .node_id ()
143157 node_id_2 = node_2 .node_id ()
158+ print ("Node ID 1:" , node_id_1 )
144159 print ("Node ID 2:" , node_id_2 )
145160
161+
162+ # Send funds to both addresses in parallel
163+
146164 address_1 = node_1 .onchain_payment ().new_address ()
147165 txid_1 = send_to_address (address_1 , 100000 )
148166 address_2 = node_2 .onchain_payment ().new_address ()
@@ -153,37 +171,28 @@ def test_channel_full_cycle(self):
153171
154172 mine_and_wait (esplora_endpoint , 6 )
155173
174+ # Sync both nodes
156175 node_1 .sync_wallets ()
157176 node_2 .sync_wallets ()
158177
159- spendable_balance_1 = node_1 .list_balances ().spendable_onchain_balance_sats
160- spendable_balance_2 = node_2 .list_balances ().spendable_onchain_balance_sats
161- total_balance_1 = node_1 .list_balances ().total_onchain_balance_sats
162- total_balance_2 = node_2 .list_balances ().total_onchain_balance_sats
163-
164- print ("SPENDABLE 1:" , spendable_balance_1 )
165- self .assertEqual (spendable_balance_1 , 100000 )
166-
167- print ("SPENDABLE 2:" , spendable_balance_2 )
168- self .assertEqual (spendable_balance_2 , 100000 )
178+ # verify balances
169179
170- print ("TOTAL 1:" , total_balance_1 )
171- self .assertEqual (total_balance_1 , 100000 )
172-
173- print ("TOTAL 2:" , total_balance_2 )
174- self .assertEqual (total_balance_2 , 100000 )
180+ for node , name in [(node_1 , '1' ), (node_2 ,"2" )] :
181+ spendable_balance = node .list_balances ().spendable_onchain_balance_sats
182+ total_balance = node .list_balances ().total_onchain_balance_sats
183+ self .assertEqual (spendable_balance , 100000 , f"Node { name } spendable balance should be 100000 sats, the test spotted { spendable_balance } sats" )
184+ self .assertEqual (total_balance , 100000 , f"Node { name } total balance should be 100000 sats, the test spotted { total_balance } sats" )
185+
175186
176187 node_1 .open_channel (node_id_2 , listening_addresses_2 [0 ], 50000 , None , None )
177188
189+ # check if both nodes received the channel pending event
178190 channel_pending_event_1 = node_1 .wait_next_event ()
179191 assert isinstance (channel_pending_event_1 , Event .CHANNEL_PENDING )
180192 print ("EVENT:" , channel_pending_event_1 )
181193 node_1 .event_handled ()
182194
183- channel_pending_event_2 = node_2 .wait_next_event ()
184- assert isinstance (channel_pending_event_2 , Event .CHANNEL_PENDING )
185- print ("EVENT:" , channel_pending_event_2 )
186- node_2 .event_handled ()
195+ event_handling (node_2 , Event .CHANNEL_PENDING )
187196
188197 funding_txid = channel_pending_event_1 .funding_txo .txid
189198 wait_for_tx (esplora_endpoint , funding_txid )
@@ -198,37 +207,28 @@ def test_channel_full_cycle(self):
198207 print ("funding_txo:" , funding_txid )
199208 node_1 .event_handled ()
200209
201- channel_ready_event_2 = node_2 .wait_next_event ()
202- assert isinstance (channel_ready_event_2 , Event .CHANNEL_READY )
203- print ("EVENT:" , channel_ready_event_2 )
204- node_2 .event_handled ()
210+ # check if the channel is active
211+ event_handling (node_2 , Event .CHANNEL_READY )
205212
206213 description = Bolt11InvoiceDescription .DIRECT ("asdf" )
207214 invoice = node_2 .bolt11_payment ().receive (2500000 , description , 9217 )
208215 node_1 .bolt11_payment ().send (invoice , None )
209216
210- payment_successful_event_1 = node_1 .wait_next_event ()
211- assert isinstance (payment_successful_event_1 , Event .PAYMENT_SUCCESSFUL )
212- print ("EVENT:" , payment_successful_event_1 )
213- node_1 .event_handled ()
214217
215- payment_received_event_2 = node_2 . wait_next_event ()
216- assert isinstance ( payment_received_event_2 , Event .PAYMENT_RECEIVED )
217- print ( "EVENT:" , payment_received_event_2 )
218- node_2 . event_handled ( )
218+ # check if the payment is done
219+ event_handling ( node_1 , Event .PAYMENT_SUCCESSFUL )
220+ # check if the node 2 received the payment
221+ event_handling ( node_2 , Event . PAYMENT_RECEIVED )
219222
220- node_2 .close_channel (channel_ready_event_2 .user_channel_id , node_id_1 )
221223
222- channel_closed_event_1 = node_1 .wait_next_event ()
223- assert isinstance (channel_closed_event_1 , Event .CHANNEL_CLOSED )
224- print ("EVENT:" , channel_closed_event_1 )
225- node_1 .event_handled ()
224+
225+
226+ node_2 .close_channel (channel_ready_event_2 .user_channel_id , node_id_1 )
226227
227- channel_closed_event_2 = node_2 .wait_next_event ()
228- assert isinstance (channel_closed_event_2 , Event .CHANNEL_CLOSED )
229- print ("EVENT:" , channel_closed_event_2 )
230- node_2 .event_handled ()
228+ for node in [node_1 , node_2 ]:
229+ event_handling (node , Event .CHANNEL_CLOSED )
231230
231+ # mine and wait for the close transaction to confirm
232232 mine_and_wait (esplora_endpoint , 1 )
233233
234234 node_1 .sync_wallets ()
0 commit comments