use bitcoin::hash_types::BlockHash;
use bitcoin::blockdata::script::{Builder, Script};
use bitcoin::blockdata::opcodes;
-use bitcoin::blockdata::constants::genesis_block;
+use bitcoin::blockdata::constants::ChainHash;
use bitcoin::network::constants::Network;
use bitcoin::{PackedLockTime, Sequence, Transaction, TxIn, TxOut, Witness};
use bitcoin::OutPoint as BitcoinOutPoint;
for e in events {
match e {
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
- MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
+ MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::DisconnectPeer { ref msg } } => {
assert_eq!(node_id, nodes[1].node.get_our_node_id());
- assert_eq!(msg.data, "Channel closed because commitment or closing transaction was confirmed on chain.");
+ assert_eq!(msg.as_ref().unwrap().data, "Channel closed because commitment or closing transaction was confirmed on chain.");
},
MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
assert!(update_add_htlcs.is_empty());
_ => panic!("Unexpected event"),
};
match events[1] {
- MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id } => {
+ MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { .. }, node_id } => {
assert_eq!(node_id, nodes[4].node.get_our_node_id());
},
_ => panic!("Unexpected event"),
_ => panic!("Unexpected event"),
};
match events[1] {
- MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id } => {
+ MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { .. }, node_id } => {
assert_eq!(node_id, nodes[3].node.get_our_node_id());
},
_ => panic!("Unexpected event"),
let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut events);
match nodes_2_event {
- MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id: _ } => {},
+ MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { .. }, node_id: _ } => {},
_ => panic!("Unexpected event"),
}
let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
match nodes_2_event {
- MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { msg: msgs::ErrorMessage { channel_id, ref data } }, node_id: _ } => {
+ MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { msg: Some(msgs::ErrorMessage { channel_id, ref data }) }, node_id: _ } => {
assert_eq!(channel_id, chan_2.2);
assert_eq!(data.as_str(), "Channel closed because commitment or closing transaction was confirmed on chain.");
},
let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut msg_events);
match nodes_2_event {
- MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id: _ } => {},
+ MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { .. }, node_id: _ } => {},
_ => panic!("Unexpected event"),
}
assert!(node0_to_1_send_open_channel.to_self_delay==BREAKDOWN_TIMEOUT);
// BOLT #2 spec: Sending node must ensure the chain_hash value identifies the chain it wishes to open the channel within.
- let chain_hash=genesis_block(Network::Testnet).header.block_hash();
- assert_eq!(node0_to_1_send_open_channel.chain_hash,chain_hash);
+ let chain_hash = ChainHash::using_genesis_block(Network::Testnet);
+ assert_eq!(node0_to_1_send_open_channel.chain_hash, chain_hash);
// BOLT #2 spec: Sending node must set funding_pubkey, revocation_basepoint, htlc_basepoint, payment_basepoint, and delayed_payment_basepoint to valid DER-encoded, compressed, secp256k1 pubkeys.
assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.funding_pubkey.serialize()).is_ok());
let close_ev = nodes[1].node.get_and_clear_pending_msg_events();
assert_eq!(close_ev.len(), 1);
match close_ev[0] {
- MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, ref node_id } => {
+ MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { ref msg }, ref node_id } => {
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
- assert_eq!(msg.data, "Channel closed because funding transaction failed to confirm within 2016 blocks");
+ assert_eq!(msg.as_ref().unwrap().data, "Channel closed because funding transaction failed to confirm within 2016 blocks");
},
_ => panic!("Unexpected event"),
}
nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id, data: "Hi".to_owned() });
assert!(nodes[0].chain_monitor.added_monitors.lock().unwrap().is_empty());
check_closed_event!(nodes[0], 2, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString("Hi".to_string()) }, true,
- [nodes[1].node.get_our_node_id(); 2], 100000);
+ [nodes[1].node.get_our_node_id()], 100000);
}
#[test]
assert_eq!(events_2.len(), 1);
if let MessageSendEvent::HandleError { node_id, action } = &events_2[0] {
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
- if let msgs::ErrorAction::SendErrorMessage { msg } = action {
- assert_eq!(msg.data, "Channel closed because of an exception: ".to_owned() + expected_err);
+ if let msgs::ErrorAction::DisconnectPeer { msg } = action {
+ assert_eq!(msg.as_ref().unwrap().data, "Channel closed because of an exception: ".to_owned() + expected_err);
} else { panic!(); }
} else { panic!(); }
assert_eq!(nodes[1].node.list_channels().len(), 0);
// The channels in the batch will close immediately.
let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
- let events = nodes[0].node.get_and_clear_pending_events();
- assert_eq!(events.len(), 4);
- assert!(events.iter().any(|e| matches!(
- e,
- Event::ChannelClosed {
- channel_id,
- ..
- } if channel_id == &channel_id_1
- )));
- assert!(events.iter().any(|e| matches!(
- e,
- Event::ChannelClosed {
- channel_id,
- ..
- } if channel_id == &channel_id_2
- )));
- assert_eq!(events.iter().filter(|e| matches!(
- e,
- Event::DiscardFunding { .. },
- )).count(), 2);
+ check_closed_events(&nodes[0], &[
+ ExpectedCloseEvent {
+ channel_id: Some(channel_id_1),
+ discard_funding: true,
+ ..Default::default()
+ },
+ ExpectedCloseEvent {
+ channel_id: Some(channel_id_2),
+ discard_funding: true,
+ ..Default::default()
+ },
+ ]);
// The monitor should become closed.
check_added_monitors(&nodes[0], 1);
}
// All channels in the batch should close immediately.
- let events = nodes[0].node.get_and_clear_pending_events();
- assert_eq!(events.len(), 4);
- assert!(events.iter().any(|e| matches!(
- e,
- Event::ChannelClosed {
- channel_id,
- ..
- } if channel_id == &channel_id_1
- )));
- assert!(events.iter().any(|e| matches!(
- e,
- Event::ChannelClosed {
- channel_id,
- ..
- } if channel_id == &channel_id_2
- )));
- assert_eq!(events.iter().filter(|e| matches!(
- e,
- Event::DiscardFunding { .. },
- )).count(), 2);
+ check_closed_events(&nodes[0], &[
+ ExpectedCloseEvent {
+ channel_id: Some(channel_id_1),
+ discard_funding: true,
+ ..Default::default()
+ },
+ ExpectedCloseEvent {
+ channel_id: Some(channel_id_2),
+ discard_funding: true,
+ ..Default::default()
+ },
+ ]);
// Ensure the channels don't exist anymore.
assert!(nodes[0].node.list_channels().is_empty());
}
+
+fn do_test_funding_and_commitment_tx_confirm_same_block(confirm_remote_commitment: bool) {
+ // Tests that a node will forget the channel (when it only requires 1 confirmation) if the
+ // funding and commitment transaction confirm in the same block.
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let mut min_depth_1_block_cfg = test_default_channel_config();
+ min_depth_1_block_cfg.channel_handshake_config.minimum_depth = 1;
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(min_depth_1_block_cfg), Some(min_depth_1_block_cfg)]);
+ let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
+ let chan_id = chain::transaction::OutPoint { txid: funding_tx.txid(), index: 0 }.to_channel_id();
+
+ assert_eq!(nodes[0].node.list_channels().len(), 1);
+ assert_eq!(nodes[1].node.list_channels().len(), 1);
+
+ let (closing_node, other_node) = if confirm_remote_commitment {
+ (&nodes[1], &nodes[0])
+ } else {
+ (&nodes[0], &nodes[1])
+ };
+
+ closing_node.node.force_close_broadcasting_latest_txn(&chan_id, &other_node.node.get_our_node_id()).unwrap();
+ let mut msg_events = closing_node.node.get_and_clear_pending_msg_events();
+ assert_eq!(msg_events.len(), 1);
+ match msg_events.pop().unwrap() {
+ MessageSendEvent::HandleError { action: msgs::ErrorAction::DisconnectPeer { .. }, .. } => {},
+ _ => panic!("Unexpected event"),
+ }
+ check_added_monitors(closing_node, 1);
+ check_closed_event(closing_node, 1, ClosureReason::HolderForceClosed, false, &[other_node.node.get_our_node_id()], 1_000_000);
+
+ let commitment_tx = {
+ let mut txn = closing_node.tx_broadcaster.txn_broadcast();
+ assert_eq!(txn.len(), 1);
+ let commitment_tx = txn.pop().unwrap();
+ check_spends!(commitment_tx, funding_tx);
+ commitment_tx
+ };
+
+ mine_transactions(&nodes[0], &[&funding_tx, &commitment_tx]);
+ mine_transactions(&nodes[1], &[&funding_tx, &commitment_tx]);
+
+ check_closed_broadcast(other_node, 1, true);
+ check_added_monitors(other_node, 1);
+ check_closed_event(other_node, 1, ClosureReason::CommitmentTxConfirmed, false, &[closing_node.node.get_our_node_id()], 1_000_000);
+
+ assert!(nodes[0].node.list_channels().is_empty());
+ assert!(nodes[1].node.list_channels().is_empty());
+}
+
+#[test]
+fn test_funding_and_commitment_tx_confirm_same_block() {
+ do_test_funding_and_commitment_tx_confirm_same_block(false);
+ do_test_funding_and_commitment_tx_confirm_same_block(true);
+}