c4f1b9062a7462905ef0bc7f5b105277fff558a6
[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.*;
9 import org.ldk.enums.Currency;
10 import org.ldk.structs.*;
11 import org.ldk.util.UInt5;
12
13 import java.io.IOException;
14 import java.lang.ref.WeakReference;
15 import java.net.InetSocketAddress;
16 import java.util.*;
17
18 class HumanObjectPeerTestInstance {
19     private final boolean nice_close;
20     private final boolean use_km_wrapper;
21     private final boolean use_manual_watch;
22     private final boolean reload_peers;
23     private final boolean break_cross_peer_refs;
24     private final boolean use_nio_peer_handler;
25     private final boolean use_filter;
26     private final boolean use_ignore_handler;
27     private final boolean use_chan_manager_constructor;
28     private final boolean use_invoice_payer;
29
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, boolean use_filter, boolean use_ignore_handler, boolean use_chan_manager_constructor, boolean use_invoice_payer) {
31         this.nice_close = nice_close;
32         this.use_km_wrapper = use_km_wrapper;
33         this.use_manual_watch = use_manual_watch;
34         this.reload_peers = reload_peers;
35         this.break_cross_peer_refs = break_cross_peer_refs;
36         this.use_nio_peer_handler = use_nio_peer_handler;
37         this.use_filter = use_filter;
38         this.use_ignore_handler = use_ignore_handler;
39         this.use_chan_manager_constructor = use_chan_manager_constructor;
40         this.use_invoice_payer = use_invoice_payer;
41     }
42
43     class Peer {
44         KeysInterface manual_keysif(KeysInterface underlying_if) {
45             return KeysInterface.new_impl(new KeysInterface.KeysInterfaceInterface() {
46                 @Override public Result_SecretKeyNoneZ get_node_secret(Recipient recipient) { return underlying_if.get_node_secret(recipient); }
47                 @Override public byte[] get_destination_script() { return underlying_if.get_destination_script(); }
48                 @Override public ShutdownScript get_shutdown_scriptpubkey() { return underlying_if.get_shutdown_scriptpubkey(); }
49
50                 @Override
51                 public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) {
52                     Sign underlying_ck = underlying_if.get_channel_signer(inbound, channel_value_satoshis);
53                     BaseSign underlying_base = underlying_ck.get_base_sign();
54                     BaseSign.BaseSignInterface bsi = new BaseSign.BaseSignInterface() {
55                         @Override public byte[] get_per_commitment_point(long idx) {
56                             return underlying_base.get_per_commitment_point(idx);
57                         }
58                         @Override public byte[] release_commitment_secret(long idx) {
59                             return underlying_base.release_commitment_secret(idx);
60                         }
61                         @Override public Result_NoneNoneZ validate_holder_commitment(HolderCommitmentTransaction holder_tx, byte[][] preimages) {
62                             return underlying_base.validate_holder_commitment(holder_tx, preimages);
63                         }
64                         @Override public byte[] channel_keys_id() {
65                             return new byte[32];
66                         }
67                         @Override public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_counterparty_commitment(CommitmentTransaction commitment_tx, byte[][] preimages) {
68                             return underlying_base.sign_counterparty_commitment(commitment_tx, preimages);
69                         }
70                         @Override public Result_NoneNoneZ validate_counterparty_revocation(long idx, byte[] secret) {
71                             return underlying_base.validate_counterparty_revocation(idx, secret);
72                         }
73                         @Override public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_holder_commitment_and_htlcs(HolderCommitmentTransaction holder_commitment_tx) {
74                             return underlying_base.sign_holder_commitment_and_htlcs(holder_commitment_tx);
75                         }
76                         @Override public Result_SignatureNoneZ sign_justice_revoked_output(byte[] justice_tx, long input, long amount, byte[] per_commitment_key) {
77                             return underlying_base.sign_justice_revoked_output(justice_tx, input, amount, per_commitment_key);
78                         }
79                         @Override public Result_SignatureNoneZ sign_justice_revoked_htlc(byte[] justice_tx, long input, long amount, byte[] per_commitment_key, HTLCOutputInCommitment htlc) {
80                             return underlying_base.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc);
81                         }
82                         @Override public Result_SignatureNoneZ sign_counterparty_htlc_transaction(byte[] htlc_tx, long input, long amount, byte[] per_commitment_point, HTLCOutputInCommitment htlc) {
83                             return underlying_base.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc);
84                         }
85                         @Override public Result_SignatureNoneZ sign_closing_transaction(ClosingTransaction closing_tx) {
86                             return underlying_base.sign_closing_transaction(closing_tx);
87                         }
88                         @Override public Result_C2Tuple_SignatureSignatureZNoneZ sign_channel_announcement(UnsignedChannelAnnouncement msg) {
89                             return underlying_base.sign_channel_announcement(msg);
90                         }
91                         @Override public void ready_channel(ChannelTransactionParameters params) {
92                             underlying_base.ready_channel(params);
93                         }
94                     };
95                     Sign.SignInterface si = new Sign.SignInterface() {
96                         @Override
97                         public byte[] write() {
98                             return underlying_ck.write();
99                         }
100                     };
101                     Sign resp = Sign.new_impl(si, bsi, underlying_base.get_pubkeys());
102                     must_free_objs.add(new WeakReference<>(si));
103                     must_free_objs.add(new WeakReference<>(bsi));
104                     must_free_objs.add(new WeakReference<>(resp));
105                     must_free_objs.add(new WeakReference<>(underlying_base));
106                     must_free_objs.add(new WeakReference<>(underlying_ck));
107                     return resp;
108                 }
109
110                 @Override
111                 public byte[] get_secure_random_bytes() {
112                     return underlying_if.get_secure_random_bytes();
113                 }
114
115                 @Override
116                 public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) {
117                     return underlying_if.read_chan_signer(reader);
118                 }
119
120                 @Override
121                 public Result_RecoverableSignatureNoneZ sign_invoice(byte[] hrp_bytes, UInt5[] invoice_data, Recipient receipient) {
122                     return underlying_if.sign_invoice(hrp_bytes, invoice_data, receipient);
123                 }
124
125                 @Override
126                 public byte[] get_inbound_payment_key_material() {
127                     return underlying_if.get_inbound_payment_key_material();
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_NoneNoneZ update_res = monitors.get(txid).update_monitor(update, tx_broadcaster, fee_estimator, logger);
146                         assert update_res instanceof Result_NoneNoneZ.Result_NoneNoneZ_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 NetworkGraph router;
178         NetGraphMsgHandler route_handler;
179         final Watch chain_watch;
180         final HashSet<String> filter_additions;
181         Option_FilterZ filter;
182         ChannelManager chan_manager;
183         PeerManager peer_manager;
184         final HashMap<String, ChannelMonitor> monitors; // Wow I forgot just how terrible Java is - we can't put a byte array here.
185         byte[] node_id;
186         byte[] connected_peer_node_id = null;
187         final LinkedList<byte[]> broadcast_set = new LinkedList<>();
188         final LinkedList<Event> pending_manager_events = new LinkedList<>();
189         private final CustomMessageHandler custom_message_handler;
190         final LinkedList<byte[]> received_custom_messages = new LinkedList<>();
191         final LinkedList<byte[]> custom_messages_to_send = new LinkedList<>();
192         ChannelManagerConstructor constructor = null;
193         InvoicePayer payer = null;
194         GcCheck obj = new GcCheck();
195
196         private ChannelMonitor test_mon_roundtrip(OutPoint expected_id, byte[] data) {
197             // Because get_funding_txo() returns an OutPoint in a tuple that is a reference to an OutPoint inside the
198             // ChannelMonitor, its a good test to ensure that the OutPoint isn't freed (or is cloned) before the
199             // ChannelMonitor is. This used to be broken.
200             Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ roundtrip_monitor = UtilMethods.C2Tuple_BlockHashChannelMonitorZ_read(data, keys_interface);
201             assert roundtrip_monitor instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
202             TwoTuple_OutPointScriptZ funding_txo = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) roundtrip_monitor).res.get_b().get_funding_txo();
203             System.gc(); System.runFinalization(); // Give the GC a chance to run.
204             assert Arrays.equals(funding_txo.get_a().get_txid(), expected_id.get_txid());
205             assert funding_txo.get_a().get_index() == expected_id.get_index();
206             Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ roundtrip_two = UtilMethods.C2Tuple_BlockHashChannelMonitorZ_read(data, keys_interface);
207             assert roundtrip_two instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
208             return ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) roundtrip_two).res.get_b();
209         }
210
211         private Peer(Object _dummy, byte seed) {
212             logger = Logger.new_impl((org.ldk.structs.Record arg)->{
213                 if (arg.get_level() == Level.LDKLevel_Error)
214                     System.err.println(seed + ": " + arg.get_module_path() + " - " + arg.get_args());
215                 else
216                     System.out.println(seed + ": " + arg.get_module_path() + " - " + arg.get_args());
217             });
218             fee_estimator = FeeEstimator.new_impl((confirmation_target -> 253));
219             tx_broadcaster = BroadcasterInterface.new_impl(tx -> {
220                 synchronized (broadcast_set) {
221                     broadcast_set.add(tx);
222                     broadcast_set.notifyAll();
223                 }
224             });
225             monitors = new HashMap<>();
226             this.seed = seed;
227             Persist persister = Persist.new_impl(new Persist.PersistInterface() {
228                 @Override
229                 public Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint id, ChannelMonitor data, MonitorUpdateId update_id) {
230                     synchronized (monitors) {
231                         String key = Arrays.toString(id.to_channel_id());
232                         ChannelMonitor res = test_mon_roundtrip(id, data.write());
233                         assert monitors.put(key, res) == null;
234                     }
235                     return Result_NoneChannelMonitorUpdateErrZ.ok();
236                 }
237
238                 @Override
239                 public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id, ChannelMonitorUpdate update, ChannelMonitor data, MonitorUpdateId update_id) {
240                     synchronized (monitors) {
241                         String key = Arrays.toString(id.to_channel_id());
242                         ChannelMonitor res = test_mon_roundtrip(id, data.write());
243                         // Note that we use a serialization-roundtrip copy of data here, not the original, as this can
244                         // expose the JVM JIT bug where it finalize()s things still being called.
245                         assert monitors.put(key, res) != null;
246                     }
247                     return Result_NoneChannelMonitorUpdateErrZ.ok();
248                 }
249             });
250
251             filter_additions = new HashSet<>();
252             if (use_filter) {
253                 this.filter = Option_FilterZ.some(Filter.new_impl(new Filter.FilterInterface() {
254                     @Override public void register_tx(byte[] txid, byte[] script_pubkey) {
255                         filter_additions.add(Arrays.toString(txid));
256                     }
257                     @Override public Option_C2Tuple_usizeTransactionZZ register_output(WatchedOutput output) {
258                         filter_additions.add(Arrays.toString(output.get_outpoint().get_txid()) + ":" + output.get_outpoint().get_index());
259                         return Option_C2Tuple_usizeTransactionZZ.none();
260                     }
261                 }));
262             } else {
263                 this.filter = Option_FilterZ.none();
264             }
265
266             if (use_manual_watch) {
267                 chain_watch = get_manual_watch();
268                 chain_monitor = null;
269             } else {
270                 chain_monitor = ChainMonitor.of(filter, tx_broadcaster, logger, fee_estimator, persister);
271                 chain_watch = chain_monitor.as_Watch();
272             }
273
274             byte[] key_seed = new byte[32];
275             for (byte i = 0; i < 32; i++) {
276                 key_seed[i] = (byte) (i ^ seed);
277             }
278             KeysManager keys = KeysManager.of(key_seed, System.currentTimeMillis() / 1000, (int) (System.currentTimeMillis() * 1000));
279             if (use_km_wrapper) {
280                 this.keys_interface = manual_keysif(keys.as_KeysInterface());
281                 this.explicit_keys_manager = null;
282             } else {
283                 this.keys_interface = keys.as_KeysInterface();
284                 this.explicit_keys_manager = keys;
285             }
286             this.router = NetworkGraph.of(new byte[32]);
287             this.route_handler = NetGraphMsgHandler.of(this.router, Option_AccessZ.some(Access.new_impl(new Access.AccessInterface() {
288                 @Override
289                 public Result_TxOutAccessErrorZ get_utxo(byte[] genesis_hash, long short_channel_id) {
290                     // We don't exchange any gossip, so should never actually get called, but providing a Some(Access)
291                     // is a good test of our Option<Trait> free'ing, which used to be broken and relies on a dirty hack.
292                     assert false;
293                     return Result_TxOutAccessErrorZ.err(AccessError.LDKAccessError_UnknownTx);
294                 }
295             })), logger);
296
297             this.custom_message_handler = CustomMessageHandler.new_impl(new CustomMessageHandler.CustomMessageHandlerInterface() {
298                 @Override
299                 public Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id) {
300                     synchronized (received_custom_messages) {
301                         received_custom_messages.add(msg.write());
302                         received_custom_messages.notifyAll();
303                     }
304                     return Result_NoneLightningErrorZ.ok();
305                 }
306
307                 @Override
308                 public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
309                     byte[][] bytes;
310                     synchronized (custom_messages_to_send) {
311                         bytes = custom_messages_to_send.toArray(new byte[0][0]);
312                         custom_messages_to_send.clear();
313                     }
314                     TwoTuple_PublicKeyTypeZ[] ret = new TwoTuple_PublicKeyTypeZ[bytes.length];
315                     for (int i = 0; i < bytes.length; i++) {
316                         final int msg_idx = i;
317                         ret[i] = TwoTuple_PublicKeyTypeZ.of(connected_peer_node_id, Type.new_impl(new Type.TypeInterface() {
318                             @Override public short type_id() { return 4096; }
319                             @Override public String debug_str() { return "Custom Java Message"; }
320                             @Override public byte[] write() { return bytes[msg_idx]; }
321                         }));
322                     }
323                     return ret;
324                 }
325             }, (message_type, buffer) -> {
326                 assert message_type == 4096;
327                 return Result_COption_TypeZDecodeErrorZ.ok(Option_TypeZ.some(Type.new_impl(new Type.TypeInterface() {
328                     @Override public short type_id() { return 4096; }
329                     @Override public String debug_str() { return "Custom Java-Decoded Message"; }
330                     @Override public byte[] write() { return buffer; }
331                 })));
332             });
333         }
334         private void bind_nio() {
335             if (!use_nio_peer_handler) return;
336             if (use_chan_manager_constructor) {
337                 this.nio_peer_handler = this.constructor.nio_peer_handler;
338             } else {
339                 try { this.nio_peer_handler = new NioPeerHandler(peer_manager); } catch (IOException e) { assert false; }
340             }
341             for (short i = 10_000; true; i++) {
342                 try {
343                     nio_peer_handler.bind_listener(new InetSocketAddress("127.0.0.1", i));
344                     nio_port = i;
345                     break;
346                 } catch (IOException e) { assert i < 10_500; }
347             }
348         }
349         Peer(byte seed) {
350             this(null, seed);
351             if (use_chan_manager_constructor) {
352                 if (use_ignore_handler) {
353                     this.constructor = new ChannelManagerConstructor(Network.LDKNetwork_Bitcoin, UserConfig.with_default(), new byte[32], 0,
354                                                         this.keys_interface, this.fee_estimator, this.chain_monitor, null, this.tx_broadcaster, this.logger);
355                 } else {
356                     this.constructor = new ChannelManagerConstructor(Network.LDKNetwork_Bitcoin, UserConfig.with_default(), new byte[32], 0,
357                                                         this.keys_interface, this.fee_estimator, this.chain_monitor, this.router, this.tx_broadcaster, this.logger);
358                 }
359                 Result_ScorerDecodeErrorZ score_res = Scorer.read(Scorer.with_default().write());
360                 assert score_res.is_ok();
361                 Score score = ((Result_ScorerDecodeErrorZ.Result_ScorerDecodeErrorZ_OK) score_res).res.as_Score();
362                 MultiThreadedLockableScore scorer = null;
363                 if (use_invoice_payer) { scorer = MultiThreadedLockableScore.of(score); }
364                 constructor.chain_sync_completed(new ChannelManagerConstructor.EventHandler() {
365                     @Override public void handle_event(Event event) {
366                         synchronized (pending_manager_events) {
367                             pending_manager_events.add(event);
368                             pending_manager_events.notifyAll();
369                         }
370                     }
371                     @Override public void persist_manager(byte[] channel_manager_bytes) { assert channel_manager_bytes.length > 1; }
372                 }, scorer);
373                 this.chan_manager = constructor.channel_manager;
374                 this.peer_manager = constructor.peer_manager;
375                 this.payer = constructor.payer;
376                 must_free_objs.add(new WeakReference<>(this.chan_manager));
377             } else {
378                 ChainParameters params = ChainParameters.of(Network.LDKNetwork_Bitcoin, BestBlock.of(new byte[32], 0));
379                 this.chan_manager = ChannelManager.of(this.fee_estimator, chain_watch, tx_broadcaster, logger, this.keys_interface, UserConfig.with_default(), params);
380                 byte[] random_data = keys_interface.get_secure_random_bytes();
381                 Result_SecretKeyNoneZ node_secret = keys_interface.get_node_secret(Recipient.LDKRecipient_Node);
382                 assert node_secret.is_ok();
383                 this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), route_handler.as_RoutingMessageHandler(),
384                         ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res, random_data, logger, this.custom_message_handler);
385                 if (use_invoice_payer) {
386                     this.payer = InvoicePayer.of(this.chan_manager.as_Payer(), Router.new_impl(new Router.RouterInterface() {
387                         @Override
388                         public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters params, byte[] payment_hash, ChannelDetails[] first_hops, Score scorer) {
389                             return UtilMethods.find_route(payer, params, router, first_hops, logger, scorer);
390                         }
391                     }), MultiThreadedLockableScore.of(Score.new_impl(new Score.ScoreInterface() {
392                         @Override public void payment_path_failed(RouteHop[] path, long scid) {}
393                         @Override public long channel_penalty_msat(long short_channel_id, long send_amt_msat, long channel_capacity_msat, NodeId source, NodeId target) { return 0; }
394                         @Override public void payment_path_successful(RouteHop[] path) {}
395                         @Override public byte[] write() { assert false; return null; }
396                     })), logger, EventHandler.new_impl(new EventHandler.EventHandlerInterface() {
397                         @Override public void handle_event(Event event) {
398                             synchronized (pending_manager_events) {
399                                 pending_manager_events.add(event);
400                                 pending_manager_events.notifyAll();
401                             }
402                         }
403                     }), RetryAttempts.of(0));
404                 }
405             }
406
407             this.node_id = chan_manager.get_our_node_id();
408             this.filter = null;
409             bind_nio();
410             System.gc();
411         }
412
413         Object ptr_to;
414         Peer(Peer orig) {
415             this(null, orig.seed);
416             if (use_chan_manager_constructor) {
417                 byte[][] monitors = {orig.monitors.values().stream().iterator().next().write()};
418                 byte[] serialized = orig.chan_manager.write();
419                 try {
420                     Filter filter_nullable = null;
421                     if (this.filter instanceof Option_FilterZ.Some) {
422                         filter_nullable = ((Option_FilterZ.Some) this.filter).some;
423                     }
424                     if (use_ignore_handler) {
425                         this.constructor = new ChannelManagerConstructor(serialized, monitors, UserConfig.with_default(),
426                                 this.keys_interface, this.fee_estimator, this.chain_monitor, filter_nullable,
427                                 null, this.tx_broadcaster, this.logger);
428                     } else {
429                         this.constructor = new ChannelManagerConstructor(serialized, monitors, UserConfig.with_default(),
430                                 this.keys_interface, this.fee_estimator, this.chain_monitor, filter_nullable,
431                                 this.router, this.tx_broadcaster, this.logger);
432                         try {
433                             // Test that ChannelManagerConstructor correctly rejects duplicate ChannelMonitors
434                             byte[][] monitors_dupd = new byte[2][];
435                             monitors_dupd[0] = monitors[0];
436                             monitors_dupd[1] = monitors[0];
437                             ChannelManagerConstructor constr = this.constructor = new ChannelManagerConstructor(serialized, monitors_dupd, UserConfig.with_default(),
438                                     this.keys_interface, this.fee_estimator, this.chain_monitor, filter_nullable,
439                                     null, this.tx_broadcaster, this.logger);
440                             assert false;
441                         } catch (ChannelManagerConstructor.InvalidSerializedDataException e) {}
442                     }
443                     MultiThreadedLockableScore scorer = null;
444                     if (use_invoice_payer) { scorer = MultiThreadedLockableScore.of(Scorer.with_default().as_Score()); }
445                     constructor.chain_sync_completed(new ChannelManagerConstructor.EventHandler() {
446                         @Override public void handle_event(Event event) {
447                             synchronized (pending_manager_events) {
448                                 pending_manager_events.add(event);
449                                 pending_manager_events.notifyAll();
450                             }
451                         }
452                         @Override public void persist_manager(byte[] channel_manager_bytes) { assert channel_manager_bytes.length > 1; }
453                     }, scorer);
454                     this.chan_manager = constructor.channel_manager;
455                     this.payer = constructor.payer;
456                     this.peer_manager = constructor.peer_manager;
457                     must_free_objs.add(new WeakReference<>(this.chan_manager));
458                     // If we are using a ChannelManagerConstructor, we may have pending events waiting on the old peer
459                     // which have been removed from the ChannelManager but which we still need to handle.
460                     this.pending_manager_events.addAll(orig.pending_manager_events);
461                     if (!this.pending_manager_events.isEmpty()) {
462                         // However, this implies cross_reload_ref_pollution
463                         cross_reload_ref_pollution = true;
464                     }
465                 } catch (ChannelManagerConstructor.InvalidSerializedDataException e) {
466                     assert false;
467                 }
468             } else {
469                 ChannelMonitor[] monitors = new ChannelMonitor[1];
470                 assert orig.monitors.size() == 1;
471                 if (!break_cross_peer_refs) {
472                     monitors[0] = orig.monitors.values().stream().iterator().next();
473                 } else {
474                     byte[] serialized = orig.monitors.values().stream().iterator().next().write();
475                     Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res =
476                             UtilMethods.C2Tuple_BlockHashChannelMonitorZ_read(serialized, this.keys_interface);
477                     assert res instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
478                     monitors[0] = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) res).res.get_b();
479                 }
480                 byte[] serialized = orig.chan_manager.write();
481                 Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ read_res =
482                         UtilMethods.C2Tuple_BlockHashChannelManagerZ_read(serialized, this.keys_interface, this.fee_estimator, this.chain_watch, this.tx_broadcaster, this.logger, UserConfig.with_default(), monitors);
483                 assert read_res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK;
484                 this.chan_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.get_b();
485                 this.chain_watch.watch_channel(monitors[0].get_funding_txo().get_a(), monitors[0]);
486                 byte[] random_data = keys_interface.get_secure_random_bytes();
487                 Result_SecretKeyNoneZ node_secret = keys_interface.get_node_secret(Recipient.LDKRecipient_Node);
488                 assert node_secret.is_ok();
489                 this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), route_handler.as_RoutingMessageHandler(),
490                         ((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res,
491                         random_data, logger, this.custom_message_handler);
492                 if (!break_cross_peer_refs && (use_manual_watch || use_km_wrapper)) {
493                     // When we pass monitors[0] into chain_watch.watch_channel we create a reference from the new Peer to a
494                     // field in the old peer, preventing freeing of the original Peer until the new Peer is freed. Thus, we
495                     // shouldn't bother waiting for the original to be freed later on.
496                     cross_reload_ref_pollution = true;
497                 }
498                 if (use_invoice_payer) {
499                     this.payer = InvoicePayer.of(this.chan_manager.as_Payer(), Router.new_impl(new Router.RouterInterface() {
500                         @Override
501                         public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters params, byte[] _payment_hash, ChannelDetails[] first_hops, Score scorer) {
502                             return UtilMethods.find_route(payer, params, router, first_hops, logger, scorer);
503                         }
504                     }), MultiThreadedLockableScore.of(Score.new_impl(new Score.ScoreInterface() {
505                         @Override public long channel_penalty_msat(long short_channel_id, long send_amt_msat, long channel_capacity_msat, NodeId source, NodeId target) { return 0; }
506                         @Override public void payment_path_failed(RouteHop[] path, long scid) {}
507                         @Override public void payment_path_successful(RouteHop[] path) {}
508                         @Override public byte[] write() { assert false; return null; }
509                     })), logger, EventHandler.new_impl(new EventHandler.EventHandlerInterface() {
510                         @Override public void handle_event(Event event) {
511                             synchronized (pending_manager_events) {
512                                 pending_manager_events.add(event);
513                                 pending_manager_events.notifyAll();
514                             }
515                         }
516                     }), RetryAttempts.of(0));
517                 }
518             }
519             this.node_id = chan_manager.get_our_node_id();
520             bind_nio();
521
522             if (cross_reload_ref_pollution) {
523                 // This really, really needs to be handled at the bindings layer, but its rather complicated -
524                 // ChannelSigners can be cloned and passed around without java being involved, resulting in them being
525                 // owned by both one or more ChannelMonitors and a ChannelManager, with only one having proper pointers
526                 // to the ChannelSigner. Ideally, the ChannelSigner would have a global reference to the Java
527                 // implementation class, but that results in circular references. Instead, we need some ability to,
528                 // while cloning ChannelSigners, add new references in the calling Java struct (ie ChannelMonitor) to
529                 // the ChannelSigner.
530                 this.ptr_to = orig.chan_manager;
531             }
532             this.filter = null;
533         }
534
535         TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] connect_block(Block b, int height, long expected_monitor_update_len) {
536             byte[] header = Arrays.copyOfRange(b.bitcoinSerialize(), 0, 80);
537             TwoTuple_usizeTransactionZ[] txn;
538             if (b.hasTransactions()) {
539                 assert b.getTransactions().size() == 1;
540                 TwoTuple_usizeTransactionZ txp = TwoTuple_usizeTransactionZ.of((long) 0, b.getTransactions().get(0).bitcoinSerialize());
541                 txn = new TwoTuple_usizeTransactionZ[]{txp};
542             } else
543                 txn = new TwoTuple_usizeTransactionZ[0];
544             if (chain_monitor != null) {
545                 chan_manager.as_Listen().block_connected(b.bitcoinSerialize(), height);
546                 chain_monitor.as_Listen().block_connected(b.bitcoinSerialize(), height);
547             } else {
548                 chan_manager.as_Confirm().transactions_confirmed(header, txn, height);
549                 chan_manager.as_Confirm().best_block_updated(header, height);
550                 // Connect manually if we aren't using a ChainMonitor and are implementing Watch ourselves
551                 synchronized (monitors) {
552                     assert monitors.size() == 1;
553                     for (ChannelMonitor mon : monitors.values()) {
554                         TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret = mon.block_connected(header, txn, height, tx_broadcaster, fee_estimator, logger);
555                         assert ret.length == expected_monitor_update_len;
556                         return ret;
557                     }
558                 }
559             }
560             return null;
561         }
562
563         Event[] get_monitor_events(int expected_len) {
564             if (use_chan_manager_constructor) {
565                 while (true) {
566                     synchronized (this.pending_manager_events) {
567                         if (expected_len != 0 && this.pending_manager_events.size() == expected_len) {
568                             break;
569                         }
570                     }
571                     if (expected_len == 0) {
572                         try { Thread.sleep(500); } catch (InterruptedException e) { assert false; }
573                         break;
574                     } else {
575                         Thread.yield();
576                     }
577                 }
578                 synchronized (this.pending_manager_events) {
579                     Event[] res = this.pending_manager_events.toArray(new Event[0]);
580                     this.pending_manager_events.clear();
581                     assert res.length == expected_len;
582                     return res;
583                 }
584             } else if (chain_monitor != null) {
585                 ArrayList<Event> l = new ArrayList<Event>();
586                 chain_monitor.as_EventsProvider().process_pending_events(EventHandler.new_impl(l::add));
587                 assert l.size() == expected_len;
588                 return l.toArray(new Event[0]);
589             } else {
590                 synchronized (monitors) {
591                     assert monitors.size() == 1;
592                     for (ChannelMonitor mon : monitors.values()) {
593                         Event[] res = mon.get_and_clear_pending_events();
594                         assert res.length == expected_len;
595                         return res;
596                     }
597                     return null;
598                 }
599             }
600         }
601
602         Event[] get_manager_events(int expected_len, Peer peer1, Peer peer2) {
603             assert expected_len != 0;
604             if (!use_nio_peer_handler) {
605                 maybe_exchange_peer_messages(peer1, peer2);
606             }
607             Event[] res = new Event[0];
608             if (use_chan_manager_constructor) {
609                 while (res.length < expected_len) {
610                     synchronized (this.pending_manager_events) {
611                         if (this.pending_manager_events.size() >= expected_len) {
612                             res = this.pending_manager_events.toArray(res);
613                             assert res.length == expected_len;
614                             this.pending_manager_events.clear();
615                         }
616                         if (res.length < expected_len) {
617                             try { this.pending_manager_events.wait(); } catch (InterruptedException e) { assert false; }
618                         }
619                     }
620                 }
621             } else {
622                 ArrayList<Event> l = new ArrayList<Event>();
623                 while (l.size() < expected_len) {
624                     Thread.yield();
625                     if (use_nio_peer_handler) {
626                         peer1.nio_peer_handler.check_events();
627                         peer2.nio_peer_handler.check_events();
628                     }
629                     chan_manager.as_EventsProvider().process_pending_events(EventHandler.new_impl(l::add));
630                 }
631                 return l.toArray(new Event[0]);
632             }
633             assert res.length == expected_len;
634             return res;
635         }
636     }
637
638     static class DescriptorHolder { SocketDescriptor val; }
639
640     boolean running = false;
641     final LinkedList<Runnable> runqueue = new LinkedList();
642     boolean ran = false;
643     Thread t = new Thread(() -> {
644             while (true) {
645                 try {
646                     Runnable r;
647                     synchronized (runqueue) {
648                         while (runqueue.isEmpty()) {
649                             runqueue.wait();
650                         }
651                         running = true;
652                         r = runqueue.pollFirst();
653                     }
654                     r.run();
655                     synchronized (runqueue) {
656                         running = false;
657                         runqueue.notifyAll();
658                     }
659                 } catch (InterruptedException e) {
660                     return;
661                 }
662             }
663     });
664
665     void maybe_exchange_peer_messages(Peer peer1, Peer peer2) {
666         if (!use_nio_peer_handler) {
667             synchronized (runqueue) {
668                 ran = false;
669             }
670             while (true) {
671                 peer1.peer_manager.process_events();
672                 peer2.peer_manager.process_events();
673                 synchronized (runqueue) {
674                     if (runqueue.isEmpty() && !running) {
675                         if (ran) {
676                             ran = false;
677                             continue;
678                         } else { break; }
679                     }
680                     try { runqueue.wait(); } catch (InterruptedException e) { assert false; }
681                 }
682             }
683         } else if (!use_chan_manager_constructor) {
684             peer1.nio_peer_handler.check_events();
685             peer2.nio_peer_handler.check_events();
686         }
687     }
688     void do_read_event(PeerManager pm, SocketDescriptor descriptor, byte[] data) {
689         if (!t.isAlive()) t.start();
690         synchronized (runqueue) {
691             ran = true;
692             runqueue.add(() -> {
693                 Result_boolPeerHandleErrorZ res = pm.read_event(descriptor, data);
694                 assert res instanceof Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK;
695             });
696             runqueue.notifyAll();
697         }
698         must_free_objs.add(new WeakReference<>(data));
699     }
700
701     void connect_peers(final Peer peer1, final Peer peer2) {
702         peer2.connected_peer_node_id = peer1.node_id;
703         peer1.connected_peer_node_id = peer2.node_id;
704         if (use_nio_peer_handler) {
705             try {
706                 peer1.nio_peer_handler.connect(peer2.chan_manager.get_our_node_id(), new InetSocketAddress("127.0.0.1", peer2.nio_port), 100);
707             } catch (IOException e) { assert false; }
708             while (peer1.peer_manager.get_peer_node_ids().length == 0 || peer2.peer_manager.get_peer_node_ids().length == 0) {
709                 Thread.yield();
710             }
711         } else {
712             DescriptorHolder descriptor1 = new DescriptorHolder();
713             DescriptorHolder descriptor1ref = descriptor1;
714             SocketDescriptor descriptor2 = SocketDescriptor.new_impl(new SocketDescriptor.SocketDescriptorInterface() {
715                 @Override
716                 public long send_data(byte[] data, boolean resume_read) {
717                     do_read_event(peer1.peer_manager, descriptor1ref.val, data);
718                     return data.length;
719                 }
720
721                 @Override public void disconnect_socket() { assert false; }
722                 @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == 2; }
723                 @Override public long hash() { return 2; }
724             });
725
726             descriptor1.val = SocketDescriptor.new_impl(new SocketDescriptor.SocketDescriptorInterface() {
727                 @Override
728                 public long send_data(byte[] data, boolean resume_read) {
729                     do_read_event(peer2.peer_manager, descriptor2, data);
730                     return data.length;
731                 }
732
733                 @Override public void disconnect_socket() { assert false; }
734                 @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == 1; }
735                 @Override public long hash() { return 1; }
736             });
737
738             Result_CVec_u8ZPeerHandleErrorZ conn_res = peer1.peer_manager.new_outbound_connection(peer2.node_id, descriptor1.val);
739             assert conn_res instanceof Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK;
740
741             Result_NonePeerHandleErrorZ inbound_conn_res = peer2.peer_manager.new_inbound_connection(descriptor2);
742             assert inbound_conn_res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_OK;
743             do_read_event(peer2.peer_manager, descriptor2, ((Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) conn_res).res);
744
745             maybe_exchange_peer_messages(peer1, peer2);
746         }
747     }
748
749     TestState do_test_message_handler() throws InterruptedException {
750         Peer peer1 = new Peer((byte) 1);
751         Peer peer2 = new Peer((byte) 2);
752
753         connect_peers(peer1, peer2);
754
755         Result__u832APIErrorZ cc_res = peer1.chan_manager.create_channel(peer2.node_id, 100000, 1000, 42, null);
756         assert cc_res instanceof Result__u832APIErrorZ.Result__u832APIErrorZ_OK;
757
758         // Previously, this was a SEGFAULT instead of get_funding_txo() returning null.
759         ChannelDetails pre_funding_chan = peer1.chan_manager.list_channels()[0];
760         assert pre_funding_chan.get_funding_txo() == null;
761
762         Event[] events = peer1.get_manager_events(1, peer1, peer2);
763         assert events[0] instanceof Event.FundingGenerationReady;
764         assert ((Event.FundingGenerationReady) events[0]).channel_value_satoshis == 100000;
765         assert ((Event.FundingGenerationReady) events[0]).user_channel_id == 42;
766         byte[] funding_spk = ((Event.FundingGenerationReady) events[0]).output_script;
767         assert funding_spk.length == 34 && funding_spk[0] == 0 && funding_spk[1] == 32; // P2WSH
768         byte[] chan_id = ((Event.FundingGenerationReady) events[0]).temporary_channel_id;
769
770         NetworkParameters bitcoinj_net = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
771
772         Transaction funding = new Transaction(bitcoinj_net);
773         funding.addInput(new TransactionInput(bitcoinj_net, funding, new byte[0]));
774         funding.getInputs().get(0).setWitness(new TransactionWitness(2)); // Make sure we don't complain about lack of witness
775         funding.getInput(0).getWitness().setPush(0, new byte[]{0x1});
776         funding.addOutput(Coin.SATOSHI.multiply(100000), new Script(funding_spk));
777         Result_NoneAPIErrorZ funding_res = peer1.chan_manager.funding_transaction_generated(chan_id, funding.bitcoinSerialize());
778         assert funding_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
779
780         maybe_exchange_peer_messages(peer1, peer2);
781         synchronized (peer1.broadcast_set) {
782             while (peer1.broadcast_set.size() != 1) {
783                 peer1.broadcast_set.wait();
784             }
785         }
786
787         assert peer1.broadcast_set.size() == 1;
788         assert Arrays.equals(peer1.broadcast_set.get(0), funding.bitcoinSerialize());
789         peer1.broadcast_set.clear();
790
791         Block b = new Block(bitcoinj_net, 2, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 42, 0, 0, Arrays.asList(new Transaction[]{funding}));
792         peer1.connect_block(b, 1, 0);
793         peer2.connect_block(b, 1, 0);
794
795         for (int height = 2; height < 10; height++) {
796             b = new Block(bitcoinj_net, 2, b.getHash(), Sha256Hash.ZERO_HASH, 42, 0, 0, Arrays.asList(new Transaction[0]));
797             peer1.connect_block(b, height, 0);
798             peer2.connect_block(b, height, 0);
799         }
800
801         maybe_exchange_peer_messages(peer1, peer2);
802         while (peer1.chan_manager.list_usable_channels().length != 1 || peer2.chan_manager.list_usable_channels().length != 1)
803
804         peer1.chan_manager.list_channels();
805         ChannelDetails[] peer1_chans = peer1.chan_manager.list_usable_channels();
806         ChannelDetails[] peer2_chans = peer2.chan_manager.list_usable_channels();
807         assert peer1_chans.length == 1;
808         assert peer2_chans.length == 1;
809         assert peer1_chans[0].get_channel_value_satoshis() == 100000;
810         assert peer1_chans[0].get_is_usable();
811         Option_u64Z short_chan_id = peer1_chans[0].get_short_channel_id();
812         assert short_chan_id instanceof Option_u64Z.Some;
813         assert ((Option_u64Z.Some)short_chan_id).some == (1L << 40); // 0th output in the 0th transaction in the 1st block
814         assert Arrays.equals(peer1_chans[0].get_channel_id(), funding.getTxId().getReversedBytes());
815         assert Arrays.equals(peer2_chans[0].get_channel_id(), funding.getTxId().getReversedBytes());
816
817         Result_InvoiceSignOrCreationErrorZ invoice = UtilMethods.create_invoice_from_channelmanager(peer2.chan_manager, peer2.keys_interface, Currency.LDKCurrency_Bitcoin, Option_u64Z.some(10000000), "Invoice Description");
818         assert invoice instanceof Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK;
819         System.out.println("Got invoice: " + ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
820
821         Result_InvoiceNoneZ parsed_invoice = Invoice.from_str(((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
822         assert parsed_invoice instanceof Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK;
823         assert Arrays.equals(((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res.payment_hash(), ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash());
824         SignedRawInvoice signed_raw = ((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res.into_signed_raw();
825         RawInvoice raw_invoice = signed_raw.raw_invoice();
826         byte[] desc_hash = raw_invoice.hash();
827         Description raw_invoice_description = raw_invoice.description();
828         String description_string = raw_invoice_description.into_inner();
829         assert description_string.equals("Invoice Description");
830
831         if (!use_invoice_payer) {
832             byte[] payment_hash = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash();
833             byte[] payment_secret = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_secret();
834             byte[] dest_node_id = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.recover_payee_pub_key();
835             assert Arrays.equals(dest_node_id, peer2.node_id);
836             InvoiceFeatures invoice_features = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.features();
837             RouteHint[] route_hints = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.route_hints();
838
839             PaymentParameters payee = PaymentParameters.of(peer2.node_id, invoice_features, route_hints, Option_u64Z.none(), 6*24*14);
840             RouteParameters route_params = RouteParameters.of(payee, 10000000, 42);
841             Result_RouteLightningErrorZ route_res = UtilMethods.find_route(
842                     peer1.chan_manager.get_our_node_id(), route_params, peer1.router,
843                     peer1_chans, peer1.logger, Scorer.with_default().as_Score());
844             assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
845             Route route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
846             assert route.get_paths().length == 1;
847             assert route.get_paths()[0].length == 1;
848             assert route.get_paths()[0][0].get_fee_msat() == 10000000;
849
850             Result_PaymentIdPaymentSendFailureZ payment_res = peer1.chan_manager.send_payment(route, payment_hash, payment_secret);
851             assert payment_res instanceof Result_PaymentIdPaymentSendFailureZ.Result_PaymentIdPaymentSendFailureZ_OK;
852
853             RouteHop[][] hops = new RouteHop[1][1];
854             byte[] hop_pubkey = new byte[33];
855             hop_pubkey[0] = 3;
856             hop_pubkey[1] = 42;
857             hops[0][0] = RouteHop.of(hop_pubkey, NodeFeatures.known(), 42, ChannelFeatures.known(), 100, 0);
858             Route r2 = Route.of(hops, payee);
859             payment_res = peer1.chan_manager.send_payment(r2, payment_hash, payment_secret);
860             assert payment_res instanceof Result_PaymentIdPaymentSendFailureZ.Result_PaymentIdPaymentSendFailureZ_Err;
861         } else {
862             Result_PaymentIdPaymentErrorZ send_res = peer1.payer.pay_invoice(((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res);
863             assert send_res instanceof Result_PaymentIdPaymentErrorZ.Result_PaymentIdPaymentErrorZ_OK;
864         }
865
866         if (reload_peers) {
867             if (use_chan_manager_constructor) {
868                 peer1.constructor.interrupt();
869                 peer2.constructor.interrupt();
870             } else if (use_nio_peer_handler) {
871                 peer1.nio_peer_handler.interrupt();
872                 peer2.nio_peer_handler.interrupt();
873             }
874             WeakReference<Peer> op1 = new WeakReference<Peer>(peer1);
875             peer1 = new Peer(peer1);
876             peer2 = new Peer(peer2);
877             return new TestState(op1, peer1, peer2, b.getHash());
878         }
879         return new TestState(null, peer1, peer2, b.getHash());
880     }
881
882     boolean cross_reload_ref_pollution = false;
883     class TestState {
884         private final WeakReference<Peer> ref_block;
885         private final Peer peer1;
886         private final Peer peer2;
887         public Sha256Hash best_blockhash;
888
889         public TestState(WeakReference<Peer> ref_block, Peer peer1, Peer peer2, Sha256Hash best_blockhash) {
890             this.ref_block = ref_block;
891             this.peer1 = peer1;
892             this.peer2 = peer2;
893             this.best_blockhash = best_blockhash;
894         }
895     }
896     void do_test_message_handler_b(TestState state) throws InterruptedException {
897         GcCheck obj = new GcCheck();
898         if (state.ref_block != null) {
899             // Ensure the original peers get freed before we move on. Note that we have to be in a different function
900             // scope to do so as the (at least current OpenJDK) JRE won't release anything created in the same scope.
901             while (!cross_reload_ref_pollution && state.ref_block.get() != null) {
902                 System.gc();
903                 System.runFinalization();
904             }
905             connect_peers(state.peer1, state.peer2);
906         }
907
908         Event[] events = state.peer2.get_manager_events(1, state.peer1, state.peer2);
909         assert events[0] instanceof Event.PendingHTLCsForwardable;
910         state.peer2.chan_manager.process_pending_htlc_forwards();
911
912         events = state.peer2.get_manager_events(1, state.peer1, state.peer2);
913         assert events[0] instanceof Event.PaymentReceived;
914         assert ((Event.PaymentReceived)events[0]).purpose instanceof PaymentPurpose.InvoicePayment;
915         byte[] payment_preimage = ((PaymentPurpose.InvoicePayment)((Event.PaymentReceived)events[0]).purpose).payment_preimage;
916         assert !Arrays.equals(payment_preimage, new byte[32]);
917         state.peer2.chan_manager.claim_funds(payment_preimage);
918
919         events = state.peer1.get_manager_events(2, state.peer1, state.peer2);
920         assert events[0] instanceof Event.PaymentSent;
921         assert Arrays.equals(((Event.PaymentSent) events[0]).payment_preimage, payment_preimage);
922         assert events[1] instanceof Event.PaymentPathSuccessful;
923         assert Arrays.equals(((Event.PaymentPathSuccessful) events[1]).payment_hash, ((Event.PaymentSent) events[0]).payment_hash);
924
925         if (use_nio_peer_handler) {
926             // We receive PaymentSent immediately upon receipt of the payment preimage, but we expect to not have an
927             // HTLC transaction to broadcast below, which requires a bit more time to fully complete the
928             // commitment-transaction-update dance between both peers.
929             Thread.sleep(100);
930         }
931
932         if (state.peer1.chain_monitor != null) {
933             Balance[] peer1_balances = state.peer1.chain_monitor.get_claimable_balances(state.peer1.chan_manager.list_channels());
934             assert peer1_balances.length == 0;
935             Balance[] peer2_balances = state.peer2.chain_monitor.get_claimable_balances(state.peer2.chan_manager.list_channels());
936             assert peer2_balances.length == 0;
937         }
938
939         ChannelDetails[] peer1_chans = state.peer1.chan_manager.list_channels();
940
941         if (nice_close) {
942             Result_NoneAPIErrorZ close_res = state.peer1.chan_manager.close_channel(peer1_chans[0].get_channel_id());
943             assert close_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
944             maybe_exchange_peer_messages(state.peer1, state.peer2);
945             synchronized (state.peer1.broadcast_set) {
946                 while (state.peer1.broadcast_set.size() != 1) state.peer1.broadcast_set.wait();
947             }
948             synchronized (state.peer2.broadcast_set) {
949                 while (state.peer2.broadcast_set.size() != 1) state.peer2.broadcast_set.wait();
950             }
951
952             assert state.peer1.broadcast_set.size() == 1;
953             assert state.peer2.broadcast_set.size() == 1;
954         } else {
955             state.peer1.chan_manager.force_close_all_channels();
956             maybe_exchange_peer_messages(state.peer1, state.peer2);
957             synchronized (state.peer1.broadcast_set) {
958                 while (state.peer1.broadcast_set.size() != 1) state.peer1.broadcast_set.wait();
959             }
960             synchronized (state.peer2.broadcast_set) {
961                 while (state.peer2.broadcast_set.size() != 1) state.peer2.broadcast_set.wait();
962             }
963
964             assert state.peer1.broadcast_set.size() == 1;
965             assert state.peer2.broadcast_set.size() == 1;
966         }
967
968         events = state.peer2.get_manager_events(1, state.peer1, state.peer2);
969         assert events[0] instanceof Event.ChannelClosed;
970         events = state.peer1.get_manager_events(1, state.peer1, state.peer2);
971         assert events[0] instanceof Event.ChannelClosed;
972
973         if (state.peer1.chain_monitor != null) {
974             Balance[] peer1_balances = state.peer1.chain_monitor.get_claimable_balances(state.peer1.chan_manager.list_channels());
975             assert peer1_balances.length == 1;
976             for (Balance bal : peer1_balances) {
977                 assert bal instanceof Balance.ClaimableOnChannelClose;
978                 long expected_tx_fee = 183;
979                 assert ((Balance.ClaimableOnChannelClose) bal).claimable_amount_satoshis == 100000 - 1 - 10000 - expected_tx_fee;
980             }
981             Balance[] peer2_balances = state.peer2.chain_monitor.get_claimable_balances(state.peer2.chan_manager.list_channels());
982             assert peer2_balances.length == 1;
983             for (Balance bal : peer2_balances) {
984                 assert bal instanceof Balance.ClaimableOnChannelClose;
985                 assert ((Balance.ClaimableOnChannelClose) bal).claimable_amount_satoshis == 10000 + 1;
986             }
987         }
988
989         if (!nice_close) {
990             NetworkParameters bitcoinj_net = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
991             Transaction tx = new Transaction(bitcoinj_net, state.peer1.broadcast_set.getFirst());
992             Block b = new Block(bitcoinj_net, 2, state.best_blockhash, Sha256Hash.ZERO_HASH, 42, 0, 0,
993                     Arrays.asList(new Transaction[]{tx}));
994             TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] watch_outputs = state.peer2.connect_block(b, 10, 1);
995             if (watch_outputs != null) { // We only process watch_outputs manually when we use a manually-build Watch impl
996                 assert watch_outputs.length == 1;
997                 assert Arrays.equals(watch_outputs[0].get_a(), tx.getTxId().getReversedBytes());
998                 assert watch_outputs[0].get_b().length == 2;
999                 assert watch_outputs[0].get_b()[0].get_a() == 0;
1000                 assert watch_outputs[0].get_b()[1].get_a() == 1;
1001             }
1002
1003             for (int i = 11; i < 21; i++) {
1004                 b = new Block(bitcoinj_net, 2, b.getHash(), Sha256Hash.ZERO_HASH, 42, 0, 0, new ArrayList<>());
1005                 state.peer2.connect_block(b, i, 0);
1006             }
1007
1008             Event[] broadcastable_event = state.peer2.get_monitor_events(1);
1009             for (ChannelMonitor mon : state.peer2.monitors.values()) {
1010                 // This used to be buggy and double-free, so go ahead and fetch them!
1011                 byte[][] txn = mon.get_latest_holder_commitment_txn(state.peer2.logger);
1012             }
1013             assert broadcastable_event.length == 1;
1014             assert broadcastable_event[0] instanceof Event.SpendableOutputs;
1015             if (state.peer2.explicit_keys_manager != null) {
1016                 TxOut[] additional_outputs = new TxOut[]{new TxOut(420, new byte[]{0x42})};
1017                 Result_TransactionNoneZ tx_res = state.peer2.explicit_keys_manager.spend_spendable_outputs(((Event.SpendableOutputs) broadcastable_event[0]).outputs, additional_outputs, new byte[]{0x00}, 253);
1018                 assert tx_res instanceof Result_TransactionNoneZ.Result_TransactionNoneZ_OK;
1019                 Transaction built_tx = new Transaction(bitcoinj_net, ((Result_TransactionNoneZ.Result_TransactionNoneZ_OK) tx_res).res);
1020                 assert built_tx.getOutputs().size() == 2;
1021                 assert Arrays.equals(built_tx.getOutput(1).getScriptBytes(), new byte[]{0x00});
1022                 assert Arrays.equals(built_tx.getOutput(0).getScriptBytes(), new byte[]{0x42});
1023                 assert built_tx.getOutput(0).getValue().value == 420;
1024             }
1025         }
1026
1027         // Test exchanging a custom message (note that ChannelManagerConstructor) always loads an IgnorimgMessageHandler
1028         // so we cannot exchange custom messages with it
1029         if (!use_chan_manager_constructor) {
1030             byte[] custom_message_bytes = new byte[]{0x42, 0x44, 0x43, 0x00};
1031             state.peer1.custom_messages_to_send.add(custom_message_bytes);
1032             state.peer1.peer_manager.process_events();
1033             synchronized (state.peer2.received_custom_messages) {
1034                 while (true) {
1035                     if (state.peer2.received_custom_messages.isEmpty()) {
1036                         state.peer2.received_custom_messages.wait();
1037                         continue;
1038                     }
1039                     assert state.peer2.received_custom_messages.size() == 1;
1040                     assert Arrays.equals(state.peer2.received_custom_messages.get(0), custom_message_bytes);
1041                     break;
1042                 }
1043             }
1044         }
1045
1046         if (use_nio_peer_handler) {
1047             state.peer1.peer_manager.disconnect_by_node_id(state.peer2.chan_manager.get_our_node_id(), false);
1048             while (state.peer1.peer_manager.get_peer_node_ids().length != 0) Thread.yield();
1049             while (state.peer2.peer_manager.get_peer_node_ids().length != 0) Thread.yield();
1050             state.peer1.nio_peer_handler.interrupt();
1051             state.peer2.nio_peer_handler.interrupt();
1052         }
1053
1054         state.peer1.get_monitor_events(0);
1055         state.peer2.get_monitor_events(0);
1056
1057         if (use_chan_manager_constructor) {
1058             state.peer1.constructor.interrupt();
1059             state.peer2.constructor.interrupt();
1060         }
1061
1062         t.interrupt();
1063
1064         state.peer1.router.write();
1065
1066         // Construct the only Option_Enum::Variant(OpaqueStruct) we have in the codebase as this used to cause double-frees:
1067         byte[] serd = new byte[] {(byte)0xd9,(byte)0x77,(byte)0xcb,(byte)0x9b,(byte)0x53,(byte)0xd9,(byte)0x3a,(byte)0x6f,(byte)0xf6,(byte)0x4b,(byte)0xb5,(byte)0xf1,(byte)0xe1,(byte)0x58,(byte)0xb4,(byte)0x09,(byte)0x4b,(byte)0x66,(byte)0xe7,(byte)0x98,(byte)0xfb,(byte)0x12,(byte)0x91,(byte)0x11,(byte)0x68,(byte)0xa3,(byte)0xcc,(byte)0xdf,(byte)0x80,(byte)0xa8,(byte)0x30,(byte)0x96,(byte)0x34,(byte)0x0a,(byte)0x6a,(byte)0x95,(byte)0xda,(byte)0x0a,(byte)0xe8,(byte)0xd9,(byte)0xf7,(byte)0x76,(byte)0x52,(byte)0x8e,(byte)0xec,(byte)0xdb,(byte)0xb7,(byte)0x47,(byte)0xeb,(byte)0x6b,(byte)0x54,(byte)0x54,(byte)0x95,(byte)0xa4,(byte)0x31,(byte)0x9e,(byte)0xd5,(byte)0x37,(byte)0x8e,(byte)0x35,(byte)0xb2,(byte)0x1e,(byte)0x07,(byte)0x3a,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x19,(byte)0xd6,(byte)0x68,(byte)0x9c,(byte)0x08,(byte)0x5a,(byte)0xe1,(byte)0x65,(byte)0x83,(byte)0x1e,(byte)0x93,(byte)0x4f,(byte)0xf7,(byte)0x63,(byte)0xae,(byte)0x46,(byte)0xa2,(byte)0xa6,(byte)0xc1,(byte)0x72,(byte)0xb3,(byte)0xf1,(byte)0xb6,(byte)0x0a,(byte)0x8c,(byte)0xe2,(byte)0x6f,(byte)0x00,(byte)0x08,(byte)0x3a,(byte)0x84,(byte)0x00,(byte)0x00,(byte)0x03,(byte)0x4d,(byte)0x01,(byte)0x34,(byte)0x13,(byte)0xa7,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x90,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x0f,(byte)0x42,(byte)0x40,(byte)0x00,(byte)0x00,(byte)0x27,(byte)0x10,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x14,};
1068         Result_ChannelUpdateDecodeErrorZ upd_msg = ChannelUpdate.read(serd);
1069         assert upd_msg instanceof Result_ChannelUpdateDecodeErrorZ.Result_ChannelUpdateDecodeErrorZ_OK;
1070         Option_NetworkUpdateZ upd = Option_NetworkUpdateZ.some(NetworkUpdate.channel_update_message(((Result_ChannelUpdateDecodeErrorZ.Result_ChannelUpdateDecodeErrorZ_OK) upd_msg).res));
1071     }
1072
1073     java.util.LinkedList<WeakReference<Object>> must_free_objs = new java.util.LinkedList();
1074     int gc_count = 0;
1075     int gc_exp_count = 0;
1076     class GcCheck {
1077         GcCheck() { gc_exp_count += 1; }
1078         @Override
1079         protected void finalize() throws Throwable {
1080             gc_count += 1;
1081             super.finalize();
1082         }
1083     }
1084 }
1085 public class HumanObjectPeerTest {
1086     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_ignoring_routing_handler, boolean use_chan_manager_constructor, boolean use_invoice_payer) throws InterruptedException {
1087         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_ignoring_routing_handler, use_chan_manager_constructor, use_invoice_payer);
1088         HumanObjectPeerTestInstance.TestState state = instance.do_test_message_handler();
1089         instance.do_test_message_handler_b(state);
1090         return instance;
1091     }
1092     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_ignoring_routing_handler, boolean use_chan_manager_constructor, boolean use_invoice_payer) throws InterruptedException {
1093         HumanObjectPeerTestInstance instance = do_test_run(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_peer_refs, nio_peer_handler, use_ignoring_routing_handler, use_chan_manager_constructor, use_invoice_payer);
1094         while (instance.gc_count != instance.gc_exp_count) {
1095             System.gc();
1096             System.runFinalization();
1097         }
1098         for (WeakReference<Object> o : instance.must_free_objs)
1099             assert o.get() == null;
1100     }
1101     @Test
1102     public void test_message_handler() throws InterruptedException {
1103         Thread gc_thread = new Thread(() -> {
1104             while (true) {
1105                 System.gc();
1106                 System.runFinalization();
1107                 try { Thread.sleep(0, 1); } catch (InterruptedException e) { break; }
1108             }
1109         }, "Test GC Thread");
1110         gc_thread.start();
1111         for (int i = 0; i < (1 << 9) - 1; i++) {
1112             boolean nice_close =                   (i & (1 << 0)) != 0;
1113             boolean use_km_wrapper =               (i & (1 << 1)) != 0;
1114             boolean use_manual_watch =             (i & (1 << 2)) != 0;
1115             boolean reload_peers =                 (i & (1 << 3)) != 0;
1116             boolean break_cross_refs =             (i & (1 << 4)) != 0;
1117             boolean use_ignoring_routing_handler = (i & (1 << 5)) != 0;
1118             boolean use_chan_manager_constructor = (i & (1 << 6)) != 0;
1119             boolean use_invoice_payer =            (i & (1 << 7)) != 0;
1120             boolean nio_peer_handler =             (i & (1 << 8)) != 0;
1121             if (break_cross_refs && !reload_peers) {
1122                 // There are no cross refs to break without reloading peers.
1123                 continue;
1124             }
1125             if (use_chan_manager_constructor && (use_manual_watch || !nio_peer_handler)) {
1126                 // ChannelManagerConstructor requires a ChainMonitor as the Watch and creates a NioPeerHandler for us.
1127                 continue;
1128             }
1129             if (!use_chan_manager_constructor && (use_ignoring_routing_handler)) {
1130                 // We rely on the ChannelManagerConstructor to convert null into an IgnoringMessageHandler, so don't
1131                 // try to run with an IgnoringMessageHandler unless we're also using a ChannelManagerConstructor.
1132                 continue;
1133             }
1134             if (use_chan_manager_constructor && use_ignoring_routing_handler && use_invoice_payer) {
1135                 // As documented on ChannelManagerConstructor, if we provide a `null` NetworkGraph (because we want to
1136                 // use an IgnoringMessageHandler), no InvoicePayer will be constructored for us, thus we cannot use an
1137                 // InvoicePayer to send payments in this case.
1138                 continue;
1139             }
1140             System.err.println("Running test with flags " + i);
1141             do_test(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_refs, nio_peer_handler, use_ignoring_routing_handler, use_chan_manager_constructor, use_invoice_payer);
1142         }
1143         gc_thread.interrupt();
1144         gc_thread.join();
1145     }
1146
1147     // This is used in the test jar to test the built jar is runnable
1148     public static void main(String[] args) {
1149         try {
1150             new HumanObjectPeerTest().test_message_handler();
1151         } catch (Exception e) {
1152             System.err.println("Caught exception:");
1153             System.err.println(e);
1154             System.exit(1);
1155         }
1156     }
1157 }