Skip to content

Commit 58d16d5

Browse files
committed
fix: focusing the refactoring on the expect_event method
1 parent b376446 commit 58d16d5

1 file changed

Lines changed: 90 additions & 89 deletions

File tree

bindings/python/src/ldk_node/test_ldk_node.py

Lines changed: 90 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,24 @@ def get_esplora_endpoint():
115115
return str(os.environ['ESPLORA_ENDPOINT'])
116116
return DEFAULT_ESPLORA_SERVER_URL
117117

118-
# handling events
118+
# handling expect events
119119

120-
def event_handling(node, expected_event_type):
120+
def expect_event(node, expected_event_type):
121121
event = node.wait_next_event()
122122
assert isinstance(event, expected_event_type)
123123
print("EVENT:", event)
124124
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
125136

126137
class TestLdkNode(unittest.TestCase):
127138
def setUp(self):
@@ -134,127 +145,117 @@ def setUp(self):
134145
def test_channel_full_cycle(self):
135146
esplora_endpoint = get_esplora_endpoint()
136147

137-
## Setup NodeS 1 & 2 in paralel
148+
## Setup Node 1
138149
tmp_dir_1 = tempfile.TemporaryDirectory("_ldk_node_1")
139-
tmp_dir_2 = tempfile.TemporaryDirectory("_ldk_node_2")
140-
try:
141-
print("TMP DIR 1:", tmp_dir_1.name)
142-
print("TMP DIR 2:", tmp_dir_2.name)
150+
print("TMP DIR 1:", tmp_dir_1.name)
143151

144-
# listening addresses
145-
listening_addresses_1 = ["127.0.0.1:2323"]
146-
listening_addresses_2 = ["127.0.0.1:2324"]
152+
listening_addresses_1 = ["127.0.0.1:2323"]
153+
node_1 = setup_node(tmp_dir_1.name, esplora_endpoint, listening_addresses_1)
154+
node_1.start()
155+
node_id_1 = node_1.node_id()
156+
print("Node ID 1:", node_id_1)
147157

148-
# Start both node
158+
# Setup Node 2
159+
tmp_dir_2 = tempfile.TemporaryDirectory("_ldk_node_2")
160+
print("TMP DIR 2:", tmp_dir_2.name)
149161

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)
162+
listening_addresses_2 = ["127.0.0.1:2324"]
163+
node_2 = setup_node(tmp_dir_2.name, esplora_endpoint, listening_addresses_2)
164+
node_2.start()
165+
node_id_2 = node_2.node_id()
166+
print("Node ID 2:", node_id_2)
152167

153-
node_1.start()
154-
node_2.start()
168+
address_1 = node_1.onchain_payment().new_address()
169+
txid_1 = send_to_address(address_1, 100000)
170+
address_2 = node_2.onchain_payment().new_address()
171+
txid_2 = send_to_address(address_2, 100000)
155172

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)
173+
wait_for_tx(esplora_endpoint, txid_1)
174+
wait_for_tx(esplora_endpoint, txid_2)
161175

176+
mine_and_wait(esplora_endpoint, 6)
162177

163-
# Send funds to both addresses in parallel
178+
node_1.sync_wallets()
179+
node_2.sync_wallets()
164180

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)
181+
spendable_balance_1 = node_1.list_balances().spendable_onchain_balance_sats
182+
spendable_balance_2 = node_2.list_balances().spendable_onchain_balance_sats
183+
total_balance_1 = node_1.list_balances().total_onchain_balance_sats
184+
total_balance_2 = node_2.list_balances().total_onchain_balance_sats
169185

170-
wait_for_tx(esplora_endpoint, txid_1)
171-
wait_for_tx(esplora_endpoint, txid_2)
186+
print("SPENDABLE 1:", spendable_balance_1)
187+
self.assertEqual(spendable_balance_1, 100000)
172188

173-
mine_and_wait(esplora_endpoint, 6)
189+
print("SPENDABLE 2:", spendable_balance_2)
190+
self.assertEqual(spendable_balance_2, 100000)
174191

175-
# Sync both nodes
176-
node_1.sync_wallets()
177-
node_2.sync_wallets()
192+
print("TOTAL 1:", total_balance_1)
193+
self.assertEqual(total_balance_1, 100000)
178194

179-
# verify balances
195+
print("TOTAL 2:", total_balance_2)
196+
self.assertEqual(total_balance_2, 100000)
180197

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-
198+
node_1.open_channel(node_id_2, listening_addresses_2[0], 50000, None, None)
187199

188-
node_1.open_channel(node_id_2, listening_addresses_2[0], 50000, None, None)
200+
# expect the channel pending event on the node 1 then get the funding txid from the event
189201

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()
202+
funding_txid = expect_event(node_1, Event.CHANNEL_PENDING)
195203

196-
event_handling(node_2, Event.CHANNEL_PENDING)
204+
# expect channel pending on node 2
205+
expect_event(node_2, Event.CHANNEL_PENDING)
197206

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)
207+
wait_for_tx(esplora_endpoint, funding_txid)
208+
mine_and_wait(esplora_endpoint, 6)
201209

202-
node_1.sync_wallets()
203-
node_2.sync_wallets()
210+
node_1.sync_wallets()
211+
node_2.sync_wallets()
204212

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()
213+
expect_event(node_1, Event.CHANNEL_READY)
214+
print(f"Node 1 channel ready with node 2, funding txid: {funding_txid}")
210215

211-
# check if the channel is active
212-
event_handling(node_2, Event.CHANNEL_READY)
216+
node_2_channel_id = expect_event(node_2, Event.CHANNEL_READY)
213217

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

222+
# expect payment successful on node 1
223+
expect_event(node_1, Event.PAYMENT_SUCCESSFUL)
218224

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()
225+
# expect payment received on node_2
226+
expect_event(node_2, Event.PAYMENT_RECEIVED)
227+
226228

229+
node_2.close_channel(node_2_channel_id, node_id_1)
227230

228-
231+
#Expecting node 1 channel closed
232+
expect_event(node_1, Event.CHANNEL_CLOSED)
229233

230-
node_2.close_channel(channel_ready_event_2.user_channel_id, node_id_1)
234+
#expecting channel close
235+
expect_event(node_2, Event.CHANNEL_CLOSED)
231236

232-
for node in [node_1, node_2]:
233-
event_handling(node, Event.CHANNEL_CLOSED)
237+
mine_and_wait(esplora_endpoint, 1)
234238

235-
# mine and wait for the close transaction to confirm
236-
mine_and_wait(esplora_endpoint, 1)
239+
node_1.sync_wallets()
240+
node_2.sync_wallets()
237241

238-
node_1.sync_wallets()
239-
node_2.sync_wallets()
242+
spendable_balance_after_close_1 = node_1.list_balances().spendable_onchain_balance_sats
243+
assert spendable_balance_after_close_1 > 95000
244+
assert spendable_balance_after_close_1 < 100000
245+
spendable_balance_after_close_2 = node_2.list_balances().spendable_onchain_balance_sats
246+
self.assertEqual(spendable_balance_after_close_2, 102500)
240247

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)
248+
# Stop nodes
249+
node_1.stop()
250+
node_2.stop()
246251

247-
# Stop nodes
248-
node_1.stop()
249-
node_2.stop()
252+
# Cleanup
253+
time.sleep(1) # Wait a sec so our logs can finish writing
254+
tmp_dir_1.cleanup()
255+
tmp_dir_2.cleanup()
250256

251-
finally:
252-
# Cleanup
253-
time.sleep(1) # Wait a sec so our logs can finish writing
254-
# Cleanup even if the test fails
255-
tmp_dir_1.cleanup()
256-
tmp_dir_2.cleanup()
257257

258+
258259
if __name__ == '__main__':
259260
unittest.main()
260261

0 commit comments

Comments
 (0)