Update batteries to new Tuple-mapping API
[ldk-java] / src / main / java / org / ldk / batteries / ChannelManagerConstructor.java
index d55854084053b629f50e2de311fe8a9c04b1c37b..8cf57536a5a0d58d94ab46dec31bca3f1755c865 100644 (file)
@@ -1,6 +1,6 @@
 package org.ldk.batteries;
 
-import org.jetbrains.annotations.Nullable;
+import javax.annotation.Nullable;
 import org.ldk.enums.Network;
 import org.ldk.structs.*;
 import org.ldk.util.TwoTuple;
@@ -36,7 +36,7 @@ public class ChannelManagerConstructor {
      * After doing so (and syncing the blockchain on the channel manager as well), you should call chain_sync_completed()
      * and then continue to normal application operation.
      */
-    public final TwoTuple<ChannelMonitor, byte[]>[] channel_monitors;
+    public final TwoTuple_BlockHashChannelMonitorZ[] channel_monitors;
     /**
      * A PeerManager which is constructed to pass messages and handle connections to peers.
      */
@@ -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,34 +63,43 @@ 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];
+        this.channel_monitors = new TwoTuple_BlockHashChannelMonitorZ[monitors.length];
         for (int i = 0; i < monitors.length; i++) {
-            Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res = UtilMethods.BlockHashChannelMonitorZ_read(channel_monitors_serialized[i], keys_interface);
+            Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res = UtilMethods.C2Tuple_BlockHashChannelMonitorZ_read(channel_monitors_serialized[i], keys_interface);
             if (res instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_Err) {
                 throw new InvalidSerializedDataException();
             }
-            monitors[i] = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) res).res.b;
-            this.channel_monitors[i] = new TwoTuple<>(monitors[i], ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK)res).res.a);
+            byte[] block_hash = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK)res).res.get_a();
+            monitors[i] = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) res).res.get_b();
+            this.channel_monitors[i] = TwoTuple_BlockHashChannelMonitorZ.of(block_hash, monitors[i]);
         }
         Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ res =
-                UtilMethods.BlockHashChannelManagerZ_read(channel_manager_serialized, keys_interface, fee_estimator, chain_monitor.as_Watch(), tx_broadcaster,
+                UtilMethods.C2Tuple_BlockHashChannelManagerZ_read(channel_manager_serialized, keys_interface, fee_estimator, chain_monitor.as_Watch(), tx_broadcaster,
                         logger, UserConfig.with_default(), monitors);
         if (res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_Err) {
             throw new InvalidSerializedDataException();
         }
-        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.channel_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.get_b();
+        this.channel_manager_latest_block_hash = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.get_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 { nio_peer_handler = new NioPeerHandler(this.peer_manager); } catch (IOException e) { assert false; }
+        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) {
@@ -104,21 +115,29 @@ public class ChannelManagerConstructor {
                                      KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor,
                                      @Nullable NetGraphMsgHandler router,
                                      BroadcasterInterface tx_broadcaster, Logger logger) {
-        channel_monitors = new TwoTuple[0];
+        final IgnoringMessageHandler no_custom_messages = IgnoringMessageHandler.of();
+        channel_monitors = new TwoTuple_BlockHashChannelMonitorZ[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 { nio_peer_handler = new NioPeerHandler(this.peer_manager); } catch (IOException e) { assert false; }
+        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;
     }
 
@@ -126,7 +145,7 @@ public class ChannelManagerConstructor {
      * Abstract interface which should handle Events and persist the ChannelManager. When you call chain_sync_completed
      * a background thread is started which will automatically call these methods for you when events occur.
      */
-    public interface ChannelManagerPersister {
+    public interface EventHandler {
         void handle_event(Event events);
         void persist_manager(byte[] channel_manager_bytes);
     }
@@ -138,18 +157,18 @@ public class ChannelManagerConstructor {
      * ChannelManager are processed as normal.
      *
      * This also spawns a background thread which will call the appropriate methods on the provided
-     * ChannelManagerPersister as required.
+     * EventHandler as required.
      */
-    public void chain_sync_completed(ChannelManagerPersister persister) {
+    public void chain_sync_completed(EventHandler event_handler) {
         if (background_processor != null) { return; }
-        for (TwoTuple<ChannelMonitor, byte[]> monitor: channel_monitors) {
-            this.chain_monitor.as_Watch().watch_channel(monitor.a.get_funding_txo().a, monitor.a);
+        for (TwoTuple_BlockHashChannelMonitorZ monitor: channel_monitors) {
+            this.chain_monitor.as_Watch().watch_channel(monitor.get_b().get_funding_txo().get_a(), monitor.get_b());
         }
         background_processor = BackgroundProcessor.start(org.ldk.structs.ChannelManagerPersister.new_impl(channel_manager -> {
-            persister.persist_manager(channel_manager.write());
+            event_handler.persist_manager(channel_manager.write());
             return Result_NoneErrorZ.ok();
-        }), EventHandler.new_impl(persister::handle_event),
-        this.chain_monitor, this.channel_manager, this.peer_manager, this.logger);
+        }), org.ldk.structs.EventHandler.new_impl(event_handler::handle_event),
+        this.chain_monitor, this.channel_manager, this.router, this.peer_manager, this.logger);
     }
 
     /**