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.Currency;
9 import org.ldk.enums.Network;
10 import org.ldk.structs.*;
11 import org.ldk.util.TwoTuple;
13 import java.io.IOException;
14 import java.lang.ref.WeakReference;
15 import java.net.InetSocketAddress;
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_chan_manager_constructor;
28 HumanObjectPeerTestInstance(boolean nice_close, boolean use_km_wrapper, boolean use_manual_watch, boolean reload_peers, boolean break_cross_peer_refs, boolean use_nio_peer_handler, boolean use_filter, boolean use_chan_manager_constructor) {
29 this.nice_close = nice_close;
30 this.use_km_wrapper = use_km_wrapper;
31 this.use_manual_watch = use_manual_watch;
32 this.reload_peers = reload_peers;
33 this.break_cross_peer_refs = break_cross_peer_refs;
34 this.use_nio_peer_handler = use_nio_peer_handler;
35 this.use_filter = use_filter;
36 this.use_chan_manager_constructor = use_chan_manager_constructor;
40 KeysInterface manual_keysif(KeysInterface underlying_if) {
41 return KeysInterface.new_impl(new KeysInterface.KeysInterfaceInterface() {
42 @Override public byte[] get_node_secret() { return underlying_if.get_node_secret(); }
43 @Override public byte[] get_destination_script() { return underlying_if.get_destination_script(); }
44 @Override public byte[] get_shutdown_pubkey() { return underlying_if.get_shutdown_pubkey(); }
47 public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) {
48 Sign underlying_ck = underlying_if.get_channel_signer(inbound, channel_value_satoshis);
49 BaseSign underlying_base = underlying_ck.get_base_sign();
50 BaseSign.BaseSignInterface bsi = new BaseSign.BaseSignInterface() {
52 public byte[] get_per_commitment_point(long idx) {
53 return underlying_base.get_per_commitment_point(idx);
57 public byte[] release_commitment_secret(long idx) {
58 return underlying_base.release_commitment_secret(idx);
62 public byte[] channel_keys_id() {
67 public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_counterparty_commitment(CommitmentTransaction commitment_tx) {
68 return underlying_base.sign_counterparty_commitment(commitment_tx);
72 public Result_C2Tuple_SignatureCVec_SignatureZZNoneZ sign_holder_commitment_and_htlcs(HolderCommitmentTransaction holder_commitment_tx) {
73 return underlying_base.sign_holder_commitment_and_htlcs(holder_commitment_tx);
77 public Result_SignatureNoneZ sign_justice_revoked_output(byte[] justice_tx, long input, long amount, byte[] per_commitment_key) {
78 return underlying_base.sign_justice_revoked_output(justice_tx, input, amount, per_commitment_key);
82 public Result_SignatureNoneZ sign_justice_revoked_htlc(byte[] justice_tx, long input, long amount, byte[] per_commitment_key, HTLCOutputInCommitment htlc) {
83 return underlying_base.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc);
87 public Result_SignatureNoneZ sign_counterparty_htlc_transaction(byte[] htlc_tx, long input, long amount, byte[] per_commitment_point, HTLCOutputInCommitment htlc) {
88 return underlying_base.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc);
92 public Result_SignatureNoneZ sign_closing_transaction(byte[] closing_tx) {
93 return underlying_base.sign_closing_transaction(closing_tx);
97 public Result_SignatureNoneZ sign_channel_announcement(UnsignedChannelAnnouncement msg) {
98 return underlying_base.sign_channel_announcement(msg);
102 public void ready_channel(ChannelTransactionParameters params) {
103 underlying_base.ready_channel(params);
106 Sign.SignInterface si = new Sign.SignInterface() {
108 public byte[] write() {
109 return underlying_ck.write();
112 Sign resp = Sign.new_impl(si, bsi, underlying_base.get_pubkeys());
113 must_free_objs.add(new WeakReference<>(si));
114 must_free_objs.add(new WeakReference<>(bsi));
115 must_free_objs.add(new WeakReference<>(resp));
116 must_free_objs.add(new WeakReference<>(underlying_base));
117 must_free_objs.add(new WeakReference<>(underlying_ck));
122 public byte[] get_secure_random_bytes() {
123 return underlying_if.get_secure_random_bytes();
127 public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) {
128 return underlying_if.read_chan_signer(reader);
132 public Result_RecoverableSignatureNoneZ sign_invoice(byte[] invoice_preimage) {
133 return underlying_if.sign_invoice(invoice_preimage);
138 Watch get_manual_watch() {
139 Watch.WatchInterface watch_impl = new Watch.WatchInterface() {
140 public Result_NoneChannelMonitorUpdateErrZ watch_channel(OutPoint funding_txo, ChannelMonitor monitor) {
141 synchronized (monitors) {
142 assert monitors.put(Arrays.toString(funding_txo.get_txid()), monitor) == null;
144 return Result_NoneChannelMonitorUpdateErrZ.ok();
147 public Result_NoneChannelMonitorUpdateErrZ update_channel(OutPoint funding_txo, ChannelMonitorUpdate update) {
148 synchronized (monitors) {
149 String txid = Arrays.toString(funding_txo.get_txid());
150 assert monitors.containsKey(txid);
151 Result_NoneMonitorUpdateErrorZ update_res = monitors.get(txid).update_monitor(update, tx_broadcaster, fee_estimator, logger);
152 assert update_res instanceof Result_NoneMonitorUpdateErrorZ.Result_NoneMonitorUpdateErrorZ_OK;
154 return Result_NoneChannelMonitorUpdateErrZ.ok();
158 public MonitorEvent[] release_pending_monitor_events() {
159 synchronized (monitors) {
160 assert monitors.size() <= 1;
161 for (ChannelMonitor mon : monitors.values()) {
162 return mon.get_and_clear_pending_monitor_events();
165 return new MonitorEvent[0];
168 Watch watch = Watch.new_impl(watch_impl);
169 must_free_objs.add(new WeakReference<>(watch_impl));
170 must_free_objs.add(new WeakReference<>(watch));
174 NioPeerHandler nio_peer_handler;
178 final FeeEstimator fee_estimator;
179 final BroadcasterInterface tx_broadcaster;
180 final KeysManager explicit_keys_manager;
181 final KeysInterface keys_interface;
182 final ChainMonitor chain_monitor;
183 final NetGraphMsgHandler router;
184 final Watch chain_watch;
185 final HashSet<String> filter_additions;
187 ChannelManager chan_manager;
188 PeerManager peer_manager;
189 final HashMap<String, ChannelMonitor> monitors; // Wow I forgot just how terrible Java is - we can't put a byte array here.
191 final LinkedList<byte[]> broadcast_set = new LinkedList<>();
192 final LinkedList<Event> pending_manager_events = new LinkedList<>();
193 ChannelManagerConstructor constructor = null;
194 GcCheck obj = new GcCheck();
196 private TwoTuple<OutPoint, byte[]> test_mon_roundtrip(ChannelMonitor mon) {
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.BlockHashChannelMonitorZ_read(mon.write(), keys_interface);
201 assert roundtrip_monitor instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
202 TwoTuple<OutPoint, byte[]> funding_txo = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) roundtrip_monitor).res.b.get_funding_txo();
203 System.gc(); System.runFinalization(); // Give the GC a chance to run.
207 private Peer(Object _dummy, byte seed) {
208 logger = Logger.new_impl((String arg) -> System.out.println(seed + ": " + arg));
209 fee_estimator = FeeEstimator.new_impl((confirmation_target -> 253));
210 tx_broadcaster = BroadcasterInterface.new_impl(tx -> {
211 synchronized (broadcast_set) {
212 broadcast_set.add(tx);
213 broadcast_set.notifyAll();
216 monitors = new HashMap<>();
218 Persist persister = Persist.new_impl(new Persist.PersistInterface() {
220 public Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint id, ChannelMonitor data) {
221 synchronized (monitors) {
222 String key = Arrays.toString(id.to_channel_id());
223 assert monitors.put(key, data) == null;
224 TwoTuple<OutPoint, byte[]> res = test_mon_roundtrip(data);
225 assert Arrays.equals(res.a.get_txid(), id.get_txid());
226 assert res.a.get_index() == id.get_index();
228 return Result_NoneChannelMonitorUpdateErrZ.ok();
232 public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id, ChannelMonitorUpdate update, ChannelMonitor data) {
233 synchronized (monitors) {
234 String key = Arrays.toString(id.to_channel_id());
235 assert monitors.put(key, data) != null;
236 TwoTuple<OutPoint, byte[]> res = test_mon_roundtrip(data);
237 assert Arrays.equals(res.a.get_txid(), id.get_txid());
238 assert res.a.get_index() == id.get_index();
240 return Result_NoneChannelMonitorUpdateErrZ.ok();
244 filter_additions = new HashSet<>();
246 this.filter = Filter.new_impl(new Filter.FilterInterface() {
247 @Override public void register_tx(byte[] txid, byte[] script_pubkey) {
248 filter_additions.add(Arrays.toString(txid));
250 @Override public Option_C2Tuple_usizeTransactionZZ register_output(WatchedOutput output) {
251 filter_additions.add(Arrays.toString(output.get_outpoint().get_txid()) + ":" + output.get_outpoint().get_index());
252 return Option_C2Tuple_usizeTransactionZZ.none();
259 if (use_manual_watch) {
260 chain_watch = get_manual_watch();
261 chain_monitor = null;
263 chain_monitor = ChainMonitor.of(filter, tx_broadcaster, logger, fee_estimator, persister);
264 chain_watch = chain_monitor.as_Watch();
267 byte[] key_seed = new byte[32];
268 for (byte i = 0; i < 32; i++) {
269 key_seed[i] = (byte) (i ^ seed);
271 KeysManager keys = KeysManager.of(key_seed, System.currentTimeMillis() / 1000, (int) (System.currentTimeMillis() * 1000));
272 if (use_km_wrapper) {
273 this.keys_interface = manual_keysif(keys.as_KeysInterface());
274 this.explicit_keys_manager = null;
276 this.keys_interface = keys.as_KeysInterface();
277 this.explicit_keys_manager = keys;
279 this.router = NetGraphMsgHandler.of(new byte[32], null, logger);
281 private void bind_nio() {
282 if (!use_nio_peer_handler) return;
283 if (use_chan_manager_constructor) {
284 this.nio_peer_handler = this.constructor.nio_peer_handler;
286 try { this.nio_peer_handler = new NioPeerHandler(peer_manager); } catch (IOException e) { assert false; }
288 for (short i = 10_000; true; i++) {
290 nio_peer_handler.bind_listener(new InetSocketAddress("127.0.0.1", i));
293 } catch (IOException e) { assert i < 10_500; }
298 if (use_chan_manager_constructor) {
299 this.constructor = new ChannelManagerConstructor(Network.LDKNetwork_Bitcoin, UserConfig.with_default(), new byte[32], 0,
300 this.keys_interface, this.fee_estimator, this.chain_monitor, this.router, this.tx_broadcaster, this.logger);
301 constructor.chain_sync_completed(new ChannelManagerConstructor.ChannelManagerPersister() {
302 @Override public void handle_event(Event event) {
303 synchronized (pending_manager_events) {
304 pending_manager_events.add(event);
305 pending_manager_events.notifyAll();
308 @Override public void persist_manager(byte[] channel_manager_bytes) { assert channel_manager_bytes.length > 1; }
310 this.chan_manager = constructor.channel_manager;
311 this.peer_manager = constructor.peer_manager;
312 must_free_objs.add(new WeakReference<>(this.chan_manager));
314 ChainParameters params = ChainParameters.of(Network.LDKNetwork_Bitcoin, BestBlock.of(new byte[32], 0));
315 this.chan_manager = ChannelManager.of(this.fee_estimator, chain_watch, tx_broadcaster, logger, this.keys_interface, UserConfig.with_default(), params);
316 byte[] random_data = keys_interface.get_secure_random_bytes();
317 this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
320 this.node_id = chan_manager.get_our_node_id();
327 this(null, orig.seed);
328 if (use_chan_manager_constructor) {
329 byte[][] monitors = {orig.monitors.values().stream().iterator().next().write()};
330 byte[] serialized = orig.chan_manager.write();
332 this.constructor = new ChannelManagerConstructor(serialized, monitors, this.keys_interface,
333 this.fee_estimator, this.chain_monitor, this.filter, this.router, this.tx_broadcaster, this.logger);
334 constructor.chain_sync_completed(new ChannelManagerConstructor.ChannelManagerPersister() {
335 @Override public void handle_event(Event event) {
336 synchronized (pending_manager_events) {
337 pending_manager_events.add(event);
338 pending_manager_events.notifyAll();
341 @Override public void persist_manager(byte[] channel_manager_bytes) { assert channel_manager_bytes.length > 1; }
343 this.chan_manager = constructor.channel_manager;
344 this.peer_manager = constructor.peer_manager;
345 must_free_objs.add(new WeakReference<>(this.chan_manager));
346 // If we are using a ChannelManagerConstructor, we may have pending events waiting on the old peer
347 // which have been removed from the ChannelManager but which we still need to handle.
348 this.pending_manager_events.addAll(orig.pending_manager_events);
349 if (!this.pending_manager_events.isEmpty()) {
350 // However, this implies cross_reload_ref_pollution
351 cross_reload_ref_pollution = true;
353 } catch (ChannelManagerConstructor.InvalidSerializedDataException e) {
357 ChannelMonitor[] monitors = new ChannelMonitor[1];
358 assert orig.monitors.size() == 1;
359 if (!break_cross_peer_refs) {
360 monitors[0] = orig.monitors.values().stream().iterator().next();
362 byte[] serialized = orig.monitors.values().stream().iterator().next().write();
363 Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res =
364 UtilMethods.BlockHashChannelMonitorZ_read(serialized, this.keys_interface);
365 assert res instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK;
366 monitors[0] = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) res).res.b;
368 byte[] serialized = orig.chan_manager.write();
369 Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ read_res =
370 UtilMethods.BlockHashChannelManagerZ_read(serialized, this.keys_interface, this.fee_estimator, this.chain_watch, this.tx_broadcaster, this.logger, UserConfig.with_default(), monitors);
371 assert read_res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK;
372 this.chan_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.b;
373 this.chain_watch.watch_channel(monitors[0].get_funding_txo().a, monitors[0]);
374 byte[] random_data = keys_interface.get_secure_random_bytes();
375 this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
376 if (!break_cross_peer_refs && (use_manual_watch || use_km_wrapper)) {
377 // When we pass monitors[0] into chain_watch.watch_channel we create a reference from the new Peer to a
378 // field in the old peer, preventing freeing of the original Peer until the new Peer is freed. Thus, we
379 // shouldn't bother waiting for the original to be freed later on.
380 cross_reload_ref_pollution = true;
383 this.node_id = chan_manager.get_our_node_id();
386 if (cross_reload_ref_pollution) {
387 // This really, really needs to be handled at the bindings layer, but its rather complicated -
388 // ChannelSigners can be cloned and passed around without java being involved, resulting in them being
389 // owned by both one or more ChannelMonitors and a ChannelManager, with only one having proper pointers
390 // to the ChannelSigner. Ideally, the ChannelSigner would have a global reference to the Java
391 // implementation class, but that results in circular references. Instead, we need some ability to,
392 // while cloning ChannelSigners, add new references in the calling Java struct (ie ChannelMonitor) to
393 // the ChannelSigner.
394 this.ptr_to = orig.chan_manager;
398 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] connect_block(Block b, int height, long expected_monitor_update_len) {
399 byte[] header = Arrays.copyOfRange(b.bitcoinSerialize(), 0, 80);
400 TwoTuple<Long, byte[]>[] txn;
401 if (b.hasTransactions()) {
402 assert b.getTransactions().size() == 1;
403 TwoTuple<Long, byte[]> txp = new TwoTuple<>((long) 0, b.getTransactions().get(0).bitcoinSerialize());
404 txn = new TwoTuple[]{txp};
406 txn = new TwoTuple[0];
407 if (chain_monitor != null) {
408 chan_manager.as_Listen().block_connected(b.bitcoinSerialize(), height);
409 chain_monitor.as_Listen().block_connected(b.bitcoinSerialize(), height);
411 chan_manager.as_Confirm().transactions_confirmed(header, txn, height);
412 chan_manager.as_Confirm().best_block_updated(header, height);
413 // Connect manually if we aren't using a ChainMonitor and are implementing Watch ourselves
414 synchronized (monitors) {
415 assert monitors.size() == 1;
416 for (ChannelMonitor mon : monitors.values()) {
417 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] ret = mon.block_connected(header, txn, height, tx_broadcaster, fee_estimator, logger);
418 assert ret.length == expected_monitor_update_len;
426 Event[] get_monitor_events(int expected_len) {
427 if (use_chan_manager_constructor) {
429 synchronized (this.pending_manager_events) {
430 if (expected_len != 0 && this.pending_manager_events.size() == expected_len) {
434 if (expected_len == 0) {
435 try { Thread.sleep(500); } catch (InterruptedException e) { assert false; }
441 synchronized (this.pending_manager_events) {
442 Event[] res = this.pending_manager_events.toArray(new Event[0]);
443 this.pending_manager_events.clear();
444 assert res.length == expected_len;
447 } else if (chain_monitor != null) {
448 ArrayList<Event> l = new ArrayList<Event>();
449 chain_monitor.as_EventsProvider().process_pending_events(EventHandler.new_impl(l::add));
450 assert l.size() == expected_len;
451 return l.toArray(new Event[0]);
453 synchronized (monitors) {
454 assert monitors.size() == 1;
455 for (ChannelMonitor mon : monitors.values()) {
456 Event[] res = mon.get_and_clear_pending_events();
457 assert res.length == expected_len;
465 Event[] get_manager_events(int expected_len, Peer peer1, Peer peer2) {
466 assert expected_len != 0;
467 if (!use_nio_peer_handler) {
468 maybe_exchange_peer_messages(peer1, peer2);
470 Event[] res = new Event[0];
471 if (use_chan_manager_constructor) {
472 while (res.length < expected_len) {
473 synchronized (this.pending_manager_events) {
474 res = this.pending_manager_events.toArray(res);
475 assert res.length == expected_len || res.length == 0; // We don't handle partial results
476 this.pending_manager_events.clear();
477 if (res.length < expected_len) {
478 try { this.pending_manager_events.wait(); } catch (InterruptedException e) { assert false; }
483 ArrayList<Event> l = new ArrayList<Event>();
484 while (l.size() < expected_len) {
486 if (use_nio_peer_handler) {
487 peer1.nio_peer_handler.check_events();
488 peer2.nio_peer_handler.check_events();
490 chan_manager.as_EventsProvider().process_pending_events(EventHandler.new_impl(l::add));
491 assert l.size() == expected_len || l.size() == 0; // We don't handle partial results
493 return l.toArray(new Event[0]);
495 assert res.length == expected_len;
500 static class DescriptorHolder { SocketDescriptor val; }
502 boolean running = false;
503 final LinkedList<Runnable> runqueue = new LinkedList();
505 Thread t = new Thread(() -> {
509 synchronized (runqueue) {
510 while (runqueue.isEmpty()) {
514 r = runqueue.pollFirst();
517 synchronized (runqueue) {
519 runqueue.notifyAll();
521 } catch (InterruptedException e) {
527 void maybe_exchange_peer_messages(Peer peer1, Peer peer2) {
528 if (!use_nio_peer_handler) {
529 synchronized (runqueue) {
533 peer1.peer_manager.process_events();
534 peer2.peer_manager.process_events();
535 synchronized (runqueue) {
536 if (runqueue.isEmpty() && !running) {
542 try { runqueue.wait(); } catch (InterruptedException e) { assert false; }
545 } else if (!use_chan_manager_constructor) {
546 peer1.nio_peer_handler.check_events();
547 peer2.nio_peer_handler.check_events();
550 void do_read_event(PeerManager pm, SocketDescriptor descriptor, byte[] data) {
551 if (!t.isAlive()) t.start();
552 synchronized (runqueue) {
555 Result_boolPeerHandleErrorZ res = pm.read_event(descriptor, data);
556 assert res instanceof Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK;
558 runqueue.notifyAll();
560 must_free_objs.add(new WeakReference<>(data));
563 void connect_peers(final Peer peer1, final Peer peer2) {
564 if (use_nio_peer_handler) {
566 peer1.nio_peer_handler.connect(peer2.chan_manager.get_our_node_id(), new InetSocketAddress("127.0.0.1", peer2.nio_port), 100);
567 } catch (IOException e) { assert false; }
568 while (peer1.peer_manager.get_peer_node_ids().length == 0 || peer2.peer_manager.get_peer_node_ids().length == 0) {
572 DescriptorHolder descriptor1 = new DescriptorHolder();
573 DescriptorHolder descriptor1ref = descriptor1;
574 SocketDescriptor descriptor2 = SocketDescriptor.new_impl(new SocketDescriptor.SocketDescriptorInterface() {
576 public long send_data(byte[] data, boolean resume_read) {
577 do_read_event(peer1.peer_manager, descriptor1ref.val, data);
581 @Override public void disconnect_socket() { assert false; }
582 @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == 2; }
583 @Override public long hash() { return 2; }
586 descriptor1.val = SocketDescriptor.new_impl(new SocketDescriptor.SocketDescriptorInterface() {
588 public long send_data(byte[] data, boolean resume_read) {
589 do_read_event(peer2.peer_manager, descriptor2, data);
593 @Override public void disconnect_socket() { assert false; }
594 @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == 1; }
595 @Override public long hash() { return 1; }
598 Result_CVec_u8ZPeerHandleErrorZ conn_res = peer1.peer_manager.new_outbound_connection(peer2.node_id, descriptor1.val);
599 assert conn_res instanceof Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK;
601 Result_NonePeerHandleErrorZ inbound_conn_res = peer2.peer_manager.new_inbound_connection(descriptor2);
602 assert inbound_conn_res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_OK;
603 do_read_event(peer2.peer_manager, descriptor2, ((Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) conn_res).res);
605 maybe_exchange_peer_messages(peer1, peer2);
609 TestState do_test_message_handler() throws InterruptedException {
610 Peer peer1 = new Peer((byte) 1);
611 Peer peer2 = new Peer((byte) 2);
613 connect_peers(peer1, peer2);
615 Result_NoneAPIErrorZ cc_res = peer1.chan_manager.create_channel(peer2.node_id, 100000, 1000, 42, null);
616 assert cc_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
618 Event[] events = peer1.get_manager_events(1, peer1, peer2);
619 assert events[0] instanceof Event.FundingGenerationReady;
620 assert ((Event.FundingGenerationReady) events[0]).channel_value_satoshis == 100000;
621 assert ((Event.FundingGenerationReady) events[0]).user_channel_id == 42;
622 byte[] funding_spk = ((Event.FundingGenerationReady) events[0]).output_script;
623 assert funding_spk.length == 34 && funding_spk[0] == 0 && funding_spk[1] == 32; // P2WSH
624 byte[] chan_id = ((Event.FundingGenerationReady) events[0]).temporary_channel_id;
626 NetworkParameters bitcoinj_net = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
628 Transaction funding = new Transaction(bitcoinj_net);
629 funding.addInput(new TransactionInput(bitcoinj_net, funding, new byte[0]));
630 funding.getInputs().get(0).setWitness(new TransactionWitness(2)); // Make sure we don't complain about lack of witness
631 funding.getInput(0).getWitness().setPush(0, new byte[]{0x1});
632 funding.addOutput(Coin.SATOSHI.multiply(100000), new Script(funding_spk));
633 Result_NoneAPIErrorZ funding_res = peer1.chan_manager.funding_transaction_generated(chan_id, funding.bitcoinSerialize());
634 assert funding_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
636 maybe_exchange_peer_messages(peer1, peer2);
637 synchronized (peer1.broadcast_set) {
638 while (peer1.broadcast_set.size() != 1) {
639 peer1.broadcast_set.wait();
643 assert peer1.broadcast_set.size() == 1;
644 assert Arrays.equals(peer1.broadcast_set.get(0), funding.bitcoinSerialize());
645 peer1.broadcast_set.clear();
647 Block b = new Block(bitcoinj_net, 2, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 42, 0, 0, Arrays.asList(new Transaction[]{funding}));
648 peer1.connect_block(b, 1, 0);
649 peer2.connect_block(b, 1, 0);
651 for (int height = 2; height < 10; height++) {
652 b = new Block(bitcoinj_net, 2, b.getHash(), Sha256Hash.ZERO_HASH, 42, 0, 0, Arrays.asList(new Transaction[0]));
653 peer1.connect_block(b, height, 0);
654 peer2.connect_block(b, height, 0);
657 maybe_exchange_peer_messages(peer1, peer2);
658 while (peer1.chan_manager.list_usable_channels().length != 1 || peer2.chan_manager.list_usable_channels().length != 1)
660 peer1.chan_manager.list_channels();
661 ChannelDetails[] peer1_chans = peer1.chan_manager.list_usable_channels();
662 ChannelDetails[] peer2_chans = peer2.chan_manager.list_usable_channels();
663 assert peer1_chans.length == 1;
664 assert peer2_chans.length == 1;
665 assert peer1_chans[0].get_channel_value_satoshis() == 100000;
666 assert peer1_chans[0].get_is_usable();
667 Option_u64Z short_chan_id = peer1_chans[0].get_short_channel_id();
668 assert short_chan_id instanceof Option_u64Z.Some;
669 assert ((Option_u64Z.Some)short_chan_id).some == (1L << 40); // 0th output in the 0th transaction in the 1st block
670 assert Arrays.equals(peer1_chans[0].get_channel_id(), funding.getTxId().getReversedBytes());
671 assert Arrays.equals(peer2_chans[0].get_channel_id(), funding.getTxId().getReversedBytes());
673 Result_InvoiceSignOrCreationErrorZ invoice = UtilMethods.create_invoice_from_channelmanager(peer2.chan_manager, peer2.keys_interface, Currency.LDKCurrency_Bitcoin, Option_u64Z.none(), "Invoice Description");
674 assert invoice instanceof Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK;
675 System.out.println("Got invoice: " + ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
677 Result_InvoiceNoneZ parsed_invoice = Invoice.from_str(((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
678 assert parsed_invoice instanceof Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK;
679 assert Arrays.equals(((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res.payment_hash(), ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash());
680 SignedRawInvoice signed_raw = ((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res.into_signed_raw();
681 RawInvoice raw_invoice = signed_raw.raw_invoice();
682 byte[] desc_hash = raw_invoice.hash();
683 Description raw_invoice_description = raw_invoice.description();
684 String description_string = raw_invoice_description.into_inner();
685 assert description_string.equals("Invoice Description");
686 byte[] payment_hash = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash();
687 byte[] payment_secret = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_secret();
688 byte[] dest_node_id = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.recover_payee_pub_key();
689 assert Arrays.equals(dest_node_id, peer2.node_id);
690 InvoiceFeatures invoice_features = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.features();
691 RouteHint[] route_hints = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.route_hints();
694 try (LockedNetworkGraph netgraph = peer1.router.read_locked_graph()) {
695 NetworkGraph graph = netgraph.graph();
696 Result_RouteLightningErrorZ route_res = UtilMethods.get_route(peer1.chan_manager.get_our_node_id(), graph, peer2.node_id, invoice_features, peer1_chans, route_hints, 10000000, 42, peer1.logger);
697 assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
698 route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
701 Result_NonePaymentSendFailureZ payment_res = peer1.chan_manager.send_payment(route, payment_hash, payment_secret);
702 assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_OK;
704 RouteHop[][] hops = new RouteHop[1][1];
705 byte[] hop_pubkey = new byte[33];
708 hops[0][0] = RouteHop.of(hop_pubkey, NodeFeatures.known(), 42, ChannelFeatures.known(), 100, 0);
709 Route r2 = Route.of(hops);
710 payment_res = peer1.chan_manager.send_payment(r2, payment_hash, payment_secret);
711 assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_Err;
714 if (use_chan_manager_constructor) {
715 peer1.constructor.interrupt();
716 peer2.constructor.interrupt();
717 } else if (use_nio_peer_handler) {
718 peer1.nio_peer_handler.interrupt();
719 peer2.nio_peer_handler.interrupt();
721 WeakReference<Peer> op1 = new WeakReference<Peer>(peer1);
722 peer1 = new Peer(peer1);
723 peer2 = new Peer(peer2);
724 return new TestState(op1, peer1, peer2, b.getHash());
726 return new TestState(null, peer1, peer2, b.getHash());
729 boolean cross_reload_ref_pollution = false;
731 private final WeakReference<Peer> ref_block;
732 private final Peer peer1;
733 private final Peer peer2;
734 public Sha256Hash best_blockhash;
736 public TestState(WeakReference<Peer> ref_block, Peer peer1, Peer peer2, Sha256Hash best_blockhash) {
737 this.ref_block = ref_block;
740 this.best_blockhash = best_blockhash;
743 void do_test_message_handler_b(TestState state) throws InterruptedException {
744 GcCheck obj = new GcCheck();
745 if (state.ref_block != null) {
746 // Ensure the original peers get freed before we move on. Note that we have to be in a different function
747 // scope to do so as the (at least current OpenJDK) JRE won't release anything created in the same scope.
748 while (!cross_reload_ref_pollution && state.ref_block.get() != null) {
750 System.runFinalization();
752 connect_peers(state.peer1, state.peer2);
755 Event[] events = state.peer2.get_manager_events(1, state.peer1, state.peer2);
756 assert events[0] instanceof Event.PendingHTLCsForwardable;
757 state.peer2.chan_manager.process_pending_htlc_forwards();
759 events = state.peer2.get_manager_events(1, state.peer1, state.peer2);
760 assert events[0] instanceof Event.PaymentReceived;
761 byte[] payment_preimage = ((Event.PaymentReceived)events[0]).payment_preimage;
762 assert !Arrays.equals(payment_preimage, new byte[32]);
763 state.peer2.chan_manager.claim_funds(payment_preimage);
765 events = state.peer1.get_manager_events(1, state.peer1, state.peer2);
766 assert events[0] instanceof Event.PaymentSent;
767 assert Arrays.equals(((Event.PaymentSent) events[0]).payment_preimage, payment_preimage);
769 if (use_nio_peer_handler) {
770 // We receive PaymentSent immediately upon receipt of the payment preimage, but we expect to not have an
771 // HTLC transaction to broadcast below, which requires a bit more time to fully complete the
772 // commitment-transaction-update dance between both peers.
776 ChannelDetails[] peer1_chans = state.peer1.chan_manager.list_channels();
779 Result_NoneAPIErrorZ close_res = state.peer1.chan_manager.close_channel(peer1_chans[0].get_channel_id());
780 assert close_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
781 maybe_exchange_peer_messages(state.peer1, state.peer2);
782 synchronized (state.peer1.broadcast_set) {
783 while (state.peer1.broadcast_set.size() != 1) state.peer1.broadcast_set.wait();
785 synchronized (state.peer2.broadcast_set) {
786 while (state.peer2.broadcast_set.size() != 1) state.peer2.broadcast_set.wait();
789 assert state.peer1.broadcast_set.size() == 1;
790 assert state.peer2.broadcast_set.size() == 1;
792 state.peer1.chan_manager.force_close_all_channels();
793 maybe_exchange_peer_messages(state.peer1, state.peer2);
794 synchronized (state.peer1.broadcast_set) {
795 while (state.peer1.broadcast_set.size() != 1) state.peer1.broadcast_set.wait();
797 synchronized (state.peer2.broadcast_set) {
798 while (state.peer2.broadcast_set.size() != 1) state.peer2.broadcast_set.wait();
801 assert state.peer1.broadcast_set.size() == 1;
802 assert state.peer2.broadcast_set.size() == 1;
804 NetworkParameters bitcoinj_net = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
805 Transaction tx = new Transaction(bitcoinj_net, state.peer1.broadcast_set.getFirst());
806 Block b = new Block(bitcoinj_net, 2, state.best_blockhash, Sha256Hash.ZERO_HASH, 42, 0, 0,
807 Arrays.asList(new Transaction[]{tx}));
808 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] watch_outputs = state.peer2.connect_block(b, 10, 1);
809 if (watch_outputs != null) { // We only process watch_outputs manually when we use a manually-build Watch impl
810 assert watch_outputs.length == 1;
811 assert Arrays.equals(watch_outputs[0].a, tx.getTxId().getReversedBytes());
812 assert watch_outputs[0].b.length == 2;
813 assert watch_outputs[0].b[0].a == 0;
814 assert watch_outputs[0].b[1].a == 1;
817 for (int i = 11; i < 21; i++) {
818 b = new Block(bitcoinj_net, 2, b.getHash(), Sha256Hash.ZERO_HASH, 42, 0, 0, new ArrayList<>());
819 state.peer2.connect_block(b, i, 0);
822 Event[] broadcastable_event = state.peer2.get_monitor_events(1);
823 for (ChannelMonitor mon : state.peer2.monitors.values()) {
824 // This used to be buggy and double-free, so go ahead and fetch them!
825 byte[][] txn = mon.get_latest_holder_commitment_txn(state.peer2.logger);
827 assert broadcastable_event.length == 1;
828 assert broadcastable_event[0] instanceof Event.SpendableOutputs;
829 if (state.peer2.explicit_keys_manager != null) {
830 TxOut[] additional_outputs = new TxOut[] { new TxOut(420, new byte[] { 0x42 }) };
831 Result_TransactionNoneZ tx_res = state.peer2.explicit_keys_manager.spend_spendable_outputs(((Event.SpendableOutputs) broadcastable_event[0]).outputs, additional_outputs, new byte[] {0x00}, 253);
832 assert tx_res instanceof Result_TransactionNoneZ.Result_TransactionNoneZ_OK;
833 Transaction built_tx = new Transaction(bitcoinj_net, ((Result_TransactionNoneZ.Result_TransactionNoneZ_OK) tx_res).res);
834 assert built_tx.getOutputs().size() == 2;
835 assert Arrays.equals(built_tx.getOutput(1).getScriptBytes(), new byte[]{0x00});
836 assert Arrays.equals(built_tx.getOutput(0).getScriptBytes(), new byte[]{0x42});
837 assert built_tx.getOutput(0).getValue().value == 420;
841 if (use_nio_peer_handler) {
842 state.peer1.peer_manager.disconnect_by_node_id(state.peer2.chan_manager.get_our_node_id(), false);
843 while (state.peer1.peer_manager.get_peer_node_ids().length != 0) Thread.yield();
844 while (state.peer2.peer_manager.get_peer_node_ids().length != 0) Thread.yield();
845 state.peer1.nio_peer_handler.interrupt();
846 state.peer2.nio_peer_handler.interrupt();
849 state.peer1.get_monitor_events(0);
850 state.peer2.get_monitor_events(0);
852 if (use_chan_manager_constructor) {
853 state.peer1.constructor.interrupt();
854 state.peer2.constructor.interrupt();
860 java.util.LinkedList<WeakReference<Object>> must_free_objs = new java.util.LinkedList();
862 int gc_exp_count = 0;
864 GcCheck() { gc_exp_count += 1; }
866 protected void finalize() throws Throwable {
872 public class HumanObjectPeerTest {
873 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 {
874 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);
875 HumanObjectPeerTestInstance.TestState state = instance.do_test_message_handler();
876 instance.do_test_message_handler_b(state);
879 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 {
880 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);
881 while (instance.gc_count != instance.gc_exp_count) {
883 System.runFinalization();
885 for (WeakReference<Object> o : instance.must_free_objs)
886 assert o.get() == null;
889 public void test_message_handler() throws InterruptedException {
890 for (int i = 0; i < (1 << 7) - 1; i++) {
891 boolean nice_close = (i & (1 << 0)) != 0;
892 boolean use_km_wrapper = (i & (1 << 1)) != 0;
893 boolean use_manual_watch = (i & (1 << 2)) != 0;
894 boolean reload_peers = (i & (1 << 3)) != 0;
895 boolean break_cross_refs = (i & (1 << 4)) != 0;
896 boolean nio_peer_handler = (i & (1 << 5)) != 0;
897 boolean use_chan_manager_constructor = (i & (1 << 6)) != 0;
898 if (break_cross_refs && !reload_peers) {
899 // There are no cross refs to break without reloading peers.
902 if (use_chan_manager_constructor && (use_manual_watch || !nio_peer_handler)) {
903 // ChannelManagerConstructor requires a ChainMonitor as the Watch and creates a NioPeerHandler for us.
906 System.err.println("Running test with flags " + i);
907 do_test(nice_close, use_km_wrapper, use_manual_watch, reload_peers, break_cross_refs, nio_peer_handler, use_chan_manager_constructor);
911 // This is used in the test jar to test the built jar is runnable
912 public static void main(String[] args) {
914 new HumanObjectPeerTest().test_message_handler();
915 } catch (Exception e) {
916 System.err.println("Caught exception:");
917 System.err.println(e);