Update tests for latest upstream API
[ldk-java] / src / test / java / org / ldk / HumanObjectPeerTest.java
index 57b750427fbe28b8af191a3d3cc4601a93dcaf6d..656c2ebb7ce26fca0ed7c359e62bce8a4b456eb4 100644 (file)
@@ -5,6 +5,7 @@ import org.bitcoinj.script.Script;
 import org.junit.jupiter.api.Test;
 import org.ldk.batteries.ChannelManagerConstructor;
 import org.ldk.batteries.NioPeerHandler;
+import org.ldk.enums.AccessError;
 import org.ldk.enums.Currency;
 import org.ldk.enums.Network;
 import org.ldk.structs.*;
@@ -50,58 +51,43 @@ class HumanObjectPeerTestInstance {
                     Sign underlying_ck = underlying_if.get_channel_signer(inbound, channel_value_satoshis);
                     BaseSign underlying_base = underlying_ck.get_base_sign();
                     BaseSign.BaseSignInterface bsi = new BaseSign.BaseSignInterface() {
-                        @Override
-                        public byte[] get_per_commitment_point(long idx) {
+                        @Override public byte[] get_per_commitment_point(long idx) {
                             return underlying_base.get_per_commitment_point(idx);
                         }
-
-                        @Override
-                        public byte[] release_commitment_secret(long idx) {
+                        @Override public byte[] release_commitment_secret(long idx) {
                             return underlying_base.release_commitment_secret(idx);
                         }
-
-                        @Override
-                        public byte[] channel_keys_id() {
+                        @Override public Result_NoneNoneZ validate_holder_commitment(HolderCommitmentTransaction holder_tx) {
+                            return underlying_base.validate_holder_commitment(holder_tx);
+                        }
+                        @Override public byte[] channel_keys_id() {
                             return new byte[32];
                         }
-
-                        @Override
-                        public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_counterparty_commitment(CommitmentTransaction commitment_tx) {
+                        @Override public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_counterparty_commitment(CommitmentTransaction commitment_tx) {
                             return underlying_base.sign_counterparty_commitment(commitment_tx);
                         }
-
-                        @Override
-                        public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_holder_commitment_and_htlcs(HolderCommitmentTransaction holder_commitment_tx) {
+                        @Override public Result_NoneNoneZ validate_counterparty_revocation(long idx, byte[] secret) {
+                            return underlying_base.validate_counterparty_revocation(idx, secret);
+                        }
+                        @Override public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_holder_commitment_and_htlcs(HolderCommitmentTransaction holder_commitment_tx) {
                             return underlying_base.sign_holder_commitment_and_htlcs(holder_commitment_tx);
                         }
-
-                        @Override
-                        public Result_SignatureNoneZ sign_justice_revoked_output(byte[] justice_tx, long input, long amount, byte[] per_commitment_key) {
+                        @Override public Result_SignatureNoneZ sign_justice_revoked_output(byte[] justice_tx, long input, long amount, byte[] per_commitment_key) {
                             return underlying_base.sign_justice_revoked_output(justice_tx, input, amount, per_commitment_key);
                         }
-
-                        @Override
-                        public Result_SignatureNoneZ sign_justice_revoked_htlc(byte[] justice_tx, long input, long amount, byte[] per_commitment_key, HTLCOutputInCommitment htlc) {
+                        @Override public Result_SignatureNoneZ sign_justice_revoked_htlc(byte[] justice_tx, long input, long amount, byte[] per_commitment_key, HTLCOutputInCommitment htlc) {
                             return underlying_base.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc);
                         }
-
-                        @Override
-                        public Result_SignatureNoneZ sign_counterparty_htlc_transaction(byte[] htlc_tx, long input, long amount, byte[] per_commitment_point, HTLCOutputInCommitment htlc) {
+                        @Override public Result_SignatureNoneZ sign_counterparty_htlc_transaction(byte[] htlc_tx, long input, long amount, byte[] per_commitment_point, HTLCOutputInCommitment htlc) {
                             return underlying_base.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc);
                         }
-
-                        @Override
-                        public Result_SignatureNoneZ sign_closing_transaction(byte[] closing_tx) {
+                        @Override public Result_SignatureNoneZ sign_closing_transaction(ClosingTransaction closing_tx) {
                             return underlying_base.sign_closing_transaction(closing_tx);
                         }
-
-                        @Override
-                        public Result_SignatureNoneZ sign_channel_announcement(UnsignedChannelAnnouncement msg) {
+                        @Override public Result_SignatureNoneZ sign_channel_announcement(UnsignedChannelAnnouncement msg) {
                             return underlying_base.sign_channel_announcement(msg);
                         }
-
-                        @Override
-                        public void ready_channel(ChannelTransactionParameters params) {
+                        @Override public void ready_channel(ChannelTransactionParameters params) {
                             underlying_base.ready_channel(params);
                         }
                     };
@@ -182,26 +168,31 @@ 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 Filter filter;
+        final Option_FilterZ filter;
         ChannelManager chan_manager;
         PeerManager peer_manager;
         final HashMap<String, ChannelMonitor> monitors; // Wow I forgot just how terrible Java is - we can't put a byte array here.
         byte[] node_id;
+        byte[] connected_peer_node_id = null;
         final LinkedList<byte[]> broadcast_set = new LinkedList<>();
         final LinkedList<Event> pending_manager_events = new LinkedList<>();
+        private final CustomMessageHandler custom_message_handler;
+        final LinkedList<byte[]> received_custom_messages = new LinkedList<>();
+        final LinkedList<byte[]> custom_messages_to_send = new LinkedList<>();
         ChannelManagerConstructor constructor = null;
         GcCheck obj = new GcCheck();
 
-        private TwoTuple<OutPoint, byte[]> test_mon_roundtrip(ChannelMonitor mon) {
+        private TwoTuple_OutPointScriptZ test_mon_roundtrip(ChannelMonitor mon) {
             // Because get_funding_txo() returns an OutPoint in a tuple that is a reference to an OutPoint inside the
             // ChannelMonitor, its a good test to ensure that the OutPoint isn't freed (or is cloned) before the
             // ChannelMonitor is. This used to be broken.
-            Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ roundtrip_monitor = UtilMethods.BlockHashChannelMonitorZ_read(mon.write(), keys_interface);
+            Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ roundtrip_monitor = UtilMethods.C2Tuple_BlockHashChannelMonitorZ_read(mon.write(), keys_interface);
             assert roundtrip_monitor instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
-            TwoTuple<OutPoint, byte[]> funding_txo = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) roundtrip_monitor).res.b.get_funding_txo();
+            TwoTuple_OutPointScriptZ funding_txo = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) roundtrip_monitor).res.get_b().get_funding_txo();
             System.gc(); System.runFinalization(); // Give the GC a chance to run.
             return funding_txo;
         }
@@ -219,25 +210,25 @@ 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;
-                        TwoTuple<OutPoint, byte[]> res = test_mon_roundtrip(data);
-                        assert Arrays.equals(res.a.get_txid(), id.get_txid());
-                        assert res.a.get_index() == id.get_index();
+                        TwoTuple_OutPointScriptZ res = test_mon_roundtrip(data);
+                        assert Arrays.equals(res.get_a().get_txid(), id.get_txid());
+                        assert res.get_a().get_index() == id.get_index();
                     }
                     return Result_NoneChannelMonitorUpdateErrZ.ok();
                 }
 
                 @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;
-                        TwoTuple<OutPoint, byte[]> res = test_mon_roundtrip(data);
-                        assert Arrays.equals(res.a.get_txid(), id.get_txid());
-                        assert res.a.get_index() == id.get_index();
+                        TwoTuple_OutPointScriptZ res = test_mon_roundtrip(data);
+                        assert Arrays.equals(res.get_a().get_txid(), id.get_txid());
+                        assert res.get_a().get_index() == id.get_index();
                     }
                     return Result_NoneChannelMonitorUpdateErrZ.ok();
                 }
@@ -245,7 +236,7 @@ class HumanObjectPeerTestInstance {
 
             filter_additions = new HashSet<>();
             if (use_filter) {
-                this.filter = Filter.new_impl(new Filter.FilterInterface() {
+                this.filter = Option_FilterZ.some(Filter.new_impl(new Filter.FilterInterface() {
                     @Override public void register_tx(byte[] txid, byte[] script_pubkey) {
                         filter_additions.add(Arrays.toString(txid));
                     }
@@ -253,9 +244,9 @@ class HumanObjectPeerTestInstance {
                         filter_additions.add(Arrays.toString(output.get_outpoint().get_txid()) + ":" + output.get_outpoint().get_index());
                         return Option_C2Tuple_usizeTransactionZZ.none();
                     }
-                });
+                }));
             } else {
-                this.filter = null;
+                this.filter = Option_FilterZ.none();
             }
 
             if (use_manual_watch) {
@@ -278,7 +269,53 @@ class HumanObjectPeerTestInstance {
                 this.keys_interface = keys.as_KeysInterface();
                 this.explicit_keys_manager = keys;
             }
-            this.router = NetGraphMsgHandler.of(new byte[32], null, logger);
+            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)
+                    // is a good test of our Option<Trait> free'ing, which used to be broken and relies on a dirty hack.
+                    assert false;
+                    return Result_TxOutAccessErrorZ.err(AccessError.LDKAccessError_UnknownTx);
+                }
+            })), logger);
+
+            this.custom_message_handler = CustomMessageHandler.new_impl(new CustomMessageHandler.CustomMessageHandlerInterface() {
+                @Override
+                public Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id) {
+                    synchronized (received_custom_messages) {
+                        received_custom_messages.add(msg.write());
+                        received_custom_messages.notifyAll();
+                    }
+                    return Result_NoneLightningErrorZ.ok();
+                }
+
+                @Override
+                public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
+                    byte[][] bytes;
+                    synchronized (custom_messages_to_send) {
+                        bytes = custom_messages_to_send.toArray(new byte[0][0]);
+                        custom_messages_to_send.clear();
+                    }
+                    TwoTuple_PublicKeyTypeZ[] ret = new TwoTuple_PublicKeyTypeZ[bytes.length];
+                    for (int i = 0; i < bytes.length; i++) {
+                        final int msg_idx = i;
+                        ret[i] = TwoTuple_PublicKeyTypeZ.of(connected_peer_node_id, Type.new_impl(new Type.TypeInterface() {
+                            @Override public short type_id() { return 4096; }
+                            @Override public String debug_str() { return "Custom Java Message"; }
+                            @Override public byte[] write() { return bytes[msg_idx]; }
+                        }));
+                    }
+                    return ret;
+                }
+            }, (message_type, buffer) -> {
+                assert message_type == 4096;
+                return Result_COption_TypeZDecodeErrorZ.ok(Option_TypeZ.some(Type.new_impl(new Type.TypeInterface() {
+                    @Override public short type_id() { return 4096; }
+                    @Override public String debug_str() { return "Custom Java-Decoded Message"; }
+                    @Override public byte[] write() { return buffer; }
+                })));
+            });
         }
         private void bind_nio() {
             if (!use_nio_peer_handler) return;
@@ -298,13 +335,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;
+                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);
-                constructor.chain_sync_completed(new ChannelManagerConstructor.ChannelManagerPersister() {
+                constructor.chain_sync_completed(new ChannelManagerConstructor.EventHandler() {
                     @Override public void handle_event(Event event) {
                         synchronized (pending_manager_events) {
                             pending_manager_events.add(event);
@@ -320,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.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();
@@ -335,9 +371,16 @@ class HumanObjectPeerTestInstance {
                 byte[][] monitors = {orig.monitors.values().stream().iterator().next().write()};
                 byte[] serialized = orig.chan_manager.write();
                 try {
+                    Filter filter_nullable = null;
+                    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, this.filter, this.router, this.tx_broadcaster, this.logger);
-                    constructor.chain_sync_completed(new ChannelManagerConstructor.ChannelManagerPersister() {
+                            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) {
                                 pending_manager_events.add(event);
@@ -367,18 +410,18 @@ class HumanObjectPeerTestInstance {
                 } else {
                     byte[] serialized = orig.monitors.values().stream().iterator().next().write();
                     Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res =
-                            UtilMethods.BlockHashChannelMonitorZ_read(serialized, this.keys_interface);
+                            UtilMethods.C2Tuple_BlockHashChannelMonitorZ_read(serialized, this.keys_interface);
                     assert res instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
-                    monitors[0] = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) res).res.b;
+                    monitors[0] = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) res).res.get_b();
                 }
                 byte[] serialized = orig.chan_manager.write();
                 Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ read_res =
-                        UtilMethods.BlockHashChannelManagerZ_read(serialized, this.keys_interface, this.fee_estimator, this.chain_watch, this.tx_broadcaster, this.logger, UserConfig.with_default(), monitors);
+                        UtilMethods.C2Tuple_BlockHashChannelManagerZ_read(serialized, this.keys_interface, this.fee_estimator, this.chain_watch, this.tx_broadcaster, this.logger, UserConfig.with_default(), monitors);
                 assert read_res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK;
-                this.chan_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.b;
-                this.chain_watch.watch_channel(monitors[0].get_funding_txo().a, monitors[0]);
+                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.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
@@ -401,15 +444,15 @@ class HumanObjectPeerTestInstance {
             }
         }
 
-        TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] connect_block(Block b, int height, long expected_monitor_update_len) {
+        TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] connect_block(Block b, int height, long expected_monitor_update_len) {
             byte[] header = Arrays.copyOfRange(b.bitcoinSerialize(), 0, 80);
-            TwoTuple<Long, byte[]>[] txn;
+            TwoTuple_usizeTransactionZ[] txn;
             if (b.hasTransactions()) {
                 assert b.getTransactions().size() == 1;
-                TwoTuple<Long, byte[]> txp = new TwoTuple<>((long) 0, b.getTransactions().get(0).bitcoinSerialize());
-                txn = new TwoTuple[]{txp};
+                TwoTuple_usizeTransactionZ txp = TwoTuple_usizeTransactionZ.of((long) 0, b.getTransactions().get(0).bitcoinSerialize());
+                txn = new TwoTuple_usizeTransactionZ[]{txp};
             } else
-                txn = new TwoTuple[0];
+                txn = new TwoTuple_usizeTransactionZ[0];
             if (chain_monitor != null) {
                 chan_manager.as_Listen().block_connected(b.bitcoinSerialize(), height);
                 chain_monitor.as_Listen().block_connected(b.bitcoinSerialize(), height);
@@ -420,7 +463,7 @@ class HumanObjectPeerTestInstance {
                 synchronized (monitors) {
                     assert monitors.size() == 1;
                     for (ChannelMonitor mon : monitors.values()) {
-                        TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] ret = mon.block_connected(header, txn, height, tx_broadcaster, fee_estimator, logger);
+                        TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret = mon.block_connected(header, txn, height, tx_broadcaster, fee_estimator, logger);
                         assert ret.length == expected_monitor_update_len;
                         return ret;
                     }
@@ -567,6 +610,8 @@ class HumanObjectPeerTestInstance {
     }
 
     void connect_peers(final Peer peer1, final Peer peer2) {
+        peer2.connected_peer_node_id = peer1.node_id;
+        peer1.connected_peer_node_id = peer2.node_id;
         if (use_nio_peer_handler) {
             try {
                 peer1.nio_peer_handler.connect(peer2.chan_manager.get_our_node_id(), new InetSocketAddress("127.0.0.1", peer2.nio_port), 100);
@@ -618,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];
@@ -700,25 +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();
 
-        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, 10000000, 42, peer1.logger);
-            assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
-            route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
-        }
+        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) {
@@ -784,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) {
@@ -811,18 +866,41 @@ 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,
                     Arrays.asList(new Transaction[]{tx}));
-            TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] watch_outputs = state.peer2.connect_block(b, 10, 1);
+            TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] watch_outputs = state.peer2.connect_block(b, 10, 1);
             if (watch_outputs != null) { // We only process watch_outputs manually when we use a manually-build Watch impl
                 assert watch_outputs.length == 1;
-                assert Arrays.equals(watch_outputs[0].a, tx.getTxId().getReversedBytes());
-                assert watch_outputs[0].b.length == 2;
-                assert watch_outputs[0].b[0].a == 0;
-                assert watch_outputs[0].b[1].a == 1;
+                assert Arrays.equals(watch_outputs[0].get_a(), tx.getTxId().getReversedBytes());
+                assert watch_outputs[0].get_b().length == 2;
+                assert watch_outputs[0].get_b()[0].get_a() == 0;
+                assert watch_outputs[0].get_b()[1].get_a() == 1;
             }
 
             for (int i = 11; i < 21; i++) {
@@ -838,8 +916,8 @@ class HumanObjectPeerTestInstance {
             assert broadcastable_event.length == 1;
             assert broadcastable_event[0] instanceof Event.SpendableOutputs;
             if (state.peer2.explicit_keys_manager != null) {
-                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);
+                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() == 2;
@@ -849,6 +927,25 @@ class HumanObjectPeerTestInstance {
             }
         }
 
+        // 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;
+                }
+            }
+        }
+
         if (use_nio_peer_handler) {
             state.peer1.peer_manager.disconnect_by_node_id(state.peer2.chan_manager.get_our_node_id(), false);
             while (state.peer1.peer_manager.get_peer_node_ids().length != 0) Thread.yield();
@@ -866,6 +963,12 @@ class HumanObjectPeerTestInstance {
         }
 
         t.interrupt();
+
+        // Construct the only Option_Enum::Variant(OpaqueStruct) we have in the codebase as this used to cause double-frees:
+        byte[] serd = new byte[] {(byte)0xd9,(byte)0x77,(byte)0xcb,(byte)0x9b,(byte)0x53,(byte)0xd9,(byte)0x3a,(byte)0x6f,(byte)0xf6,(byte)0x4b,(byte)0xb5,(byte)0xf1,(byte)0xe1,(byte)0x58,(byte)0xb4,(byte)0x09,(byte)0x4b,(byte)0x66,(byte)0xe7,(byte)0x98,(byte)0xfb,(byte)0x12,(byte)0x91,(byte)0x11,(byte)0x68,(byte)0xa3,(byte)0xcc,(byte)0xdf,(byte)0x80,(byte)0xa8,(byte)0x30,(byte)0x96,(byte)0x34,(byte)0x0a,(byte)0x6a,(byte)0x95,(byte)0xda,(byte)0x0a,(byte)0xe8,(byte)0xd9,(byte)0xf7,(byte)0x76,(byte)0x52,(byte)0x8e,(byte)0xec,(byte)0xdb,(byte)0xb7,(byte)0x47,(byte)0xeb,(byte)0x6b,(byte)0x54,(byte)0x54,(byte)0x95,(byte)0xa4,(byte)0x31,(byte)0x9e,(byte)0xd5,(byte)0x37,(byte)0x8e,(byte)0x35,(byte)0xb2,(byte)0x1e,(byte)0x07,(byte)0x3a,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x19,(byte)0xd6,(byte)0x68,(byte)0x9c,(byte)0x08,(byte)0x5a,(byte)0xe1,(byte)0x65,(byte)0x83,(byte)0x1e,(byte)0x93,(byte)0x4f,(byte)0xf7,(byte)0x63,(byte)0xae,(byte)0x46,(byte)0xa2,(byte)0xa6,(byte)0xc1,(byte)0x72,(byte)0xb3,(byte)0xf1,(byte)0xb6,(byte)0x0a,(byte)0x8c,(byte)0xe2,(byte)0x6f,(byte)0x00,(byte)0x08,(byte)0x3a,(byte)0x84,(byte)0x00,(byte)0x00,(byte)0x03,(byte)0x4d,(byte)0x01,(byte)0x34,(byte)0x13,(byte)0xa7,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x90,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x0f,(byte)0x42,(byte)0x40,(byte)0x00,(byte)0x00,(byte)0x27,(byte)0x10,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x14,};
+        Result_ChannelUpdateDecodeErrorZ upd_msg = ChannelUpdate.read(serd);
+        assert upd_msg instanceof Result_ChannelUpdateDecodeErrorZ.Result_ChannelUpdateDecodeErrorZ_OK;
+        Option_NetworkUpdateZ upd = Option_NetworkUpdateZ.some(NetworkUpdate.channel_update_message(((Result_ChannelUpdateDecodeErrorZ.Result_ChannelUpdateDecodeErrorZ_OK) upd_msg).res));
     }
 
     java.util.LinkedList<WeakReference<Object>> must_free_objs = new java.util.LinkedList();
@@ -898,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;