From: Matt Corallo Date: Mon, 8 Mar 2021 05:10:59 +0000 (-0500) Subject: Use the new ChannelMonitor util functions and test them X-Git-Tag: v0.0.98~12^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=d69a40d071871d3c0e4160744202c6e85941a4cc;p=ldk-java Use the new ChannelMonitor util functions and test them --- diff --git a/pom.xml b/pom.xml index ce7857d8..35bbba05 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,18 @@ RELEASE test + + org.jetbrains + annotations + 13.0 + compile + + + org.jetbrains + annotations + 13.0 + compile + 1.8 diff --git a/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java b/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java index f8e3484b..4310af1c 100644 --- a/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java +++ b/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java @@ -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); + } + } } /** diff --git a/src/test/java/org/ldk/HumanObjectPeerTest.java b/src/test/java/org/ldk/HumanObjectPeerTest.java index a2d364be..2cc67a00 100644 --- a/src/test/java/org/ldk/HumanObjectPeerTest.java +++ b/src/test/java/org/ldk/HumanObjectPeerTest.java @@ -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 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;