]> git.bitcoin.ninja Git - ldk-java/commitdiff
Update batteries and tests to latest upstream API
authorMatt Corallo <git@bluematt.me>
Fri, 24 Sep 2021 17:53:23 +0000 (17:53 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 24 Sep 2021 22:13:24 +0000 (22:13 +0000)
With the upstream v0.0.101, memory leaks during tests are now:
    411 allocations remained for 1156930 bytes.

src/main/java/org/ldk/batteries/ChannelManagerConstructor.java
src/test/java/org/ldk/HumanObjectPeerTest.java
src/test/java/org/ldk/ManualMsgHandlingPeerTest.java
src/test/java/org/ldk/PeerTest.java

index a2181261c32722c5ffbd51df1fd601529a9ca1aa..4f591c11956fa54a6839aae57f444a40f522b54d 100644 (file)
@@ -50,6 +50,8 @@ public class ChannelManagerConstructor {
 
     private final Logger logger;
 
+    public final @Nullable NetGraphMsgHandler router;
+
     /**
      * Deserializes a channel manager and a set of channel monitors from the given serialized copies and interface implementations
      *
@@ -61,6 +63,7 @@ public class ChannelManagerConstructor {
                                      KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor, @Nullable Filter filter,
                                      @Nullable NetGraphMsgHandler router,
                                      BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException {
+        final IgnoringMessageHandler no_custom_messages = IgnoringMessageHandler.of();
         final ChannelMonitor[] monitors = new ChannelMonitor[channel_monitors_serialized.length];
         this.channel_monitors = new TwoTuple[monitors.length];
         for (int i = 0; i < monitors.length; i++) {
@@ -80,12 +83,15 @@ public class ChannelManagerConstructor {
         this.channel_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.b;
         this.channel_manager_latest_block_hash = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.a;
         this.chain_monitor = chain_monitor;
+        this.router = router;
         this.logger = logger;
         byte[] random_data = keys_interface.get_secure_random_bytes();
         if (router != null) {
-            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
+            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(),
+                    keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
         } else {
-            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), (IgnoringMessageHandler.of()).as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
+            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), (IgnoringMessageHandler.of()).as_RoutingMessageHandler(),
+                    keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
         }
         NioPeerHandler nio_peer_handler = null;
         try {
@@ -108,18 +114,22 @@ public class ChannelManagerConstructor {
                                      KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor,
                                      @Nullable NetGraphMsgHandler router,
                                      BroadcasterInterface tx_broadcaster, Logger logger) {
+        final IgnoringMessageHandler no_custom_messages = IgnoringMessageHandler.of();
         channel_monitors = new TwoTuple[0];
         channel_manager_latest_block_hash = null;
         this.chain_monitor = chain_monitor;
+        this.router = router;
         BestBlock block = BestBlock.of(current_blockchain_tip_hash, current_blockchain_tip_height);
         ChainParameters params = ChainParameters.of(network, block);
         channel_manager = ChannelManager.of(fee_estimator, chain_monitor.as_Watch(), tx_broadcaster, logger, keys_interface, config, params);
         this.logger = logger;
         byte[] random_data = keys_interface.get_secure_random_bytes();
         if (router != null) {
-            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
+            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(),
+                    keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
         } else {
-            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), (IgnoringMessageHandler.of()).as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
+            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), (IgnoringMessageHandler.of()).as_RoutingMessageHandler(),
+                    keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
         }
         NioPeerHandler nio_peer_handler = null;
         try {
@@ -157,7 +167,7 @@ public class ChannelManagerConstructor {
             event_handler.persist_manager(channel_manager.write());
             return Result_NoneErrorZ.ok();
         }), org.ldk.structs.EventHandler.new_impl(event_handler::handle_event),
-        this.chain_monitor, this.channel_manager, this.peer_manager, this.logger);
+        this.chain_monitor, this.channel_manager, this.router, this.peer_manager, this.logger);
     }
 
     /**
index 1be48264d5742efaff53a8666092f273c29ee719..3d1c19b0fbbc5c235fac31f8bebc807cc4e99ea4 100644 (file)
@@ -5,6 +5,7 @@ import org.bitcoinj.script.Script;
 import org.junit.jupiter.api.Test;
 import org.ldk.batteries.ChannelManagerConstructor;
 import org.ldk.batteries.NioPeerHandler;
+import org.ldk.enums.AccessError;
 import org.ldk.enums.Currency;
 import org.ldk.enums.Network;
 import org.ldk.structs.*;
@@ -50,58 +51,43 @@ class HumanObjectPeerTestInstance {
                     Sign underlying_ck = underlying_if.get_channel_signer(inbound, channel_value_satoshis);
                     BaseSign underlying_base = underlying_ck.get_base_sign();
                     BaseSign.BaseSignInterface bsi = new BaseSign.BaseSignInterface() {
-                        @Override
-                        public byte[] get_per_commitment_point(long idx) {
+                        @Override public byte[] get_per_commitment_point(long idx) {
                             return underlying_base.get_per_commitment_point(idx);
                         }
-
-                        @Override
-                        public byte[] release_commitment_secret(long idx) {
+                        @Override public byte[] release_commitment_secret(long idx) {
                             return underlying_base.release_commitment_secret(idx);
                         }
-
-                        @Override
-                        public byte[] channel_keys_id() {
+                        @Override public Result_NoneNoneZ validate_holder_commitment(HolderCommitmentTransaction holder_tx) {
+                            return underlying_base.validate_holder_commitment(holder_tx);
+                        }
+                        @Override public byte[] channel_keys_id() {
                             return new byte[32];
                         }
-
-                        @Override
-                        public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_counterparty_commitment(CommitmentTransaction commitment_tx) {
+                        @Override public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_counterparty_commitment(CommitmentTransaction commitment_tx) {
                             return underlying_base.sign_counterparty_commitment(commitment_tx);
                         }
-
-                        @Override
-                        public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_holder_commitment_and_htlcs(HolderCommitmentTransaction holder_commitment_tx) {
+                        @Override public Result_NoneNoneZ validate_counterparty_revocation(long idx, byte[] secret) {
+                            return underlying_base.validate_counterparty_revocation(idx, secret);
+                        }
+                        @Override public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_holder_commitment_and_htlcs(HolderCommitmentTransaction holder_commitment_tx) {
                             return underlying_base.sign_holder_commitment_and_htlcs(holder_commitment_tx);
                         }
-
-                        @Override
-                        public Result_SignatureNoneZ sign_justice_revoked_output(byte[] justice_tx, long input, long amount, byte[] per_commitment_key) {
+                        @Override public Result_SignatureNoneZ sign_justice_revoked_output(byte[] justice_tx, long input, long amount, byte[] per_commitment_key) {
                             return underlying_base.sign_justice_revoked_output(justice_tx, input, amount, per_commitment_key);
                         }
-
-                        @Override
-                        public Result_SignatureNoneZ sign_justice_revoked_htlc(byte[] justice_tx, long input, long amount, byte[] per_commitment_key, HTLCOutputInCommitment htlc) {
+                        @Override public Result_SignatureNoneZ sign_justice_revoked_htlc(byte[] justice_tx, long input, long amount, byte[] per_commitment_key, HTLCOutputInCommitment htlc) {
                             return underlying_base.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc);
                         }
-
-                        @Override
-                        public Result_SignatureNoneZ sign_counterparty_htlc_transaction(byte[] htlc_tx, long input, long amount, byte[] per_commitment_point, HTLCOutputInCommitment htlc) {
+                        @Override public Result_SignatureNoneZ sign_counterparty_htlc_transaction(byte[] htlc_tx, long input, long amount, byte[] per_commitment_point, HTLCOutputInCommitment htlc) {
                             return underlying_base.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc);
                         }
-
-                        @Override
-                        public Result_SignatureNoneZ sign_closing_transaction(byte[] closing_tx) {
+                        @Override public Result_SignatureNoneZ sign_closing_transaction(ClosingTransaction closing_tx) {
                             return underlying_base.sign_closing_transaction(closing_tx);
                         }
-
-                        @Override
-                        public Result_SignatureNoneZ sign_channel_announcement(UnsignedChannelAnnouncement msg) {
+                        @Override public Result_SignatureNoneZ sign_channel_announcement(UnsignedChannelAnnouncement msg) {
                             return underlying_base.sign_channel_announcement(msg);
                         }
-
-                        @Override
-                        public void ready_channel(ChannelTransactionParameters params) {
+                        @Override public void ready_channel(ChannelTransactionParameters params) {
                             underlying_base.ready_channel(params);
                         }
                     };
@@ -185,7 +171,7 @@ class HumanObjectPeerTestInstance {
         final NetGraphMsgHandler router;
         final Watch chain_watch;
         final HashSet<String> filter_additions;
-        final Filter filter;
+        final Option_FilterZ filter;
         ChannelManager chan_manager;
         PeerManager peer_manager;
         final HashMap<String, ChannelMonitor> monitors; // Wow I forgot just how terrible Java is - we can't put a byte array here.
@@ -245,7 +231,7 @@ class HumanObjectPeerTestInstance {
 
             filter_additions = new HashSet<>();
             if (use_filter) {
-                this.filter = Filter.new_impl(new Filter.FilterInterface() {
+                this.filter = Option_FilterZ.some(Filter.new_impl(new Filter.FilterInterface() {
                     @Override public void register_tx(byte[] txid, byte[] script_pubkey) {
                         filter_additions.add(Arrays.toString(txid));
                     }
@@ -253,9 +239,9 @@ class HumanObjectPeerTestInstance {
                         filter_additions.add(Arrays.toString(output.get_outpoint().get_txid()) + ":" + output.get_outpoint().get_index());
                         return Option_C2Tuple_usizeTransactionZZ.none();
                     }
-                });
+                }));
             } else {
-                this.filter = null;
+                this.filter = Option_FilterZ.none();
             }
 
             if (use_manual_watch) {
@@ -278,7 +264,7 @@ class HumanObjectPeerTestInstance {
                 this.keys_interface = keys.as_KeysInterface();
                 this.explicit_keys_manager = keys;
             }
-            this.router = NetGraphMsgHandler.of(new byte[32], null, logger);
+            this.router = NetGraphMsgHandler.of(new byte[32], Option_AccessZ.none(), logger);
         }
         private void bind_nio() {
             if (!use_nio_peer_handler) return;
@@ -320,7 +306,7 @@ class HumanObjectPeerTestInstance {
                 ChainParameters params = ChainParameters.of(Network.LDKNetwork_Bitcoin, BestBlock.of(new byte[32], 0));
                 this.chan_manager = ChannelManager.of(this.fee_estimator, chain_watch, tx_broadcaster, logger, this.keys_interface, UserConfig.with_default(), params);
                 byte[] random_data = keys_interface.get_secure_random_bytes();
-                this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
+                this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger, IgnoringMessageHandler.of().as_CustomMessageHandler());
             }
 
             this.node_id = chan_manager.get_our_node_id();
@@ -335,8 +321,12 @@ class HumanObjectPeerTestInstance {
                 byte[][] monitors = {orig.monitors.values().stream().iterator().next().write()};
                 byte[] serialized = orig.chan_manager.write();
                 try {
+                    Filter filter_nullable = null;
+                    if (this.filter instanceof Option_FilterZ.Some) {
+                        filter_nullable = ((Option_FilterZ.Some) this.filter).some;
+                    }
                     this.constructor = new ChannelManagerConstructor(serialized, monitors, this.keys_interface,
-                            this.fee_estimator, this.chain_monitor, this.filter, this.router, this.tx_broadcaster, this.logger);
+                            this.fee_estimator, this.chain_monitor, filter_nullable, this.router, this.tx_broadcaster, this.logger);
                     constructor.chain_sync_completed(new ChannelManagerConstructor.EventHandler() {
                         @Override public void handle_event(Event event) {
                             synchronized (pending_manager_events) {
@@ -378,7 +368,7 @@ class HumanObjectPeerTestInstance {
                 this.chan_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.b;
                 this.chain_watch.watch_channel(monitors[0].get_funding_txo().a, monitors[0]);
                 byte[] random_data = keys_interface.get_secure_random_bytes();
-                this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
+                this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger, IgnoringMessageHandler.of().as_CustomMessageHandler());
                 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
@@ -700,13 +690,9 @@ class HumanObjectPeerTestInstance {
         InvoiceFeatures invoice_features = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.features();
         RouteHint[] route_hints = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.route_hints();
 
-        Route route;
-        try (LockedNetworkGraph netgraph = peer1.router.read_locked_graph()) {
-            NetworkGraph graph = netgraph.graph();
-            Result_RouteLightningErrorZ route_res = UtilMethods.get_route(peer1.chan_manager.get_our_node_id(), graph, peer2.node_id, invoice_features, peer1_chans, route_hints, 10000000, 42, peer1.logger);
-            assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
-            route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
-        }
+        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);
+        assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
+        Route route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
 
         Result_NonePaymentSendFailureZ payment_res = peer1.chan_manager.send_payment(route, payment_hash, payment_secret);
         assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_OK;
index 091186b89daa23b673fa618c448111fbda7e23e7..90320d4bc32e1b806a872683dc760703bd1443da 100644 (file)
@@ -120,9 +120,6 @@ public class ManualMsgHandlingPeerTest {
             }
             @Override public long handle_channel_update(long msg) {
                 return 0;
-            }
-            @Override public void handle_htlc_fail_channel_update(long update) {
-
             }
             @Override public long[] get_next_channel_announcements(long starting_point, byte batch_amount) {
                 return new long[0];
@@ -161,7 +158,9 @@ public class ManualMsgHandlingPeerTest {
         byte[] random_data = new byte[32];
         for (byte i = 0; i < 32; i++) { random_data[i] = i; our_node_secret[i] = (byte) (i ^ 0xff); }
 
-        long peer_manager = bindings.PeerManager_new(message_handler, our_node_secret, random_data, logger);
+        long ignoring_message_handler = bindings.IgnoringMessageHandler_new();
+        long peer_manager = bindings.PeerManager_new(message_handler, our_node_secret, random_data, logger,
+                bindings.IgnoringMessageHandler_as_CustomMessageHandler(ignoring_message_handler));
 
         // Test Level_max() since its the only place we create a java object from a Rust-returned enum.
         assert bindings.Level_max() == Level.LDKLevel_Trace;
index de96a8bfa0aa223626b15e436d7523cae3b04dbf..c58515bfd3ad4604434480b931aa09e999f81a48 100644 (file)
@@ -26,6 +26,7 @@ public class PeerTest {
         final long router;
         final long route_handler;
         final long message_handler;
+        final long custom_message_handler;
         final long peer_manager;
         HashMap<String, Long> monitors; // Wow I forgot just how terrible Java is - we can't put a byte array here.
         byte[] node_id;
@@ -98,13 +99,15 @@ public class PeerTest {
             this.chan_manager_events = bindings.ChannelManager_as_EventsProvider(chan_manager);
 
             this.chan_handler = bindings.ChannelManager_as_ChannelMessageHandler(chan_manager);
-            this.router = bindings.NetGraphMsgHandler_new(new byte[32], 0, logger);
+            this.router = bindings.NetGraphMsgHandler_new(bindings.NetworkGraph_new(new byte[32]), bindings.COption_AccessZ_none(), logger);
             this.route_handler = bindings.NetGraphMsgHandler_as_RoutingMessageHandler(router);
             this.message_handler = bindings.MessageHandler_new(chan_handler, route_handler);
+            this.custom_message_handler = bindings.IgnoringMessageHandler_new();
 
             byte[] random_data = new byte[32];
             for (byte i = 0; i < 32; i++) { random_data[i] = (byte) ((i ^ seed) ^ 0xf0); }
-            this.peer_manager = bindings.PeerManager_new(message_handler, bindings.KeysInterface_get_node_secret(keys_interface), random_data, logger);
+            this.peer_manager = bindings.PeerManager_new(message_handler, bindings.KeysInterface_get_node_secret(keys_interface), random_data,
+                    logger, bindings.IgnoringMessageHandler_as_CustomMessageHandler(this.custom_message_handler));
         }
 
         void connect_block(Block b, Transaction t, int height) {
@@ -276,12 +279,12 @@ public class PeerTest {
         long no_min_val = bindings.COption_u64Z_none();
         long inbound_payment = bindings.ChannelManager_create_inbound_payment(peer2.chan_manager, no_min_val, 7200, 42);
         bindings.COption_u64Z_free(no_min_val);
-        long netgraph = bindings.NetGraphMsgHandler_read_locked_graph(peer1.router);
-        long route = bindings.get_route(peer1.node_id, bindings.LockedNetworkGraph_graph(netgraph), peer2.node_id, 0L, peer1_chans,
+        long netgraph = bindings.NetGraphMsgHandler_get_network_graph(peer1.router);
+        long route = bindings.get_route(peer1.node_id, netgraph, peer2.node_id, 0L, peer1_chans,
                 new long[0], 1000, 42, peer1.logger);
         for (long chan : peer1_chans) bindings.ChannelDetails_free(chan);
         assert bindings.LDKCResult_RouteLightningErrorZ_result_ok(route);
-        bindings.LockedNetworkGraph_free(netgraph);
+        bindings.NetworkGraph_free(netgraph);
         long payment_res = bindings.ChannelManager_send_payment(peer1.chan_manager, bindings.LDKCResult_RouteLightningErrorZ_get_ok(route),
                 bindings.LDKC2Tuple_PaymentHashPaymentSecretZ_get_a(inbound_payment), bindings.LDKC2Tuple_PaymentHashPaymentSecretZ_get_b(inbound_payment));
         bindings.CResult_RouteLightningErrorZ_free(route);