From f5faaf0743b195d6aa8288bb9cb84b87660fa7c2 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 30 Sep 2022 01:07:46 +0000 Subject: [PATCH] [Java] Update ChannelManagerConstructor and tests to latest API --- .../batteries/ChannelManagerConstructor.java | 41 +++++++---- .../java/org/ldk/HumanObjectPeerTest.java | 69 +++++++++++-------- .../org/ldk/ManualMsgHandlingPeerTest.java | 48 +++++++++++-- src/test/java/org/ldk/PeerTest.java | 12 +++- 4 files changed, 119 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java b/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java index 109acbe3..dc1cee48 100644 --- a/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java +++ b/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java @@ -88,7 +88,7 @@ public class ChannelManagerConstructor { KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor, @Nullable Filter filter, @Nullable byte[] net_graph_serialized, BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException { - final IgnoringMessageHandler no_custom_messages = IgnoringMessageHandler.of(); + final IgnoringMessageHandler ignoring_handler = IgnoringMessageHandler.of(); final ChannelMonitor[] monitors = new ChannelMonitor[channel_monitors_serialized.length]; this.channel_monitors = new TwoTuple_BlockHashChannelMonitorZ[monitors.length]; HashSet monitor_funding_set = new HashSet(); @@ -130,13 +130,18 @@ public class ChannelManagerConstructor { this.graph_msg_handler = P2PGossipSync.of(net_graph, Option_AccessZ.none(), logger); this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), graph_msg_handler.as_RoutingMessageHandler(), + ignoring_handler.as_OnionMessageHandler(), ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res, - random_data, logger, no_custom_messages.as_CustomMessageHandler()); + System.currentTimeMillis() / 1000, + random_data, logger, ignoring_handler.as_CustomMessageHandler()); } else { this.graph_msg_handler = null; - this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), no_custom_messages.as_RoutingMessageHandler(), + this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), + ignoring_handler.as_RoutingMessageHandler(), + ignoring_handler.as_OnionMessageHandler(), ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res, - random_data, logger, no_custom_messages.as_CustomMessageHandler()); + System.currentTimeMillis() / 1000, + random_data, logger, ignoring_handler.as_CustomMessageHandler()); } NioPeerHandler nio_peer_handler = null; try { @@ -160,7 +165,7 @@ public class ChannelManagerConstructor { KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor, @Nullable NetworkGraph net_graph, BroadcasterInterface tx_broadcaster, Logger logger) { - final IgnoringMessageHandler no_custom_messages = IgnoringMessageHandler.of(); + final IgnoringMessageHandler ignoring_handler = IgnoringMessageHandler.of(); channel_monitors = new TwoTuple_BlockHashChannelMonitorZ[0]; channel_manager_latest_block_hash = null; this.chain_monitor = chain_monitor; @@ -177,13 +182,18 @@ public class ChannelManagerConstructor { this.graph_msg_handler = P2PGossipSync.of(net_graph, Option_AccessZ.none(), logger); this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), graph_msg_handler.as_RoutingMessageHandler(), + ignoring_handler.as_OnionMessageHandler(), ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res, - random_data, logger, no_custom_messages.as_CustomMessageHandler()); + System.currentTimeMillis() / 1000, + random_data, logger, ignoring_handler.as_CustomMessageHandler()); } else { this.graph_msg_handler = null; - this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), no_custom_messages.as_RoutingMessageHandler(), + this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), + ignoring_handler.as_RoutingMessageHandler(), + ignoring_handler.as_OnionMessageHandler(), ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res, - random_data, logger, no_custom_messages.as_CustomMessageHandler()); + System.currentTimeMillis() / 1000, + random_data, logger, ignoring_handler.as_CustomMessageHandler()); } NioPeerHandler nio_peer_handler = null; try { @@ -222,9 +232,8 @@ public class ChannelManagerConstructor { } org.ldk.structs.EventHandler ldk_handler = org.ldk.structs.EventHandler.new_impl(event_handler::handle_event); if (this.net_graph != null && scorer != null) { - Router router = DefaultRouter.of(net_graph, logger, router_rand_bytes).as_Router(); - this.payer = InvoicePayer.of(this.channel_manager.as_Payer(), router, scorer, this.logger, ldk_handler, Retry.attempts(3)); -assert this.payer != null; + Router router = DefaultRouter.of(net_graph, logger, router_rand_bytes, scorer.as_LockableScore()).as_Router(); + this.payer = InvoicePayer.of(this.channel_manager.as_Payer(), router, this.logger, ldk_handler, Retry.attempts(3)); ldk_handler = this.payer.as_EventHandler(); } @@ -234,6 +243,12 @@ assert this.payer != null; else gossip_sync = GossipSync.p2_p(this.graph_msg_handler); + Option_WriteableScoreZ writeable_score; + if (scorer != null) + writeable_score = Option_WriteableScoreZ.some(scorer.as_WriteableScore()); + else + writeable_score = Option_WriteableScoreZ.none(); + background_processor = BackgroundProcessor.start(Persister.new_impl(new Persister.PersisterInterface() { @Override public Result_NoneErrorZ persist_manager(ChannelManager channel_manager) { @@ -248,11 +263,11 @@ assert this.payer != null; } @Override - public Result_NoneErrorZ persist_scorer(MultiThreadedLockableScore scorer) { + public Result_NoneErrorZ persist_scorer(WriteableScore scorer) { event_handler.persist_scorer(scorer.write()); return Result_NoneErrorZ.ok(); } - }), ldk_handler, this.chain_monitor, this.channel_manager, gossip_sync, this.peer_manager, this.logger, scorer); + }), ldk_handler, this.chain_monitor, this.channel_manager, gossip_sync, this.peer_manager, this.logger, writeable_score); } /** diff --git a/src/test/java/org/ldk/HumanObjectPeerTest.java b/src/test/java/org/ldk/HumanObjectPeerTest.java index 1a1f5f48..f12bd785 100644 --- a/src/test/java/org/ldk/HumanObjectPeerTest.java +++ b/src/test/java/org/ldk/HumanObjectPeerTest.java @@ -46,6 +46,8 @@ class HumanObjectPeerTestInstance { KeysInterface manual_keysif(KeysInterface underlying_if) { return KeysInterface.new_impl(new KeysInterface.KeysInterfaceInterface() { @Override public Result_SecretKeyNoneZ get_node_secret(Recipient recipient) { return underlying_if.get_node_secret(recipient); } + + @Override public Result_SharedSecretNoneZ ecdh(Recipient recipient, byte[] other_key, Option_ScalarZ tweak) { return underlying_if.ecdh(recipient, other_key, tweak); } @Override public byte[] get_destination_script() { return underlying_if.get_destination_script(); } @Override public ShutdownScript get_shutdown_scriptpubkey() { return underlying_if.get_shutdown_scriptpubkey(); } @@ -271,9 +273,8 @@ class HumanObjectPeerTestInstance { @Override public void register_tx(byte[] txid, byte[] script_pubkey) { filter_additions.add(Arrays.toString(txid)); } - @Override public Option_C2Tuple_usizeTransactionZZ register_output(WatchedOutput output) { + @Override public void register_output(WatchedOutput output) { filter_additions.add(Arrays.toString(output.get_outpoint().get_txid()) + ":" + output.get_outpoint().get_index()); - return Option_C2Tuple_usizeTransactionZZ.none(); } })); } else { @@ -399,11 +400,18 @@ class HumanObjectPeerTestInstance { Result_SecretKeyNoneZ node_secret = keys_interface.get_node_secret(Recipient.LDKRecipient_Node); assert node_secret.is_ok(); this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), route_handler.as_RoutingMessageHandler(), - ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res, random_data, logger, this.custom_message_handler); + IgnoringMessageHandler.of().as_OnionMessageHandler(), + ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res, System.currentTimeMillis() / 1000, + random_data, logger, this.custom_message_handler); if (use_invoice_payer) { this.payer = InvoicePayer.of(this.chan_manager.as_Payer(), Router.new_impl(new Router.RouterInterface() { + @Override public void notify_payment_path_failed(RouteHop[] path, long short_channel_id) {} + @Override public void notify_payment_path_successful(RouteHop[] path) {} + @Override public void notify_payment_probe_successful(RouteHop[] path) {} + @Override public void notify_payment_probe_failed(RouteHop[] path, long short_channel_id) {} + @Override - public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters params, byte[] payment_hash, ChannelDetails[] first_hops, Score scorer) { + public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters params, byte[] payment_hash, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs) { while (true) { try (ReadOnlyNetworkGraph graph = router.read_only()) { assert graph.channel(424242) == null; @@ -411,9 +419,8 @@ class HumanObjectPeerTestInstance { if (channels.length != 1) { // If we're using a NioPeerHandler, the handling of announcement signatures and // channel broadcasting may be done async, so just wait until the channel shows up. - // TODO: Once https://github.com/lightningdevkit/rust-lightning/pull/1666 lands swap this for a continue and add the assertion - //assert !use_nio_peer_handler; - break; + assert use_nio_peer_handler; + continue; } ChannelInfo chan = graph.channel(channels[0]); assert Arrays.equals(chan.get_node_one().as_slice(), chan.get_node_one().write()); @@ -422,16 +429,16 @@ class HumanObjectPeerTestInstance { break; } } - return UtilMethods.find_route(payer, params, router, first_hops, logger, scorer, new byte[32]); + return UtilMethods.find_route(payer, params, router, first_hops, logger, Score.new_impl(new Score.ScoreInterface() { + @Override public void payment_path_failed(RouteHop[] path, long scid) {} + @Override public long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target, ChannelUsage usage) { return 0; } + @Override public void payment_path_successful(RouteHop[] path) {} + @Override public void probe_failed(RouteHop[] path, long short_channel_id) { assert false; } + @Override public void probe_successful(RouteHop[] path) { assert false; } + @Override public byte[] write() { assert false; return null; } + }), new byte[32]); } - }), MultiThreadedLockableScore.of(Score.new_impl(new Score.ScoreInterface() { - @Override public void payment_path_failed(RouteHop[] path, long scid) {} - @Override public long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target, ChannelUsage usage) { return 0; } - @Override public void payment_path_successful(RouteHop[] path) {} - @Override public void probe_failed(RouteHop[] path, long short_channel_id) { assert false; } - @Override public void probe_successful(RouteHop[] path) { assert false; } - @Override public byte[] write() { assert false; return null; } - })), logger, EventHandler.new_impl(new EventHandler.EventHandlerInterface() { + }), logger, EventHandler.new_impl(new EventHandler.EventHandlerInterface() { @Override public void handle_event(Event event) { synchronized (pending_manager_events) { pending_manager_events.add(event); @@ -532,7 +539,8 @@ class HumanObjectPeerTestInstance { Result_SecretKeyNoneZ node_secret = keys_interface.get_node_secret(Recipient.LDKRecipient_Node); assert node_secret.is_ok(); this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), route_handler.as_RoutingMessageHandler(), - ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res, + IgnoringMessageHandler.of().as_OnionMessageHandler(), + ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res, System.currentTimeMillis() / 1000, random_data, logger, this.custom_message_handler); if (!break_cross_peer_refs && (use_manual_watch || use_km_wrapper)) { // When we pass monitors[0] into chain_watch.watch_channel we create a reference from the new Peer to a @@ -542,18 +550,23 @@ class HumanObjectPeerTestInstance { } if (use_invoice_payer) { this.payer = InvoicePayer.of(this.chan_manager.as_Payer(), Router.new_impl(new Router.RouterInterface() { + @Override public void notify_payment_path_failed(RouteHop[] path, long short_channel_id) {} + @Override public void notify_payment_path_successful(RouteHop[] path) {} + @Override public void notify_payment_probe_successful(RouteHop[] path) {} + @Override public void notify_payment_probe_failed(RouteHop[] path, long short_channel_id) {} + @Override - public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters params, byte[] _payment_hash, ChannelDetails[] first_hops, Score scorer) { - return UtilMethods.find_route(payer, params, router, first_hops, logger, scorer, new byte[32]); + public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters route_params, byte[] payment_hash, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs) { + return UtilMethods.find_route(payer, route_params, router, first_hops, logger, Score.new_impl(new Score.ScoreInterface() { + @Override public long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target, ChannelUsage usage) { return 0; } + @Override public void payment_path_failed(RouteHop[] path, long scid) {} + @Override public void payment_path_successful(RouteHop[] path) {} + @Override public void probe_failed(RouteHop[] path, long short_channel_id) { assert false; } + @Override public void probe_successful(RouteHop[] path) { assert false; } + @Override public byte[] write() { assert false; return null; } + }), new byte[32]); } - }), MultiThreadedLockableScore.of(Score.new_impl(new Score.ScoreInterface() { - @Override public long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target, ChannelUsage usage) { return 0; } - @Override public void payment_path_failed(RouteHop[] path, long scid) {} - @Override public void payment_path_successful(RouteHop[] path) {} - @Override public void probe_failed(RouteHop[] path, long short_channel_id) { assert false; } - @Override public void probe_successful(RouteHop[] path) { assert false; } - @Override public byte[] write() { assert false; return null; } - })), logger, EventHandler.new_impl(new EventHandler.EventHandlerInterface() { + }), logger, EventHandler.new_impl(new EventHandler.EventHandlerInterface() { @Override public void handle_event(Event event) { synchronized (pending_manager_events) { pending_manager_events.add(event); @@ -869,7 +882,7 @@ class HumanObjectPeerTestInstance { assert Arrays.equals(((Result_InvoiceParseOrSemanticErrorZ.Result_InvoiceParseOrSemanticErrorZ_OK) parsed_invoice).res.payment_hash(), ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash()); SignedRawInvoice signed_raw = ((Result_InvoiceParseOrSemanticErrorZ.Result_InvoiceParseOrSemanticErrorZ_OK) parsed_invoice).res.into_signed_raw(); RawInvoice raw_invoice = signed_raw.raw_invoice(); - byte[] desc_hash = raw_invoice.hash(); + byte[] desc_hash = raw_invoice.signable_hash(); Description raw_invoice_description = raw_invoice.description(); String description_string = raw_invoice_description.into_inner(); assert description_string.equals("Invoice Description"); diff --git a/src/test/java/org/ldk/ManualMsgHandlingPeerTest.java b/src/test/java/org/ldk/ManualMsgHandlingPeerTest.java index 40c536ee..1cd1eb30 100644 --- a/src/test/java/org/ldk/ManualMsgHandlingPeerTest.java +++ b/src/test/java/org/ldk/ManualMsgHandlingPeerTest.java @@ -112,6 +112,18 @@ public class ManualMsgHandlingPeerTest { public void handle_error(byte[] their_node_id, long msg) { } + + @Override + public long provided_node_features() { + assert false; + return 0; + } + + @Override + public long provided_init_features(byte[] their_node_id) { + assert false; + return 0; + } }, () -> new long[0]); long route_handler = bindings.LDKRoutingMessageHandler_new(new bindings.LDKRoutingMessageHandler() { @Override public long handle_node_announcement(long msg) { @@ -123,11 +135,17 @@ public class ManualMsgHandlingPeerTest { @Override public long handle_channel_update(long msg) { return 0; } - @Override public long[] get_next_channel_announcements(long starting_point, byte batch_amount) { - return new long[0]; + + @Override + public long get_next_channel_announcement(long starting_point) { + assert false; + return 0; } - @Override public long[] get_next_node_announcements(byte[] starting_point, byte batch_amount) { - return new long[0]; + + @Override + public long get_next_node_announcement(byte[] starting_point) { + assert false; + return 0; } @Override public void peer_connected(byte[] their_node_id, long init) { } @@ -151,14 +169,28 @@ public class ManualMsgHandlingPeerTest { public long handle_query_short_channel_ids(byte[] their_node_id, long msg) { return 0; } + + @Override + public long provided_node_features() { + assert false; + return 0; + } + + @Override + public long provided_init_features(byte[] their_node_id) { + assert false; + return 0; + } }, () -> new long[0]); - long message_handler = bindings.MessageHandler_new(chan_handler, route_handler); + long ignoring_message_handler = bindings.IgnoringMessageHandler_new(); + long onion_message_handler = bindings.IgnoringMessageHandler_as_OnionMessageHandler(ignoring_message_handler); + long message_handler = bindings.MessageHandler_new(chan_handler, route_handler, onion_message_handler); byte[] our_node_secret = new byte[32]; byte[] random_data = new byte[32]; for (byte i = 0; i < 32; i++) { random_data[i] = i; our_node_secret[i] = (byte) (i ^ 0xff); } - long ignoring_message_handler = bindings.IgnoringMessageHandler_new(); - long peer_manager = bindings.PeerManager_new(message_handler, our_node_secret, random_data, logger, + long peer_manager = bindings.PeerManager_new(message_handler, our_node_secret, System.currentTimeMillis() / 1000, + random_data, logger, bindings.IgnoringMessageHandler_as_CustomMessageHandler(ignoring_message_handler)); // Test Level_max() since its the only place we create a java object from a Rust-returned enum. @@ -168,6 +200,8 @@ public class ManualMsgHandlingPeerTest { bindings.Logger_free(logger); bindings.ChannelMessageHandler_free(chan_handler); bindings.RoutingMessageHandler_free(route_handler); + bindings.OnionMessageHandler_free(onion_message_handler); + bindings.IgnoringMessageHandler_free(ignoring_message_handler); //bindings.MessageHandler_free(message_handler); bindings.PeerManager_free(peer_manager); } diff --git a/src/test/java/org/ldk/PeerTest.java b/src/test/java/org/ldk/PeerTest.java index ba1b4e0e..63a4e169 100644 --- a/src/test/java/org/ldk/PeerTest.java +++ b/src/test/java/org/ldk/PeerTest.java @@ -31,7 +31,9 @@ public class PeerTest { final long router_wrapper; final long route_handler; final long message_handler; + final long ignoring_message_handler; final long custom_message_handler; + final long onion_message_handler; final long peer_manager; HashMap monitors; // Wow I forgot just how terrible Java is - we can't put a byte array here. byte[] node_id; @@ -110,8 +112,10 @@ public class PeerTest { this.router = bindings.NetworkGraph_new(new byte[32], logger); this.router_wrapper = bindings.P2PGossipSync_new(router, bindings.COption_AccessZ_none(), logger); this.route_handler = bindings.P2PGossipSync_as_RoutingMessageHandler(router_wrapper); - this.message_handler = bindings.MessageHandler_new(chan_handler, route_handler); - this.custom_message_handler = bindings.IgnoringMessageHandler_new(); + this.ignoring_message_handler = bindings.IgnoringMessageHandler_new(); + this.custom_message_handler = bindings.IgnoringMessageHandler_as_CustomMessageHandler(this.ignoring_message_handler); + this.onion_message_handler = bindings.IgnoringMessageHandler_as_OnionMessageHandler(this.ignoring_message_handler); + this.message_handler = bindings.MessageHandler_new(chan_handler, route_handler, this.onion_message_handler); byte[] random_data = new byte[32]; for (byte i = 0; i < 32; i++) { random_data[i] = (byte) ((i ^ seed) ^ 0xf0); } @@ -119,7 +123,7 @@ public class PeerTest { long node_id_result = bindings.KeysInterface_get_node_secret(keys_interface, Recipient.LDKRecipient_Node); assert bindings.CResult_SecretKeyNoneZ_is_ok(node_id_result); this.peer_manager = bindings.PeerManager_new(message_handler, bindings.CResult_SecretKeyNoneZ_get_ok(node_id_result), - random_data, logger, bindings.IgnoringMessageHandler_as_CustomMessageHandler(this.custom_message_handler)); + System.currentTimeMillis() / 1000, random_data, logger, this.custom_message_handler); bindings.CResult_SecretKeyNoneZ_free(node_id_result); } @@ -158,6 +162,8 @@ public class PeerTest { bindings.NetworkGraph_free(router); bindings.P2PGossipSync_free(router_wrapper); bindings.RoutingMessageHandler_free(route_handler); + bindings.CustomMessageHandler_free(custom_message_handler); + bindings.OnionMessageHandler_free(onion_message_handler); //MessageHandler was actually moved into the route_handler!: bindings.MessageHandler_free(message_handler); bindings.PeerManager_free(peer_manager); synchronized (monitors) { -- 2.39.5