Use the new ChannelMonitor util functions and test them
authorMatt Corallo <git@bluematt.me>
Mon, 8 Mar 2021 05:10:59 +0000 (00:10 -0500)
committerMatt Corallo <git@bluematt.me>
Mon, 8 Mar 2021 05:11:34 +0000 (00:11 -0500)
pom.xml
src/main/java/org/ldk/batteries/ChannelManagerConstructor.java
src/test/java/org/ldk/HumanObjectPeerTest.java

diff --git a/pom.xml b/pom.xml
index ce7857d862fcc4be9c3af1a61de6d6a93f125a6e..35bbba051ddb0ac49950b35e98a0c823f6286c48 100644 (file)
--- a/pom.xml
+++ b/pom.xml
             <version>RELEASE</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.jetbrains</groupId>
+            <artifactId>annotations</artifactId>
+            <version>13.0</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.jetbrains</groupId>
+            <artifactId>annotations</artifactId>
+            <version>13.0</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
     <properties>
         <maven.compiler.source>1.8</maven.compiler.source>
index f8e3484b7e0b7b74779833f2a1e232a79dd13adc..4310af1cdc314f7c038def6befabca92d5b5a5a5 100644 (file)
@@ -1,5 +1,6 @@
 package org.ldk.batteries;
 
+import org.jetbrains.annotations.Nullable;
 import org.ldk.enums.LDKNetwork;
 import org.ldk.structs.*;
 import org.ldk.util.TwoTuple;
@@ -37,9 +38,11 @@ public class ChannelManagerConstructor {
 
     /**
      * Deserializes a channel manager and a set of channel monitors from the given serialized copies and interface implementations
+     *
+     * @param filter If provided, the outputs which were previously registered to be monitored for will be loaded into the filter.
      */
     public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] channel_monitors_serialized,
-                                     KeysInterface keys_interface, FeeEstimator fee_estimator, Watch chain_watch,
+                                     KeysInterface keys_interface, FeeEstimator fee_estimator, Watch chain_watch, @Nullable Filter filter,
                                      BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException {
         final ChannelMonitor[] monitors = new ChannelMonitor[channel_monitors_serialized.length];
         this.channel_monitors = new TwoTuple[monitors.length];
@@ -60,6 +63,11 @@ 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_watch = chain_watch;
+        if (filter != null) {
+            for (ChannelMonitor monitor : monitors) {
+                monitor.load_outputs_to_watch(filter);
+            }
+        }
     }
 
     /**
index a2d364be6b71f94fbd8a0002de10596084a10ae5..2cc67a0013afe9b16e9f08377d6b77c458a8242f 100644 (file)
@@ -14,10 +14,7 @@ import org.ldk.util.TwoTuple;
 import java.io.IOException;
 import java.lang.ref.WeakReference;
 import java.net.InetSocketAddress;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.LinkedList;
+import java.util.*;
 
 class HumanObjectPeerTestInstance {
     private final boolean nice_close;
@@ -26,14 +23,16 @@ class HumanObjectPeerTestInstance {
     private final boolean reload_peers;
     private final boolean break_cross_peer_refs;
     private final boolean use_nio_peer_handler;
+    private final boolean use_filter;
 
-    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) {
+    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) {
         this.nice_close = nice_close;
         this.use_km_wrapper = use_km_wrapper;
         this.use_manual_watch = use_manual_watch;
         this.reload_peers = reload_peers;
         this.break_cross_peer_refs = break_cross_peer_refs;
         this.use_nio_peer_handler = use_nio_peer_handler;
+        this.use_filter = use_filter;
     }
 
     class Peer {
@@ -167,6 +166,8 @@ class HumanObjectPeerTestInstance {
         final ChainMonitor chain_monitor;
         final NetGraphMsgHandler router;
         final Watch chain_watch;
+        final HashSet<String> filter_additions;
+        final Filter filter;
         ChannelManager chan_manager;
         EventsProvider chan_manager_events;
         PeerManager peer_manager;
@@ -218,11 +219,26 @@ class HumanObjectPeerTestInstance {
                     return new Result_NoneChannelMonitorUpdateErrZ.Result_NoneChannelMonitorUpdateErrZ_OK();
                 }
             });
+
+            filter_additions = new HashSet<>();
+            if (use_filter) {
+                this.filter = Filter.new_impl(new Filter.FilterInterface() {
+                    @Override public void register_tx(byte[] txid, byte[] script_pubkey) {
+                        filter_additions.add(Arrays.toString(txid));
+                    }
+                    @Override public void register_output(OutPoint outpoint, byte[] script_pubkey) {
+                        filter_additions.add(Arrays.toString(outpoint.get_txid()) + ":" + outpoint.get_index());
+                    }
+                });
+            } else {
+                this.filter = null;
+            }
+
             if (use_manual_watch) {
                 chain_watch = get_manual_watch();
                 chain_monitor = null;
             } else {
-                chain_monitor = ChainMonitor.constructor_new(null, tx_broadcaster, logger, fee_estimator, persister);
+                chain_monitor = ChainMonitor.constructor_new(filter, tx_broadcaster, logger, fee_estimator, persister);
                 chain_watch = chain_monitor.as_Watch();
             }
 
@@ -286,9 +302,14 @@ class HumanObjectPeerTestInstance {
                 }
                 byte[] serialized = orig.chan_manager.write();
                 try {
-                    ChannelManagerConstructor constructed = new ChannelManagerConstructor(serialized, channel_monitors.toArray(new byte[1][]), this.keys_interface, this.fee_estimator, this.chain_watch, this.tx_broadcaster, this.logger);
+                    ChannelManagerConstructor constructed = new ChannelManagerConstructor(serialized, channel_monitors.toArray(new byte[1][]), this.keys_interface, this.fee_estimator, this.chain_watch, this.filter, this.tx_broadcaster, this.logger);
                     this.chan_manager = constructed.channel_manager;
                     constructed.chain_sync_completed();
+                    if (use_filter && !use_manual_watch) {
+                        // With a manual watch we don't actually use the filter object at all.
+                        assert this.filter_additions.containsAll(orig.filter_additions) &&
+                                orig.filter_additions.containsAll(this.filter_additions);
+                    }
                 } catch (ChannelManagerConstructor.InvalidSerializedDataException e) {
                     assert false;
                 }
@@ -673,7 +694,7 @@ 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) throws InterruptedException {
-        HumanObjectPeerTestInstance instance = new HumanObjectPeerTestInstance(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler);
+        HumanObjectPeerTestInstance instance = new HumanObjectPeerTestInstance(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler, !nio_peer_handler);
         HumanObjectPeerTestInstance.TestState state = instance.do_test_message_handler();
         instance.do_test_message_handler_b(state);
         return instance;