Skip to content

Commit d69a40d

Browse files
committed
Use the new ChannelMonitor util functions and test them
1 parent 53b03e8 commit d69a40d

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626
<version>RELEASE</version>
2727
<scope>test</scope>
2828
</dependency>
29+
<dependency>
30+
<groupId>org.jetbrains</groupId>
31+
<artifactId>annotations</artifactId>
32+
<version>13.0</version>
33+
<scope>compile</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.jetbrains</groupId>
37+
<artifactId>annotations</artifactId>
38+
<version>13.0</version>
39+
<scope>compile</scope>
40+
</dependency>
2941
</dependencies>
3042
<properties>
3143
<maven.compiler.source>1.8</maven.compiler.source>

src/main/java/org/ldk/batteries/ChannelManagerConstructor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.ldk.batteries;
22

3+
import org.jetbrains.annotations.Nullable;
34
import org.ldk.enums.LDKNetwork;
45
import org.ldk.structs.*;
56
import org.ldk.util.TwoTuple;
@@ -37,9 +38,11 @@ public static class InvalidSerializedDataException extends Exception {}
3738

3839
/**
3940
* Deserializes a channel manager and a set of channel monitors from the given serialized copies and interface implementations
41+
*
42+
* @param filter If provided, the outputs which were previously registered to be monitored for will be loaded into the filter.
4043
*/
4144
public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] channel_monitors_serialized,
42-
KeysInterface keys_interface, FeeEstimator fee_estimator, Watch chain_watch,
45+
KeysInterface keys_interface, FeeEstimator fee_estimator, Watch chain_watch, @Nullable Filter filter,
4346
BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException {
4447
final ChannelMonitor[] monitors = new ChannelMonitor[channel_monitors_serialized.length];
4548
this.channel_monitors = new TwoTuple[monitors.length];
@@ -60,6 +63,11 @@ public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] cha
6063
this.channel_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.b;
6164
this.channel_manager_latest_block_hash = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.a;
6265
this.chain_watch = chain_watch;
66+
if (filter != null) {
67+
for (ChannelMonitor monitor : monitors) {
68+
monitor.load_outputs_to_watch(filter);
69+
}
70+
}
6371
}
6472

6573
/**

src/test/java/org/ldk/HumanObjectPeerTest.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
import java.io.IOException;
1515
import java.lang.ref.WeakReference;
1616
import java.net.InetSocketAddress;
17-
import java.util.ArrayList;
18-
import java.util.Arrays;
19-
import java.util.HashMap;
20-
import java.util.LinkedList;
17+
import java.util.*;
2118

2219
class HumanObjectPeerTestInstance {
2320
private final boolean nice_close;
@@ -26,14 +23,16 @@ class HumanObjectPeerTestInstance {
2623
private final boolean reload_peers;
2724
private final boolean break_cross_peer_refs;
2825
private final boolean use_nio_peer_handler;
26+
private final boolean use_filter;
2927

30-
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) {
28+
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) {
3129
this.nice_close = nice_close;
3230
this.use_km_wrapper = use_km_wrapper;
3331
this.use_manual_watch = use_manual_watch;
3432
this.reload_peers = reload_peers;
3533
this.break_cross_peer_refs = break_cross_peer_refs;
3634
this.use_nio_peer_handler = use_nio_peer_handler;
35+
this.use_filter = use_filter;
3736
}
3837

3938
class Peer {
@@ -167,6 +166,8 @@ public MonitorEvent[] release_pending_monitor_events() {
167166
final ChainMonitor chain_monitor;
168167
final NetGraphMsgHandler router;
169168
final Watch chain_watch;
169+
final HashSet<String> filter_additions;
170+
final Filter filter;
170171
ChannelManager chan_manager;
171172
EventsProvider chan_manager_events;
172173
PeerManager peer_manager;
@@ -218,11 +219,26 @@ public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id,
218219
return new Result_NoneChannelMonitorUpdateErrZ.Result_NoneChannelMonitorUpdateErrZ_OK();
219220
}
220221
});
222+
223+
filter_additions = new HashSet<>();
224+
if (use_filter) {
225+
this.filter = Filter.new_impl(new Filter.FilterInterface() {
226+
@Override public void register_tx(byte[] txid, byte[] script_pubkey) {
227+
filter_additions.add(Arrays.toString(txid));
228+
}
229+
@Override public void register_output(OutPoint outpoint, byte[] script_pubkey) {
230+
filter_additions.add(Arrays.toString(outpoint.get_txid()) + ":" + outpoint.get_index());
231+
}
232+
});
233+
} else {
234+
this.filter = null;
235+
}
236+
221237
if (use_manual_watch) {
222238
chain_watch = get_manual_watch();
223239
chain_monitor = null;
224240
} else {
225-
chain_monitor = ChainMonitor.constructor_new(null, tx_broadcaster, logger, fee_estimator, persister);
241+
chain_monitor = ChainMonitor.constructor_new(filter, tx_broadcaster, logger, fee_estimator, persister);
226242
chain_watch = chain_monitor.as_Watch();
227243
}
228244

@@ -286,9 +302,14 @@ private void bind_nio() {
286302
}
287303
byte[] serialized = orig.chan_manager.write();
288304
try {
289-
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);
305+
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);
290306
this.chan_manager = constructed.channel_manager;
291307
constructed.chain_sync_completed();
308+
if (use_filter && !use_manual_watch) {
309+
// With a manual watch we don't actually use the filter object at all.
310+
assert this.filter_additions.containsAll(orig.filter_additions) &&
311+
orig.filter_additions.containsAll(this.filter_additions);
312+
}
292313
} catch (ChannelManagerConstructor.InvalidSerializedDataException e) {
293314
assert false;
294315
}
@@ -673,7 +694,7 @@ protected void finalize() throws Throwable {
673694
}
674695
public class HumanObjectPeerTest {
675696
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 {
676-
HumanObjectPeerTestInstance instance = new HumanObjectPeerTestInstance(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler);
697+
HumanObjectPeerTestInstance instance = new HumanObjectPeerTestInstance(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler, !nio_peer_handler);
677698
HumanObjectPeerTestInstance.TestState state = instance.do_test_message_handler();
678699
instance.do_test_message_handler_b(state);
679700
return instance;

0 commit comments

Comments
 (0)