- peer1.chan_manager.force_close_all_channels();
-
- peer1.peer_manager.process_events();
- while (!list.isEmpty()) { list.poll().join(); }
- peer2.peer_manager.process_events();
- while (!list.isEmpty()) { list.poll().join(); }
- peer1.peer_manager.process_events();
- while (!list.isEmpty()) { list.poll().join(); }
- peer2.peer_manager.process_events();
- while (!list.isEmpty()) { list.poll().join(); }
-
- assert peer1.broadcast_set.size() == 1;
- assert peer2.broadcast_set.size() == 0;
-
- //b = new Block(bitcoinj_net, 2, b.getHash(), Sha256Hash.ZERO_HASH, 42, 0, 0, Arrays.asList(new Transaction[]{new Transaction(bitcoinj_net, peer1.broadcast_set.getFirst())}));
- //peer2.connect_block(b, 1);
+ state.peer1.chan_manager.force_close_all_channels();
+ maybe_exchange_peer_messages(state.peer1, state.peer2);
+ synchronized (state.peer1.broadcast_set) {
+ while (state.peer1.broadcast_set.size() != 1) state.peer1.broadcast_set.wait();
+ }
+ synchronized (state.peer2.broadcast_set) {
+ while (state.peer2.broadcast_set.size() != 1) state.peer2.broadcast_set.wait();
+ }
+
+ assert state.peer1.broadcast_set.size() == 1;
+ assert state.peer2.broadcast_set.size() == 1;
+ }
+
+ if (state.peer1.chain_monitor != null) {
+ Balance[] peer1_balances = state.peer1.chain_monitor.get_claimable_balances(state.peer1.chan_manager.list_channels());
+ assert peer1_balances.length == 1;
+ for (Balance bal : peer1_balances) {
+ assert bal instanceof Balance.ClaimableOnChannelClose;
+ long expected_tx_fee = 183;
+ assert ((Balance.ClaimableOnChannelClose) bal).claimable_amount_satoshis == 100000 - 1 - 10000 - expected_tx_fee;
+ }
+ Balance[] peer2_balances = state.peer2.chain_monitor.get_claimable_balances(state.peer2.chan_manager.list_channels());
+ assert peer2_balances.length == 1;
+ for (Balance bal : peer2_balances) {
+ assert bal instanceof Balance.ClaimableOnChannelClose;
+ assert ((Balance.ClaimableOnChannelClose) bal).claimable_amount_satoshis == 10000 + 1;
+ }
+ }
+
+ if (!nice_close) {
+ NetworkParameters bitcoinj_net = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
+ Transaction tx = new Transaction(bitcoinj_net, state.peer1.broadcast_set.getFirst());
+ Block b = new Block(bitcoinj_net, 2, state.best_blockhash, Sha256Hash.ZERO_HASH, 42, 0, 0,
+ Arrays.asList(new Transaction[]{tx}));
+ TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] watch_outputs = state.peer2.connect_block(b, 10, 1);
+ if (watch_outputs != null) { // We only process watch_outputs manually when we use a manually-build Watch impl
+ assert watch_outputs.length == 1;
+ assert Arrays.equals(watch_outputs[0].get_a(), tx.getTxId().getReversedBytes());
+ assert watch_outputs[0].get_b().length == 2;
+ assert watch_outputs[0].get_b()[0].get_a() == 0;
+ assert watch_outputs[0].get_b()[1].get_a() == 1;
+ }
+
+ for (int i = 11; i < 21; i++) {
+ b = new Block(bitcoinj_net, 2, b.getHash(), Sha256Hash.ZERO_HASH, 42, 0, 0, new ArrayList<>());
+ state.peer2.connect_block(b, i, 0);
+ }
+
+ Event[] broadcastable_event = state.peer2.get_monitor_events(1);
+ for (ChannelMonitor mon : state.peer2.monitors.values()) {
+ // This used to be buggy and double-free, so go ahead and fetch them!
+ byte[][] txn = mon.get_latest_holder_commitment_txn(state.peer2.logger);
+ }
+ assert broadcastable_event.length == 1;
+ assert broadcastable_event[0] instanceof Event.SpendableOutputs;
+ if (state.peer2.explicit_keys_manager != null) {
+ TxOut[] additional_outputs = new TxOut[]{new TxOut(420, new byte[]{0x42})};
+ Result_TransactionNoneZ tx_res = state.peer2.explicit_keys_manager.spend_spendable_outputs(((Event.SpendableOutputs) broadcastable_event[0]).outputs, additional_outputs, new byte[]{0x00}, 253);
+ assert tx_res instanceof Result_TransactionNoneZ.Result_TransactionNoneZ_OK;
+ Transaction built_tx = new Transaction(bitcoinj_net, ((Result_TransactionNoneZ.Result_TransactionNoneZ_OK) tx_res).res);
+ assert built_tx.getOutputs().size() == 2;
+ assert Arrays.equals(built_tx.getOutput(1).getScriptBytes(), new byte[]{0x00});
+ assert Arrays.equals(built_tx.getOutput(0).getScriptBytes(), new byte[]{0x42});
+ assert built_tx.getOutput(0).getValue().value == 420;
+ }
+ }
+
+ // Test exchanging a custom message
+ byte[] custom_message_bytes = new byte[] { 0x42, 0x44, 0x43, 0x00 };
+ state.peer1.custom_messages_to_send.add(custom_message_bytes);
+ state.peer1.peer_manager.process_events();
+ synchronized (state.peer2.received_custom_messages) {
+ while (true) {
+ if (state.peer2.received_custom_messages.isEmpty()) {
+ state.peer2.received_custom_messages.wait();
+ continue;
+ }
+ assert state.peer2.received_custom_messages.size() == 1;
+ assert Arrays.equals(state.peer2.received_custom_messages.get(0), custom_message_bytes);
+ break;
+ }
+ }
+
+ if (use_nio_peer_handler) {
+ state.peer1.peer_manager.disconnect_by_node_id(state.peer2.chan_manager.get_our_node_id(), false);
+ while (state.peer1.peer_manager.get_peer_node_ids().length != 0) Thread.yield();
+ while (state.peer2.peer_manager.get_peer_node_ids().length != 0) Thread.yield();
+ state.peer1.nio_peer_handler.interrupt();
+ state.peer2.nio_peer_handler.interrupt();
+ }
+
+ state.peer1.get_monitor_events(0);
+ state.peer2.get_monitor_events(0);
+
+ if (use_chan_manager_constructor) {
+ state.peer1.constructor.interrupt();
+ state.peer2.constructor.interrupt();