Skip to content

Commit fa2c431

Browse files
committed
fix: node_2 close event and clean up the temp dir even test fails
1 parent 215d5b2 commit fa2c431

1 file changed

Lines changed: 87 additions & 79 deletions

File tree

bindings/python/src/ldk_node/test_ldk_node.py

Lines changed: 87 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -137,117 +137,125 @@ def test_channel_full_cycle(self):
137137
## Setup NodeS 1 & 2 in paralel
138138
tmp_dir_1 = tempfile.TemporaryDirectory("_ldk_node_1")
139139
tmp_dir_2 = tempfile.TemporaryDirectory("_ldk_node_2")
140-
print("TMP DIR 1:", tmp_dir_1.name)
141-
print("TMP DIR 2:", tmp_dir_2.name)
140+
try:
141+
print("TMP DIR 1:", tmp_dir_1.name)
142+
print("TMP DIR 2:", tmp_dir_2.name)
142143

143-
# listening addresses
144-
listening_addresses_1 = ["127.0.0.1:2323"]
145-
listening_addresses_2 = ["127.0.0.1:2324"]
144+
# listening addresses
145+
listening_addresses_1 = ["127.0.0.1:2323"]
146+
listening_addresses_2 = ["127.0.0.1:2324"]
146147

147-
# Start both node
148+
# Start both node
148149

149-
node_1 = setup_node(tmp_dir=tmp_dir_1.name , esplora_endpoint=esplora_endpoint, listening_addresses=listening_addresses_1)
150-
node_2 = setup_node(tmp_dir=tmp_dir_2.name, esplora_endpoint=esplora_endpoint, listening_addresses=listening_addresses_2)
150+
node_1 = setup_node(tmp_dir=tmp_dir_1.name , esplora_endpoint=esplora_endpoint, listening_addresses=listening_addresses_1)
151+
node_2 = setup_node(tmp_dir=tmp_dir_2.name, esplora_endpoint=esplora_endpoint, listening_addresses=listening_addresses_2)
151152

152-
node_1.start()
153-
node_2.start()
153+
node_1.start()
154+
node_2.start()
154155

155-
# get Nodes IDs
156-
node_id_1 = node_1.node_id()
157-
node_id_2 = node_2.node_id()
158-
print("Node ID 1:", node_id_1)
159-
print("Node ID 2:", node_id_2)
156+
# get Nodes IDs
157+
node_id_1 = node_1.node_id()
158+
node_id_2 = node_2.node_id()
159+
print("Node ID 1:", node_id_1)
160+
print("Node ID 2:", node_id_2)
160161

161162

162-
# Send funds to both addresses in parallel
163+
# Send funds to both addresses in parallel
163164

164-
address_1 = node_1.onchain_payment().new_address()
165-
txid_1 = send_to_address(address_1, 100000)
166-
address_2 = node_2.onchain_payment().new_address()
167-
txid_2 = send_to_address(address_2, 100000)
165+
address_1 = node_1.onchain_payment().new_address()
166+
txid_1 = send_to_address(address_1, 100000)
167+
address_2 = node_2.onchain_payment().new_address()
168+
txid_2 = send_to_address(address_2, 100000)
168169

169-
wait_for_tx(esplora_endpoint, txid_1)
170-
wait_for_tx(esplora_endpoint, txid_2)
170+
wait_for_tx(esplora_endpoint, txid_1)
171+
wait_for_tx(esplora_endpoint, txid_2)
171172

172-
mine_and_wait(esplora_endpoint, 6)
173+
mine_and_wait(esplora_endpoint, 6)
173174

174-
# Sync both nodes
175-
node_1.sync_wallets()
176-
node_2.sync_wallets()
175+
# Sync both nodes
176+
node_1.sync_wallets()
177+
node_2.sync_wallets()
177178

178-
# verify balances
179+
# verify balances
179180

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-
181+
for node, name in [(node_1, '1'), (node_2,"2")] :
182+
spendable_balance = node.list_balances().spendable_onchain_balance_sats
183+
total_balance = node.list_balances().total_onchain_balance_sats
184+
self.assertEqual(spendable_balance, 100000, f"Node {name} spendable balance should be 100000 sats, the test spotted {spendable_balance} sats")
185+
self.assertEqual(total_balance, 100000, f"Node {name} total balance should be 100000 sats, the test spotted {total_balance} sats")
186+
186187

187-
node_1.open_channel(node_id_2, listening_addresses_2[0], 50000, None, None)
188+
node_1.open_channel(node_id_2, listening_addresses_2[0], 50000, None, None)
188189

189-
# check if both nodes received the channel pending event
190-
channel_pending_event_1 = node_1.wait_next_event()
191-
assert isinstance(channel_pending_event_1, Event.CHANNEL_PENDING)
192-
print("EVENT:", channel_pending_event_1)
193-
node_1.event_handled()
190+
# check if both nodes received the channel pending event
191+
channel_pending_event_1 = node_1.wait_next_event()
192+
assert isinstance(channel_pending_event_1, Event.CHANNEL_PENDING)
193+
print("EVENT:", channel_pending_event_1)
194+
node_1.event_handled()
194195

195-
event_handling(node_2, Event.CHANNEL_PENDING)
196+
event_handling(node_2, Event.CHANNEL_PENDING)
196197

197-
funding_txid = channel_pending_event_1.funding_txo.txid
198-
wait_for_tx(esplora_endpoint, funding_txid)
199-
mine_and_wait(esplora_endpoint, 6)
198+
funding_txid = channel_pending_event_1.funding_txo.txid
199+
wait_for_tx(esplora_endpoint, funding_txid)
200+
mine_and_wait(esplora_endpoint, 6)
200201

201-
node_1.sync_wallets()
202-
node_2.sync_wallets()
202+
node_1.sync_wallets()
203+
node_2.sync_wallets()
203204

204-
channel_ready_event_1 = node_1.wait_next_event()
205-
assert isinstance(channel_ready_event_1, Event.CHANNEL_READY)
206-
print("EVENT:", channel_ready_event_1)
207-
print("funding_txo:", funding_txid)
208-
node_1.event_handled()
205+
channel_ready_event_1 = node_1.wait_next_event()
206+
assert isinstance(channel_ready_event_1, Event.CHANNEL_READY)
207+
print("EVENT:", channel_ready_event_1)
208+
print("funding_txo:", funding_txid)
209+
node_1.event_handled()
209210

210-
# check if the channel is active
211-
event_handling(node_2, Event.CHANNEL_READY)
211+
# check if the channel is active
212+
event_handling(node_2, Event.CHANNEL_READY)
212213

213-
description = Bolt11InvoiceDescription.DIRECT("asdf")
214-
invoice = node_2.bolt11_payment().receive(2500000, description, 9217)
215-
node_1.bolt11_payment().send(invoice, None)
214+
description = Bolt11InvoiceDescription.DIRECT("asdf")
215+
invoice = node_2.bolt11_payment().receive(2500000, description, 9217)
216+
node_1.bolt11_payment().send(invoice, None)
216217

217218

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)
219+
# check if the payment is done
220+
event_handling(node_1, Event.PAYMENT_SUCCESSFUL)
221+
# check if the node 2 received the payment
222+
channel_ready_event_2 = node_2.wait_next_event()
223+
assert isinstance(channel_ready_event_2, Event.PAYMENT_RECEIVED)
224+
print("EVENT:", channel_ready_event_2)
225+
node_2.event_handled()
222226

223227

224-
228+
225229

226-
node_2.close_channel(channel_ready_event_2.user_channel_id, node_id_1)
230+
node_2.close_channel(channel_ready_event_2.user_channel_id, node_id_1)
227231

228-
for node in [node_1, node_2]:
229-
event_handling(node, Event.CHANNEL_CLOSED)
232+
for node in [node_1, node_2]:
233+
event_handling(node, Event.CHANNEL_CLOSED)
230234

231-
# mine and wait for the close transaction to confirm
232-
mine_and_wait(esplora_endpoint, 1)
235+
# mine and wait for the close transaction to confirm
236+
mine_and_wait(esplora_endpoint, 1)
233237

234-
node_1.sync_wallets()
235-
node_2.sync_wallets()
238+
node_1.sync_wallets()
239+
node_2.sync_wallets()
236240

237-
spendable_balance_after_close_1 = node_1.list_balances().spendable_onchain_balance_sats
238-
assert spendable_balance_after_close_1 > 95000
239-
assert spendable_balance_after_close_1 < 100000
240-
spendable_balance_after_close_2 = node_2.list_balances().spendable_onchain_balance_sats
241-
self.assertEqual(spendable_balance_after_close_2, 102500)
241+
spendable_balance_after_close_1 = node_1.list_balances().spendable_onchain_balance_sats
242+
assert spendable_balance_after_close_1 > 95000
243+
assert spendable_balance_after_close_1 < 100000
244+
spendable_balance_after_close_2 = node_2.list_balances().spendable_onchain_balance_sats
245+
self.assertEqual(spendable_balance_after_close_2, 102500)
242246

243-
# Stop nodes
244-
node_1.stop()
245-
node_2.stop()
247+
# Stop nodes
248+
node_1.stop()
249+
node_2.stop()
246250

247-
# Cleanup
248-
time.sleep(1) # Wait a sec so our logs can finish writing
249-
tmp_dir_1.cleanup()
250-
tmp_dir_2.cleanup()
251+
# Cleanup
252+
time.sleep(1) # Wait a sec so our logs can finish writing
253+
tmp_dir_1.cleanup()
254+
tmp_dir_2.cleanup()
255+
finally:
256+
# Cleanup even if the test fails
257+
tmp_dir_1.cleanup()
258+
tmp_dir_2.cleanup()
251259

252260
if __name__ == '__main__':
253261
unittest.main()

0 commit comments

Comments
 (0)