Expand and update ChannelManagerConstructor+tests for BP and Event API
[ldk-java] / src / test / java / org / ldk / HumanObjectPeerTest.java
1 package org.ldk;
2
3 import org.bitcoinj.core.*;
4 import org.bitcoinj.script.Script;
5 import org.junit.jupiter.api.Test;
6 import org.ldk.batteries.ChannelManagerConstructor;
7 import org.ldk.batteries.NioPeerHandler;
8 import org.ldk.enums.LDKCurrency;
9 import org.ldk.enums.LDKNetwork;
10 import org.ldk.impl.bindings;
11 import org.ldk.structs.*;
12 import org.ldk.util.TwoTuple;
13
14 import java.io.IOException;
15 import java.lang.ref.WeakReference;
16 import java.net.InetSocketAddress;
17 import java.util.*;
18
19 class HumanObjectPeerTestInstance {
20     private final boolean nice_close;
21     private final boolean use_km_wrapper;
22     private final boolean use_manual_watch;
23     private final boolean reload_peers;
24     private final boolean break_cross_peer_refs;
25     private final boolean use_nio_peer_handler;
26     private final boolean use_filter;
27     private final boolean use_chan_manager_constructor;
28
29     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, boolean use_chan_manager_constructor) {
30         this.nice_close = nice_close;
31         this.use_km_wrapper = use_km_wrapper;
32         this.use_manual_watch = use_manual_watch;
33         this.reload_peers = reload_peers;
34         this.break_cross_peer_refs = break_cross_peer_refs;
35         this.use_nio_peer_handler = use_nio_peer_handler;
36         this.use_filter = use_filter;
37         this.use_chan_manager_constructor = use_chan_manager_constructor;
38     }
39
40     class Peer {
41         KeysInterface manual_keysif(KeysInterface underlying_if) {
42             return KeysInterface.new_impl(new KeysInterface.KeysInterfaceInterface() {
43                 @Override public byte[] get_node_secret() { return underlying_if.get_node_secret(); }
44                 @Override public byte[] get_destination_script() { return underlying_if.get_destination_script(); }
45                 @Override public byte[] get_shutdown_pubkey() { return underlying_if.get_shutdown_pubkey(); }
46
47                 @Override
48                 public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) {
49                     Sign underlying_ck = underlying_if.get_channel_signer(inbound, channel_value_satoshis);
50                     // TODO: Expose the underlying signer from a Sign
51                     /*BaseSign.BaseSignInterface si = new BaseSign.BaseSignInterface() {
52                         @Override
53                         public byte[] get_per_commitment_point(long idx) {
54                             return underlying_ck.get_per_commitment_point(idx);
55                         }
56
57                         @Override
58                         public byte[] release_commitment_secret(long idx) {
59                             return underlying_ck.release_commitment_secret(idx);
60                         }
61
62                         @Override
63                         public byte[] channel_keys_id() {
64                             return new byte[32];
65                         }
66
67                         @Override
68                         public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_counterparty_commitment(CommitmentTransaction commitment_tx) {
69                             return underlying_ck.sign_counterparty_commitment(commitment_tx);
70                         }
71
72                         @Override
73                         public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_holder_commitment_and_htlcs(HolderCommitmentTransaction holder_commitment_tx) {
74                             return underlying_ck.sign_holder_commitment_and_htlcs(holder_commitment_tx);
75                         }
76
77                         @Override
78                         public Result_SignatureNoneZ sign_justice_transaction(byte[] justice_tx, long input, long amount, byte[] per_commitment_key, HTLCOutputInCommitment htlc) {
79                             return underlying_ck.sign_justice_transaction(justice_tx, input, amount, per_commitment_key, htlc);
80                         }
81
82                         @Override
83                         public Result_SignatureNoneZ sign_counterparty_htlc_transaction(byte[] htlc_tx, long input, long amount, byte[] per_commitment_point, HTLCOutputInCommitment htlc) {
84                             return underlying_ck.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc);
85                         }
86
87                         @Override
88                         public Result_SignatureNoneZ sign_closing_transaction(byte[] closing_tx) {
89                             return underlying_ck.sign_closing_transaction(closing_tx);
90                         }
91
92                         @Override
93                         public Result_SignatureNoneZ sign_channel_announcement(UnsignedChannelAnnouncement msg) {
94                             return underlying_ck.sign_channel_announcement(msg);
95                         }
96
97                         @Override
98                         public void ready_channel(ChannelTransactionParameters params) {
99                             underlying_ck.ready_channel(params);
100                         }
101
102                         @Override
103                         public byte[] write() {
104                             return underlying_ck.write();
105                         }
106                     };*/
107                     //Sign resp = Sign.new_impl(si, underlying_ck.get_pubkeys());
108                     //must_free_objs.add(new WeakReference<>(si));
109                     //must_free_objs.add(new WeakReference<>(resp));
110                     must_free_objs.add(new WeakReference<>(underlying_ck));
111                     //return resp;
112                     return underlying_ck;
113                 }
114
115                 @Override
116                 public byte[] get_secure_random_bytes() {
117                     return underlying_if.get_secure_random_bytes();
118                 }
119
120                 @Override
121                 public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) {
122                     return underlying_if.read_chan_signer(reader);
123                 }
124
125                 @Override
126                 public Result_RecoverableSignatureNoneZ sign_invoice(byte[] invoice_preimage) {
127                     return underlying_if.sign_invoice(invoice_preimage);
128                 }
129             });
130         }
131
132         Watch get_manual_watch() {
133             Watch.WatchInterface watch_impl = new Watch.WatchInterface() {
134                 public Result_NoneChannelMonitorUpdateErrZ watch_channel(OutPoint funding_txo, ChannelMonitor monitor) {
135                     synchronized (monitors) {
136                         assert monitors.put(Arrays.toString(funding_txo.get_txid()), monitor) == null;
137                     }
138                     return Result_NoneChannelMonitorUpdateErrZ.ok();
139                 }
140
141                 public Result_NoneChannelMonitorUpdateErrZ update_channel(OutPoint funding_txo, ChannelMonitorUpdate update) {
142                     synchronized (monitors) {
143                         String txid = Arrays.toString(funding_txo.get_txid());
144                         assert monitors.containsKey(txid);
145                         Result_NoneMonitorUpdateErrorZ update_res = monitors.get(txid).update_monitor(update, tx_broadcaster, fee_estimator, logger);
146                         assert update_res instanceof Result_NoneMonitorUpdateErrorZ.Result_NoneMonitorUpdateErrorZ_OK;
147                     }
148                     return Result_NoneChannelMonitorUpdateErrZ.ok();
149                 }
150
151                 @Override
152                 public MonitorEvent[] release_pending_monitor_events() {
153                     synchronized (monitors) {
154                         assert monitors.size() <= 1;
155                         for (ChannelMonitor mon : monitors.values()) {
156                             return mon.get_and_clear_pending_monitor_events();
157                         }
158                     }
159                     return new MonitorEvent[0];
160                 }
161             };
162             Watch watch = Watch.new_impl(watch_impl);
163             must_free_objs.add(new WeakReference<>(watch_impl));
164             must_free_objs.add(new WeakReference<>(watch));
165             return watch;
166         }
167
168         NioPeerHandler nio_peer_handler;
169         short nio_port;
170         final byte seed;
171         final Logger logger;
172         final FeeEstimator fee_estimator;
173         final BroadcasterInterface tx_broadcaster;
174         final KeysManager explicit_keys_manager;
175         final KeysInterface keys_interface;
176         final ChainMonitor chain_monitor;
177         final NetGraphMsgHandler router;
178         final Watch chain_watch;
179         final HashSet<String> filter_additions;
180         final Filter filter;
181         ChannelManager chan_manager;
182         PeerManager peer_manager;
183         final HashMap<String, ChannelMonitor> monitors; // Wow I forgot just how terrible Java is - we can't put a byte array here.
184         byte[] node_id;
185         final LinkedList<byte[]> broadcast_set = new LinkedList<>();
186         final LinkedList<Event> pending_manager_events = new LinkedList<>();
187         ChannelManagerConstructor constructor = null;
188         GcCheck obj = new GcCheck();
189
190         private TwoTuple<OutPoint, byte[]> test_mon_roundtrip(ChannelMonitor mon) {
191             // Because get_funding_txo() returns an OutPoint in a tuple that is a reference to an OutPoint inside the
192             // ChannelMonitor, its a good test to ensure that the OutPoint isn't freed (or is cloned) before the
193             // ChannelMonitor is. This used to be broken.
194             Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ roundtrip_monitor = UtilMethods.BlockHashChannelMonitorZ_read(mon.write(), keys_interface);
195             assert roundtrip_monitor instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
196             TwoTuple<OutPoint, byte[]> funding_txo = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) roundtrip_monitor).res.b.get_funding_txo();
197             System.gc(); System.runFinalization(); // Give the GC a chance to run.
198             return funding_txo;
199         }
200
201         private Peer(Object _dummy, byte seed) {
202             logger = Logger.new_impl((String arg) -> System.out.println(seed + ": " + arg));
203             fee_estimator = FeeEstimator.new_impl((confirmation_target -> 253));
204             tx_broadcaster = BroadcasterInterface.new_impl(tx -> {
205                 broadcast_set.add(tx);
206             });
207             monitors = new HashMap<>();
208             this.seed = seed;
209             Persist persister = Persist.new_impl(new Persist.PersistInterface() {
210                 @Override
211                 public Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint id, ChannelMonitor data) {
212                     synchronized (monitors) {
213                         String key = Arrays.toString(id.to_channel_id());
214                         assert monitors.put(key, data) == null;
215                         TwoTuple<OutPoint, byte[]> res = test_mon_roundtrip(data);
216                         assert Arrays.equals(res.a.get_txid(), id.get_txid());
217                         assert res.a.get_index() == id.get_index();
218                     }
219                     return Result_NoneChannelMonitorUpdateErrZ.ok();
220                 }
221
222                 @Override
223                 public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id, ChannelMonitorUpdate update, ChannelMonitor data) {
224                     synchronized (monitors) {
225                         String key = Arrays.toString(id.to_channel_id());
226                         assert monitors.put(key, data) != null;
227                         TwoTuple<OutPoint, byte[]> res = test_mon_roundtrip(data);
228                         assert Arrays.equals(res.a.get_txid(), id.get_txid());
229                         assert res.a.get_index() == id.get_index();
230                     }
231                     return Result_NoneChannelMonitorUpdateErrZ.ok();
232                 }
233             });
234
235             filter_additions = new HashSet<>();
236             if (use_filter) {
237                 this.filter = Filter.new_impl(new Filter.FilterInterface() {
238                     @Override public void register_tx(byte[] txid, byte[] script_pubkey) {
239                         filter_additions.add(Arrays.toString(txid));
240                     }
241                     @Override public Option_C2Tuple_usizeTransactionZZ register_output(WatchedOutput output) {
242                         filter_additions.add(Arrays.toString(output.get_outpoint().get_txid()) + ":" + output.get_outpoint().get_index());
243                         return Option_C2Tuple_usizeTransactionZZ.none();
244                     }
245                 });
246             } else {
247                 this.filter = null;
248             }
249
250             if (use_manual_watch) {
251                 chain_watch = get_manual_watch();
252                 chain_monitor = null;
253             } else {
254                 chain_monitor = ChainMonitor.of(filter, tx_broadcaster, logger, fee_estimator, persister);
255                 chain_watch = chain_monitor.as_Watch();
256             }
257
258             byte[] key_seed = new byte[32];
259             for (byte i = 0; i < 32; i++) {
260                 key_seed[i] = (byte) (i ^ seed);
261             }
262             KeysManager keys = KeysManager.of(key_seed, System.currentTimeMillis() / 1000, (int) (System.currentTimeMillis() * 1000));
263             if (use_km_wrapper) {
264                 this.keys_interface = manual_keysif(keys.as_KeysInterface());
265                 this.explicit_keys_manager = null;
266             } else {
267                 this.keys_interface = keys.as_KeysInterface();
268                 this.explicit_keys_manager = keys;
269             }
270             this.router = NetGraphMsgHandler.of(new byte[32], null, logger);
271         }
272         private void bind_nio() {
273             if (!use_nio_peer_handler) return;
274             if (use_chan_manager_constructor) {
275                 this.nio_peer_handler = this.constructor.nio_peer_handler;
276             } else {
277                 try { this.nio_peer_handler = new NioPeerHandler(peer_manager); } catch (IOException e) { assert false; }
278             }
279             for (short i = 10_000; true; i++) {
280                 try {
281                     nio_peer_handler.bind_listener(new InetSocketAddress("127.0.0.1", i));
282                     nio_port = i;
283                     break;
284                 } catch (IOException e) { assert i < 10_500; }
285             }
286         }
287         Peer(byte seed) {
288             this(null, seed);
289             if (use_chan_manager_constructor) {
290                 try {
291                     this.constructor = new ChannelManagerConstructor(LDKNetwork.LDKNetwork_Bitcoin, UserConfig.with_default(), new byte[32], 0,
292                             this.keys_interface, this.fee_estimator, this.chain_monitor, this.router, this.tx_broadcaster, this.logger);
293                     constructor.chain_sync_completed(new ChannelManagerConstructor.ChannelManagerPersister() {
294                         @Override public void handle_event(Event event) {
295                             synchronized (pending_manager_events) {
296                                 pending_manager_events.add(event);
297                                 pending_manager_events.notifyAll();
298                             }
299                         }
300                         @Override public void persist_manager(byte[] channel_manager_bytes) { }
301                     });
302                     this.chan_manager = constructor.channel_manager;
303                     this.peer_manager = constructor.peer_manager;
304                     must_free_objs.add(new WeakReference<>(this.chan_manager));
305                 } catch (ChannelManagerConstructor.InvalidSerializedDataException e) {
306                     assert false;
307                 }
308             } else {
309                 ChainParameters params = ChainParameters.of(LDKNetwork.LDKNetwork_Bitcoin, BestBlock.of(new byte[32], 0));
310                 this.chan_manager = ChannelManager.of(this.fee_estimator, chain_watch, tx_broadcaster, logger, this.keys_interface, UserConfig.with_default(), params);
311                 byte[] random_data = keys_interface.get_secure_random_bytes();
312                 this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
313             }
314
315             this.node_id = chan_manager.get_our_node_id();
316             bind_nio();
317             System.gc();
318         }
319
320         Object ptr_to;
321         Peer(Peer orig) {
322             this(null, orig.seed);
323             if (use_chan_manager_constructor) {
324                 byte[][] monitors = {orig.monitors.values().stream().iterator().next().write()};
325                 byte[] serialized = orig.chan_manager.write();
326                 try {
327                     this.constructor = new ChannelManagerConstructor(serialized, monitors, this.keys_interface,
328                             this.fee_estimator, this.chain_monitor, this.filter, this.router, this.tx_broadcaster, this.logger);
329                     constructor.chain_sync_completed(new ChannelManagerConstructor.ChannelManagerPersister() {
330                         @Override public void handle_event(Event event) {
331                             synchronized (pending_manager_events) {
332                                 pending_manager_events.add(event);
333                                 pending_manager_events.notifyAll();
334                             }
335                         }
336                         @Override public void persist_manager(byte[] channel_manager_bytes) { }
337                     });
338                     this.chan_manager = constructor.channel_manager;
339                     this.peer_manager = constructor.peer_manager;
340                     must_free_objs.add(new WeakReference<>(this.chan_manager));
341                     // If we are using a ChannelManagerConstructor, we may have pending events waiting on the old peer
342                     // which have been removed from the ChannelManager but which we still need to handle.
343                     this.pending_manager_events.addAll(orig.pending_manager_events);
344                     if (!this.pending_manager_events.isEmpty()) {
345                         // However, this implies cross_reload_ref_pollution
346                         cross_reload_ref_pollution = true;
347                     }
348                 } catch (ChannelManagerConstructor.InvalidSerializedDataException e) {
349                     assert false;
350                 }
351             } else {
352                 ChannelMonitor[] monitors = new ChannelMonitor[1];
353                 assert orig.monitors.size() == 1;
354                 if (!break_cross_peer_refs) {
355                     monitors[0] = orig.monitors.values().stream().iterator().next();
356                 } else {
357                     byte[] serialized = orig.monitors.values().stream().iterator().next().write();
358                     Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res =
359                             UtilMethods.BlockHashChannelMonitorZ_read(serialized, this.keys_interface);
360                     assert res instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
361                     monitors[0] = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) res).res.b;
362                 }
363                 byte[] serialized = orig.chan_manager.write();
364                 Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ read_res =
365                         UtilMethods.BlockHashChannelManagerZ_read(serialized, this.keys_interface, this.fee_estimator, this.chain_watch, this.tx_broadcaster, this.logger, UserConfig.with_default(), monitors);
366                 assert read_res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK;
367                 this.chan_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.b;
368                 this.chain_watch.watch_channel(monitors[0].get_funding_txo().a, monitors[0]);
369                 byte[] random_data = keys_interface.get_secure_random_bytes();
370                 this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
371                 if (!break_cross_peer_refs && (use_manual_watch || use_km_wrapper)) {
372                     // When we pass monitors[0] into chain_watch.watch_channel we create a reference from the new Peer to a
373                     // field in the old peer, preventing freeing of the original Peer until the new Peer is freed. Thus, we
374                     // shouldn't bother waiting for the original to be freed later on.
375                     cross_reload_ref_pollution = true;
376                 }
377             }
378             this.node_id = chan_manager.get_our_node_id();
379             bind_nio();
380
381             if (cross_reload_ref_pollution) {
382                 // This really, really needs to be handled at the bindings layer, but its rather complicated -
383                 // ChannelSigners can be cloned and passed around without java being involved, resulting in them being
384                 // owned by both one or more ChannelMonitors and a ChannelManager, with only one having proper pointers
385                 // to the ChannelSigner. Ideally, the ChannelSigner would have a global reference to the Java
386                 // implementation class, but that results in circular references. Instead, we need some ability to,
387                 // while cloning ChannelSigners, add new references in the calling Java struct (ie ChannelMonitor) to
388                 // the ChannelSigner.
389                 this.ptr_to = orig.chan_manager;
390             }
391         }
392
393         TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] connect_block(Block b, int height, long expected_monitor_update_len) {
394             byte[] header = Arrays.copyOfRange(b.bitcoinSerialize(), 0, 80);
395             TwoTuple<Long, byte[]>[] txn;
396             if (b.hasTransactions()) {
397                 assert b.getTransactions().size() == 1;
398                 TwoTuple<Long, byte[]> txp = new TwoTuple<>((long) 0, b.getTransactions().get(0).bitcoinSerialize());
399                 txn = new TwoTuple[]{txp};
400             } else
401                 txn = new TwoTuple[0];
402             if (chain_monitor != null) {
403                 chan_manager.as_Listen().block_connected(b.bitcoinSerialize(), height);
404                 chain_monitor.as_Listen().block_connected(b.bitcoinSerialize(), height);
405             } else {
406                 chan_manager.as_Confirm().transactions_confirmed(header, txn, height);
407                 chan_manager.as_Confirm().best_block_updated(header, height);
408                 // Connect manually if we aren't using a ChainMonitor and are implementing Watch ourselves
409                 synchronized (monitors) {
410                     assert monitors.size() == 1;
411                     for (ChannelMonitor mon : monitors.values()) {
412                         TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] ret = mon.block_connected(header, txn, height, tx_broadcaster, fee_estimator, logger);
413                         assert ret.length == expected_monitor_update_len;
414                         return ret;
415                     }
416                 }
417             }
418             return null;
419         }
420
421         Event[] get_monitor_events(int expected_len) {
422             if (use_chan_manager_constructor) {
423                 while (true) {
424                     synchronized (this.pending_manager_events) {
425                         if (expected_len != 0 && this.pending_manager_events.size() != expected_len) {
426                             break;
427                         }
428                     }
429                     try { Thread.sleep(500); } catch (InterruptedException e) { assert false; }
430                     break;
431                 }
432                 synchronized (this.pending_manager_events) {
433                     Event[] res = this.pending_manager_events.toArray(new Event[0]);
434                     this.pending_manager_events.clear();
435                     assert res.length == expected_len;
436                     return res;
437                 }
438             } else if (chain_monitor != null) {
439                 ArrayList<Event> l = new ArrayList<Event>();
440                 chain_monitor.as_EventsProvider().process_pending_events(EventHandler.new_impl(l::add));
441                 assert l.size() == expected_len;
442                 return l.toArray(new Event[0]);
443             } else {
444                 synchronized (monitors) {
445                     assert monitors.size() == 1;
446                     for (ChannelMonitor mon : monitors.values()) {
447                         Event[] res = mon.get_and_clear_pending_events();
448                         assert res.length == expected_len;
449                         return res;
450                     }
451                     return null;
452                 }
453             }
454         }
455
456         Event[] get_manager_events(int expected_len) {
457             Event[] res = new Event[0];
458             if (use_chan_manager_constructor) {
459                 while (res.length < expected_len) {
460                     synchronized (this.pending_manager_events) {
461                         res = this.pending_manager_events.toArray(res);
462                         assert res.length == expected_len || res.length == 0; // We don't handle partial results
463                         this.pending_manager_events.clear();
464                         if (res.length < expected_len) {
465                             try { this.pending_manager_events.wait(); } catch (InterruptedException e) { assert false; }
466                         }
467                     }
468                 }
469             } else {
470                 ArrayList<Event> l = new ArrayList<Event>();
471                 chan_manager.as_EventsProvider().process_pending_events(EventHandler.new_impl(l::add));
472                 return l.toArray(new Event[0]);
473             }
474             assert res.length == expected_len;
475             return res;
476         }
477
478         Route get_route(byte[] dest_node, ChannelDetails[] our_chans) {
479             try (LockedNetworkGraph netgraph = this.router.read_locked_graph()) {
480                 NetworkGraph graph = netgraph.graph();
481                 long res = bindings.get_route(this.node_id, graph._test_only_get_ptr(), dest_node, 0L, new long[]{our_chans[0]._test_only_get_ptr()},
482                         new long[0], 1000000, 42, this.logger._test_only_get_ptr());
483                 assert bindings.LDKCResult_RouteLightningErrorZ_result_ok(res);
484                 byte[] serialized_route = bindings.Route_write(bindings.LDKCResult_RouteLightningErrorZ_get_ok(res));
485                 must_free_objs.add(new WeakReference<>(serialized_route));
486                 Result_RouteDecodeErrorZ copy = Route.read(serialized_route);
487                 assert copy instanceof Result_RouteDecodeErrorZ.Result_RouteDecodeErrorZ_OK;
488                 bindings.CResult_RouteLightningErrorZ_free(res);
489                 return ((Result_RouteDecodeErrorZ.Result_RouteDecodeErrorZ_OK) copy).res;
490             }
491         }
492     }
493
494     static class DescriptorHolder { SocketDescriptor val; }
495
496     boolean running = false;
497     final LinkedList<Runnable> runqueue = new LinkedList();
498     boolean ran = false;
499     Thread t = new Thread(() -> {
500             while (true) {
501                 try {
502                     Runnable r;
503                     synchronized (runqueue) {
504                         while (runqueue.isEmpty()) {
505                             runqueue.wait();
506                         }
507                         running = true;
508                         r = runqueue.pollFirst();
509                     }
510                     r.run();
511                     synchronized (runqueue) {
512                         running = false;
513                         runqueue.notifyAll();
514                     }
515                 } catch (InterruptedException e) {
516                     return;
517                 }
518             }
519     });
520     void wait_events_processed(Peer peer1, Peer peer2) {
521         if (use_nio_peer_handler) {
522             peer1.nio_peer_handler.check_events();
523             peer2.nio_peer_handler.check_events();
524             try { Thread.sleep(400); } catch (InterruptedException e) { assert false; }
525         } else {
526             synchronized (runqueue) {
527                 ran = false;
528             }
529             while (true) {
530                 peer1.peer_manager.process_events();
531                 peer2.peer_manager.process_events();
532                 synchronized (runqueue) {
533                     if (runqueue.isEmpty() && !running) {
534                         if (ran) {
535                             ran = false;
536                             continue;
537                         } else { break; }
538                     }
539                     try { runqueue.wait(); } catch (InterruptedException e) { assert false; }
540                 }
541             }
542         }
543     }
544     void do_read_event(PeerManager pm, SocketDescriptor descriptor, byte[] data) {
545         if (!t.isAlive()) t.start();
546         synchronized (runqueue) {
547             ran = true;
548             runqueue.add(() -> {
549                 Result_boolPeerHandleErrorZ res = pm.read_event(descriptor, data);
550                 assert res instanceof Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK;
551             });
552             runqueue.notifyAll();
553         }
554         must_free_objs.add(new WeakReference<>(data));
555     }
556
557     void connect_peers(final Peer peer1, final Peer peer2) {
558         if (use_nio_peer_handler) {
559             try {
560                 peer1.nio_peer_handler.connect(peer2.chan_manager.get_our_node_id(), new InetSocketAddress("127.0.0.1", peer2.nio_port), 100);
561             } catch (IOException e) { assert false; }
562         } else {
563             DescriptorHolder descriptor1 = new DescriptorHolder();
564             DescriptorHolder descriptor1ref = descriptor1;
565             SocketDescriptor descriptor2 = SocketDescriptor.new_impl(new SocketDescriptor.SocketDescriptorInterface() {
566                 @Override
567                 public long send_data(byte[] data, boolean resume_read) {
568                     do_read_event(peer1.peer_manager, descriptor1ref.val, data);
569                     return data.length;
570                 }
571
572                 @Override public void disconnect_socket() { assert false; }
573                 @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == 2; }
574                 @Override public long hash() { return 2; }
575             });
576
577             descriptor1.val = SocketDescriptor.new_impl(new SocketDescriptor.SocketDescriptorInterface() {
578                 @Override
579                 public long send_data(byte[] data, boolean resume_read) {
580                     do_read_event(peer2.peer_manager, descriptor2, data);
581                     return data.length;
582                 }
583
584                 @Override public void disconnect_socket() { assert false; }
585                 @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == 1; }
586                 @Override public long hash() { return 1; }
587             });
588
589             Result_CVec_u8ZPeerHandleErrorZ conn_res = peer1.peer_manager.new_outbound_connection(peer2.node_id, descriptor1.val);
590             assert conn_res instanceof Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK;
591
592             Result_NonePeerHandleErrorZ inbound_conn_res = peer2.peer_manager.new_inbound_connection(descriptor2);
593             assert inbound_conn_res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_OK;
594             do_read_event(peer2.peer_manager, descriptor2, ((Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) conn_res).res);
595         }
596     }
597
598     TestState do_test_message_handler() throws InterruptedException {
599         Peer peer1 = new Peer((byte) 1);
600         Peer peer2 = new Peer((byte) 2);
601
602         connect_peers(peer1, peer2);
603         wait_events_processed(peer1, peer2);
604
605         Result_NoneAPIErrorZ cc_res = peer1.chan_manager.create_channel(peer2.node_id, 10000, 1000, 42, null);
606         assert cc_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
607         wait_events_processed(peer1, peer2);
608
609         Event[] events = peer1.get_manager_events(1);
610         assert events[0] instanceof Event.FundingGenerationReady;
611         assert ((Event.FundingGenerationReady) events[0]).channel_value_satoshis == 10000;
612         assert ((Event.FundingGenerationReady) events[0]).user_channel_id == 42;
613         byte[] funding_spk = ((Event.FundingGenerationReady) events[0]).output_script;
614         assert funding_spk.length == 34 && funding_spk[0] == 0 && funding_spk[1] == 32; // P2WSH
615         byte[] chan_id = ((Event.FundingGenerationReady) events[0]).temporary_channel_id;
616
617         NetworkParameters bitcoinj_net = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
618
619         Transaction funding = new Transaction(bitcoinj_net);
620         funding.addInput(new TransactionInput(bitcoinj_net, funding, new byte[0]));
621         funding.getInputs().get(0).setWitness(new TransactionWitness(2)); // Make sure we don't complain about lack of witness
622         funding.getInput(0).getWitness().setPush(0, new byte[]{0x1});
623         funding.addOutput(Coin.SATOSHI.multiply(10000), new Script(funding_spk));
624         Result_NoneAPIErrorZ funding_res = peer1.chan_manager.funding_transaction_generated(chan_id, funding.bitcoinSerialize());
625         assert funding_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
626         wait_events_processed(peer1, peer2);
627
628         assert peer1.broadcast_set.size() == 1;
629         assert Arrays.equals(peer1.broadcast_set.get(0), funding.bitcoinSerialize());
630         peer1.broadcast_set.clear();
631
632         Block b = new Block(bitcoinj_net, 2, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 42, 0, 0, Arrays.asList(new Transaction[]{funding}));
633         peer1.connect_block(b, 1, 0);
634         peer2.connect_block(b, 1, 0);
635
636         for (int height = 2; height < 10; height++) {
637             b = new Block(bitcoinj_net, 2, b.getHash(), Sha256Hash.ZERO_HASH, 42, 0, 0, Arrays.asList(new Transaction[0]));
638             peer1.connect_block(b, height, 0);
639             peer2.connect_block(b, height, 0);
640         }
641         wait_events_processed(peer1, peer2);
642
643         peer1.chan_manager.list_channels();
644         ChannelDetails[] peer1_chans = peer1.chan_manager.list_usable_channels();
645         ChannelDetails[] peer2_chans = peer2.chan_manager.list_usable_channels();
646         assert peer1_chans.length == 1;
647         assert peer2_chans.length == 1;
648         assert peer1_chans[0].get_channel_value_satoshis() == 10000;
649         assert peer1_chans[0].get_is_usable();
650         Option_u64Z short_chan_id = peer1_chans[0].get_short_channel_id();
651         assert short_chan_id instanceof Option_u64Z.Some;
652         assert ((Option_u64Z.Some)short_chan_id).some == (1L << 40); // 0th output in the 0th transaction in the 1st block
653         assert Arrays.equals(peer1_chans[0].get_channel_id(), funding.getTxId().getReversedBytes());
654         assert Arrays.equals(peer2_chans[0].get_channel_id(), funding.getTxId().getReversedBytes());
655
656         Result_InvoiceSignOrCreationErrorZ invoice = UtilMethods.invoice_from_channelmanager(peer2.chan_manager, peer2.keys_interface, LDKCurrency.LDKCurrency_Bitcoin, Option_u64Z.none(), "Invoice Description");
657         assert invoice instanceof Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK;
658         System.out.println("Got invoice: " + ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
659         Result_InvoiceNoneZ parsed_invoice = Invoice.from_str(((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
660         assert parsed_invoice instanceof Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK;
661         assert Arrays.equals(((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res.payment_hash(), ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash());
662         SignedRawInvoice signed_raw = ((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res.into_signed_raw();
663         RawInvoice raw_invoice = signed_raw.raw_invoice();
664         byte[] desc_hash = raw_invoice.hash();
665         Description raw_invoice_description = raw_invoice.description();
666         String description_string = raw_invoice_description.into_inner();
667         assert description_string.equals("Invoice Description");
668         byte[] payment_hash = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash();
669         byte[] payment_secret = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_secret();
670
671         Route route = peer1.get_route(peer2.node_id, peer1_chans);
672         Result_NonePaymentSendFailureZ payment_res = peer1.chan_manager.send_payment(route, payment_hash, payment_secret);
673         assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_OK;
674         wait_events_processed(peer1, peer2);
675
676         RouteHop[][] hops = new RouteHop[1][1];
677         byte[] hop_pubkey = new byte[33];
678         hop_pubkey[0] = 3;
679         hop_pubkey[1] = 42;
680         hops[0][0] = RouteHop.of(hop_pubkey, NodeFeatures.known(), 42, ChannelFeatures.known(), 100, 0);
681         Route r2 = Route.of(hops);
682         payment_res = peer1.chan_manager.send_payment(r2, payment_hash, payment_secret);
683         assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_Err;
684
685         if (!use_chan_manager_constructor) {
686             peer1.get_monitor_events(0);
687             peer2.get_monitor_events(0);
688         } else {
689             // The events are combined across manager + monitors but peer1 still has no events
690         }
691
692         if (reload_peers) {
693             if (use_nio_peer_handler) {
694                 peer1.nio_peer_handler.interrupt();
695                 peer2.nio_peer_handler.interrupt();
696             }
697             if (use_chan_manager_constructor) {
698                 peer1.constructor.interrupt();
699                 peer2.constructor.interrupt();
700             }
701             WeakReference<Peer> op1 = new WeakReference<Peer>(peer1);
702             peer1 = new Peer(peer1);
703             peer2 = new Peer(peer2);
704             return new TestState(op1, peer1, peer2, b.getHash());
705         }
706         return new TestState(null, peer1, peer2, b.getHash());
707     }
708
709     boolean cross_reload_ref_pollution = false;
710     class TestState {
711         private final WeakReference<Peer> ref_block;
712         private final Peer peer1;
713         private final Peer peer2;
714         public Sha256Hash best_blockhash;
715
716         public TestState(WeakReference<Peer> ref_block, Peer peer1, Peer peer2, Sha256Hash best_blockhash) {
717             this.ref_block = ref_block;
718             this.peer1 = peer1;
719             this.peer2 = peer2;
720             this.best_blockhash = best_blockhash;
721         }
722     }
723     void do_test_message_handler_b(TestState state) {
724         GcCheck obj = new GcCheck();
725         if (state.ref_block != null) {
726             // Ensure the original peers get freed before we move on. Note that we have to be in a different function
727             // scope to do so as the (at least current OpenJDK) JRE won't release anything created in the same scope.
728             while (!cross_reload_ref_pollution && state.ref_block.get() != null) {
729                 System.gc();
730                 System.runFinalization();
731             }
732             connect_peers(state.peer1, state.peer2);
733         }
734         wait_events_processed(state.peer1, state.peer2);
735
736         Event[] events = state.peer2.get_manager_events(1);
737         assert events[0] instanceof Event.PendingHTLCsForwardable;
738         state.peer2.chan_manager.process_pending_htlc_forwards();
739
740         events = state.peer2.get_manager_events(1);
741         assert events[0] instanceof Event.PaymentReceived;
742         byte[] payment_preimage = ((Event.PaymentReceived)events[0]).payment_preimage;
743         assert !Arrays.equals(payment_preimage, new byte[32]);
744         state.peer2.chan_manager.claim_funds(payment_preimage);
745         wait_events_processed(state.peer1, state.peer2);
746
747         events = state.peer1.get_manager_events(1);
748         assert events[0] instanceof Event.PaymentSent;
749         assert Arrays.equals(((Event.PaymentSent) events[0]).payment_preimage, payment_preimage);
750         wait_events_processed(state.peer1, state.peer2);
751
752         ChannelDetails[] peer1_chans = state.peer1.chan_manager.list_channels();
753
754         if (nice_close) {
755             Result_NoneAPIErrorZ close_res = state.peer1.chan_manager.close_channel(peer1_chans[0].get_channel_id());
756             assert close_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
757             wait_events_processed(state.peer1, state.peer2);
758
759             assert state.peer1.broadcast_set.size() == 1;
760             assert state.peer2.broadcast_set.size() == 1;
761         } else {
762             state.peer1.chan_manager.force_close_all_channels();
763             wait_events_processed(state.peer1, state.peer2);
764
765             assert state.peer1.broadcast_set.size() == 1;
766             assert state.peer2.broadcast_set.size() == 1;
767
768             NetworkParameters bitcoinj_net = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
769             Transaction tx = new Transaction(bitcoinj_net, state.peer1.broadcast_set.getFirst());
770             Block b = new Block(bitcoinj_net, 2, state.best_blockhash, Sha256Hash.ZERO_HASH, 42, 0, 0,
771                     Arrays.asList(new Transaction[]{tx}));
772             TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] watch_outputs = state.peer2.connect_block(b, 10, 1);
773             if (watch_outputs != null) { // We only process watch_outputs manually when we use a manually-build Watch impl
774                 assert watch_outputs.length == 1;
775                 assert Arrays.equals(watch_outputs[0].a, tx.getTxId().getReversedBytes());
776                 assert watch_outputs[0].b.length == 2;
777                 assert watch_outputs[0].b[0].a == 0;
778                 assert watch_outputs[0].b[1].a == 1;
779             }
780
781             for (int i = 11; i < 21; i++) {
782                 b = new Block(bitcoinj_net, 2, b.getHash(), Sha256Hash.ZERO_HASH, 42, 0, 0, new ArrayList<>());
783                 state.peer2.connect_block(b, i, 0);
784             }
785
786             Event[] broadcastable_event = state.peer2.get_monitor_events(1);
787             for (ChannelMonitor mon : state.peer2.monitors.values()) {
788                 // This used to be buggy and double-free, so go ahead and fetch them!
789                 byte[][] txn = mon.get_latest_holder_commitment_txn(state.peer2.logger);
790             }
791             assert broadcastable_event.length == 1;
792             assert broadcastable_event[0] instanceof Event.SpendableOutputs;
793             if (state.peer2.explicit_keys_manager != null) {
794                 Result_TransactionNoneZ tx_res = state.peer2.explicit_keys_manager.spend_spendable_outputs(((Event.SpendableOutputs) broadcastable_event[0]).outputs, new TxOut[0], new byte[] {0x00}, 253);
795                 assert tx_res instanceof Result_TransactionNoneZ.Result_TransactionNoneZ_OK;
796                 Transaction built_tx = new Transaction(bitcoinj_net, ((Result_TransactionNoneZ.Result_TransactionNoneZ_OK) tx_res).res);
797                 assert built_tx.getOutputs().size() == 1;
798                 assert Arrays.equals(built_tx.getOutput(0).getScriptBytes(), new byte[]{0x00});
799             }
800         }
801
802         if (use_nio_peer_handler) {
803             state.peer1.peer_manager.disconnect_by_node_id(state.peer2.chan_manager.get_our_node_id(), false);
804             wait_events_processed(state.peer1, state.peer2);
805             assert state.peer1.peer_manager.get_peer_node_ids().length == 0;
806             assert state.peer2.peer_manager.get_peer_node_ids().length == 0;
807             state.peer1.nio_peer_handler.interrupt();
808             state.peer2.nio_peer_handler.interrupt();
809         }
810
811         state.peer1.get_monitor_events(0);
812         state.peer2.get_monitor_events(0);
813
814         if (use_chan_manager_constructor) {
815             state.peer1.constructor.interrupt();
816             state.peer2.constructor.interrupt();
817         }
818
819         t.interrupt();
820     }
821
822     java.util.LinkedList<WeakReference<Object>> must_free_objs = new java.util.LinkedList();
823     int gc_count = 0;
824     int gc_exp_count = 0;
825     class GcCheck {
826         GcCheck() { gc_exp_count += 1; }
827         @Override
828         protected void finalize() throws Throwable {
829             gc_count += 1;
830             super.finalize();
831         }
832     }
833 }
834 public class HumanObjectPeerTest {
835     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, boolean use_chan_manager_constructor) throws InterruptedException {
836         HumanObjectPeerTestInstance instance = new HumanObjectPeerTestInstance(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler, !nio_peer_handler, use_chan_manager_constructor);
837         HumanObjectPeerTestInstance.TestState state = instance.do_test_message_handler();
838         instance.do_test_message_handler_b(state);
839         return instance;
840     }
841     void do_test(boolean nice_close, boolean use_km_wrapper, boolean use_manual_watch, boolean reload_peers, boolean break_cross_peer_refs, boolean nio_peer_handler, boolean use_chan_manager_constructor) throws InterruptedException {
842         HumanObjectPeerTestInstance instance = do_test_run(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler, use_chan_manager_constructor);
843         while (instance.gc_count != instance.gc_exp_count) {
844             System.gc();
845             System.runFinalization();
846         }
847         for (WeakReference<Object> o : instance.must_free_objs)
848             assert o.get() == null;
849     }
850     @Test
851     public void test_message_handler() throws InterruptedException {
852         for (int i = 0; i < (1 << 7) - 1; i++) {
853             boolean nice_close =                   (i & (1 << 0)) != 0;
854             boolean use_km_wrapper =               (i & (1 << 1)) != 0;
855             boolean use_manual_watch =             (i & (1 << 2)) != 0;
856             boolean reload_peers =                 (i & (1 << 3)) != 0;
857             boolean break_cross_refs =             (i & (1 << 4)) != 0;
858             boolean nio_peer_handler =             (i & (1 << 5)) != 0;
859             boolean use_chan_manager_constructor = (i & (1 << 6)) != 0;
860             if (break_cross_refs && !reload_peers) {
861                 // There are no cross refs to break without reloading peers.
862                 continue;
863             }
864             if (use_chan_manager_constructor && (use_manual_watch || !nio_peer_handler)) {
865                 // ChannelManagerConstructor requires a ChainMonitor as the Watch and creates a NioPeerHandler for us.
866                 continue;
867             }
868             System.err.println("Running test with flags " + i);
869             do_test(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_refs, nio_peer_handler, use_chan_manager_constructor);
870         }
871     }
872 }