diff --git a/lightning/src/ln/reorg_tests.rs b/lightning/src/ln/reorg_tests.rs index 89d2f2c5ae6..5b5160148d7 100644 --- a/lightning/src/ln/reorg_tests.rs +++ b/lightning/src/ln/reorg_tests.rs @@ -1,5 +1,3 @@ -#![cfg_attr(rustfmt, rustfmt_skip)] - // This file is Copyright its original authors, visible in version control // history. // @@ -12,10 +10,10 @@ //! Further functional tests which test blockchain reorganizations. use crate::chain::chaininterface::LowerBoundedFeeEstimator; -use crate::chain::channelmonitor::{ANTI_REORG_DELAY, Balance, LATENCY_GRACE_PERIOD_BLOCKS}; +use crate::chain::channelmonitor::{Balance, ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS}; use crate::chain::transaction::OutPoint; use crate::chain::Confirm; -use crate::events::{Event, ClosureReason, HTLCHandlingFailureType}; +use crate::events::{ClosureReason, Event, HTLCHandlingFailureType}; use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, Init, MessageSendEvent}; use crate::ln::types::ChannelId; use crate::sign::OutputSpender; @@ -23,8 +21,8 @@ use crate::types::payment::PaymentHash; use crate::types::string::UntrustedString; use crate::util::ser::Writeable; -use bitcoin::script::Builder; use bitcoin::opcodes; +use bitcoin::script::Builder; use bitcoin::secp256k1::Secp256k1; use crate::prelude::*; @@ -52,22 +50,26 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { let legacy_cfg = test_legacy_channel_config(); let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(legacy_cfg), None]); let nodes = create_network(3, &node_cfgs, &node_chanmgrs); + let node_id_0 = nodes[0].node.get_our_node_id(); + let node_id_1 = nodes[1].node.get_our_node_id(); + let node_id_2 = nodes[2].node.get_our_node_id(); create_announced_chan_between_nodes(&nodes, 0, 1); let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2); // Make sure all nodes are at the same starting height - connect_blocks(&nodes[0], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1); - connect_blocks(&nodes[1], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1); - connect_blocks(&nodes[2], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1); + connect_blocks(&nodes[0], 2 * CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1); + connect_blocks(&nodes[1], 2 * CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1); + connect_blocks(&nodes[2], 2 * CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1); - let (our_payment_preimage, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000); + let (our_payment_preimage, our_payment_hash, ..) = + route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000); // Provide preimage to node 2 by claiming payment nodes[2].node.claim_funds(our_payment_preimage); expect_payment_claimed!(nodes[2], our_payment_hash, 1_000_000); check_added_monitors(&nodes[2], 1); - get_htlc_update_msgs(&nodes[2], &nodes[1].node.get_our_node_id()); + get_htlc_update_msgs(&nodes[2], &node_id_1); let claim_txn = if local_commitment { // Broadcast node 1 commitment txn to broadcast the HTLC-Timeout @@ -78,18 +80,23 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { check_spends!(node_1_commitment_txn[1], node_1_commitment_txn[0]); // Give node 2 node 1's transactions and get its response (claiming the HTLC instead). - connect_block(&nodes[2], &create_dummy_block(nodes[2].best_block_hash(), 42, node_1_commitment_txn.clone())); + let block = + create_dummy_block(nodes[2].best_block_hash(), 42, node_1_commitment_txn.clone()); + connect_block(&nodes[2], &block); check_closed_broadcast(&nodes[2], 1, true); // We should get a BroadcastChannelUpdate (and *only* a BroadcstChannelUpdate) check_added_monitors(&nodes[2], 1); - check_closed_event(&nodes[2], 1, ClosureReason::CommitmentTxConfirmed, &[nodes[1].node.get_our_node_id()], 100000); - let node_2_commitment_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0); + let reason = ClosureReason::CommitmentTxConfirmed; + check_closed_event(&nodes[2], 1, reason, &[node_id_1], 100000); + let node_2_commitment_txn = nodes[2].tx_broadcaster.txn_broadcast(); assert_eq!(node_2_commitment_txn.len(), 1); // ChannelMonitor: 1 offered HTLC-Claim check_spends!(node_2_commitment_txn[0], node_1_commitment_txn[0]); // Make sure node 1's height is the same as the !local_commitment case connect_blocks(&nodes[1], 1); // Confirm node 1's commitment txn (and HTLC-Timeout) on node 1 - connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, node_1_commitment_txn.clone())); + let block = + create_dummy_block(nodes[1].best_block_hash(), 42, node_1_commitment_txn.clone()); + connect_block(&nodes[1], &block); // ...but return node 1's commitment tx in case claim is set and we're preparing to reorg vec![node_1_commitment_txn[0].clone(), node_2_commitment_txn[0].clone()] @@ -115,7 +122,7 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { }; check_closed_broadcast(&nodes[1], 1, true); // We should get a BroadcastChannelUpdate (and *only* a BroadcstChannelUpdate) check_added_monitors(&nodes[1], 1); - check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, &[nodes[2].node.get_our_node_id()], 100000); + check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, &[node_id_2], 100000); // Connect ANTI_REORG_DELAY - 2 blocks, giving us a confirmation count of ANTI_REORG_DELAY - 1. connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2); check_added_monitors(&nodes[1], 0); @@ -136,25 +143,27 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, Vec::new())); expect_and_process_pending_htlcs_and_htlc_handling_failed( &nodes[1], - &[HTLCHandlingFailureType::Forward { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }] + &[HTLCHandlingFailureType::Forward { node_id: Some(node_id_2), channel_id: chan_2.2 }], ); } check_added_monitors(&nodes[1], 1); // Which should result in an immediate claim/fail of the HTLC: - let mut htlc_updates = get_htlc_update_msgs(&nodes[1], &nodes[0].node.get_our_node_id()); + let mut htlc_updates = get_htlc_update_msgs(&nodes[1], &node_id_0); if claim { assert_eq!(htlc_updates.update_fulfill_htlcs.len(), 1); - nodes[0].node.handle_update_fulfill_htlc(nodes[1].node.get_our_node_id(), htlc_updates.update_fulfill_htlcs.remove(0)); + let update_fulfill = htlc_updates.update_fulfill_htlcs.remove(0); + nodes[0].node.handle_update_fulfill_htlc(node_id_1, update_fulfill); } else { assert_eq!(htlc_updates.update_fail_htlcs.len(), 1); - nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]); + nodes[0].node.handle_update_fail_htlc(node_id_1, &htlc_updates.update_fail_htlcs[0]); } do_commitment_signed_dance(&nodes[0], &nodes[1], &htlc_updates.commitment_signed, false, true); if claim { expect_payment_sent!(nodes[0], our_payment_preimage); } else { - expect_payment_failed_with_update!(nodes[0], our_payment_hash, false, chan_2.0.contents.short_channel_id, true); + let scid = chan_2.0.contents.short_channel_id; + expect_payment_failed_with_update!(nodes[0], our_payment_hash, false, scid, true); } } @@ -186,6 +195,8 @@ fn test_counterparty_revoked_reorg() { let legacy_cfg = test_legacy_channel_config(); let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(legacy_cfg), None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); + let node_id_0 = nodes[0].node.get_our_node_id(); + let node_id_1 = nodes[1].node.get_our_node_id(); let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000); @@ -196,25 +207,29 @@ fn test_counterparty_revoked_reorg() { // Now add two HTLCs in each direction, one dust and one not. route_payment(&nodes[0], &[&nodes[1]], 5_000_000); route_payment(&nodes[0], &[&nodes[1]], 5_000); - let (payment_preimage_3, payment_hash_3, ..) = route_payment(&nodes[1], &[&nodes[0]], 4_000_000); + let (payment_preimage_3, payment_hash_3, ..) = + route_payment(&nodes[1], &[&nodes[0]], 4_000_000); let payment_hash_4 = route_payment(&nodes[1], &[&nodes[0]], 4_000).1; nodes[0].node.claim_funds(payment_preimage_3); - let _ = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id()); + let _ = get_htlc_update_msgs(&nodes[0], &node_id_1); check_added_monitors(&nodes[0], 1); expect_payment_claimed!(nodes[0], payment_hash_3, 4_000_000); let mut unrevoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2); - assert_eq!(unrevoked_local_txn.len(), 3); // commitment + 2 HTLC txn + // There should be the commitment transaction and two HTLC transactions. + assert_eq!(unrevoked_local_txn.len(), 3); // Sort the unrevoked transactions in reverse order, ie commitment tx, then HTLC 1 then HTLC 3 - unrevoked_local_txn.sort_unstable_by_key(|tx| 1_000_000 - tx.output.iter().map(|outp| outp.value.to_sat()).sum::()); + unrevoked_local_txn.sort_unstable_by_key(|tx| { + 1_000_000 - tx.output.iter().map(|outp| outp.value.to_sat()).sum::() + }); // Now mine A's old commitment transaction, which should close the channel, but take no action // on any of the HTLCs, at least until we get six confirmations (which we won't get). mine_transaction(&nodes[1], &revoked_local_txn[0]); check_closed_broadcast(&nodes[1], 1, true); check_added_monitors(&nodes[1], 1); - check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, &[nodes[0].node.get_our_node_id()], 1000000); + check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, &[node_id_0], 1000000); // Connect up to one block before the revoked transaction would be considered final, then do a // reorg that disconnects the full chain and goes up to the height at which the revoked @@ -248,7 +263,10 @@ fn test_counterparty_revoked_reorg() { expect_payment_failed_conditions(&nodes[1], payment_hash_4, false, conditions) } -fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_unconfirmed: bool, connect_style: ConnectStyle) { +fn do_test_unconf_chan( + reload_node: bool, reorg_after_reload: bool, use_funding_unconfirmed: bool, + connect_style: ConnectStyle, +) { // After creating a chan between nodes, we disconnect all blocks previously seen to force a // channel close on nodes[0] side. We also use this to provide very basic testing of logic // around freeing background events which store monitor updates during block_[dis]connected. @@ -264,12 +282,15 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_ let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); *nodes[0].connect_style.borrow_mut() = connect_style; - let chan_conf_height = core::cmp::max(nodes[0].best_block_info().1 + 1, nodes[1].best_block_info().1 + 1); + let node_id_0 = nodes[0].node.get_our_node_id(); + let node_id_1 = nodes[1].node.get_our_node_id(); + let chan_conf_height = + core::cmp::max(nodes[0].best_block_info().1 + 1, nodes[1].best_block_info().1 + 1); let chan = create_announced_chan_between_nodes(&nodes, 0, 1); { let per_peer_state = nodes[0].node.per_peer_state.read().unwrap(); - let peer_state = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap(); + let peer_state = per_peer_state.get(&node_id_1).unwrap().lock().unwrap(); assert_eq!(peer_state.channel_by_id.len(), 1); assert_eq!(nodes[0].node.short_to_chan_info.read().unwrap().len(), 2); } @@ -306,12 +327,12 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_ let relevant_txids = nodes[0].node.get_relevant_txids(); assert_eq!(relevant_txids.len(), 0); - let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0); + let txn = nodes[0].tx_broadcaster.txn_broadcast(); assert_eq!(txn.len(), 1); { let per_peer_state = nodes[0].node.per_peer_state.read().unwrap(); - let peer_state = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap(); + let peer_state = per_peer_state.get(&node_id_1).unwrap().lock().unwrap(); assert_eq!(peer_state.channel_by_id.len(), 0); assert_eq!(nodes[0].node.short_to_chan_info.read().unwrap().len(), 0); } @@ -320,11 +341,18 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_ } let expected_err = "Funding transaction was un-confirmed, originally locked at 6 confs."; + let broadcast_close_msg = + "Channel closed because of an exception: Funding transaction was un-confirmed, originally locked at 6 confs."; + let counterparty_force_closed_reason = || ClosureReason::CounterpartyForceClosed { + peer_msg: UntrustedString(format!( + "Channel closed because of an exception: {}", + expected_err + )), + }; if reload_node && !reorg_after_reload { - handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed, originally locked at 6 confs."); + handle_announce_close_broadcast_events(&nodes, 0, 1, true, broadcast_close_msg); check_added_monitors(&nodes[1], 1); - let reason = ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(format!("Channel closed because of an exception: {}", expected_err)) }; - check_closed_event(&nodes[1], 1, reason, &[nodes[0].node.get_our_node_id()], 100000); + check_closed_event(&nodes[1], 1, counterparty_force_closed_reason(), &[node_id_0], 100000); } if reload_node { @@ -334,10 +362,20 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_ // it when we go to deserialize, and then use the ChannelManager. let nodes_0_serialized = nodes[0].node.encode(); let chan_0_monitor_serialized = get_monitor!(nodes[0], chan.2).encode(); + let current_config = nodes[0].node.get_current_config(); + let serialized_monitors = [&chan_0_monitor_serialized[..]]; - reload_node!(nodes[0], nodes[0].node.get_current_config(), &nodes_0_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_0_deserialized); + reload_node!( + nodes[0], + current_config, + &nodes_0_serialized, + &serialized_monitors, + persister, + new_chain_monitor, + nodes_0_deserialized + ); - nodes[1].node.peer_disconnected(nodes[0].node.get_our_node_id()); + nodes[1].node.peer_disconnected(node_id_0); if reorg_after_reload { // If we haven't yet closed the channel, reconnect the peers so that nodes[0] will @@ -381,7 +419,7 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_ { let per_peer_state = nodes[0].node.per_peer_state.read().unwrap(); - let peer_state = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap(); + let peer_state = per_peer_state.get(&node_id_1).unwrap().lock().unwrap(); assert_eq!(peer_state.channel_by_id.len(), 0); assert_eq!(nodes[0].node.short_to_chan_info.read().unwrap().len(), 0); } @@ -393,30 +431,36 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_ } check_added_monitors(&nodes[0], 1); - let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0); + let txn = nodes[0].tx_broadcaster.txn_broadcast(); assert_eq!(txn.len(), 1); } if reorg_after_reload || !reload_node { - handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Channel closed because of an exception: Funding transaction was un-confirmed, originally locked at 6 confs."); + handle_announce_close_broadcast_events(&nodes, 0, 1, true, broadcast_close_msg); check_added_monitors(&nodes[1], 1); - let reason = ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(format!("Channel closed because of an exception: {}", expected_err)) }; - check_closed_event(&nodes[1], 1, reason, &[nodes[0].node.get_our_node_id()], 100000); + check_closed_event(&nodes[1], 1, counterparty_force_closed_reason(), &[node_id_0], 100000); } - check_closed_event(&nodes[0], 1, ClosureReason::ProcessingError { err: expected_err.to_owned() }, &[nodes[1].node.get_our_node_id()], 100000); + let processing_error = ClosureReason::ProcessingError { err: expected_err.to_owned() }; + check_closed_event(&nodes[0], 1, processing_error, &[node_id_1], 100000); // Now check that we can create a new channel if reload_node && !reorg_after_reload { // If we dropped the channel before reloading the node, nodes[1] was also dropped from // nodes[0] storage, and hence not connected again on startup. We therefore need to // reconnect to the node before attempting to create a new channel. - nodes[0].node.peer_connected(nodes[1].node.get_our_node_id(), &Init { - features: nodes[1].node.init_features(), networks: None, remote_network_address: None - }, true).unwrap(); - nodes[1].node.peer_connected(nodes[0].node.get_our_node_id(), &Init { - features: nodes[0].node.init_features(), networks: None, remote_network_address: None - }, true).unwrap(); + let node_1_init = Init { + features: nodes[1].node.init_features(), + networks: None, + remote_network_address: None, + }; + let node_0_init = Init { + features: nodes[0].node.init_features(), + networks: None, + remote_network_address: None, + }; + nodes[0].node.peer_connected(node_id_1, &node_1_init, true).unwrap(); + nodes[1].node.peer_connected(node_id_0, &node_0_init, true).unwrap(); } create_announced_chan_between_nodes(&nodes, 0, 1); @@ -472,10 +516,14 @@ fn test_set_outpoints_partial_claiming() { let legacy_cfg = test_legacy_channel_config(); let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(legacy_cfg), None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); + let node_id_0 = nodes[0].node.get_our_node_id(); + let node_id_1 = nodes[1].node.get_our_node_id(); let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000); - let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000); - let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000); + let (payment_preimage_1, payment_hash_1, ..) = + route_payment(&nodes[1], &[&nodes[0]], 3_000_000); + let (payment_preimage_2, payment_hash_2, ..) = + route_payment(&nodes[1], &[&nodes[0]], 3_000_000); // Remote commitment txn with 4 outputs: to_local, to_remote, 2 outgoing HTLC let remote_txn = get_local_commitment_txn!(nodes[1], chan.2); @@ -498,7 +546,7 @@ fn test_set_outpoints_partial_claiming() { // Connect blocks on node A commitment transaction mine_transaction(&nodes[0], &remote_txn[0]); check_closed_broadcast(&nodes[0], 1, true); - check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, &[nodes[1].node.get_our_node_id()], 1000000); + check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, &[node_id_1], 1000000); check_added_monitors(&nodes[0], 1); // Verify node A broadcast tx claiming both HTLCs { @@ -513,16 +561,17 @@ fn test_set_outpoints_partial_claiming() { // Connect blocks on node B connect_blocks(&nodes[1], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1); check_closed_broadcast(&nodes[1], 1, true); - check_closed_events(&nodes[1], &[ExpectedCloseEvent { + let expected_close = ExpectedCloseEvent { channel_capacity_sats: Some(1_000_000), channel_id: Some(chan.2), - counterparty_node_id: Some(nodes[0].node.get_our_node_id()), + counterparty_node_id: Some(node_id_0), discard_funding: false, splice_failed: false, reason: None, // Could be due to either HTLC timing out, so don't bother checking channel_funding_txo: None, user_channel_id: None, - }]); + }; + check_closed_events(&nodes[1], &[expected_close]); check_added_monitors(&nodes[1], 1); // Verify node B broadcast 2 HTLC-timeout txn let partial_claim_tx = { @@ -581,6 +630,8 @@ fn do_test_to_remote_after_local_detection(style: ConnectStyle) { let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); + let node_id_0 = nodes[0].node.get_our_node_id(); + let node_id_1 = nodes[1].node.get_our_node_id(); *nodes[0].connect_style.borrow_mut() = style; *nodes[1].connect_style.borrow_mut() = style; @@ -599,11 +650,11 @@ fn do_test_to_remote_after_local_detection(style: ConnectStyle) { check_closed_broadcast(&nodes[0], 1, true); assert!(nodes[0].node.list_channels().is_empty()); check_added_monitors(&nodes[0], 1); - check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, &[nodes[1].node.get_our_node_id()], 1000000); + check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, &[node_id_1], 1000000); check_closed_broadcast(&nodes[1], 1, true); assert!(nodes[1].node.list_channels().is_empty()); check_added_monitors(&nodes[1], 1); - check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, &[nodes[0].node.get_our_node_id()], 1000000); + check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, &[node_id_0], 1000000); assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty()); assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty()); @@ -637,11 +688,25 @@ fn do_test_to_remote_after_local_detection(style: ConnectStyle) { let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events(); assert_eq!(node_a_spendable.len(), 1); - if let Event::SpendableOutputs { outputs, channel_id, counterparty_node_id: _ } = node_a_spendable.pop().unwrap() { + if let Event::SpendableOutputs { outputs, channel_id, counterparty_node_id: _ } = + node_a_spendable.pop().unwrap() + { assert_eq!(outputs.len(), 1); assert_eq!(channel_id, Some(chan_id)); - let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(), - Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, None, &Secp256k1::new()).unwrap(); + let spendable_outputs = [&outputs[0]]; + let destination_script = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(); + let spend_tx = nodes[0] + .keys_manager + .backing + .spend_spendable_outputs( + &spendable_outputs, + Vec::new(), + destination_script, + 253, + None, + &Secp256k1::new(), + ) + .unwrap(); check_spends!(spend_tx, remote_txn_b[0]); } @@ -658,11 +723,25 @@ fn do_test_to_remote_after_local_detection(style: ConnectStyle) { let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events(); assert_eq!(node_b_spendable.len(), 1); - if let Event::SpendableOutputs { outputs, channel_id, counterparty_node_id: _ } = node_b_spendable.pop().unwrap() { + if let Event::SpendableOutputs { outputs, channel_id, counterparty_node_id: _ } = + node_b_spendable.pop().unwrap() + { assert_eq!(outputs.len(), 1); assert_eq!(channel_id, Some(chan_id)); - let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(), - Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, None, &Secp256k1::new()).unwrap(); + let spendable_outputs = [&outputs[0]]; + let destination_script = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(); + let spend_tx = nodes[1] + .keys_manager + .backing + .spend_spendable_outputs( + &spendable_outputs, + Vec::new(), + destination_script, + 253, + None, + &Secp256k1::new(), + ) + .unwrap(); check_spends!(spend_tx, remote_txn_a[0]); } } @@ -688,6 +767,8 @@ fn test_htlc_preimage_claim_holder_commitment_after_counterparty_commitment_reor let legacy_cfg = test_legacy_channel_config(); let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(legacy_cfg), None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); + let node_id_0 = nodes[0].node.get_our_node_id(); + let node_id_1 = nodes[1].node.get_our_node_id(); let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1); @@ -699,7 +780,7 @@ fn test_htlc_preimage_claim_holder_commitment_after_counterparty_commitment_reor // holder commitment. nodes[0] .node - .force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id(), message.clone()) + .force_close_broadcasting_latest_txn(&chan_id, &node_id_1, message.clone()) .unwrap(); check_closed_broadcast(&nodes[0], 1, true); check_added_monitors(&nodes[0], 1); @@ -707,16 +788,16 @@ fn test_htlc_preimage_claim_holder_commitment_after_counterparty_commitment_reor broadcasted_latest_txn: Some(true), message: message.clone(), }; - check_closed_event(&nodes[0], 1, reason, &[nodes[1].node.get_our_node_id()], 100000); + check_closed_event(&nodes[0], 1, reason, &[node_id_1], 100000); nodes[1] .node - .force_close_broadcasting_latest_txn(&chan_id, &nodes[0].node.get_our_node_id(), message.clone()) + .force_close_broadcasting_latest_txn(&chan_id, &node_id_0, message.clone()) .unwrap(); check_closed_broadcast(&nodes[1], 1, true); check_added_monitors(&nodes[1], 1); let reason = ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true), message }; - check_closed_event(&nodes[1], 1, reason, &[nodes[0].node.get_our_node_id()], 100000); + check_closed_event(&nodes[1], 1, reason, &[node_id_0], 100000); let mut txn = nodes[0].tx_broadcaster.txn_broadcast(); assert_eq!(txn.len(), 1); @@ -743,8 +824,11 @@ fn test_htlc_preimage_claim_holder_commitment_after_counterparty_commitment_reor // Provide the preimage now, such that we only claim from the holder commitment (since it's // currently confirmed) and not the counterparty's. get_monitor!(nodes[1], chan_id).provide_payment_preimage_unsafe_legacy( - &payment_hash, &payment_preimage, &nodes[1].tx_broadcaster, - &LowerBoundedFeeEstimator(nodes[1].fee_estimator), &nodes[1].logger + &payment_hash, + &payment_preimage, + &nodes[1].tx_broadcaster, + &LowerBoundedFeeEstimator(nodes[1].fee_estimator), + &nodes[1].logger, ); let mut txn = nodes[1].tx_broadcaster.txn_broadcast(); @@ -754,7 +838,8 @@ fn test_htlc_preimage_claim_holder_commitment_after_counterparty_commitment_reor } #[test] -fn test_htlc_preimage_claim_prev_counterparty_commitment_after_current_counterparty_commitment_reorg() { +fn test_htlc_preimage_claim_prev_counterparty_commitment_after_current_counterparty_commitment_reorg( +) { // We detect a counterparty commitment confirm onchain, followed by a reorg and a // confirmation of the previous (still unrevoked) counterparty commitment. Then, if we learn // of the preimage for an HTLC in both commitments, test that we only claim the currently @@ -764,6 +849,8 @@ fn test_htlc_preimage_claim_prev_counterparty_commitment_after_current_counterpa let legacy_cfg = test_legacy_channel_config(); let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(legacy_cfg), None]); let nodes = create_network(2, &node_cfgs, &node_chanmgrs); + let node_id_0 = nodes[0].node.get_our_node_id(); + let node_id_1 = nodes[1].node.get_our_node_id(); let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1); @@ -778,26 +865,32 @@ fn test_htlc_preimage_claim_prev_counterparty_commitment_after_current_counterpa check_added_monitors(&nodes[0], 1); let mut msg_events = nodes[0].node.get_and_clear_pending_msg_events(); assert_eq!(msg_events.len(), 1); - let (update_fee, commit_sig) = if let MessageSendEvent::UpdateHTLCs { node_id, channel_id: _, mut updates } = msg_events.pop().unwrap() { - assert_eq!(node_id, nodes[1].node.get_our_node_id()); - (updates.update_fee.take().unwrap(), updates.commitment_signed) - } else { + let MessageSendEvent::UpdateHTLCs { node_id, channel_id: _, mut updates } = + msg_events.pop().unwrap() + else { panic!("Unexpected message send event"); }; + assert_eq!(node_id, node_id_1); + let update_fee = updates.update_fee.take().unwrap(); + let commit_sig = updates.commitment_signed; // Handle the fee update on the other side, but don't send the last RAA such that the previous // commitment is still valid (unrevoked). - nodes[1].node().handle_update_fee(nodes[0].node.get_our_node_id(), &update_fee); - let _last_revoke_and_ack = commitment_signed_dance_return_raa(&nodes[1], &nodes[0], &commit_sig, false); + nodes[1].node().handle_update_fee(node_id_0, &update_fee); + let _last_revoke_and_ack = + commitment_signed_dance_return_raa(&nodes[1], &nodes[0], &commit_sig, false); let message = "Channel force-closed".to_owned(); // Force close with the latest commitment, confirm it, and reorg it with the previous commitment. - nodes[0].node.force_close_broadcasting_latest_txn(&chan_id, &nodes[1].node.get_our_node_id(), message.clone()).unwrap(); + nodes[0] + .node + .force_close_broadcasting_latest_txn(&chan_id, &node_id_1, message.clone()) + .unwrap(); check_closed_broadcast(&nodes[0], 1, true); check_added_monitors(&nodes[0], 1); let reason = ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true), message }; - check_closed_event(&nodes[0], 1, reason, &[nodes[1].node.get_our_node_id()], 100000); + check_closed_event(&nodes[0], 1, reason, &[node_id_1], 100000); let mut txn = nodes[0].tx_broadcaster.txn_broadcast(); assert_eq!(txn.len(), 1); @@ -810,7 +903,7 @@ fn test_htlc_preimage_claim_prev_counterparty_commitment_after_current_counterpa check_closed_broadcast(&nodes[1], 1, true); check_added_monitors(&nodes[1], 1); - check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, &[nodes[0].node.get_our_node_id()], 100000); + check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, &[node_id_0], 100000); disconnect_blocks(&nodes[0], 1); disconnect_blocks(&nodes[1], 1); @@ -821,8 +914,11 @@ fn test_htlc_preimage_claim_prev_counterparty_commitment_after_current_counterpa // Provide the preimage now, such that we only claim from the previous commitment (since it's // currently confirmed) and not the latest. get_monitor!(nodes[1], chan_id).provide_payment_preimage_unsafe_legacy( - &payment_hash, &payment_preimage, &nodes[1].tx_broadcaster, - &LowerBoundedFeeEstimator(nodes[1].fee_estimator), &nodes[1].logger + &payment_hash, + &payment_preimage, + &nodes[1].tx_broadcaster, + &LowerBoundedFeeEstimator(nodes[1].fee_estimator), + &nodes[1].logger, ); let mut txn = nodes[1].tx_broadcaster.txn_broadcast(); @@ -831,10 +927,15 @@ fn test_htlc_preimage_claim_prev_counterparty_commitment_after_current_counterpa check_spends!(htlc_preimage_tx, prev_commitment_a); // Make sure it was indeed a preimage claim and not a revocation claim since the previous // commitment (still unrevoked) is the currently confirmed closing transaction. - assert_eq!(htlc_preimage_tx.input[0].witness.second_to_last().unwrap(), &payment_preimage.0[..]); + assert_eq!( + htlc_preimage_tx.input[0].witness.second_to_last().unwrap(), + &payment_preimage.0[..] + ); } -fn do_test_retries_own_commitment_broadcast_after_reorg(keyed_anchors: bool, p2a_anchor: bool, revoked_counterparty_commitment: bool) { +fn do_test_retries_own_commitment_broadcast_after_reorg( + keyed_anchors: bool, p2a_anchor: bool, revoked_counterparty_commitment: bool, +) { // Tests that a node will retry broadcasting its own commitment after seeing a confirmed // counterparty commitment be reorged out. let mut chanmon_cfgs = create_chanmon_cfgs(2); @@ -847,9 +948,12 @@ fn do_test_retries_own_commitment_broadcast_after_reorg(keyed_anchors: bool, p2a config.channel_handshake_config.negotiate_anchor_zero_fee_commitments = p2a_anchor; let persister; let new_chain_monitor; - let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config.clone()), Some(config.clone())]); + let node_chanmgrs = + create_node_chanmgrs(2, &node_cfgs, &[Some(config.clone()), Some(config.clone())]); let nodes_1_deserialized; let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); + let node_id_0 = nodes[0].node.get_our_node_id(); + let node_id_1 = nodes[1].node.get_our_node_id(); let coinbase_tx = provide_anchor_reserves(&nodes); @@ -862,11 +966,18 @@ fn do_test_retries_own_commitment_broadcast_after_reorg(keyed_anchors: bool, p2a // Trigger a new commitment by routing a dummy HTLC. We will have B broadcast the previous commitment. let serialized_node = nodes[1].node.encode(); let serialized_monitor = get_monitor!(nodes[1], chan_id).encode(); + let serialized_monitors = [&serialized_monitor[..]]; let _ = route_payment(&nodes[0], &[&nodes[1]], 1000); reload_node!( - nodes[1], config, &serialized_node, &[&serialized_monitor], persister, new_chain_monitor, nodes_1_deserialized + nodes[1], + config, + &serialized_node, + &serialized_monitors, + persister, + new_chain_monitor, + nodes_1_deserialized ); } @@ -875,7 +986,7 @@ fn do_test_retries_own_commitment_broadcast_after_reorg(keyed_anchors: bool, p2a check_closed_broadcast(&nodes[0], 1, true); check_added_monitors(&nodes[0], 1); let reason = ClosureReason::HTLCsTimedOut { payment_hash: Some(payment_hash) }; - check_closed_event(&nodes[0], 1, reason, &[nodes[1].node.get_our_node_id()], 100_000); + check_closed_event(&nodes[0], 1, reason, &[node_id_1], 100_000); if keyed_anchors || p2a_anchor { handle_bump_close_event(&nodes[0]); } @@ -905,12 +1016,12 @@ fn do_test_retries_own_commitment_broadcast_after_reorg(keyed_anchors: bool, p2a let message = "Channel force-closed".to_owned(); nodes[1] .node - .force_close_broadcasting_latest_txn(&chan_id, &nodes[0].node.get_our_node_id(), message.clone()) + .force_close_broadcasting_latest_txn(&chan_id, &node_id_0, message.clone()) .unwrap(); check_closed_broadcast(&nodes[1], 1, !revoked_counterparty_commitment); check_added_monitors(&nodes[1], 1); let reason = ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true), message }; - check_closed_event(&nodes[1], 1, reason, &[nodes[0].node.get_our_node_id()], 100_000); + check_closed_event(&nodes[1], 1, reason, &[node_id_0], 100_000); if keyed_anchors || p2a_anchor { handle_bump_close_event(&nodes[1]); } @@ -925,7 +1036,6 @@ fn do_test_retries_own_commitment_broadcast_after_reorg(keyed_anchors: bool, p2a // Confirm B's commitment, A should now broadcast an HTLC timeout for commitment B. mine_transactions(&nodes[0], &[&tx, &anchor_tx]); tx - } else { let mut txn = nodes[1].tx_broadcaster.txn_broadcast(); assert_eq!(txn.len(), 1); @@ -946,7 +1056,7 @@ fn do_test_retries_own_commitment_broadcast_after_reorg(keyed_anchors: bool, p2a assert_eq!(txn.len(), 3); check_spends!(txn[0], commitment_b); check_spends!(txn[1], funding_tx); - check_spends!(txn[2], txn[1], coinbase_tx); // Anchor output spend transaction. + check_spends!(txn[2], txn[1], coinbase_tx); // Anchor output spend transaction. } else { let mut txn = nodes[0].tx_broadcaster.txn_broadcast(); assert_eq!(txn.len(), 2); @@ -971,7 +1081,7 @@ fn do_test_retries_own_commitment_broadcast_after_reorg(keyed_anchors: bool, p2a if keyed_anchors || p2a_anchor { assert_eq!(txn.len(), 2); check_spends!(txn[0], funding_tx); - check_spends!(txn[1], txn[0], coinbase_tx); // Anchor output spend. + check_spends!(txn[1], txn[0], coinbase_tx); // Anchor output spend. } else { assert_eq!(txn.len(), 2); check_spends!(txn[0], txn[1]); // HTLC timeout A @@ -1009,8 +1119,8 @@ fn do_test_split_htlc_expiry_tracking(use_third_htlc: bool, reorg_out: bool, p2a let coinbase_tx = provide_anchor_reserves(&nodes); - let node_a_id = nodes[0].node.get_our_node_id(); - let node_b_id = nodes[1].node.get_our_node_id(); + let node_id_0 = nodes[0].node.get_our_node_id(); + let node_id_1 = nodes[1].node.get_our_node_id(); let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0); @@ -1028,8 +1138,8 @@ fn do_test_split_htlc_expiry_tracking(use_third_htlc: bool, reorg_out: bool, p2a }; // First disconnect peers so that we don't have to deal with messages: - nodes[0].node.peer_disconnected(node_b_id); - nodes[1].node.peer_disconnected(node_a_id); + nodes[0].node.peer_disconnected(node_id_1); + nodes[1].node.peer_disconnected(node_id_0); // Give node B preimages so that it will claim the first two HTLCs on-chain. nodes[1].node.claim_funds(preimage_a); @@ -1042,12 +1152,12 @@ fn do_test_split_htlc_expiry_tracking(use_third_htlc: bool, reorg_out: bool, p2a // Force-close and fetch node B's commitment transaction and the transaction claiming the first // two HTLCs. - nodes[1].node.force_close_broadcasting_latest_txn(&chan_id, &node_a_id, err).unwrap(); + nodes[1].node.force_close_broadcasting_latest_txn(&chan_id, &node_id_0, err).unwrap(); check_closed_broadcast(&nodes[1], 1, false); check_added_monitors(&nodes[1], 1); let message = "Channel force-closed".to_owned(); let reason = ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true), message }; - check_closed_event(&nodes[1], 1, reason, &[node_a_id], 10_000_000); + check_closed_event(&nodes[1], 1, reason, &[node_id_0], 10_000_000); handle_bump_close_event(&nodes[1]); let mut txn = nodes[1].tx_broadcaster.txn_broadcast(); @@ -1072,7 +1182,7 @@ fn do_test_split_htlc_expiry_tracking(use_third_htlc: bool, reorg_out: bool, p2a } check_closed_broadcast(&nodes[0], 1, false); let reason = ClosureReason::CommitmentTxConfirmed; - check_closed_event(&nodes[0], 1, reason, &[node_b_id], 10_000_000); + check_closed_event(&nodes[0], 1, reason, &[node_id_1], 10_000_000); check_added_monitors(&nodes[0], 1); if let Some(ref a_tx) = anchor_tx { @@ -1087,10 +1197,10 @@ fn do_test_split_htlc_expiry_tracking(use_third_htlc: bool, reorg_out: bool, p2a assert_eq!(txn.len(), 3, "{txn:?}"); if p2a_anchor { check_spends!(txn[0], funding_tx); - check_spends!(txn[1], txn[0], anchor_tx.as_ref().unwrap()); // Anchor output spend. + check_spends!(txn[1], txn[0], anchor_tx.as_ref().unwrap()); // Anchor output spend. } else { check_spends!(txn[0], funding_tx); - check_spends!(txn[1], txn[0], coinbase_tx); // Anchor output spend. + check_spends!(txn[1], txn[0], coinbase_tx); // Anchor output spend. } } else { assert_eq!(txn.len(), 1, "{txn:?}"); @@ -1123,7 +1233,8 @@ fn do_test_split_htlc_expiry_tracking(use_third_htlc: bool, reorg_out: bool, p2a let mut found_expected_events = [false, false, false, false]; for event in sent_events { match event { - Event::PaymentSent { payment_hash, .. }|Event::PaymentPathSuccessful { payment_hash: Some(payment_hash), .. } => { + Event::PaymentSent { payment_hash, .. } + | Event::PaymentPathSuccessful { payment_hash: Some(payment_hash), .. } => { let path_success = matches!(event, Event::PaymentPathSuccessful { .. }); if payment_hash == payment_hash_a { found_expected_events[0 + if path_success { 1 } else { 0 }] = true; @@ -1214,7 +1325,8 @@ fn do_test_split_htlc_expiry_tracking(use_third_htlc: bool, reorg_out: bool, p2a let mut found_expected_events = [false, false]; for event in failed_events { match event { - Event::PaymentFailed { payment_hash: Some(payment_hash), .. }|Event::PaymentPathFailed { payment_hash, .. } => { + Event::PaymentFailed { payment_hash: Some(payment_hash), .. } + | Event::PaymentPathFailed { payment_hash, .. } => { let path_failed = matches!(event, Event::PaymentPathFailed { .. }); if payment_hash == payment_hash_c { found_expected_events[if path_failed { 1 } else { 0 }] = true;