Test use of IgnoringMessageHandler with ChannelManagerConstructor
[ldk-java] / src / test / java / org / ldk / HumanObjectPeerTest.java
index 79085c5362654b7913a381d0c1af24fa5090e85b..57b750427fbe28b8af191a3d3cc4601a93dcaf6d 100644 (file)
@@ -23,9 +23,10 @@ class HumanObjectPeerTestInstance {
     private final boolean break_cross_peer_refs;
     private final boolean use_nio_peer_handler;
     private final boolean use_filter;
+    private final boolean use_ignore_handler;
     private final boolean use_chan_manager_constructor;
 
-    HumanObjectPeerTestInstance(boolean nice_close, boolean use_km_wrapper, boolean use_manual_watch, boolean reload_peers, boolean break_cross_peer_refs, boolean use_nio_peer_handler, boolean use_filter, boolean use_chan_manager_constructor) {
+    HumanObjectPeerTestInstance(boolean nice_close, boolean use_km_wrapper, boolean use_manual_watch, boolean reload_peers, boolean break_cross_peer_refs, boolean use_nio_peer_handler, boolean use_filter, boolean use_ignore_handler, boolean use_chan_manager_constructor) {
         this.nice_close = nice_close;
         this.use_km_wrapper = use_km_wrapper;
         this.use_manual_watch = use_manual_watch;
@@ -33,6 +34,7 @@ class HumanObjectPeerTestInstance {
         this.break_cross_peer_refs = break_cross_peer_refs;
         this.use_nio_peer_handler = use_nio_peer_handler;
         this.use_filter = use_filter;
+        this.use_ignore_handler = use_ignore_handler;
         this.use_chan_manager_constructor = use_chan_manager_constructor;
     }
 
@@ -41,7 +43,7 @@ class HumanObjectPeerTestInstance {
             return KeysInterface.new_impl(new KeysInterface.KeysInterfaceInterface() {
                 @Override public byte[] get_node_secret() { return underlying_if.get_node_secret(); }
                 @Override public byte[] get_destination_script() { return underlying_if.get_destination_script(); }
-                @Override public byte[] get_shutdown_pubkey() { return underlying_if.get_shutdown_pubkey(); }
+                @Override public ShutdownScript get_shutdown_scriptpubkey() { return underlying_if.get_shutdown_scriptpubkey(); }
 
                 @Override
                 public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) {
@@ -296,8 +298,12 @@ class HumanObjectPeerTestInstance {
         Peer(byte seed) {
             this(null, seed);
             if (use_chan_manager_constructor) {
+                NetGraphMsgHandler route_handler = null;
+                if (!use_ignore_handler) {
+                    route_handler = router;
+                }
                 this.constructor = new ChannelManagerConstructor(Network.LDKNetwork_Bitcoin, UserConfig.with_default(), new byte[32], 0,
-                        this.keys_interface, this.fee_estimator, this.chain_monitor, this.router, this.tx_broadcaster, this.logger);
+                        this.keys_interface, this.fee_estimator, this.chain_monitor, route_handler, this.tx_broadcaster, this.logger);
                 constructor.chain_sync_completed(new ChannelManagerConstructor.ChannelManagerPersister() {
                     @Override public void handle_event(Event event) {
                         synchronized (pending_manager_events) {
@@ -612,12 +618,16 @@ class HumanObjectPeerTestInstance {
 
         connect_peers(peer1, peer2);
 
-        Result_NoneAPIErrorZ cc_res = peer1.chan_manager.create_channel(peer2.node_id, 10000, 1000, 42, null);
+        Result_NoneAPIErrorZ cc_res = peer1.chan_manager.create_channel(peer2.node_id, 100000, 1000, 42, null);
         assert cc_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
 
+        // Previously, this was a SEGFAULT instead of get_funding_txo() returning null.
+        ChannelDetails pre_funding_chan = peer1.chan_manager.list_channels()[0];
+        assert pre_funding_chan.get_funding_txo() == null;
+
         Event[] events = peer1.get_manager_events(1, peer1, peer2);
         assert events[0] instanceof Event.FundingGenerationReady;
-        assert ((Event.FundingGenerationReady) events[0]).channel_value_satoshis == 10000;
+        assert ((Event.FundingGenerationReady) events[0]).channel_value_satoshis == 100000;
         assert ((Event.FundingGenerationReady) events[0]).user_channel_id == 42;
         byte[] funding_spk = ((Event.FundingGenerationReady) events[0]).output_script;
         assert funding_spk.length == 34 && funding_spk[0] == 0 && funding_spk[1] == 32; // P2WSH
@@ -629,7 +639,7 @@ class HumanObjectPeerTestInstance {
         funding.addInput(new TransactionInput(bitcoinj_net, funding, new byte[0]));
         funding.getInputs().get(0).setWitness(new TransactionWitness(2)); // Make sure we don't complain about lack of witness
         funding.getInput(0).getWitness().setPush(0, new byte[]{0x1});
-        funding.addOutput(Coin.SATOSHI.multiply(10000), new Script(funding_spk));
+        funding.addOutput(Coin.SATOSHI.multiply(100000), new Script(funding_spk));
         Result_NoneAPIErrorZ funding_res = peer1.chan_manager.funding_transaction_generated(chan_id, funding.bitcoinSerialize());
         assert funding_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
 
@@ -662,7 +672,7 @@ class HumanObjectPeerTestInstance {
         ChannelDetails[] peer2_chans = peer2.chan_manager.list_usable_channels();
         assert peer1_chans.length == 1;
         assert peer2_chans.length == 1;
-        assert peer1_chans[0].get_channel_value_satoshis() == 10000;
+        assert peer1_chans[0].get_channel_value_satoshis() == 100000;
         assert peer1_chans[0].get_is_usable();
         Option_u64Z short_chan_id = peer1_chans[0].get_short_channel_id();
         assert short_chan_id instanceof Option_u64Z.Some;
@@ -693,7 +703,7 @@ class HumanObjectPeerTestInstance {
         Route route;
         try (LockedNetworkGraph netgraph = peer1.router.read_locked_graph()) {
             NetworkGraph graph = netgraph.graph();
-            Result_RouteLightningErrorZ route_res = UtilMethods.get_route(peer1.chan_manager.get_our_node_id(), graph, peer2.node_id, invoice_features, peer1_chans, route_hints, 1000000, 42, peer1.logger);
+            Result_RouteLightningErrorZ route_res = UtilMethods.get_route(peer1.chan_manager.get_our_node_id(), graph, peer2.node_id, invoice_features, peer1_chans, route_hints, 10000000, 42, peer1.logger);
             assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
             route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
         }
@@ -758,7 +768,8 @@ class HumanObjectPeerTestInstance {
 
         events = state.peer2.get_manager_events(1, state.peer1, state.peer2);
         assert events[0] instanceof Event.PaymentReceived;
-        byte[] payment_preimage = ((Event.PaymentReceived)events[0]).payment_preimage;
+        assert ((Event.PaymentReceived)events[0]).purpose instanceof PaymentPurpose.InvoicePayment;
+        byte[] payment_preimage = ((PaymentPurpose.InvoicePayment)((Event.PaymentReceived)events[0]).purpose).payment_preimage;
         assert !Arrays.equals(payment_preimage, new byte[32]);
         state.peer2.chan_manager.claim_funds(payment_preimage);
 
@@ -827,11 +838,14 @@ class HumanObjectPeerTestInstance {
             assert broadcastable_event.length == 1;
             assert broadcastable_event[0] instanceof Event.SpendableOutputs;
             if (state.peer2.explicit_keys_manager != null) {
-                Result_TransactionNoneZ tx_res = state.peer2.explicit_keys_manager.spend_spendable_outputs(((Event.SpendableOutputs) broadcastable_event[0]).outputs, new TxOut[0], new byte[] {0x00}, 253);
+                TxOut[] additional_outputs = new TxOut[] { new TxOut(420, new byte[] { 0x42 }) };
+                Result_TransactionNoneZ tx_res = state.peer2.explicit_keys_manager.spend_spendable_outputs(((Event.SpendableOutputs) broadcastable_event[0]).outputs, additional_outputs, new byte[] {0x00}, 253);
                 assert tx_res instanceof Result_TransactionNoneZ.Result_TransactionNoneZ_OK;
                 Transaction built_tx = new Transaction(bitcoinj_net, ((Result_TransactionNoneZ.Result_TransactionNoneZ_OK) tx_res).res);
-                assert built_tx.getOutputs().size() == 1;
-                assert Arrays.equals(built_tx.getOutput(0).getScriptBytes(), new byte[]{0x00});
+                assert built_tx.getOutputs().size() == 2;
+                assert Arrays.equals(built_tx.getOutput(1).getScriptBytes(), new byte[]{0x00});
+                assert Arrays.equals(built_tx.getOutput(0).getScriptBytes(), new byte[]{0x42});
+                assert built_tx.getOutput(0).getValue().value == 420;
             }
         }
 
@@ -867,14 +881,14 @@ class HumanObjectPeerTestInstance {
     }
 }
 public class HumanObjectPeerTest {
-    HumanObjectPeerTestInstance do_test_run(boolean nice_close, boolean use_km_wrapper, boolean use_manual_watch, boolean reload_peers, boolean break_cross_peer_refs, boolean nio_peer_handler, boolean use_chan_manager_constructor) throws InterruptedException {
-        HumanObjectPeerTestInstance instance = new HumanObjectPeerTestInstance(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler, !nio_peer_handler, use_chan_manager_constructor);
+    HumanObjectPeerTestInstance do_test_run(boolean nice_close, boolean use_km_wrapper, boolean use_manual_watch, boolean reload_peers, boolean break_cross_peer_refs, boolean nio_peer_handler, boolean use_ignoring_routing_handler, boolean use_chan_manager_constructor) throws InterruptedException {
+        HumanObjectPeerTestInstance instance = new HumanObjectPeerTestInstance(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler, !nio_peer_handler, use_ignoring_routing_handler, use_chan_manager_constructor);
         HumanObjectPeerTestInstance.TestState state = instance.do_test_message_handler();
         instance.do_test_message_handler_b(state);
         return instance;
     }
-    void do_test(boolean nice_close, boolean use_km_wrapper, boolean use_manual_watch, boolean reload_peers, boolean break_cross_peer_refs, boolean nio_peer_handler, boolean use_chan_manager_constructor) throws InterruptedException {
-        HumanObjectPeerTestInstance instance = do_test_run(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler, use_chan_manager_constructor);
+    void do_test(boolean nice_close, boolean use_km_wrapper, boolean use_manual_watch, boolean reload_peers, boolean break_cross_peer_refs, boolean nio_peer_handler, boolean use_ignoring_routing_handler, boolean use_chan_manager_constructor) throws InterruptedException {
+        HumanObjectPeerTestInstance instance = do_test_run(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler, use_ignoring_routing_handler, use_chan_manager_constructor);
         while (instance.gc_count != instance.gc_exp_count) {
             System.gc();
             System.runFinalization();
@@ -891,7 +905,8 @@ public class HumanObjectPeerTest {
             boolean reload_peers =                 (i & (1 << 3)) != 0;
             boolean break_cross_refs =             (i & (1 << 4)) != 0;
             boolean nio_peer_handler =             (i & (1 << 5)) != 0;
-            boolean use_chan_manager_constructor = (i & (1 << 6)) != 0;
+            boolean use_ignoring_routing_handler = (i & (1 << 6)) != 0;
+            boolean use_chan_manager_constructor = (i & (1 << 7)) != 0;
             if (break_cross_refs && !reload_peers) {
                 // There are no cross refs to break without reloading peers.
                 continue;
@@ -900,8 +915,13 @@ public class HumanObjectPeerTest {
                 // ChannelManagerConstructor requires a ChainMonitor as the Watch and creates a NioPeerHandler for us.
                 continue;
             }
+            if (!use_chan_manager_constructor && use_ignoring_routing_handler) {
+                // We rely on the ChannelManagerConstructor to convert null into an IgnoringMessageHandler, so don't
+                // try to run with an IgnoringMessageHandler unless we're also using a ChannelManagerConstructor.
+                continue;
+            }
             System.err.println("Running test with flags " + i);
-            do_test(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_refs, nio_peer_handler, use_chan_manager_constructor);
+            do_test(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_refs, nio_peer_handler, use_ignoring_routing_handler, use_chan_manager_constructor);
         }
     }