Update tests for latest upstream API
[ldk-java] / src / test / java / org / ldk / HumanObjectPeerTest.java
index 996382795d19d06ebd0d93a03131c0406a58eaad..656c2ebb7ce26fca0ed7c359e62bce8a4b456eb4 100644 (file)
@@ -168,7 +168,8 @@ class HumanObjectPeerTestInstance {
         final KeysManager explicit_keys_manager;
         final KeysInterface keys_interface;
         final ChainMonitor chain_monitor;
-        final NetGraphMsgHandler router;
+        final NetworkGraph router;
+        NetGraphMsgHandler route_handler;
         final Watch chain_watch;
         final HashSet<String> filter_additions;
         final Option_FilterZ filter;
@@ -209,7 +210,7 @@ class HumanObjectPeerTestInstance {
             this.seed = seed;
             Persist persister = Persist.new_impl(new Persist.PersistInterface() {
                 @Override
-                public Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint id, ChannelMonitor data) {
+                public Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint id, ChannelMonitor data, MonitorUpdateId update_id) {
                     synchronized (monitors) {
                         String key = Arrays.toString(id.to_channel_id());
                         assert monitors.put(key, data) == null;
@@ -221,7 +222,7 @@ class HumanObjectPeerTestInstance {
                 }
 
                 @Override
-                public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id, ChannelMonitorUpdate update, ChannelMonitor data) {
+                public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id, ChannelMonitorUpdate update, ChannelMonitor data, MonitorUpdateId update_id) {
                     synchronized (monitors) {
                         String key = Arrays.toString(id.to_channel_id());
                         assert monitors.put(key, data) != null;
@@ -268,7 +269,8 @@ class HumanObjectPeerTestInstance {
                 this.keys_interface = keys.as_KeysInterface();
                 this.explicit_keys_manager = keys;
             }
-            this.router = NetGraphMsgHandler.of(NetworkGraph.of(new byte[32]), Option_AccessZ.some(Access.new_impl(new Access.AccessInterface() {
+            this.router = NetworkGraph.of(new byte[32]);
+            this.route_handler = NetGraphMsgHandler.of(this.router, Option_AccessZ.some(Access.new_impl(new Access.AccessInterface() {
                 @Override
                 public Result_TxOutAccessErrorZ get_utxo(byte[] genesis_hash, long short_channel_id) {
                     // We don't exchange any gossip, so should never actually get called, but providing a Some(Access)
@@ -333,9 +335,8 @@ class HumanObjectPeerTestInstance {
         Peer(byte seed) {
             this(null, seed);
             if (use_chan_manager_constructor) {
-                NetGraphMsgHandler route_handler = null;
-                if (!use_ignore_handler) {
-                    route_handler = router;
+                if (use_ignore_handler) {
+                    this.route_handler = null;
                 }
                 this.constructor = new ChannelManagerConstructor(Network.LDKNetwork_Bitcoin, UserConfig.with_default(), new byte[32], 0,
                         this.keys_interface, this.fee_estimator, this.chain_monitor, route_handler, this.tx_broadcaster, this.logger);
@@ -355,7 +356,7 @@ class HumanObjectPeerTestInstance {
                 ChainParameters params = ChainParameters.of(Network.LDKNetwork_Bitcoin, BestBlock.of(new byte[32], 0));
                 this.chan_manager = ChannelManager.of(this.fee_estimator, chain_watch, tx_broadcaster, logger, this.keys_interface, UserConfig.with_default(), params);
                 byte[] random_data = keys_interface.get_secure_random_bytes();
-                this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger, this.custom_message_handler);
+                this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), route_handler.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger, this.custom_message_handler);
             }
 
             this.node_id = chan_manager.get_our_node_id();
@@ -374,8 +375,11 @@ class HumanObjectPeerTestInstance {
                     if (this.filter instanceof Option_FilterZ.Some) {
                         filter_nullable = ((Option_FilterZ.Some) this.filter).some;
                     }
+                    if (use_ignore_handler) {
+                        this.route_handler = null;
+                    }
                     this.constructor = new ChannelManagerConstructor(serialized, monitors, this.keys_interface,
-                            this.fee_estimator, this.chain_monitor, filter_nullable, this.router, this.tx_broadcaster, this.logger);
+                            this.fee_estimator, this.chain_monitor, filter_nullable, this.route_handler, this.tx_broadcaster, this.logger);
                     constructor.chain_sync_completed(new ChannelManagerConstructor.EventHandler() {
                         @Override public void handle_event(Event event) {
                             synchronized (pending_manager_events) {
@@ -417,7 +421,7 @@ class HumanObjectPeerTestInstance {
                 this.chan_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.get_b();
                 this.chain_watch.watch_channel(monitors[0].get_funding_txo().get_a(), monitors[0]);
                 byte[] random_data = keys_interface.get_secure_random_bytes();
-                this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger, this.custom_message_handler);
+                this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), route_handler.as_RoutingMessageHandler(), keys_interface.get_node_secret(), 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
                     // field in the old peer, preventing freeing of the original Peer until the new Peer is freed. Thus, we
@@ -659,8 +663,8 @@ class HumanObjectPeerTestInstance {
 
         connect_peers(peer1, peer2);
 
-        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;
+        Result__u832APIErrorZ cc_res = peer1.chan_manager.create_channel(peer2.node_id, 100000, 1000, 42, null);
+        assert cc_res instanceof Result__u832APIErrorZ.Result__u832APIErrorZ_OK;
 
         // Previously, this was a SEGFAULT instead of get_funding_txo() returning null.
         ChannelDetails pre_funding_chan = peer1.chan_manager.list_channels()[0];
@@ -741,24 +745,28 @@ class HumanObjectPeerTestInstance {
         InvoiceFeatures invoice_features = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.features();
         RouteHint[] route_hints = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.route_hints();
 
-        Result_RouteLightningErrorZ route_res = UtilMethods.get_route(peer1.chan_manager.get_our_node_id(), peer1.router.get_network_graph(), peer2.node_id, invoice_features, peer1_chans, route_hints, 10000000, 42, peer1.logger);
+        Payee payee = Payee.of(peer2.node_id, invoice_features, route_hints, Option_u64Z.none());
+        RouteParameters route_params = RouteParameters.of(payee, 10000000, 42);
+        Result_RouteLightningErrorZ route_res = UtilMethods.find_route(
+                peer1.chan_manager.get_our_node_id(), route_params, peer1.router,
+                peer1_chans, peer1.logger, Scorer.with_default().as_Score());
         assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
         Route route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
         assert route.get_paths().length == 1;
         assert route.get_paths()[0].length == 1;
         assert route.get_paths()[0][0].get_fee_msat() == 10000000;
 
-        Result_NonePaymentSendFailureZ payment_res = peer1.chan_manager.send_payment(route, payment_hash, payment_secret);
-        assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_OK;
+        Result_PaymentIdPaymentSendFailureZ payment_res = peer1.chan_manager.send_payment(route, payment_hash, payment_secret);
+        assert payment_res instanceof Result_PaymentIdPaymentSendFailureZ.Result_PaymentIdPaymentSendFailureZ_OK;
 
         RouteHop[][] hops = new RouteHop[1][1];
         byte[] hop_pubkey = new byte[33];
         hop_pubkey[0] = 3;
         hop_pubkey[1] = 42;
         hops[0][0] = RouteHop.of(hop_pubkey, NodeFeatures.known(), 42, ChannelFeatures.known(), 100, 0);
-        Route r2 = Route.of(hops);
+        Route r2 = Route.of(hops, payee);
         payment_res = peer1.chan_manager.send_payment(r2, payment_hash, payment_secret);
-        assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_Err;
+        assert payment_res instanceof Result_PaymentIdPaymentSendFailureZ.Result_PaymentIdPaymentSendFailureZ_Err;
 
         if (reload_peers) {
             if (use_chan_manager_constructor) {
@@ -824,6 +832,13 @@ class HumanObjectPeerTestInstance {
             Thread.sleep(100);
         }
 
+        if (state.peer1.chain_monitor != null) {
+            Balance[] peer1_balances = state.peer1.chain_monitor.get_claimable_balances(state.peer1.chan_manager.list_channels());
+            assert peer1_balances.length == 0;
+            Balance[] peer2_balances = state.peer2.chain_monitor.get_claimable_balances(state.peer2.chan_manager.list_channels());
+            assert peer2_balances.length == 0;
+        }
+
         ChannelDetails[] peer1_chans = state.peer1.chan_manager.list_channels();
 
         if (nice_close) {
@@ -851,7 +866,30 @@ class HumanObjectPeerTestInstance {
 
             assert state.peer1.broadcast_set.size() == 1;
             assert state.peer2.broadcast_set.size() == 1;
+        }
+
+        events = state.peer2.get_manager_events(1, state.peer1, state.peer2);
+        assert events[0] instanceof Event.ChannelClosed;
+        events = state.peer1.get_manager_events(1, state.peer1, state.peer2);
+        assert events[0] instanceof Event.ChannelClosed;
+
+        if (state.peer1.chain_monitor != null) {
+            Balance[] peer1_balances = state.peer1.chain_monitor.get_claimable_balances(state.peer1.chan_manager.list_channels());
+            assert peer1_balances.length == 1;
+            for (Balance bal : peer1_balances) {
+                assert bal instanceof Balance.ClaimableOnChannelClose;
+                long expected_tx_fee = 183;
+                assert ((Balance.ClaimableOnChannelClose) bal).claimable_amount_satoshis == 100000 - 1 - 10000 - expected_tx_fee;
+            }
+            Balance[] peer2_balances = state.peer2.chain_monitor.get_claimable_balances(state.peer2.chan_manager.list_channels());
+            assert peer2_balances.length == 1;
+            for (Balance bal : peer2_balances) {
+                assert bal instanceof Balance.ClaimableOnChannelClose;
+                assert ((Balance.ClaimableOnChannelClose) bal).claimable_amount_satoshis == 10000 + 1;
+            }
+        }
 
+        if (!nice_close) {
             NetworkParameters bitcoinj_net = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
             Transaction tx = new Transaction(bitcoinj_net, state.peer1.broadcast_set.getFirst());
             Block b = new Block(bitcoinj_net, 2, state.best_blockhash, Sha256Hash.ZERO_HASH, 42, 0, 0,
@@ -889,19 +927,22 @@ class HumanObjectPeerTestInstance {
             }
         }
 
-        // Test exchanging a custom message
-        byte[] custom_message_bytes = new byte[] { 0x42, 0x44, 0x43, 0x00 };
-        state.peer1.custom_messages_to_send.add(custom_message_bytes);
-        state.peer1.peer_manager.process_events();
-        synchronized (state.peer2.received_custom_messages) {
-            while (true) {
-                if (state.peer2.received_custom_messages.isEmpty()) {
-                    state.peer2.received_custom_messages.wait();
-                    continue;
+        // Test exchanging a custom message (note that ChannelManagerConstructor) always loads an IgnorimgMessageHandler
+        // so we cannot exchange custom messages with it
+        if (!use_chan_manager_constructor) {
+            byte[] custom_message_bytes = new byte[]{0x42, 0x44, 0x43, 0x00};
+            state.peer1.custom_messages_to_send.add(custom_message_bytes);
+            state.peer1.peer_manager.process_events();
+            synchronized (state.peer2.received_custom_messages) {
+                while (true) {
+                    if (state.peer2.received_custom_messages.isEmpty()) {
+                        state.peer2.received_custom_messages.wait();
+                        continue;
+                    }
+                    assert state.peer2.received_custom_messages.size() == 1;
+                    assert Arrays.equals(state.peer2.received_custom_messages.get(0), custom_message_bytes);
+                    break;
                 }
-                assert state.peer2.received_custom_messages.size() == 1;
-                assert Arrays.equals(state.peer2.received_custom_messages.get(0), custom_message_bytes);
-                break;
             }
         }
 
@@ -960,7 +1001,7 @@ public class HumanObjectPeerTest {
     }
     @Test
     public void test_message_handler() throws InterruptedException {
-        for (int i = 0; i < (1 << 7) - 1; i++) {
+        for (int i = 0; i < (1 << 8) - 1; i++) {
             boolean nice_close =                   (i & (1 << 0)) != 0;
             boolean use_km_wrapper =               (i & (1 << 1)) != 0;
             boolean use_manual_watch =             (i & (1 << 2)) != 0;