From 8a63de53e66a6736bddde44dac7ab5bafa235858 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 28 Oct 2022 03:31:01 +0000 Subject: [PATCH] [TS] Update TS Tests to latest upstream API and test OMs --- node-net/test/test.mts | 4 +- ts/test/tests.mts | 93 ++++++++++++++++++++++++++++++++---------- 2 files changed, 74 insertions(+), 23 deletions(-) diff --git a/node-net/test/test.mts b/node-net/test/test.mts index 86b6b3a2..e641ec1a 100644 --- a/node-net/test/test.mts +++ b/node-net/test/test.mts @@ -27,7 +27,7 @@ const chan_handler = ldk.ErroringMessageHandler.constructor_new().as_ChannelMess const cust_handler = ldk.IgnoringMessageHandler.constructor_new().as_CustomMessageHandler(); const onion_handler = ldk.IgnoringMessageHandler.constructor_new().as_OnionMessageHandler(); -const a_pm = ldk.PeerManager.constructor_new(chan_handler, routing_handler, onion_handler, node_a_secret, 0xdeadbeefn, rng_seed, logger_a, cust_handler); +const a_pm = ldk.PeerManager.constructor_new(chan_handler, routing_handler, onion_handler, node_a_secret, 0xdeadbeef, rng_seed, logger_a, cust_handler); const a_net_handler = new node_net.NodeLDKNet(a_pm); var port = 10000; for (; port < 11000; port++) { @@ -38,7 +38,7 @@ for (; port < 11000; port++) { } catch(_) {} } -const b_pm = ldk.PeerManager.constructor_new(chan_handler, routing_handler, onion_handler, node_b_secret, 0xdeadbeefn, rng_seed, logger_b, cust_handler); +const b_pm = ldk.PeerManager.constructor_new(chan_handler, routing_handler, onion_handler, node_b_secret, 0xdeadbeef, rng_seed, logger_b, cust_handler); const b_net_handler = new node_net.NodeLDKNet(b_pm); await b_net_handler.connect_peer("127.0.0.1", port, node_a_pk); diff --git a/ts/test/tests.mts b/ts/test/tests.mts index ffd471ec..534bee55 100644 --- a/ts/test/tests.mts +++ b/ts/test/tests.mts @@ -83,11 +83,11 @@ function get_chanman(): Node { } as ldk.LoggerInterface); const persister = ldk.Persist.new_impl({ - persist_new_channel(channel_id: ldk.OutPoint, data: ldk.ChannelMonitor, update_id: ldk.MonitorUpdateId): ldk.Result_NoneChannelMonitorUpdateErrZ { - return ldk.Result_NoneChannelMonitorUpdateErrZ.constructor_ok(); + persist_new_channel(channel_id: ldk.OutPoint, data: ldk.ChannelMonitor, update_id: ldk.MonitorUpdateId): ldk.ChannelMonitorUpdateStatus { + return ldk.ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_Completed; }, - update_persisted_channel(channel_id: ldk.OutPoint, update: ldk.ChannelMonitorUpdate, data: ldk.ChannelMonitor, update_id: ldk.MonitorUpdateId): ldk.Result_NoneChannelMonitorUpdateErrZ { - return ldk.Result_NoneChannelMonitorUpdateErrZ.constructor_ok(); + update_persisted_channel(channel_id: ldk.OutPoint, update: ldk.ChannelMonitorUpdate, data: ldk.ChannelMonitor, update_id: ldk.MonitorUpdateId): ldk.ChannelMonitorUpdateStatus { + return ldk.ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_Completed; } } as ldk.PersistInterface); @@ -119,10 +119,12 @@ function exchange_messages(a: ldk.ChannelManager, b: ldk.ChannelManager) { const msg = msgs[j]; if (msg instanceof ldk.MessageSendEvent_SendOpenChannel) { if (!array_eq(msg.node_id, to.get_our_node_id())) return false; - to.as_ChannelMessageHandler().handle_open_channel(from.get_our_node_id(), ldk.InitFeatures.constructor_known(), msg.msg); + const features = from.as_ChannelMessageHandler().provided_init_features(to.get_our_node_id()); + to.as_ChannelMessageHandler().handle_open_channel(from.get_our_node_id(), features, msg.msg); } else if (msg instanceof ldk.MessageSendEvent_SendAcceptChannel) { if (!array_eq(msg.node_id, to.get_our_node_id())) return false; - to.as_ChannelMessageHandler().handle_accept_channel(from.get_our_node_id(), ldk.InitFeatures.constructor_known(), msg.msg); + const features = from.as_ChannelMessageHandler().provided_init_features(to.get_our_node_id()); + to.as_ChannelMessageHandler().handle_accept_channel(from.get_our_node_id(), features, msg.msg); } else if (msg instanceof ldk.MessageSendEvent_SendFundingCreated) { if (!array_eq(msg.node_id, to.get_our_node_id())) return false; to.as_ChannelMessageHandler().handle_funding_created(from.get_our_node_id(), msg.msg); @@ -166,8 +168,10 @@ tests.push(async () => { const a = get_chanman(); const b = get_chanman(); - a.chan_man.as_ChannelMessageHandler().peer_connected(b.chan_man.get_our_node_id(), ldk.Init.constructor_new(ldk.InitFeatures.constructor_known(), ldk.Option_NetAddressZ.constructor_none())); - b.chan_man.as_ChannelMessageHandler().peer_connected(a.chan_man.get_our_node_id(), ldk.Init.constructor_new(ldk.InitFeatures.constructor_known(), ldk.Option_NetAddressZ.constructor_none())); + const features = a.chan_man.as_ChannelMessageHandler().provided_init_features(b.chan_man.get_our_node_id()); + + a.chan_man.as_ChannelMessageHandler().peer_connected(b.chan_man.get_our_node_id(), ldk.Init.constructor_new(features, ldk.Option_NetAddressZ.constructor_none())); + b.chan_man.as_ChannelMessageHandler().peer_connected(a.chan_man.get_our_node_id(), ldk.Init.constructor_new(features, ldk.Option_NetAddressZ.constructor_none())); const chan_create_err = a.chan_man.create_channel(b.chan_man.get_our_node_id(), BigInt(0), BigInt(400), BigInt(0), ldk.UserConfig.constructor_default()); if (chan_create_err.is_ok()) return false; @@ -214,8 +218,8 @@ tests.push(async () => { const b = get_chanman(); const ignorer = ldk.IgnoringMessageHandler.constructor_new(); - const pm_a = ldk.PeerManager.constructor_new(a.chan_man.as_ChannelMessageHandler(), ignorer.as_RoutingMessageHandler(), ignorer.as_OnionMessageHandler(), a.node_secret, 0xdeadbeefn, a.node_secret, a.logger, ignorer.as_CustomMessageHandler()); - const pm_b = ldk.PeerManager.constructor_new(b.chan_man.as_ChannelMessageHandler(), ignorer.as_RoutingMessageHandler(), ignorer.as_OnionMessageHandler(), b.node_secret, 0xdeadbeefn, b.node_secret, b.logger, ignorer.as_CustomMessageHandler()); + const pm_a = ldk.PeerManager.constructor_new(a.chan_man.as_ChannelMessageHandler(), ignorer.as_RoutingMessageHandler(), ignorer.as_OnionMessageHandler(), a.node_secret, 0xdeadbeef, a.node_secret, a.logger, ignorer.as_CustomMessageHandler()); + const pm_b = ldk.PeerManager.constructor_new(b.chan_man.as_ChannelMessageHandler(), ignorer.as_RoutingMessageHandler(), ignorer.as_OnionMessageHandler(), b.node_secret, 0xdeadbeef, b.node_secret, b.logger, ignorer.as_CustomMessageHandler()); var sock_b: ldk.SocketDescriptor; const sock_a = ldk.SocketDescriptor.new_impl({ @@ -292,7 +296,24 @@ tests.push(async () => { const ignorer = ldk.IgnoringMessageHandler.constructor_new(); - const underlying_om_a = ldk.OnionMessenger.constructor_new(a.keys_interface, a.logger); + var a_handled_msg = false; + const om_handler_a = ldk.CustomOnionMessageHandler.new_impl({ + read_custom_message(message_type: bigint, buffer: Uint8Array): ldk.Result_COption_CustomOnionMessageContentsZDecodeErrorZ { + console.assert(message_type == 4343n); + console.assert(buffer.length == 44); + for (var i = 0; i < 44; i++) console.assert(buffer[i] == 67); + return ldk.Result_COption_CustomOnionMessageContentsZDecodeErrorZ.constructor_ok(ldk.Option_CustomOnionMessageContentsZ.constructor_some(ldk.CustomOnionMessageContents.new_impl({ + tlv_type(): bigint { return 9998n; }, + write(): Uint8Array { console.assert(false); return null; } + } as ldk.CustomOnionMessageContentsInterface))); + }, + handle_custom_message(msg: ldk.CustomOnionMessageContents) { + console.assert(msg.tlv_type() == 9998n); + a_handled_msg = true; + }, + } as ldk.CustomOnionMessageHandlerInterface); + + const underlying_om_a = ldk.OnionMessenger.constructor_new(a.keys_interface, a.logger, om_handler_a); const om_provider_a = { next_onion_message_for_peer(peer_node_id: Uint8Array): ldk.OnionMessage { return underlying_om_a.as_OnionMessageProvider().next_onion_message_for_peer(peer_node_id); @@ -302,8 +323,8 @@ tests.push(async () => { handle_onion_message(peer_node_id: Uint8Array, msg: ldk.OnionMessage) { underlying_om_a.as_OnionMessageHandler().handle_onion_message(peer_node_id, msg); }, - peer_connected(their_node_id: Uint8Array, init: ldk.Init) { - underlying_om_a.as_OnionMessageHandler().peer_connected(their_node_id, init); + peer_connected(their_node_id: Uint8Array, init: ldk.Init): ldk.Result_NoneNoneZ { + return underlying_om_a.as_OnionMessageHandler().peer_connected(their_node_id, init) }, peer_disconnected(their_node_id: Uint8Array, no_connection_possible: boolean) { underlying_om_a.as_OnionMessageHandler().peer_disconnected(their_node_id, no_connection_possible); @@ -316,10 +337,26 @@ tests.push(async () => { } } as ldk.OnionMessageHandlerInterface, om_provider_a); - const om_b = ldk.OnionMessenger.constructor_new(b.keys_interface, b.logger); + var b_handled_msg = false; + const om_handler_b = ldk.CustomOnionMessageHandler.new_impl({ + read_custom_message(message_type: bigint, buffer: Uint8Array): ldk.Result_COption_CustomOnionMessageContentsZDecodeErrorZ { + console.assert(message_type == 4242n); + console.assert(buffer.length == 43); + for (var i = 0; i < 43; i++) console.assert(buffer[i] == 66); + return ldk.Result_COption_CustomOnionMessageContentsZDecodeErrorZ.constructor_ok(ldk.Option_CustomOnionMessageContentsZ.constructor_some(ldk.CustomOnionMessageContents.new_impl({ + tlv_type(): bigint { return 9999n; }, + write(): Uint8Array { console.assert(false); return null; } + } as ldk.CustomOnionMessageContentsInterface))); + }, + handle_custom_message(msg: ldk.CustomOnionMessageContents) { + console.assert(msg.tlv_type() == 9999n); + b_handled_msg = true; + }, + } as ldk.CustomOnionMessageHandlerInterface); + const om_b = ldk.OnionMessenger.constructor_new(b.keys_interface, b.logger, om_handler_b); - const pm_a = ldk.PeerManager.constructor_new(a.chan_man.as_ChannelMessageHandler(), ignorer.as_RoutingMessageHandler(), om_a, a.node_secret, 0xdeadbeefn, a.node_secret, a.logger, ignorer.as_CustomMessageHandler()); - const pm_b = ldk.PeerManager.constructor_new(b.chan_man.as_ChannelMessageHandler(), ignorer.as_RoutingMessageHandler(), om_b.as_OnionMessageHandler(), b.node_secret, 0xdeadbeefn, b.node_secret, b.logger, ignorer.as_CustomMessageHandler()); + const pm_a = ldk.PeerManager.constructor_new(a.chan_man.as_ChannelMessageHandler(), ignorer.as_RoutingMessageHandler(), om_a, a.node_secret, 0xdeadbeef, a.node_secret, a.logger, ignorer.as_CustomMessageHandler()); + const pm_b = ldk.PeerManager.constructor_new(b.chan_man.as_ChannelMessageHandler(), ignorer.as_RoutingMessageHandler(), om_b.as_OnionMessageHandler(), b.node_secret, 0xdeadbeef, b.node_secret, b.logger, ignorer.as_CustomMessageHandler()); var sock_b: ldk.SocketDescriptor; const sock_a = ldk.SocketDescriptor.new_impl({ @@ -369,13 +406,27 @@ tests.push(async () => { console.assert(pm_a.get_peer_node_ids().length == 1); console.assert(pm_b.get_peer_node_ids().length == 1); - underlying_om_a.send_onion_message([], ldk.Destination.constructor_node(b.node_id), null); + underlying_om_a.send_custom_onion_message([], ldk.Destination.constructor_node(b.node_id), ldk.CustomOnionMessageContents.new_impl({ + tlv_type(): bigint { return 4242n; }, + write(): Uint8Array { + const ret = new Uint8Array(43); + for (var i = 0; i < 43; i++) ret[i] = 66; + return ret; + } + } as ldk.CustomOnionMessageContentsInterface), null); pm_a.process_events(); - om_b.send_onion_message([], ldk.Destination.constructor_node(a.node_id), null); + console.assert(b_handled_msg); + + om_b.send_custom_onion_message([], ldk.Destination.constructor_node(a.node_id), ldk.CustomOnionMessageContents.new_impl({ + tlv_type(): bigint { return 4343n; }, + write(): Uint8Array { + const ret = new Uint8Array(44); + for (var i = 0; i < 44; i++) ret[i] = 67; + return ret; + } + } as ldk.CustomOnionMessageContentsInterface), null); pm_b.process_events(); - - // TODO: Once OnionMessenger supports actually passing messages up, check that we received the - // messages here. + console.assert(a_handled_msg); return true; }); -- 2.39.5