diff --git a/bindings/python/src/ldk_node/test_ldk_node.py b/bindings/python/src/ldk_node/test_ldk_node.py index 0b73e6a47..e4c6214cd 100644 --- a/bindings/python/src/ldk_node/test_ldk_node.py +++ b/bindings/python/src/ldk_node/test_ldk_node.py @@ -12,6 +12,9 @@ DEFAULT_TEST_NETWORK = Network.REGTEST DEFAULT_BITCOIN_CLI_BIN = "bitcoin-cli" +DEFAULT_MAX_ATTEMPS=25 +SLEEP_TIME=0.5 + def bitcoin_cli(cmd): args = [] @@ -51,9 +54,9 @@ def mine_and_wait(esplora_endpoint, blocks): def wait_for_block(esplora_endpoint, block_hash): url = esplora_endpoint + "/block/" + block_hash + "/status" attempts = 0 - max_attempts = 30 - while attempts < max_attempts: + + while attempts < DEFAULT_MAX_ATTEMPS: try: res = requests.get(url, timeout=10) json = res.json() @@ -64,16 +67,16 @@ def wait_for_block(esplora_endpoint, block_hash): print(f"Error: {e}") attempts += 1 - time.sleep(0.5) + time.sleep(SLEEP_TIME) - raise Exception(f"Failed to confirm block {block_hash} after {max_attempts} attempts") + raise Exception(f"Failed to confirm block {block_hash} after {DEFAULT_MAX_ATTEMPS} attempts") def wait_for_tx(esplora_endpoint, txid): url = esplora_endpoint + "/tx/" + txid attempts = 0 - max_attempts = 30 + - while attempts < max_attempts: + while attempts < DEFAULT_MAX_ATTEMPS: try: res = requests.get(url, timeout=10) json = res.json() @@ -84,9 +87,9 @@ def wait_for_tx(esplora_endpoint, txid): print(f"Error: {e}") attempts += 1 - time.sleep(0.5) + time.sleep(SLEEP_TIME) - raise Exception(f"Failed to confirm transaction {txid} after {max_attempts} attempts") + raise Exception(f"Failed to confirm transaction {txid} after {DEFAULT_MAX_ATTEMPS} attempts") def send_to_address(address, amount_sats): amount_btc = amount_sats/100000000.0 @@ -112,6 +115,25 @@ def get_esplora_endpoint(): return str(os.environ['ESPLORA_ENDPOINT']) return DEFAULT_ESPLORA_SERVER_URL +# handling expect events + +def expect_event(node, expected_event_type): + event = node.wait_next_event() + assert isinstance(event, expected_event_type) + print("EVENT:", event) + node.event_handled() + #According to the event type, we may want to return some data from the event for further processing + match expected_event_type: + case Event.CHANNEL_PENDING: + return event.funding_txo.txid + case Event.PAYMENT_RECEIVED: + return event.payment_hash + case Event.CHANNEL_READY: + if node.name == "node_2": + return event.user.channel_id + case _: + return None + class TestLdkNode(unittest.TestCase): def setUp(self): bitcoin_cli("createwallet ldk_node_test") @@ -175,59 +197,42 @@ 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() + # expect the channel pending event on the node 1 then get the funding txid from the event - 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() + funding_txid = expect_event(node_1, Event.CHANNEL_PENDING) + + # expect channel pending on node 2 + expect_event(node_2, Event.CHANNEL_PENDING) - funding_txid = channel_pending_event_1.funding_txo.txid wait_for_tx(esplora_endpoint, funding_txid) mine_and_wait(esplora_endpoint, 6) 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) - print("funding_txo:", funding_txid) - node_1.event_handled() + expect_event(node_1, Event.CHANNEL_READY) + print(f"Node 1 channel ready with node 2, funding txid: {funding_txid}") - 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() + node_2_channel_id = expect_event(node_2, Event.CHANNEL_READY) 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 on node 1 + 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 payment received on node_2 + expect_event(node_2, Event.PAYMENT_RECEIVED) + - node_2.close_channel(channel_ready_event_2.user_channel_id, node_id_1) + node_2.close_channel(node_2_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() + #Expecting node 1 channel closed + 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() + #expecting channel close + expect_event(node_2, Event.CHANNEL_CLOSED) mine_and_wait(esplora_endpoint, 1) @@ -249,6 +254,8 @@ def test_channel_full_cycle(self): tmp_dir_1.cleanup() tmp_dir_2.cleanup() + + if __name__ == '__main__': unittest.main()