[Java] Update batteries and tests to LDK 0.0.121
[ldk-java] / src / test / java / org / ldk / HumanObjectPeerTest.java
index 261902b7f059197387feae6e9b6b78fbfdcac770..5485f3e3d80dbb878ecf35f1670f200f380200fb 100644 (file)
@@ -1,24 +1,31 @@
 package org.ldk;
 
+import org.bitcoinj.base.BitcoinNetwork;
+import org.bitcoinj.base.Coin;
+import org.bitcoinj.base.Sha256Hash;
 import org.bitcoinj.core.*;
+import org.bitcoinj.params.BitcoinNetworkParams;
 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.*;
 import org.ldk.enums.Currency;
-import org.ldk.enums.Network;
 import org.ldk.structs.*;
-import org.ldk.util.TwoTuple;
+import org.ldk.util.UInt128;
+import org.ldk.util.UInt5;
 
+import java.io.FileReader;
 import java.io.IOException;
 import java.lang.ref.WeakReference;
 import java.net.InetSocketAddress;
 import java.util.*;
+import java.util.function.IntConsumer;
 
 class HumanObjectPeerTestInstance {
     private final boolean nice_close;
     private final boolean use_km_wrapper;
+    private final boolean use_full_km_wrapper;
     private final boolean use_manual_watch;
     private final boolean reload_peers;
     private final boolean break_cross_peer_refs;
@@ -26,10 +33,13 @@ class HumanObjectPeerTestInstance {
     private final boolean use_filter;
     private final boolean use_ignore_handler;
     private final boolean use_chan_manager_constructor;
+    private final boolean use_invoice_payer;
+    TxOut gossip_txout = null;
 
-    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) {
+    HumanObjectPeerTestInstance(boolean nice_close, boolean use_km_wrapper, boolean use_full_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, boolean use_invoice_payer) {
         this.nice_close = nice_close;
         this.use_km_wrapper = use_km_wrapper;
+        this.use_full_km_wrapper = use_full_km_wrapper;
         this.use_manual_watch = use_manual_watch;
         this.reload_peers = reload_peers;
         this.break_cross_peer_refs = break_cross_peer_refs;
@@ -37,120 +47,147 @@ class HumanObjectPeerTestInstance {
         this.use_filter = use_filter;
         this.use_ignore_handler = use_ignore_handler;
         this.use_chan_manager_constructor = use_chan_manager_constructor;
+        this.use_invoice_payer = use_invoice_payer;
     }
 
     class Peer {
-        KeysInterface manual_keysif(KeysInterface underlying_if) {
-            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 ShutdownScript get_shutdown_scriptpubkey() { return underlying_if.get_shutdown_scriptpubkey(); }
-
-                @Override
-                public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) {
-                    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) {
-                            return underlying_base.get_per_commitment_point(idx);
-                        }
-                        @Override public byte[] release_commitment_secret(long idx) {
-                            return underlying_base.release_commitment_secret(idx);
-                        }
-                        @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) {
-                            return underlying_base.sign_counterparty_commitment(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) {
-                            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) {
-                            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) {
-                            return underlying_base.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc);
-                        }
-                        @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) {
-                            return underlying_base.sign_channel_announcement(msg);
-                        }
-                        @Override public void ready_channel(ChannelTransactionParameters params) {
-                            underlying_base.ready_channel(params);
-                        }
-                    };
-                    Sign.SignInterface si = new Sign.SignInterface() {
-                        @Override
-                        public byte[] write() {
-                            return underlying_ck.write();
-                        }
-                    };
-                    Sign resp = Sign.new_impl(si, bsi, underlying_base.get_pubkeys());
-                    must_free_objs.add(new WeakReference<>(si));
-                    must_free_objs.add(new WeakReference<>(bsi));
-                    must_free_objs.add(new WeakReference<>(resp));
-                    must_free_objs.add(new WeakReference<>(underlying_base));
-                    must_free_objs.add(new WeakReference<>(underlying_ck));
-                    return resp;
+        NodeSigner manual_node_signer(KeysManager underlying_km) {
+            NodeSigner underlying_ns = underlying_km.as_NodeSigner();
+            must_free_objs.add(new WeakReference<>(underlying_ns));
+            return NodeSigner.new_impl(new NodeSigner.NodeSignerInterface() {
+                @Override public byte[] get_inbound_payment_key_material() { return underlying_ns.get_inbound_payment_key_material(); }
+                @Override public Result_PublicKeyNoneZ get_node_id(Recipient recipient) { return underlying_ns.get_node_id(recipient); }
+                @Override public Result_ThirtyTwoBytesNoneZ ecdh(Recipient recipient, byte[] other_key, Option_BigEndianScalarZ tweak) {
+                    return underlying_ns.ecdh(recipient, other_key, tweak);
                 }
-
-                @Override
-                public byte[] get_secure_random_bytes() {
-                    return underlying_if.get_secure_random_bytes();
+                @Override public Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, Recipient recipient) {
+                    return underlying_ns.sign_invoice(hrp_bytes, invoice_data, recipient);
+                }
+                @Override public Result_SchnorrSignatureNoneZ sign_bolt12_invoice_request(UnsignedInvoiceRequest invoice_request) {
+                    return underlying_ns.sign_bolt12_invoice_request(invoice_request);
+                }
+                @Override public Result_SchnorrSignatureNoneZ sign_bolt12_invoice(UnsignedBolt12Invoice invoice) {
+                    return underlying_ns.sign_bolt12_invoice(invoice);
+                }
+                @Override public Result_ECDSASignatureNoneZ sign_gossip_message(UnsignedGossipMessage msg) {
+                    return underlying_ns.sign_gossip_message(msg);
+                }
+            });
+        }
+        SignerProvider manual_signer_provider(KeysManager underlying_km) {
+            SignerProvider underlying_sp = underlying_km.as_SignerProvider();
+            must_free_objs.add(new WeakReference<>(underlying_sp));
+            return SignerProvider.new_impl(new SignerProvider.SignerProviderInterface() {
+                @Override public Result_CVec_u8ZNoneZ get_destination_script(byte[] channel_keys_id) { return underlying_sp.get_destination_script(channel_keys_id); }
+                @Override public Result_ShutdownScriptNoneZ get_shutdown_scriptpubkey() { return underlying_sp.get_shutdown_scriptpubkey(); }
+                @Override public byte[] generate_channel_keys_id(boolean inbound, long channel_value_satoshis, UInt128 user_channel_id) {
+                    return underlying_sp.generate_channel_keys_id(inbound, channel_value_satoshis, user_channel_id);
                 }
-
                 @Override
-                public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) {
-                    return underlying_if.read_chan_signer(reader);
+                public WriteableEcdsaChannelSigner derive_channel_signer(long channel_value_satoshis, byte[] channel_keys_id) {
+                    if (!use_full_km_wrapper) {
+                        WriteableEcdsaChannelSigner underlying_signer = underlying_sp.derive_channel_signer(channel_value_satoshis, channel_keys_id);
+                        must_free_objs.add(new WeakReference<>(underlying_signer));
+                        return underlying_signer;
+                    } else {
+                        WriteableEcdsaChannelSigner underlying_wecs = underlying_sp.derive_channel_signer(channel_value_satoshis, channel_keys_id);
+                        EcdsaChannelSigner underlying_ecs = underlying_wecs.get_ecdsa_channel_signer();
+                        ChannelSigner underlying_cs = underlying_ecs.get_channel_signer();
+                        ChannelSigner.ChannelSignerInterface csi = new ChannelSigner.ChannelSignerInterface() {
+                            @Override public byte[] get_per_commitment_point(long idx) { return underlying_cs.get_per_commitment_point(idx); }
+                            @Override public byte[] release_commitment_secret(long idx) { return underlying_cs.release_commitment_secret(idx); }
+                            @Override public Result_NoneNoneZ validate_holder_commitment(HolderCommitmentTransaction holder_tx, byte[][] preimages) {
+                                return underlying_cs.validate_holder_commitment(holder_tx, preimages);
+                            }
+                            @Override public Result_NoneNoneZ validate_counterparty_revocation(long idx, byte[] secret) {
+                                return underlying_cs.validate_counterparty_revocation(idx, secret);
+                            }
+                            @Override public byte[] channel_keys_id() { return underlying_cs.channel_keys_id(); }
+                            @Override public void provide_channel_parameters(ChannelTransactionParameters channel_parameters) {
+                                underlying_cs.provide_channel_parameters(channel_parameters);
+                            }
+                        };
+                        EcdsaChannelSigner.EcdsaChannelSignerInterface ecsi = new EcdsaChannelSigner.EcdsaChannelSignerInterface() {
+                            @Override public Result_C2Tuple_ECDSASignatureCVec_ECDSASignatureZZNoneZ sign_counterparty_commitment(CommitmentTransaction commitment_tx, byte[][] inbound_htlc_preimages, byte[][] outbound_htlc_preimages) {
+                                return underlying_ecs.sign_counterparty_commitment(commitment_tx, inbound_htlc_preimages, outbound_htlc_preimages);
+                            }
+                            @Override public Result_ECDSASignatureNoneZ sign_holder_commitment(HolderCommitmentTransaction commitment_tx) {
+                                return underlying_ecs.sign_holder_commitment(commitment_tx);
+                            }
+                            @Override public Result_ECDSASignatureNoneZ sign_justice_revoked_output(byte[] justice_tx, long input, long amount, byte[] per_commitment_key) {
+                                return underlying_ecs.sign_justice_revoked_output(justice_tx, input, amount, per_commitment_key);
+                            }
+                            @Override public Result_ECDSASignatureNoneZ sign_justice_revoked_htlc(byte[] justice_tx, long input, long amount, byte[] per_commitment_key, HTLCOutputInCommitment htlc) {
+                                return underlying_ecs.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc);
+                            }
+                            @Override public Result_ECDSASignatureNoneZ sign_holder_htlc_transaction(byte[] htlc_tx, long input, HTLCDescriptor htlc_descriptor) {
+                                return underlying_ecs.sign_holder_htlc_transaction(htlc_tx, input, htlc_descriptor);
+                            }
+                            @Override public Result_ECDSASignatureNoneZ sign_counterparty_htlc_transaction(byte[] htlc_tx, long input, long amount, byte[] per_commitment_point, HTLCOutputInCommitment htlc) {
+                                return underlying_ecs.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc);
+                            }
+                            @Override public Result_ECDSASignatureNoneZ sign_closing_transaction(ClosingTransaction closing_tx) {
+                                return underlying_ecs.sign_closing_transaction(closing_tx);
+                            }
+                            @Override public Result_ECDSASignatureNoneZ sign_holder_anchor_input(byte[] anchor_tx, long input) {
+                                return underlying_ecs.sign_holder_anchor_input(anchor_tx, input);
+                            }
+                            @Override public Result_ECDSASignatureNoneZ sign_channel_announcement_with_funding_key(UnsignedChannelAnnouncement msg) {
+                                return underlying_ecs.sign_channel_announcement_with_funding_key(msg);
+                            }
+                        };
+                        WriteableEcdsaChannelSigner.WriteableEcdsaChannelSignerInterface wecsi = new WriteableEcdsaChannelSigner.WriteableEcdsaChannelSignerInterface() {
+                            @Override public byte[] write() { return underlying_wecs.write(); }
+                        };
+                        WriteableEcdsaChannelSigner resp = WriteableEcdsaChannelSigner.new_impl(wecsi, ecsi, csi, underlying_cs.get_pubkeys());
+                        must_free_objs.add(new WeakReference<>(wecsi));
+                        must_free_objs.add(new WeakReference<>(ecsi));
+                        must_free_objs.add(new WeakReference<>(csi));
+                        must_free_objs.add(new WeakReference<>(resp));
+                        must_free_objs.add(new WeakReference<>(underlying_wecs));
+                        must_free_objs.add(new WeakReference<>(underlying_ecs));
+                        must_free_objs.add(new WeakReference<>(underlying_cs));
+                        return resp;
+                    }
                 }
 
                 @Override
-                public Result_RecoverableSignatureNoneZ sign_invoice(byte[] invoice_preimage) {
-                    return underlying_if.sign_invoice(invoice_preimage);
+                public Result_WriteableEcdsaChannelSignerDecodeErrorZ read_chan_signer(byte[] reader) {
+                    return underlying_sp.read_chan_signer(reader);
                 }
             });
         }
 
         Watch get_manual_watch() {
             Watch.WatchInterface watch_impl = new Watch.WatchInterface() {
-                public Result_NoneChannelMonitorUpdateErrZ watch_channel(OutPoint funding_txo, ChannelMonitor monitor) {
+                public Result_ChannelMonitorUpdateStatusNoneZ watch_channel(OutPoint funding_txo, ChannelMonitor monitor) {
                     synchronized (monitors) {
                         assert monitors.put(Arrays.toString(funding_txo.get_txid()), monitor) == null;
                     }
-                    return Result_NoneChannelMonitorUpdateErrZ.ok();
+                    return Result_ChannelMonitorUpdateStatusNoneZ.ok(ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_Completed);
                 }
 
-                public Result_NoneChannelMonitorUpdateErrZ update_channel(OutPoint funding_txo, ChannelMonitorUpdate update) {
+                public ChannelMonitorUpdateStatus update_channel(OutPoint funding_txo, ChannelMonitorUpdate update) {
                     synchronized (monitors) {
                         String txid = Arrays.toString(funding_txo.get_txid());
                         assert monitors.containsKey(txid);
-                        Result_NoneMonitorUpdateErrorZ update_res = monitors.get(txid).update_monitor(update, tx_broadcaster, fee_estimator, logger);
-                        assert update_res instanceof Result_NoneMonitorUpdateErrorZ.Result_NoneMonitorUpdateErrorZ_OK;
+                        Result_NoneNoneZ update_res = monitors.get(txid).update_monitor(update, tx_broadcaster, fee_estimator, logger);
+                        assert update_res instanceof Result_NoneNoneZ.Result_NoneNoneZ_OK;
                     }
-                    return Result_NoneChannelMonitorUpdateErrZ.ok();
+                    return ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_Completed;
                 }
 
                 @Override
-                public MonitorEvent[] release_pending_monitor_events() {
+                public ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] release_pending_monitor_events() {
                     synchronized (monitors) {
                         assert monitors.size() <= 1;
                         for (ChannelMonitor mon : monitors.values()) {
-                            return mon.get_and_clear_pending_monitor_events();
+                            ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] res = new ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[1];
+                            res[0] = ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ.of(mon.get_funding_txo().get_a(), mon.get_and_clear_pending_monitor_events(), mon.get_counterparty_node_id());
+                            return res;
                         }
                     }
-                    return new MonitorEvent[0];
+                    return new ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[0];
                 }
             };
             Watch watch = Watch.new_impl(watch_impl);
@@ -159,19 +196,31 @@ class HumanObjectPeerTestInstance {
             return watch;
         }
 
+        UserConfig get_config() {
+            ChannelHandshakeConfig channel_config = ChannelHandshakeConfig.with_default();
+            channel_config.set_announced_channel(true);
+            UserConfig config = UserConfig.with_default();
+            config.set_channel_handshake_config(channel_config);
+            return config;
+        }
+
         NioPeerHandler nio_peer_handler;
         short nio_port;
         final byte seed;
+        final byte[] key_seed = new byte[32];
         final Logger logger;
         final FeeEstimator fee_estimator;
         final BroadcasterInterface tx_broadcaster;
         final KeysManager explicit_keys_manager;
-        final KeysInterface keys_interface;
+        final NodeSigner node_signer;
+        final SignerProvider signer_provider;
+        final EntropySource entropy_source;
         final ChainMonitor chain_monitor;
-        final NetGraphMsgHandler router;
+        NetworkGraph net_graph;
+        P2PGossipSync route_handler;
         final Watch chain_watch;
         final HashSet<String> filter_additions;
-        final Option_FilterZ filter;
+        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.
@@ -183,25 +232,42 @@ class HumanObjectPeerTestInstance {
         final LinkedList<byte[]> received_custom_messages = new LinkedList<>();
         final LinkedList<byte[]> custom_messages_to_send = new LinkedList<>();
         ChannelManagerConstructor constructor = null;
+        long network_graph_persists = 0;
         GcCheck obj = new GcCheck();
 
-        private TwoTuple_OutPointScriptZ test_mon_roundtrip(ChannelMonitor mon) {
+        private ChannelMonitor test_mon_roundtrip(OutPoint expected_id, byte[] data) {
             // 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.C2Tuple_BlockHashChannelMonitorZ_read(mon.write(), keys_interface);
-            assert roundtrip_monitor instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
-            TwoTuple_OutPointScriptZ funding_txo = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) roundtrip_monitor).res.get_b().get_funding_txo();
+            Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ roundtrip_monitor = UtilMethods.C2Tuple_ThirtyTwoBytesChannelMonitorZ_read(data, this.entropy_source, this.signer_provider);
+            assert roundtrip_monitor instanceof Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ_OK;
+            must_free_objs.add(new WeakReference<>(roundtrip_monitor));
+            TwoTuple_OutPointCVec_u8ZZ funding_txo = ((Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ_OK) roundtrip_monitor).res.get_b().get_funding_txo();
+            must_free_objs.add(new WeakReference<>(funding_txo));
             System.gc(); System.runFinalization(); // Give the GC a chance to run.
-            return funding_txo;
+            assert Arrays.equals(funding_txo.get_a().get_txid(), expected_id.get_txid());
+            assert funding_txo.get_a().get_index() == expected_id.get_index();
+            Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ roundtrip_two = UtilMethods.C2Tuple_ThirtyTwoBytesChannelMonitorZ_read(data, this.entropy_source, this.signer_provider);
+            assert roundtrip_two instanceof Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ_OK;
+            must_free_objs.add(new WeakReference<>(((Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ_OK) roundtrip_two).res.get_b()));
+            must_free_objs.add(new WeakReference<>(((Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ_OK) roundtrip_two).res));
+            must_free_objs.add(new WeakReference<>(roundtrip_two));
+            return ((Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ_OK) roundtrip_two).res.get_b();
         }
 
         private Peer(Object _dummy, byte seed) {
-            logger = Logger.new_impl((String arg) -> System.out.println(seed + ": " + arg));
+            logger = Logger.new_impl((org.ldk.structs.Record arg)->{
+                if (arg.get_level() == Level.LDKLevel_Error)
+                    System.err.println(seed + ": " + arg.get_module_path() + " - " + arg.get_args());
+                else
+                    System.out.println(seed + ": " + arg.get_module_path() + " - " + arg.get_args());
+            });
             fee_estimator = FeeEstimator.new_impl((confirmation_target -> 253));
-            tx_broadcaster = BroadcasterInterface.new_impl(tx -> {
+            tx_broadcaster = BroadcasterInterface.new_impl(txn -> {
                 synchronized (broadcast_set) {
-                    broadcast_set.add(tx);
+                    for (byte[] tx : txn){
+                        broadcast_set.add(tx);
+                    }
                     broadcast_set.notifyAll();
                 }
             });
@@ -209,27 +275,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 ChannelMonitorUpdateStatus 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_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();
+                        ChannelMonitor res = test_mon_roundtrip(id, data.write());
+                        assert monitors.put(key, res) == null;
                     }
-                    return Result_NoneChannelMonitorUpdateErrZ.ok();
+                    return ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_Completed;
                 }
 
                 @Override
-                public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id, ChannelMonitorUpdate update, ChannelMonitor data) {
+                public ChannelMonitorUpdateStatus 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_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();
+                        ChannelMonitor res = test_mon_roundtrip(id, data.write());
+                        // Note that we use a serialization-roundtrip copy of data here, not the original, as this can
+                        // expose the JVM JIT bug where it finalize()s things still being called.
+                        assert monitors.put(key, res) != null;
                     }
-                    return Result_NoneChannelMonitorUpdateErrZ.ok();
+                    return ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_Completed;
                 }
             });
 
@@ -239,9 +303,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 {
@@ -256,27 +319,23 @@ class HumanObjectPeerTestInstance {
                 chain_watch = chain_monitor.as_Watch();
             }
 
-            byte[] key_seed = new byte[32];
             for (byte i = 0; i < 32; i++) {
                 key_seed[i] = (byte) (i ^ seed);
             }
             KeysManager keys = KeysManager.of(key_seed, System.currentTimeMillis() / 1000, (int) (System.currentTimeMillis() * 1000));
             if (use_km_wrapper) {
-                this.keys_interface = manual_keysif(keys.as_KeysInterface());
+                this.signer_provider = manual_signer_provider(keys);
+                this.node_signer = manual_node_signer(keys);
+                this.entropy_source = EntropySource.new_impl(new EntropySource.EntropySourceInterface() {
+                    @Override public byte[] get_secure_random_bytes() { return keys.as_EntropySource().get_secure_random_bytes(); }
+                });
                 this.explicit_keys_manager = null;
             } else {
-                this.keys_interface = keys.as_KeysInterface();
+                this.signer_provider = keys.as_SignerProvider();
+                this.entropy_source = keys.as_EntropySource();
+                this.node_signer = keys.as_NodeSigner();
                 this.explicit_keys_manager = keys;
             }
-            this.router = NetGraphMsgHandler.of(NetworkGraph.of(new byte[32]), 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
@@ -306,6 +365,12 @@ class HumanObjectPeerTestInstance {
                     }
                     return ret;
                 }
+                @Override public NodeFeatures provided_node_features() {
+                    return NodeFeatures.empty();
+                }
+                @Override public InitFeatures provided_init_features(byte[] their_node_id) {
+                    return InitFeatures.empty();
+                }
             }, (message_type, buffer) -> {
                 assert message_type == 4096;
                 return Result_COption_TypeZDecodeErrorZ.ok(Option_TypeZ.some(Type.new_impl(new Type.TypeInterface() {
@@ -330,15 +395,28 @@ class HumanObjectPeerTestInstance {
                 } catch (IOException e) { assert i < 10_500; }
             }
         }
+        private void setup_route_handler() {
+            this.route_handler = P2PGossipSync.of(this.net_graph,
+                Option_UtxoLookupZ.some(UtxoLookup.new_impl((genesis_hash, short_channel_id) -> UtxoResult.sync(Result_TxOutUtxoLookupErrorZ.ok(gossip_txout)))), this.logger);
+        }
         Peer(byte seed) {
             this(null, seed);
+            this.net_graph = NetworkGraph.of(Network.LDKNetwork_Bitcoin, this.logger);
+            this.setup_route_handler();
+
             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, route_handler, this.tx_broadcaster, this.logger);
+                this.constructor = new ChannelManagerConstructor(Network.LDKNetwork_Bitcoin, get_config(), new byte[32], 0,
+                        this.explicit_keys_manager.as_EntropySource(), this.explicit_keys_manager.as_NodeSigner(), this.explicit_keys_manager.as_SignerProvider(),
+                        this.fee_estimator, this.chain_monitor, this.net_graph,
+                        ProbabilisticScoringDecayParameters.with_default(), ProbabilisticScoringFeeParameters.with_default(),
+                        (ChannelManagerConstructor.RouterWrapper)
+                            (payer_node_id, route_params, first_hops, inflight_htlcs, payment_hash, payment_id, default_router) -> {
+                                assert payment_hash != null && payment_id != null;
+                                Router r = default_router.as_Router();
+                                must_free_objs.add(new WeakReference<>(r));
+                                return r.find_route_with_id(payer_node_id, route_params, first_hops, inflight_htlcs, payment_hash, payment_id);
+                            },
+                        this.tx_broadcaster, this.logger);
                 constructor.chain_sync_completed(new ChannelManagerConstructor.EventHandler() {
                     @Override public void handle_event(Event event) {
                         synchronized (pending_manager_events) {
@@ -347,18 +425,66 @@ class HumanObjectPeerTestInstance {
                         }
                     }
                     @Override public void persist_manager(byte[] channel_manager_bytes) { assert channel_manager_bytes.length > 1; }
-                });
+                    @Override public void persist_network_graph(byte[] graph_bytes) { assert graph_bytes.length > 1; network_graph_persists += 1; }
+                    @Override public void persist_scorer(byte[] scorer_bytes) { assert scorer_bytes.length > 1; }
+                }, !use_ignore_handler);
                 this.chan_manager = constructor.channel_manager;
                 this.peer_manager = constructor.peer_manager;
                 must_free_objs.add(new WeakReference<>(this.chan_manager));
             } else {
+                Router router = Router.new_impl(new Router.RouterInterface() {
+                    @Override
+                    public Result_RouteLightningErrorZ find_route_with_id(byte[] payer, RouteParameters route_params, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs, byte[] _payment_hash, byte[] _payment_id) {
+                        return find_route(payer, route_params, first_hops, inflight_htlcs);
+                    }
+
+                    @Override
+                    public Result_CVec_C2Tuple_BlindedPayInfoBlindedPathZZNoneZ create_blinded_payment_paths(byte[] recipient, ChannelDetails[] first_hops, ReceiveTlvs tlvs, long amount_msats) {
+                        return Result_CVec_C2Tuple_BlindedPayInfoBlindedPathZZNoneZ.err();
+                    }
+
+                    @Override
+                    public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters params, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs) {
+                        while (true) {
+                            try (ReadOnlyNetworkGraph graph = net_graph.read_only()) {
+                                assert graph.channel(424242) == null;
+                                long[] channels = graph.list_channels();
+                                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.
+                                    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());
+                                assert Arrays.equals(chan.get_node_one().as_slice(), chan_manager.get_our_node_id()) ||
+                                        Arrays.equals(chan.get_node_two().as_slice(), chan_manager.get_our_node_id());
+                                break;
+                            }
+                        }
+                        return UtilMethods.find_route(payer, params, net_graph, first_hops, logger,
+                                ScoreLookUp.new_impl((candidate, usage, params1) -> candidate.source().as_slice()[1]),
+                                ProbabilisticScoringFeeParameters.with_default(), new byte[32]);
+                    }
+                }, new MessageRouter.MessageRouterInterface() {
+                    @Override public Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, Destination destination) {
+                        return Result_OnionMessagePathNoneZ.err();
+                    }
+                    @Override public Result_CVec_BlindedPathZNoneZ create_blinded_paths(byte[] recipient, byte[][] peers) {
+                        return Result_CVec_BlindedPathZNoneZ.err();
+                    }
+                });
                 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.chan_manager = ChannelManager.of(this.fee_estimator, chain_watch, tx_broadcaster, router, logger,
+                    this.entropy_source, this.node_signer, this.signer_provider, get_config(), params,
+                        (int)(System.currentTimeMillis() / 1000));
+                this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), route_handler.as_RoutingMessageHandler(),
+                        IgnoringMessageHandler.of().as_OnionMessageHandler(), this.custom_message_handler, 0xdeadbeef,
+                        this.entropy_source.get_secure_random_bytes(), logger, this.node_signer);
+           }
 
             this.node_id = chan_manager.get_our_node_id();
+            this.filter = null;
             bind_nio();
             System.gc();
         }
@@ -369,13 +495,34 @@ class HumanObjectPeerTestInstance {
             if (use_chan_manager_constructor) {
                 byte[][] monitors = {orig.monitors.values().stream().iterator().next().write()};
                 byte[] serialized = orig.chan_manager.write();
+                byte[] serialized_graph = orig.net_graph.write();
+                byte[] serialized_scorer = orig.constructor.scorer.write();
                 try {
                     Filter filter_nullable = null;
                     if (this.filter instanceof Option_FilterZ.Some) {
                         filter_nullable = ((Option_FilterZ.Some) this.filter).some;
                     }
-                    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.constructor = new ChannelManagerConstructor(serialized, monitors, get_config(),
+                        this.explicit_keys_manager.as_EntropySource(), this.explicit_keys_manager.as_NodeSigner(), this.explicit_keys_manager.as_SignerProvider(),
+                        this.fee_estimator, this.chain_monitor, filter_nullable,
+                            serialized_graph, ProbabilisticScoringDecayParameters.with_default(),
+                            ProbabilisticScoringFeeParameters.with_default(), serialized_scorer, null,
+                            this.tx_broadcaster, this.logger);
+                    try {
+                        // Test that ChannelManagerConstructor correctly rejects duplicate ChannelMonitors
+                        byte[][] monitors_dupd = new byte[2][];
+                        monitors_dupd[0] = monitors[0];
+                        monitors_dupd[1] = monitors[0];
+                        ChannelManagerConstructor constr = new ChannelManagerConstructor(serialized, monitors_dupd, get_config(),
+                            this.explicit_keys_manager.as_EntropySource(), this.explicit_keys_manager.as_NodeSigner(), this.explicit_keys_manager.as_SignerProvider(),
+                            this.fee_estimator, this.chain_monitor, filter_nullable,
+                                serialized_graph, ProbabilisticScoringDecayParameters.with_default(),
+                                ProbabilisticScoringFeeParameters.with_default(), serialized_scorer, null,
+                                this.tx_broadcaster, this.logger);
+                        assert false;
+                    } catch (ChannelManagerConstructor.InvalidSerializedDataException e) {}
+                    this.net_graph = this.constructor.net_graph;
+                    setup_route_handler();
                     constructor.chain_sync_completed(new ChannelManagerConstructor.EventHandler() {
                         @Override public void handle_event(Event event) {
                             synchronized (pending_manager_events) {
@@ -384,7 +531,9 @@ class HumanObjectPeerTestInstance {
                             }
                         }
                         @Override public void persist_manager(byte[] channel_manager_bytes) { assert channel_manager_bytes.length > 1; }
-                    });
+                        @Override public void persist_network_graph(byte[] graph_bytes) { assert graph_bytes.length > 1; network_graph_persists += 1; }
+                        @Override public void persist_scorer(byte[] scorer_bytes) { assert scorer_bytes.length > 1; }
+                    }, !use_ignore_handler);
                     this.chan_manager = constructor.channel_manager;
                     this.peer_manager = constructor.peer_manager;
                     must_free_objs.add(new WeakReference<>(this.chan_manager));
@@ -399,25 +548,71 @@ class HumanObjectPeerTestInstance {
                     assert false;
                 }
             } else {
+                this.net_graph = NetworkGraph.of(Network.LDKNetwork_Bitcoin, this.logger);
+                Router router = Router.new_impl(new Router.RouterInterface() {
+                    @Override
+                    public Result_RouteLightningErrorZ find_route_with_id(byte[] payer, RouteParameters route_params, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs, byte[] _payment_hash, byte[] _payment_id) {
+                        return find_route(payer, route_params, first_hops, inflight_htlcs);
+                    }
+
+                    @Override
+                    public Result_CVec_C2Tuple_BlindedPayInfoBlindedPathZZNoneZ create_blinded_payment_paths(byte[] recipient, ChannelDetails[] first_hops, ReceiveTlvs tlvs, long amount_msats) {
+                        return Result_CVec_C2Tuple_BlindedPayInfoBlindedPathZZNoneZ.err();
+                    }
+
+                    @Override
+                    public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters params, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs) {
+                        while (true) {
+                            try (ReadOnlyNetworkGraph graph = net_graph.read_only()) {
+                                assert graph.channel(424242) == null;
+                                long[] channels = graph.list_channels();
+                                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.
+                                    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());
+                                assert Arrays.equals(chan.get_node_one().as_slice(), chan_manager.get_our_node_id()) ||
+                                        Arrays.equals(chan.get_node_two().as_slice(), chan_manager.get_our_node_id());
+                                break;
+                            }
+                        }
+                        return UtilMethods.find_route(payer, params, net_graph, first_hops, logger,
+                                ScoreLookUp.new_impl((candidate, usage, params1) -> candidate.source().as_slice()[0]),
+                                ProbabilisticScoringFeeParameters.with_default(), new byte[32]);
+                    }
+                }, new MessageRouter.MessageRouterInterface() {
+                    @Override public Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, Destination destination) {
+                        return Result_OnionMessagePathNoneZ.err();
+                    }
+                    @Override public Result_CVec_BlindedPathZNoneZ create_blinded_paths(byte[] recipient, byte[][] peers) {
+                        return Result_CVec_BlindedPathZNoneZ.err();
+                    }
+                });
+                this.setup_route_handler();
                 ChannelMonitor[] monitors = new ChannelMonitor[1];
                 assert orig.monitors.size() == 1;
                 if (!break_cross_peer_refs) {
                     monitors[0] = orig.monitors.values().stream().iterator().next();
                 } else {
                     byte[] serialized = orig.monitors.values().stream().iterator().next().write();
-                    Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res =
-                            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.get_b();
+                    Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ res =
+                            UtilMethods.C2Tuple_ThirtyTwoBytesChannelMonitorZ_read(serialized, this.entropy_source, this.signer_provider);
+                    assert res instanceof Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ_OK;
+                    monitors[0] = ((Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ_OK) res).res.get_b();
                 }
                 byte[] serialized = orig.chan_manager.write();
-                Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ read_res =
-                        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.get_b();
+                Result_C2Tuple_ThirtyTwoBytesChannelManagerZDecodeErrorZ read_res =
+                        UtilMethods.C2Tuple_ThirtyTwoBytesChannelManagerZ_read(serialized, this.entropy_source, this.node_signer, this.signer_provider, this.fee_estimator, this.chain_watch, this.tx_broadcaster, router, this.logger, get_config(), monitors);
+                assert read_res instanceof Result_C2Tuple_ThirtyTwoBytesChannelManagerZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelManagerZDecodeErrorZ_OK;
+                this.chan_manager = ((Result_C2Tuple_ThirtyTwoBytesChannelManagerZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelManagerZDecodeErrorZ_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(),
+                        IgnoringMessageHandler.of().as_OnionMessageHandler(), this.custom_message_handler,
+                        (int)(System.currentTimeMillis() / 1000), this.entropy_source.get_secure_random_bytes(),
+                        logger, this.node_signer);
                 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
@@ -438,9 +633,10 @@ class HumanObjectPeerTestInstance {
                 // the ChannelSigner.
                 this.ptr_to = orig.chan_manager;
             }
+            this.filter = null;
         }
 
-        TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] connect_block(Block b, int height, long expected_monitor_update_len) {
+        TwoTuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZ[] connect_block(Block b, int height, long expected_monitor_update_len) {
             byte[] header = Arrays.copyOfRange(b.bitcoinSerialize(), 0, 80);
             TwoTuple_usizeTransactionZ[] txn;
             if (b.hasTransactions()) {
@@ -459,7 +655,7 @@ class HumanObjectPeerTestInstance {
                 synchronized (monitors) {
                     assert monitors.size() == 1;
                     for (ChannelMonitor mon : monitors.values()) {
-                        TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret = mon.block_connected(header, txn, height, tx_broadcaster, fee_estimator, logger);
+                        TwoTuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZ[] ret = mon.block_connected(header, txn, height, tx_broadcaster, fee_estimator, logger);
                         assert ret.length == expected_monitor_update_len;
                         return ret;
                     }
@@ -498,9 +694,15 @@ class HumanObjectPeerTestInstance {
                 synchronized (monitors) {
                     assert monitors.size() == 1;
                     for (ChannelMonitor mon : monitors.values()) {
-                        Event[] res = mon.get_and_clear_pending_events();
-                        assert res.length == expected_len;
-                        return res;
+                        ArrayList<Event> res = new ArrayList<Event>();
+                        EventHandler handler = EventHandler.new_impl(new EventHandler.EventHandlerInterface() {
+                            @Override public void handle_event(Event event) {
+                                res.add(event);
+                            }
+                        });
+                        mon.process_pending_events(handler);
+                        assert res.size() == expected_len;
+                        return res.toArray(new Event[0]);
                     }
                     return null;
                 }
@@ -516,9 +718,11 @@ class HumanObjectPeerTestInstance {
             if (use_chan_manager_constructor) {
                 while (res.length < expected_len) {
                     synchronized (this.pending_manager_events) {
-                        res = this.pending_manager_events.toArray(res);
-                        assert res.length == expected_len || res.length == 0; // We don't handle partial results
-                        this.pending_manager_events.clear();
+                        if (this.pending_manager_events.size() >= expected_len) {
+                            res = this.pending_manager_events.toArray(res);
+                            assert res.length == expected_len;
+                            this.pending_manager_events.clear();
+                        }
                         if (res.length < expected_len) {
                             try { this.pending_manager_events.wait(); } catch (InterruptedException e) { assert false; }
                         }
@@ -533,7 +737,6 @@ class HumanObjectPeerTestInstance {
                         peer2.nio_peer_handler.check_events();
                     }
                     chan_manager.as_EventsProvider().process_pending_events(EventHandler.new_impl(l::add));
-                    assert l.size() == expected_len || l.size() == 0; // We don't handle partial results
                 }
                 return l.toArray(new Event[0]);
             }
@@ -571,9 +774,6 @@ class HumanObjectPeerTestInstance {
 
     void maybe_exchange_peer_messages(Peer peer1, Peer peer2) {
         if (!use_nio_peer_handler) {
-            synchronized (runqueue) {
-                ran = false;
-            }
             while (true) {
                 peer1.peer_manager.process_events();
                 peer2.peer_manager.process_events();
@@ -592,6 +792,16 @@ class HumanObjectPeerTestInstance {
             peer2.nio_peer_handler.check_events();
         }
     }
+    void do_disconnect_event(PeerManager pm, SocketDescriptor descriptor) {
+        if (!t.isAlive()) t.start();
+        synchronized (runqueue) {
+            ran = true;
+            runqueue.add(() -> {
+                pm.socket_disconnected(descriptor);
+            });
+            runqueue.notifyAll();
+        }
+    }
     void do_read_event(PeerManager pm, SocketDescriptor descriptor, byte[] data) {
         if (!t.isAlive()) t.start();
         synchronized (runqueue) {
@@ -625,7 +835,7 @@ class HumanObjectPeerTestInstance {
                     return data.length;
                 }
 
-                @Override public void disconnect_socket() { assert false; }
+                @Override public void disconnect_socket() { do_disconnect_event(peer1.peer_manager, descriptor1ref.val); }
                 @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == 2; }
                 @Override public long hash() { return 2; }
             });
@@ -637,15 +847,15 @@ class HumanObjectPeerTestInstance {
                     return data.length;
                 }
 
-                @Override public void disconnect_socket() { assert false; }
+                @Override public void disconnect_socket() { do_disconnect_event(peer2.peer_manager, descriptor2); }
                 @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == 1; }
                 @Override public long hash() { return 1; }
             });
 
-            Result_CVec_u8ZPeerHandleErrorZ conn_res = peer1.peer_manager.new_outbound_connection(peer2.node_id, descriptor1.val);
+            Result_CVec_u8ZPeerHandleErrorZ conn_res = peer1.peer_manager.new_outbound_connection(peer2.node_id, descriptor1.val, Option_SocketAddressZ.none());
             assert conn_res instanceof Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK;
 
-            Result_NonePeerHandleErrorZ inbound_conn_res = peer2.peer_manager.new_inbound_connection(descriptor2);
+            Result_NonePeerHandleErrorZ inbound_conn_res = peer2.peer_manager.new_inbound_connection(descriptor2, Option_SocketAddressZ.none());
             assert inbound_conn_res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_OK;
             do_read_event(peer2.peer_manager, descriptor2, ((Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) conn_res).res);
 
@@ -659,8 +869,10 @@ class HumanObjectPeerTestInstance {
 
         connect_peers(peer1, peer2);
 
-        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;
+        UInt128 user_id = new UInt128(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
+
+        Result_ThirtyTwoBytesAPIErrorZ cc_res = peer1.chan_manager.create_channel(peer2.node_id, 100000, 1000, user_id, Option_ThirtyTwoBytesZ.none(), null);
+        assert cc_res instanceof Result_ThirtyTwoBytesAPIErrorZ.Result_ThirtyTwoBytesAPIErrorZ_OK;
 
         // Previously, this was a SEGFAULT instead of get_funding_txo() returning null.
         ChannelDetails pre_funding_chan = peer1.chan_manager.list_channels()[0];
@@ -669,21 +881,25 @@ class HumanObjectPeerTestInstance {
         Event[] events = peer1.get_manager_events(1, peer1, peer2);
         assert events[0] instanceof Event.FundingGenerationReady;
         assert ((Event.FundingGenerationReady) events[0]).channel_value_satoshis == 100000;
-        assert ((Event.FundingGenerationReady) events[0]).user_channel_id == 42;
+        assert ((Event.FundingGenerationReady) events[0]).user_channel_id.equals(user_id);
         byte[] funding_spk = ((Event.FundingGenerationReady) events[0]).output_script;
         assert funding_spk.length == 34 && funding_spk[0] == 0 && funding_spk[1] == 32; // P2WSH
         byte[] chan_id = ((Event.FundingGenerationReady) events[0]).temporary_channel_id;
 
-        NetworkParameters bitcoinj_net = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
+        BitcoinNetworkParams bitcoinj_net = BitcoinNetworkParams.of(BitcoinNetwork.MAINNET);
 
+        Transaction funding_input = new Transaction(bitcoinj_net);
+        funding_input.addOutput(Coin.SATOSHI.multiply(100000), new Script(funding_spk));
         Transaction funding = new Transaction(bitcoinj_net);
-        funding.addInput(new TransactionInput(bitcoinj_net, funding, new byte[0]));
+        funding.addInput(funding_input.getOutput(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(100000), new Script(funding_spk));
-        Result_NoneAPIErrorZ funding_res = peer1.chan_manager.funding_transaction_generated(chan_id, funding.bitcoinSerialize());
+        Result_NoneAPIErrorZ funding_res = peer1.chan_manager.funding_transaction_generated(chan_id, peer2.node_id, funding.bitcoinSerialize());
         assert funding_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
 
+        gossip_txout = new TxOut(100000, funding_spk);
+
         maybe_exchange_peer_messages(peer1, peer2);
         synchronized (peer1.broadcast_set) {
             while (peer1.broadcast_set.size() != 1) {
@@ -691,6 +907,12 @@ class HumanObjectPeerTestInstance {
             }
         }
 
+        events = peer1.get_manager_events(1, peer1, peer2);
+        assert events[0] instanceof Event.ChannelPending;
+
+        events = peer2.get_manager_events(1, peer1, peer2);
+        assert events[0] instanceof Event.ChannelPending;
+
         assert peer1.broadcast_set.size() == 1;
         assert Arrays.equals(peer1.broadcast_set.get(0), funding.bitcoinSerialize());
         peer1.broadcast_set.clear();
@@ -706,9 +928,14 @@ class HumanObjectPeerTestInstance {
         }
 
         maybe_exchange_peer_messages(peer1, peer2);
-        while (peer1.chan_manager.list_usable_channels().length != 1 || peer2.chan_manager.list_usable_channels().length != 1)
+        while (peer1.chan_manager.list_usable_channels().length != 1 || peer2.chan_manager.list_usable_channels().length != 1) ;
+
+        events = peer1.get_manager_events(1, peer1, peer2);
+        assert events[0] instanceof Event.ChannelReady;
+
+        events = peer2.get_manager_events(1, peer1, peer2);
+        assert events[0] instanceof Event.ChannelReady;
 
-        peer1.chan_manager.list_channels();
         ChannelDetails[] peer1_chans = peer1.chan_manager.list_usable_channels();
         ChannelDetails[] peer2_chans = peer2.chan_manager.list_usable_channels();
         assert peer1_chans.length == 1;
@@ -721,51 +948,115 @@ class HumanObjectPeerTestInstance {
         assert Arrays.equals(peer1_chans[0].get_channel_id(), funding.getTxId().getReversedBytes());
         assert Arrays.equals(peer2_chans[0].get_channel_id(), funding.getTxId().getReversedBytes());
 
-        Result_InvoiceSignOrCreationErrorZ invoice = UtilMethods.create_invoice_from_channelmanager(peer2.chan_manager, peer2.keys_interface, Currency.LDKCurrency_Bitcoin, Option_u64Z.none(), "Invoice Description");
-        assert invoice instanceof Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK;
-        System.out.println("Got invoice: " + ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
-
-        Result_InvoiceNoneZ parsed_invoice = Invoice.from_str(((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
-        assert parsed_invoice instanceof Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK;
-        assert Arrays.equals(((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res.payment_hash(), ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash());
-        SignedRawInvoice signed_raw = ((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res.into_signed_raw();
-        RawInvoice raw_invoice = signed_raw.raw_invoice();
-        byte[] desc_hash = raw_invoice.hash();
+        // Generate a random invoice description to exercise the string conversion logic a good bit
+        String invoice_description;
+        char[] string_bytes = new char[16];
+        try {
+            new FileReader("/dev/urandom").read(string_bytes);
+        } catch (Exception e) { assert false; }
+        invoice_description = new String(string_bytes);
+
+        Result_Bolt11InvoiceSignOrCreationErrorZ invoice = UtilMethods.create_invoice_from_channelmanager(peer2.chan_manager, peer2.node_signer, peer2.logger, Currency.LDKCurrency_Bitcoin, Option_u64Z.some(10000000), invoice_description, 7200, Option_u16Z.none());
+        assert invoice instanceof Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK;
+        System.out.println("Got invoice: " + ((Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
+
+        String fuck = ((Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str();
+        Result_Bolt11InvoiceParseOrSemanticErrorZ parsed_invoice = Bolt11Invoice.from_str(fuck);
+if (parsed_invoice instanceof Result_Bolt11InvoiceParseOrSemanticErrorZ.Result_Bolt11InvoiceParseOrSemanticErrorZ_Err) {
+    System.err.println(((Result_Bolt11InvoiceParseOrSemanticErrorZ.Result_Bolt11InvoiceParseOrSemanticErrorZ_Err) parsed_invoice).err.to_str());
+}
+        assert parsed_invoice instanceof Result_Bolt11InvoiceParseOrSemanticErrorZ.Result_Bolt11InvoiceParseOrSemanticErrorZ_OK;
+        assert ((Result_Bolt11InvoiceParseOrSemanticErrorZ.Result_Bolt11InvoiceParseOrSemanticErrorZ_OK) parsed_invoice).res.fallback_addresses().length == 0;
+        assert Arrays.equals(((Result_Bolt11InvoiceParseOrSemanticErrorZ.Result_Bolt11InvoiceParseOrSemanticErrorZ_OK) parsed_invoice).res.payment_hash(), ((Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash());
+        SignedRawBolt11Invoice signed_raw = ((Result_Bolt11InvoiceParseOrSemanticErrorZ.Result_Bolt11InvoiceParseOrSemanticErrorZ_OK) parsed_invoice).res.into_signed_raw();
+        RawBolt11Invoice raw_invoice = signed_raw.raw_invoice();
+        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");
-        byte[] payment_hash = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash();
-        byte[] payment_secret = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_secret();
-        byte[] dest_node_id = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.recover_payee_pub_key();
-        assert Arrays.equals(dest_node_id, peer2.node_id);
-        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, 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_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);
-        payment_res = peer1.chan_manager.send_payment(r2, payment_hash, payment_secret);
-        assert payment_res instanceof Result_PaymentIdPaymentSendFailureZ.Result_PaymentIdPaymentSendFailureZ_Err;
+        String description_string = raw_invoice_description.into_inner().get_a();
+        boolean has_null = false;
+        for (byte db : invoice_description.getBytes())
+            if (db == 0)
+                has_null = true;
+        if (!has_null) // Null codepoints are dropped, so may fail
+            assert description_string.equals(invoice_description);
+
+        // Do a trivial test of constructing a phantom invoice
+        Result_Bolt11InvoiceSignOrCreationErrorZ phantom_invoice = UtilMethods.create_phantom_invoice(
+                Option_u64Z.some(42000),
+                Option_ThirtyTwoBytesZ.none(),
+                "Phantom Invoice", 7200,
+                new PhantomRouteHints[]{ peer2.chan_manager.get_phantom_route_hints() },
+                peer2.entropy_source,
+                PhantomKeysManager.of(peer2.key_seed, 42, 42, new byte[32]).as_NodeSigner(),
+                peer2.logger, Currency.LDKCurrency_Bitcoin, Option_u16Z.none(),
+                System.currentTimeMillis() / 1000);
+        assert phantom_invoice.is_ok();
+        RouteHint[] hints = ((Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK)phantom_invoice).res.route_hints();
+        assert hints.length == 1;
+        RouteHintHop[] val = hints[0].get_a();
+        assert hints[0].get_a().length == 1; // Our one channel is public so we should just have a single-hop hint
+
+        if (!use_invoice_payer) {
+            byte[] payment_hash = ((Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash();
+            byte[] payment_secret = ((Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_secret();
+            byte[] dest_node_id = ((Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK) invoice).res.recover_payee_pub_key();
+            assert Arrays.equals(dest_node_id, peer2.node_id);
+            Bolt11InvoiceFeatures invoice_features = ((Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK) invoice).res.features();
+            RouteHint[] route_hints = ((Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK) invoice).res.route_hints();
+
+            Payee payee = Payee.clear(peer2.node_id, route_hints, invoice_features, 42);
+            PaymentParameters pay_params = PaymentParameters.of(payee, Option_u64Z.none(), 6*24*14, (byte)1, (byte)2, new long[0], new long[0]);
+            RouteParameters route_params = RouteParameters.from_payment_params_and_value(pay_params, 10000000);
+            Result_RouteLightningErrorZ route_res = UtilMethods.find_route(
+                    peer1.chan_manager.get_our_node_id(), route_params, peer1.net_graph,
+                    peer1_chans, peer1.logger,
+                    ProbabilisticScorer.of(ProbabilisticScoringDecayParameters.with_default(), peer1.net_graph, peer1.logger).as_ScoreLookUp(),
+                    ProbabilisticScoringFeeParameters.with_default(), new byte[32]);
+            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].get_hops().length == 1;
+            assert route.get_paths()[0].final_value_msat() == 10000000;
+
+            byte[] payment_id = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
+            Result_NonePaymentSendFailureZ payment_res = peer1.chan_manager.send_payment_with_route(route, payment_hash, RecipientOnionFields.secret_only(payment_secret), payment_id);
+            assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_OK;
+
+            RouteHop[] hops = new RouteHop[1];
+            byte[] hop_pubkey = new byte[33];
+            hop_pubkey[0] = 3;
+            hop_pubkey[1] = 42;
+            NodeFeatures node_features = NodeFeatures.empty();
+            ChannelFeatures channel_features = ChannelFeatures.empty();
+            hops[0] = RouteHop.of(hop_pubkey, node_features, 42, channel_features, 100, 0, false);
+            Path[] paths = new Path[1];
+            paths[0] = Path.of(hops, null);
+            Route r2 = Route.of(paths, RouteParameters.from_payment_params_and_value(pay_params, 100));
+            payment_res = peer1.chan_manager.send_payment_with_route(r2, payment_hash, RecipientOnionFields.secret_only(payment_secret), payment_id);
+            assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_Err;
+        } else {
+            Result_C3Tuple_ThirtyTwoBytesRecipientOnionFieldsRouteParametersZNoneZ pay_params_res =
+                UtilMethods.payment_parameters_from_invoice(((Result_Bolt11InvoiceParseOrSemanticErrorZ.Result_Bolt11InvoiceParseOrSemanticErrorZ_OK) parsed_invoice).res);
+            assert pay_params_res.is_ok();
+            Result_C3Tuple_ThirtyTwoBytesRecipientOnionFieldsRouteParametersZNoneZ.Result_C3Tuple_ThirtyTwoBytesRecipientOnionFieldsRouteParametersZNoneZ_OK pay_params =
+                (Result_C3Tuple_ThirtyTwoBytesRecipientOnionFieldsRouteParametersZNoneZ.Result_C3Tuple_ThirtyTwoBytesRecipientOnionFieldsRouteParametersZNoneZ_OK)pay_params_res;
+            Result_NoneRetryableSendFailureZ pay_res = peer1.chan_manager.send_payment(pay_params.res.get_a(), pay_params.res.get_b(), new byte[32], pay_params.res.get_c(), Retry.attempts(0));
+            assert pay_res.is_ok();
+        }
 
         if (reload_peers) {
+            assert peer1.network_graph_persists == 0;
+            assert peer2.network_graph_persists == 0;
             if (use_chan_manager_constructor) {
                 peer1.constructor.interrupt();
                 peer2.constructor.interrupt();
+                if (!use_ignore_handler) {
+                    assert peer1.network_graph_persists >= 1;
+                    assert peer2.network_graph_persists >= 1;
+                } else {
+                    assert peer1.network_graph_persists == 0;
+                    assert peer2.network_graph_persists == 0;
+                }
             } else if (use_nio_peer_handler) {
                 peer1.nio_peer_handler.interrupt();
                 peer2.nio_peer_handler.interrupt();
@@ -792,7 +1083,7 @@ class HumanObjectPeerTestInstance {
             this.best_blockhash = best_blockhash;
         }
     }
-    void do_test_message_handler_b(TestState state) throws InterruptedException {
+    void do_test_message_handler_b(TestState state) throws Exception {
         GcCheck obj = new GcCheck();
         if (state.ref_block != null) {
             // Ensure the original peers get freed before we move on. Note that we have to be in a different function
@@ -809,15 +1100,26 @@ class HumanObjectPeerTestInstance {
         state.peer2.chan_manager.process_pending_htlc_forwards();
 
         events = state.peer2.get_manager_events(1, state.peer1, state.peer2);
-        assert events[0] instanceof Event.PaymentReceived;
-        assert ((Event.PaymentReceived)events[0]).purpose instanceof PaymentPurpose.InvoicePayment;
-        byte[] payment_preimage = ((PaymentPurpose.InvoicePayment)((Event.PaymentReceived)events[0]).purpose).payment_preimage;
+        assert events[0] instanceof Event.PaymentClaimable;
+        assert ((Event.PaymentClaimable)events[0]).purpose instanceof PaymentPurpose.InvoicePayment;
+        assert ((PaymentPurpose.InvoicePayment) ((Event.PaymentClaimable)events[0]).purpose).payment_preimage instanceof Option_ThirtyTwoBytesZ.Some;
+        byte[] payment_preimage = ((Option_ThirtyTwoBytesZ.Some) ((PaymentPurpose.InvoicePayment)((Event.PaymentClaimable)events[0]).purpose).payment_preimage).some;
         assert !Arrays.equals(payment_preimage, new byte[32]);
         state.peer2.chan_manager.claim_funds(payment_preimage);
 
-        events = state.peer1.get_manager_events(1, state.peer1, state.peer2);
+        events = state.peer2.get_manager_events(1, state.peer1, state.peer2);
+        assert events[0] instanceof Event.PaymentClaimed;
+        assert ((Event.PaymentClaimed)events[0]).purpose instanceof PaymentPurpose.InvoicePayment;
+        assert ((PaymentPurpose.InvoicePayment) ((Event.PaymentClaimed)events[0]).purpose).payment_preimage instanceof  Option_ThirtyTwoBytesZ.Some;
+        payment_preimage = ((Option_ThirtyTwoBytesZ.Some)((PaymentPurpose.InvoicePayment)((Event.PaymentClaimed)events[0]).purpose).payment_preimage).some;
+        assert !Arrays.equals(payment_preimage, new byte[32]);
+
+        events = state.peer1.get_manager_events(2, state.peer1, state.peer2);
         assert events[0] instanceof Event.PaymentSent;
         assert Arrays.equals(((Event.PaymentSent) events[0]).payment_preimage, payment_preimage);
+        assert events[1] instanceof Event.PaymentPathSuccessful;
+        assert ((Event.PaymentPathSuccessful) events[1]).payment_hash instanceof Option_ThirtyTwoBytesZ.Some;
+        assert Arrays.equals(((Option_ThirtyTwoBytesZ.Some) ((Event.PaymentPathSuccessful) events[1]).payment_hash).some, ((Event.PaymentSent) events[0]).payment_hash);
 
         if (use_nio_peer_handler) {
             // We receive PaymentSent immediately upon receipt of the payment preimage, but we expect to not have an
@@ -836,7 +1138,7 @@ class HumanObjectPeerTestInstance {
         ChannelDetails[] peer1_chans = state.peer1.chan_manager.list_channels();
 
         if (nice_close) {
-            Result_NoneAPIErrorZ close_res = state.peer1.chan_manager.close_channel(peer1_chans[0].get_channel_id());
+            Result_NoneAPIErrorZ close_res = state.peer1.chan_manager.close_channel(peer1_chans[0].get_channel_id(), state.peer2.node_id);
             assert close_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
             maybe_exchange_peer_messages(state.peer1, state.peer2);
             synchronized (state.peer1.broadcast_set) {
@@ -845,11 +1147,8 @@ class HumanObjectPeerTestInstance {
             synchronized (state.peer2.broadcast_set) {
                 while (state.peer2.broadcast_set.size() != 1) state.peer2.broadcast_set.wait();
             }
-
-            assert state.peer1.broadcast_set.size() == 1;
-            assert state.peer2.broadcast_set.size() == 1;
         } else {
-            state.peer1.chan_manager.force_close_all_channels();
+            state.peer1.chan_manager.force_close_all_channels_broadcasting_latest_txn();
             maybe_exchange_peer_messages(state.peer1, state.peer2);
             synchronized (state.peer1.broadcast_set) {
                 while (state.peer1.broadcast_set.size() != 1) state.peer1.broadcast_set.wait();
@@ -857,11 +1156,11 @@ class HumanObjectPeerTestInstance {
             synchronized (state.peer2.broadcast_set) {
                 while (state.peer2.broadcast_set.size() != 1) state.peer2.broadcast_set.wait();
             }
-
-            assert state.peer1.broadcast_set.size() == 1;
-            assert state.peer2.broadcast_set.size() == 1;
         }
 
+        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);
@@ -873,22 +1172,22 @@ class HumanObjectPeerTestInstance {
             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;
+                assert ((Balance.ClaimableOnChannelClose) bal).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;
+                assert ((Balance.ClaimableOnChannelClose) bal).amount_satoshis == 10000 + 1;
             }
         }
 
         if (!nice_close) {
-            NetworkParameters bitcoinj_net = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
+            BitcoinNetworkParams bitcoinj_net = BitcoinNetworkParams.of(BitcoinNetwork.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_TxidCVec_C2Tuple_u32TxOutZZZ[] watch_outputs = state.peer2.connect_block(b, 10, 1);
+            TwoTuple_ThirtyTwoBytesCVec_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].get_a(), tx.getTxId().getReversedBytes());
@@ -911,7 +1210,7 @@ class HumanObjectPeerTestInstance {
             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);
+                Result_TransactionNoneZ tx_res = state.peer2.explicit_keys_manager.spend_spendable_outputs(((Event.SpendableOutputs) broadcastable_event[0]).outputs, additional_outputs, new byte[]{0x00}, 253, Option_u32Z.none());
                 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;
@@ -919,13 +1218,22 @@ class HumanObjectPeerTestInstance {
                 assert Arrays.equals(built_tx.getOutput(0).getScriptBytes(), new byte[]{0x42});
                 assert built_tx.getOutput(0).getValue().value == 420;
             }
+
+            while (state.peer1.peer_manager.get_peer_node_ids().length != 0 || state.peer2.peer_manager.get_peer_node_ids().length != 0) {
+                // LDK disconnects peers before sending an error message, so wait for disconnection.
+                Thread.sleep(10);
+            }
+
+            connect_peers(state.peer1, state.peer2);
         }
 
         // 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);
+            synchronized(state.peer1.custom_messages_to_send) {
+                state.peer1.custom_messages_to_send.add(custom_message_bytes);
+            }
             state.peer1.peer_manager.process_events();
             synchronized (state.peer2.received_custom_messages) {
                 while (true) {
@@ -941,7 +1249,7 @@ class HumanObjectPeerTestInstance {
         }
 
         if (use_nio_peer_handler) {
-            state.peer1.peer_manager.disconnect_by_node_id(state.peer2.chan_manager.get_our_node_id(), false);
+            state.peer1.peer_manager.disconnect_by_node_id(state.peer2.chan_manager.get_our_node_id());
             while (state.peer1.peer_manager.get_peer_node_ids().length != 0) Thread.yield();
             while (state.peer2.peer_manager.get_peer_node_ids().length != 0) Thread.yield();
             state.peer1.nio_peer_handler.interrupt();
@@ -952,17 +1260,37 @@ class HumanObjectPeerTestInstance {
         state.peer2.get_monitor_events(0);
 
         if (use_chan_manager_constructor) {
+            assert state.peer1.network_graph_persists == 0;
+            assert state.peer2.network_graph_persists == 0;
             state.peer1.constructor.interrupt();
             state.peer2.constructor.interrupt();
+            if (!use_ignore_handler) {
+                assert state.peer1.network_graph_persists >= 1;
+                assert state.peer2.network_graph_persists >= 1;
+            } else {
+                assert state.peer1.network_graph_persists == 0;
+                assert state.peer2.network_graph_persists == 0;
+            }
         }
 
         t.interrupt();
 
+        if (state.peer1.net_graph != null)
+            state.peer1.net_graph.write();
+
         // 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,};
+        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)0x01,(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,(byte)0xde,(byte)0xad,(byte)0xbe,(byte)0xef,(byte)0x42,(byte)0x42,(byte)0x42,(byte)0x42};
         Result_ChannelUpdateDecodeErrorZ upd_msg = ChannelUpdate.read(serd);
         assert upd_msg instanceof Result_ChannelUpdateDecodeErrorZ.Result_ChannelUpdateDecodeErrorZ_OK;
+        assert ((Result_ChannelUpdateDecodeErrorZ.Result_ChannelUpdateDecodeErrorZ_OK) upd_msg).res.get_contents().get_htlc_maximum_msat() == 0xdeadbeef42424242L;
         Option_NetworkUpdateZ upd = Option_NetworkUpdateZ.some(NetworkUpdate.channel_update_message(((Result_ChannelUpdateDecodeErrorZ.Result_ChannelUpdateDecodeErrorZ_OK) upd_msg).res));
+
+        if (use_chan_manager_constructor) {
+            // Lock the scorer twice back-to-back to check that the try-with-resources AutoCloseable on the scorer works.
+            try (ChannelManagerConstructor.ScorerWrapper score = state.peer1.constructor.get_locked_scorer()) {
+                score.prob_scorer.debug_log_liquidity_stats();
+            }
+        }
     }
 
     java.util.LinkedList<WeakReference<Object>> must_free_objs = new java.util.LinkedList();
@@ -978,14 +1306,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_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);
+    static HumanObjectPeerTestInstance do_test_run(boolean nice_close, boolean use_km_wrapper, boolean use_full_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, boolean use_invoice_payer) throws Exception {
+        HumanObjectPeerTestInstance instance = new HumanObjectPeerTestInstance(nice_close, use_km_wrapper, use_full_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, use_invoice_payer);
         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_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);
+    static void do_test(boolean nice_close, boolean use_km_wrapper, boolean use_full_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, boolean use_invoice_payer) throws Exception {
+        HumanObjectPeerTestInstance instance = do_test_run(nice_close, use_km_wrapper, use_full_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler, use_ignoring_routing_handler, use_chan_manager_constructor, use_invoice_payer);
         while (instance.gc_count != instance.gc_exp_count) {
             System.gc();
             System.runFinalization();
@@ -993,17 +1321,27 @@ public class HumanObjectPeerTest {
         for (WeakReference<Object> o : instance.must_free_objs)
             assert o.get() == null;
     }
-    @Test
-    public void test_message_handler() throws InterruptedException {
-        for (int i = 0; i < (1 << 8) - 1; i++) {
+    public static final int TEST_ITERATIONS = (1 << 10);
+    public static void do_test_message_handler(IntConsumer statusFunction) throws Exception {
+        Thread gc_thread = new Thread(() -> {
+            while (true) {
+                System.gc();
+                System.runFinalization();
+                try { Thread.sleep(0, 1); } catch (InterruptedException e) { break; }
+            }
+        }, "Test GC Thread");
+        gc_thread.start();
+        for (int i = 0; i < TEST_ITERATIONS; i++) {
             boolean nice_close =                   (i & (1 << 0)) != 0;
             boolean use_km_wrapper =               (i & (1 << 1)) != 0;
-            boolean use_manual_watch =             (i & (1 << 2)) != 0;
-            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_full_km_wrapper =          (i & (1 << 2)) != 0;
+            boolean use_manual_watch =             (i & (1 << 3)) != 0;
+            boolean reload_peers =                 (i & (1 << 4)) != 0;
+            boolean break_cross_refs =             (i & (1 << 5)) != 0;
             boolean use_ignoring_routing_handler = (i & (1 << 6)) != 0;
             boolean use_chan_manager_constructor = (i & (1 << 7)) != 0;
+            boolean use_invoice_payer =            (i & (1 << 8)) != 0;
+            boolean nio_peer_handler =             (i & (1 << 9)) != 0;
             if (break_cross_refs && !reload_peers) {
                 // There are no cross refs to break without reloading peers.
                 continue;
@@ -1012,20 +1350,41 @@ 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) {
+            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_ignoring_routing_handler, use_chan_manager_constructor);
+            if (use_chan_manager_constructor && use_ignoring_routing_handler && use_invoice_payer) {
+                // As documented on ChannelManagerConstructor, if we provide a `null` NetworkGraph (because we want to
+                // use an IgnoringMessageHandler), no InvoicePayer will be constructed for us, thus we cannot use an
+                // InvoicePayer to send payments in this case.
+                continue;
+            }
+            if (use_chan_manager_constructor && use_km_wrapper) {
+                // ChannelManagerConstructor requires a full KeysManager, so using wrapped keys interfaces doesn't make
+                // any sense.
+                continue;
+            }
+            if (use_full_km_wrapper && !use_km_wrapper) {
+                // Gotta use some KM wrapper if we're gonna use a full one.
+                continue;
+            }
+            statusFunction.accept(i);
+            do_test(nice_close, use_km_wrapper, use_full_km_wrapper, use_manual_watch, reload_peers, break_cross_refs, nio_peer_handler, use_ignoring_routing_handler, use_chan_manager_constructor, use_invoice_payer);
         }
+        gc_thread.interrupt();
+        gc_thread.join();
+    }
+    @Test
+    public void test_message_handler() throws Exception {
+        do_test_message_handler(i -> System.err.println("Running test with flags " + i));
     }
 
     // This is used in the test jar to test the built jar is runnable
     public static void main(String[] args) {
         try {
-            new HumanObjectPeerTest().test_message_handler();
+            do_test_message_handler(i -> System.err.println("Running test with flags " + i));
         } catch (Exception e) {
             System.err.println("Caught exception:");
             System.err.println(e);