[Java] Update tests + batteries to latest upstream API
[ldk-java] / src / main / java / org / ldk / batteries / ChannelManagerConstructor.java
index 3dd5f49f65b882e3ea3fab98cb3ef53697465125..c24d0bd35b08f36aca0d2e0a72877be3a87ccdc4 100644 (file)
@@ -2,10 +2,11 @@ package org.ldk.batteries;
 
 import javax.annotation.Nullable;
 import org.ldk.enums.Network;
+import org.ldk.enums.Recipient;
 import org.ldk.structs.*;
-import org.ldk.util.TwoTuple;
 
 import java.io.IOException;
+import java.util.HashSet;
 
 
 /**
@@ -19,7 +20,11 @@ public class ChannelManagerConstructor {
      * An Exception that indicates the serialized data is invalid and has been corrupted on disk. You should attempt to
      * restore from a backup if there is one which is known to be current. Otherwise, funds may have been lost.
      */
-    public static class InvalidSerializedDataException extends Exception {}
+    public static class InvalidSerializedDataException extends Exception {
+        InvalidSerializedDataException(String reason) {
+            super(reason);
+        }
+    }
 
     /**
      * The ChannelManager either deserialized or newly-constructed.
@@ -75,20 +80,23 @@ public class ChannelManagerConstructor {
         final IgnoringMessageHandler no_custom_messages = 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();
         for (int i = 0; i < monitors.length; i++) {
             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();
+                throw new InvalidSerializedDataException("Serialized ChannelMonitor was corrupt");
             }
             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]);
+            if (!monitor_funding_set.add(monitors[i].get_funding_txo().get_a()))
+                throw new InvalidSerializedDataException("Set of ChannelMonitors contained duplicates (ie the same funding_txo was set on multiple monitors)");
         }
         Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ res =
                 UtilMethods.C2Tuple_BlockHashChannelManagerZ_read(channel_manager_serialized, keys_interface, fee_estimator, chain_monitor.as_Watch(), tx_broadcaster,
                         logger, config, monitors);
         if (res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_Err) {
-            throw new InvalidSerializedDataException();
+            throw new InvalidSerializedDataException("Serialized ChannelManager was corrupt");
         }
         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();
@@ -96,16 +104,20 @@ public class ChannelManagerConstructor {
         this.logger = logger;
         byte[] random_data = keys_interface.get_secure_random_bytes();
         this.net_graph = net_graph;
+        Result_SecretKeyNoneZ node_secret = keys_interface.get_node_secret(Recipient.LDKRecipient_Node);
+        assert node_secret.is_ok();
         if (net_graph != null) {
             //TODO: We really need to expose the Access here to let users prevent DoS issues
             this.graph_msg_handler = NetGraphMsgHandler.of(net_graph, Option_AccessZ.none(), logger);
             this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
                     graph_msg_handler.as_RoutingMessageHandler(),
-                    keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
+                    ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res,
+                    random_data, logger, no_custom_messages.as_CustomMessageHandler());
         } else {
             this.graph_msg_handler = null;
             this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), no_custom_messages.as_RoutingMessageHandler(),
-                    keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
+                    ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res,
+                    random_data, logger, no_custom_messages.as_CustomMessageHandler());
         }
         NioPeerHandler nio_peer_handler = null;
         try {
@@ -138,16 +150,20 @@ public class ChannelManagerConstructor {
         this.logger = logger;
         byte[] random_data = keys_interface.get_secure_random_bytes();
         this.net_graph = net_graph;
+        Result_SecretKeyNoneZ node_secret = keys_interface.get_node_secret(Recipient.LDKRecipient_Node);
+        assert node_secret.is_ok();
         if (net_graph != null) {
             //TODO: We really need to expose the Access here to let users prevent DoS issues
             this.graph_msg_handler = NetGraphMsgHandler.of(net_graph, Option_AccessZ.none(), logger);
             this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
                     graph_msg_handler.as_RoutingMessageHandler(),
-                    keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
+                    ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res,
+                    random_data, logger, no_custom_messages.as_CustomMessageHandler());
         } else {
             this.graph_msg_handler = null;
             this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), no_custom_messages.as_RoutingMessageHandler(),
-                    keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
+                    ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res,
+                    random_data, logger, no_custom_messages.as_CustomMessageHandler());
         }
         NioPeerHandler nio_peer_handler = null;
         try {
@@ -176,7 +192,7 @@ public class ChannelManagerConstructor {
      * This also spawns a background thread which will call the appropriate methods on the provided
      * EventHandler as required.
      */
-    public void chain_sync_completed(EventHandler event_handler, @Nullable LockableScore scorer) {
+    public void chain_sync_completed(EventHandler event_handler, @Nullable MultiThreadedLockableScore scorer) {
         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());
@@ -199,7 +215,7 @@ assert this.payer != null;
      * Interrupt the background thread, stopping the background handling of events.
      */
     public void interrupt() {
-        this.background_processor.stop();
         this.nio_peer_handler.interrupt();
+        this.background_processor.stop();
     }
 }