Skip to content
73 changes: 40 additions & 33 deletions bindings/python/src/ldk_node/test_ldk_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,33 @@ def get_esplora_endpoint():
return str(os.environ['ESPLORA_ENDPOINT'])
return DEFAULT_ESPLORA_SERVER_URL

# handling expected event
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Please drop these comments, they are very redundant to the method names.


def expect_event(node, expected_event_type):
event = node.wait_next_event()
assert isinstance(event, expected_event_type)
print("EVENT:", event)
node.event_handled()

# handling channel pending event

def expect_channel_pending_event(node):
event = node.wait_next_event()
assert isinstance(event, Event.CHANNEL_PENDING)
print("EVENT:", event)
node.event_handled()
return event
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused: if we now simply return the event, we can just reduce that to one expect_event method again that makes the assert isinstance check and returns the event, leaving the matching to the callsite? That's also fine by me, but I don't see why we now need three different methods if they don't do the matching and don't end up having different return values?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I should just return the event in the expect_event but we will have some return throwed away for nothing.. I will just return the specific attribute depending on which variant is called that avoid the fact we throw away sometimes the returned event when it is not stored in variable (Generic event handling e.g PAYMENT_RECEIVED event)


# handling channel ready event

def expect_channel_ready_event(node):
event = node.wait_next_event()
assert isinstance(event, Event.CHANNEL_READY)
print("EVENT:", event)
node.event_handled()
return event


class TestLdkNode(unittest.TestCase):
def setUp(self):
bitcoin_cli("createwallet ldk_node_test")
Expand Down Expand Up @@ -175,15 +202,10 @@ def test_channel_full_cycle(self):

node_1.open_channel(node_id_2, listening_addresses_2[0], 50000, None, None)

channel_pending_event_1 = node_1.wait_next_event()
assert isinstance(channel_pending_event_1, Event.CHANNEL_PENDING)
print("EVENT:", channel_pending_event_1)
node_1.event_handled()
channel_pending_event_1 = expect_channel_pending_event(node_1)

channel_pending_event_2 = node_2.wait_next_event()
assert isinstance(channel_pending_event_2, Event.CHANNEL_PENDING)
print("EVENT:", channel_pending_event_2)
node_2.event_handled()
# expect channel pending event on node 2 but without return value since it doesn't contain the funding_txo
expect_event(node_2, Event.CHANNEL_PENDING)

funding_txid = channel_pending_event_1.funding_txo.txid
wait_for_tx(esplora_endpoint, funding_txid)
Expand All @@ -192,42 +214,28 @@ def test_channel_full_cycle(self):
node_1.sync_wallets()
node_2.sync_wallets()

channel_ready_event_1 = node_1.wait_next_event()
assert isinstance(channel_ready_event_1, Event.CHANNEL_READY)
print("EVENT:", channel_ready_event_1)
# expect generic channel ready event on node 1
channel_ready_event_1 = expect_event(node_1, Event.CHANNEL_READY)
print("funding_txo:", funding_txid)
node_1.event_handled()

channel_ready_event_2 = node_2.wait_next_event()
assert isinstance(channel_ready_event_2, Event.CHANNEL_READY)
print("EVENT:", channel_ready_event_2)
node_2.event_handled()
channel_ready_event_2 = expect_channel_ready_event(node_2)

description = Bolt11InvoiceDescription.DIRECT("asdf")
invoice = node_2.bolt11_payment().receive(2500000, description, 9217)
node_1.bolt11_payment().send(invoice, None)

payment_successful_event_1 = node_1.wait_next_event()
assert isinstance(payment_successful_event_1, Event.PAYMENT_SUCCESSFUL)
print("EVENT:", payment_successful_event_1)
node_1.event_handled()
# expect payment successful event on node 1 and payment received event on node 2
expect_event(node_1, Event.PAYMENT_SUCCESSFUL)

payment_received_event_2 = node_2.wait_next_event()
assert isinstance(payment_received_event_2, Event.PAYMENT_RECEIVED)
print("EVENT:", payment_received_event_2)
node_2.event_handled()
expect_event(node_2, Event.PAYMENT_RECEIVED)


node_2.close_channel(channel_ready_event_2.user_channel_id, node_id_1)

channel_closed_event_1 = node_1.wait_next_event()
assert isinstance(channel_closed_event_1, Event.CHANNEL_CLOSED)
print("EVENT:", channel_closed_event_1)
node_1.event_handled()
# expect channel closed event on both nodes
expect_event(node_1, Event.CHANNEL_CLOSED)

channel_closed_event_2 = node_2.wait_next_event()
assert isinstance(channel_closed_event_2, Event.CHANNEL_CLOSED)
print("EVENT:", channel_closed_event_2)
node_2.event_handled()
expect_event(node_2, Event.CHANNEL_CLOSED)

mine_and_wait(esplora_endpoint, 1)

Expand All @@ -251,4 +259,3 @@ def test_channel_full_cycle(self):

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

Loading