]> git.bitcoin.ninja Git - ldk-java/blobdiff - src/main/java/org/ldk/batteries/ChannelManagerConstructor.java
Only set up `NioPeerHandler` after chain sync completes
[ldk-java] / src / main / java / org / ldk / batteries / ChannelManagerConstructor.java
index 109acbe39f505afa9cf1c272a9ef08b4e039511b..246c21a49adfc4629b547610a682e0757e4b9701 100644 (file)
@@ -52,8 +52,10 @@ public class ChannelManagerConstructor {
     public final PeerManager peer_manager;
     /**
      * A NioPeerHandler which manages a background thread to handle socket events and pass them to the peer_manager.
+        *
+        * This is `null` until `chain_sync_completed` is called.
      */
-    public final NioPeerHandler nio_peer_handler;
+    public NioPeerHandler nio_peer_handler = null;
     /**
      * If a `NetworkGraph` is provided to the constructor *and* a `LockableScore` is provided to
         * `chain_sync_completed`, this will be non-null after `chain_sync_completed` returns.
@@ -88,7 +90,7 @@ public class ChannelManagerConstructor {
                                      KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor,
                                      @Nullable Filter filter, @Nullable byte[] net_graph_serialized,
                                      BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException {
-        final IgnoringMessageHandler no_custom_messages = IgnoringMessageHandler.of();
+        final IgnoringMessageHandler ignoring_handler = IgnoringMessageHandler.of();
         final ChannelMonitor[] monitors = new ChannelMonitor[channel_monitors_serialized.length];
         this.channel_monitors = new TwoTuple_BlockHashChannelMonitorZ[monitors.length];
         HashSet<OutPoint> monitor_funding_set = new HashSet();
@@ -130,21 +132,19 @@ public class ChannelManagerConstructor {
             this.graph_msg_handler = P2PGossipSync.of(net_graph, Option_AccessZ.none(), logger);
             this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
                     graph_msg_handler.as_RoutingMessageHandler(),
+                    ignoring_handler.as_OnionMessageHandler(),
                     ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res,
-                    random_data, logger, no_custom_messages.as_CustomMessageHandler());
+                    (int)(System.currentTimeMillis() / 1000),
+                    random_data, logger, ignoring_handler.as_CustomMessageHandler());
         } else {
             this.graph_msg_handler = null;
-            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), no_custom_messages.as_RoutingMessageHandler(),
+            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
+                    ignoring_handler.as_RoutingMessageHandler(),
+                    ignoring_handler.as_OnionMessageHandler(),
                     ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res,
-                    random_data, logger, no_custom_messages.as_CustomMessageHandler());
+                    (int)(System.currentTimeMillis() / 1000),
+                    random_data, logger, ignoring_handler.as_CustomMessageHandler());
         }
-        NioPeerHandler nio_peer_handler = null;
-        try {
-            nio_peer_handler = new NioPeerHandler(this.peer_manager);
-        } catch (IOException e) {
-            throw new IllegalStateException("We should never fail to construct nio objects unless we're on a platform that cannot run LDK.");
-        }
-        this.nio_peer_handler = nio_peer_handler;
         if (filter != null) {
             for (ChannelMonitor monitor : monitors) {
                 monitor.load_outputs_to_watch(filter);
@@ -160,7 +160,7 @@ public class ChannelManagerConstructor {
                                      KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor,
                                      @Nullable NetworkGraph net_graph,
                                      BroadcasterInterface tx_broadcaster, Logger logger) {
-        final IgnoringMessageHandler no_custom_messages = IgnoringMessageHandler.of();
+        final IgnoringMessageHandler ignoring_handler = IgnoringMessageHandler.of();
         channel_monitors = new TwoTuple_BlockHashChannelMonitorZ[0];
         channel_manager_latest_block_hash = null;
         this.chain_monitor = chain_monitor;
@@ -177,21 +177,19 @@ public class ChannelManagerConstructor {
             this.graph_msg_handler = P2PGossipSync.of(net_graph, Option_AccessZ.none(), logger);
             this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
                     graph_msg_handler.as_RoutingMessageHandler(),
+                    ignoring_handler.as_OnionMessageHandler(),
                     ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res,
-                    random_data, logger, no_custom_messages.as_CustomMessageHandler());
+                    (int)(System.currentTimeMillis() / 1000),
+                    random_data, logger, ignoring_handler.as_CustomMessageHandler());
         } else {
             this.graph_msg_handler = null;
-            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), no_custom_messages.as_RoutingMessageHandler(),
+            this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
+                    ignoring_handler.as_RoutingMessageHandler(),
+                    ignoring_handler.as_OnionMessageHandler(),
                     ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res,
-                    random_data, logger, no_custom_messages.as_CustomMessageHandler());
-        }
-        NioPeerHandler nio_peer_handler = null;
-        try {
-            nio_peer_handler = new NioPeerHandler(this.peer_manager);
-        } catch (IOException e) {
-            throw new IllegalStateException("We should never fail to construct nio objects unless we're on a platform that cannot run LDK.");
+                    (int)(System.currentTimeMillis() / 1000),
+                    random_data, logger, ignoring_handler.as_CustomMessageHandler());
         }
-        this.nio_peer_handler = nio_peer_handler;
         router_rand_bytes = keys_interface.get_secure_random_bytes();
     }
 
@@ -216,15 +214,20 @@ public class ChannelManagerConstructor {
      * EventHandler as required.
      */
     public void chain_sync_completed(EventHandler event_handler, @Nullable MultiThreadedLockableScore scorer) {
+        try {
+            this.nio_peer_handler = new NioPeerHandler(this.peer_manager);
+        } catch (IOException e) {
+            throw new IllegalStateException("We should never fail to construct nio objects unless we're on a platform that cannot run LDK.");
+        }
+
         if (background_processor != null) { return; }
         for (TwoTuple_BlockHashChannelMonitorZ monitor: channel_monitors) {
             this.chain_monitor.as_Watch().watch_channel(monitor.get_b().get_funding_txo().get_a(), monitor.get_b());
         }
         org.ldk.structs.EventHandler ldk_handler = org.ldk.structs.EventHandler.new_impl(event_handler::handle_event);
         if (this.net_graph != null && scorer != null) {
-            Router router = DefaultRouter.of(net_graph, logger, router_rand_bytes).as_Router();
-            this.payer = InvoicePayer.of(this.channel_manager.as_Payer(), router, scorer, this.logger, ldk_handler, Retry.attempts(3));
-assert this.payer != null;
+            Router router = DefaultRouter.of(net_graph, logger, router_rand_bytes, scorer.as_LockableScore()).as_Router();
+            this.payer = InvoicePayer.of(this.channel_manager.as_Payer(), router, this.logger, ldk_handler, Retry.attempts(3));
             ldk_handler = this.payer.as_EventHandler();
         }
 
@@ -234,6 +237,12 @@ assert this.payer != null;
         else
             gossip_sync = GossipSync.p2_p(this.graph_msg_handler);
 
+        Option_WriteableScoreZ writeable_score;
+        if (scorer != null)
+            writeable_score = Option_WriteableScoreZ.some(scorer.as_WriteableScore());
+        else
+            writeable_score = Option_WriteableScoreZ.none();
+
         background_processor = BackgroundProcessor.start(Persister.new_impl(new Persister.PersisterInterface() {
             @Override
             public Result_NoneErrorZ persist_manager(ChannelManager channel_manager) {
@@ -248,18 +257,19 @@ assert this.payer != null;
             }
 
             @Override
-            public Result_NoneErrorZ persist_scorer(MultiThreadedLockableScore scorer) {
+            public Result_NoneErrorZ persist_scorer(WriteableScore scorer) {
                 event_handler.persist_scorer(scorer.write());
                 return Result_NoneErrorZ.ok();
             }
-        }), ldk_handler, this.chain_monitor, this.channel_manager, gossip_sync, this.peer_manager, this.logger, scorer);
+        }), ldk_handler, this.chain_monitor, this.channel_manager, gossip_sync, this.peer_manager, this.logger, writeable_score);
     }
 
     /**
      * Interrupt the background thread, stopping the background handling of events.
      */
     public void interrupt() {
-        this.nio_peer_handler.interrupt();
+        if (this.nio_peer_handler != null)
+            this.nio_peer_handler.interrupt();
         this.background_processor.stop();
     }
 }