X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fmonitor_tests.rs;h=30615b86d2f6560057276370770446ce2c99881b;hb=9f1c9062db3278597429751c00b3cc7b0ef277e0;hp=80119b55787c7e75b67d791415e55dba9fb7a18d;hpb=2659a2375eb819fe050f057b068c3e27f98a30f7;p=rust-lightning diff --git a/lightning/src/ln/monitor_tests.rs b/lightning/src/ln/monitor_tests.rs index 80119b55..30615b86 100644 --- a/lightning/src/ln/monitor_tests.rs +++ b/lightning/src/ln/monitor_tests.rs @@ -9,17 +9,17 @@ //! Further functional tests which test blockchain reorganizations. -use crate::sign::{ecdsa::EcdsaChannelSigner, SpendableOutputDescriptor}; +use crate::sign::{ecdsa::EcdsaChannelSigner, OutputSpender, SpendableOutputDescriptor}; use crate::chain::channelmonitor::{ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS, Balance}; use crate::chain::transaction::OutPoint; use crate::chain::chaininterface::{LowerBoundedFeeEstimator, compute_feerate_sat_per_1000_weight}; use crate::events::bump_transaction::{BumpTransactionEvent, WalletSource}; use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination}; -use crate::ln::channel; +use crate::ln::{channel, ChannelId}; use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, PaymentId, RecipientOnionFields}; use crate::ln::msgs::ChannelMessageHandler; use crate::util::config::UserConfig; -use crate::util::crypto::sign; +use crate::crypto::utils::sign; use crate::util::ser::Writeable; use crate::util::scid_utils::block_from_scid; use crate::util::test_utils; @@ -158,6 +158,60 @@ fn revoked_output_htlc_resolution_timing() { expect_payment_failed!(nodes[1], payment_hash_1, false); } +#[test] +fn archive_fully_resolved_monitors() { + // Test we can archive fully resolved channel monitor. + let chanmon_cfgs = create_chanmon_cfgs(2); + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); + let mut user_config = test_default_channel_config(); + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config), Some(user_config)]); + let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); + + let (_, _, chan_id, funding_tx) = + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000); + + nodes[0].node.close_channel(&chan_id, &nodes[1].node.get_our_node_id()).unwrap(); + let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id()); + nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown); + let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id()); + nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown); + + let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id()); + nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed); + let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id()); + nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed); + let (_, node_0_2nd_closing_signed) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id()); + nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.unwrap()); + let (_, _) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id()); + + let shutdown_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0); + + mine_transaction(&nodes[0], &shutdown_tx[0]); + mine_transaction(&nodes[1], &shutdown_tx[0]); + + connect_blocks(&nodes[0], 6); + connect_blocks(&nodes[1], 6); + + check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 1000000); + check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 1000000); + + assert_eq!(nodes[0].chain_monitor.chain_monitor.list_monitors().len(), 1); + // First archive should set balances_empty_height to current block height + nodes[0].chain_monitor.chain_monitor.archive_fully_resolved_channel_monitors(); + assert_eq!(nodes[0].chain_monitor.chain_monitor.list_monitors().len(), 1); + connect_blocks(&nodes[0], 4032); + // Second call after 4032 blocks, should archive the monitor + nodes[0].chain_monitor.chain_monitor.archive_fully_resolved_channel_monitors(); + // Should have no monitors left + assert_eq!(nodes[0].chain_monitor.chain_monitor.list_monitors().len(), 0); + // Remove the corresponding outputs and transactions the chain source is + // watching. This is to make sure the `Drop` function assertions pass. + nodes.get_mut(0).unwrap().chain_source.remove_watched_txn_and_outputs( + OutPoint { txid: funding_tx.txid(), index: 0 }, + funding_tx.output[0].script_pubkey.clone() + ); +} + fn do_chanmon_claim_value_coop_close(anchors: bool) { // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim. // Specifically, this tests that the channel non-HTLC balances show up in @@ -176,7 +230,7 @@ fn do_chanmon_claim_value_coop_close(anchors: bool) { let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000); let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 }; - assert_eq!(funding_outpoint.to_channel_id(), chan_id); + assert_eq!(ChannelId::v1_from_funding_outpoint(funding_outpoint), chan_id); let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64; let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_id); @@ -209,8 +263,8 @@ fn do_chanmon_claim_value_coop_close(anchors: bool) { assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0)); assert_eq!(shutdown_tx.len(), 1); - let shutdown_tx_conf_height_a = block_from_scid(&mine_transaction(&nodes[0], &shutdown_tx[0])); - let shutdown_tx_conf_height_b = block_from_scid(&mine_transaction(&nodes[1], &shutdown_tx[0])); + let shutdown_tx_conf_height_a = block_from_scid(mine_transaction(&nodes[0], &shutdown_tx[0])); + let shutdown_tx_conf_height_b = block_from_scid(mine_transaction(&nodes[1], &shutdown_tx[0])); assert!(nodes[0].node.list_channels().is_empty()); assert!(nodes[1].node.list_channels().is_empty()); @@ -257,8 +311,8 @@ fn do_chanmon_claim_value_coop_close(anchors: bool) { spendable_outputs_b ); - check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 1000000); - check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 1000000); + check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 1000000); + check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 1000000); } #[test] @@ -327,7 +381,7 @@ fn do_test_claim_value_force_close(anchors: bool, prev_commitment_tx: bool) { let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000); let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 }; - assert_eq!(funding_outpoint.to_channel_id(), chan_id); + assert_eq!(ChannelId::v1_from_funding_outpoint(funding_outpoint), chan_id); // This HTLC is immediately claimed, giving node B the preimage let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000); @@ -736,8 +790,8 @@ fn do_test_balances_on_local_commitment_htlcs(anchors: bool) { check_spends!(commitment_tx, funding_tx); commitment_tx }; - let commitment_tx_conf_height_a = block_from_scid(&mine_transaction(&nodes[0], &commitment_tx)); - if anchors && nodes[0].connect_style.borrow().updates_best_block_first() { + let commitment_tx_conf_height_a = block_from_scid(mine_transaction(&nodes[0], &commitment_tx)); + if nodes[0].connect_style.borrow().updates_best_block_first() { let mut txn = nodes[0].tx_broadcaster.txn_broadcast(); assert_eq!(txn.len(), 1); assert_eq!(txn[0].txid(), commitment_tx.txid()); @@ -1121,7 +1175,7 @@ fn do_test_revoked_counterparty_commitment_balances(anchors: bool, confirm_htlc_ let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000); let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 }; - assert_eq!(funding_outpoint.to_channel_id(), chan_id); + assert_eq!(ChannelId::v1_from_funding_outpoint(funding_outpoint), chan_id); // We create five HTLCs for B to claim against A's revoked commitment transaction: // @@ -1188,14 +1242,14 @@ fn do_test_revoked_counterparty_commitment_balances(anchors: bool, confirm_htlc_ assert!(failed_payments.is_empty()); if let Event::PendingHTLCsForwardable { .. } = events[0] {} else { panic!(); } match &events[1] { - Event::ChannelClosed { reason: ClosureReason::HolderForceClosed, .. } => {}, + Event::ChannelClosed { reason: ClosureReason::HTLCsTimedOut, .. } => {}, _ => panic!(), } connect_blocks(&nodes[1], htlc_cltv_timeout + 1 - 10); check_closed_broadcast!(nodes[1], true); check_added_monitors!(nodes[1], 1); - check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 1000000); + check_closed_event!(nodes[1], 1, ClosureReason::HTLCsTimedOut, [nodes[0].node.get_our_node_id()], 1000000); // Prior to channel closure, B considers the preimage HTLC as its own, and otherwise only // lists the two on-chain timeout-able HTLCs as claimable balances. @@ -1403,7 +1457,7 @@ fn do_test_revoked_counterparty_htlc_tx_balances(anchors: bool) { let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 12_000_000); let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 }; - assert_eq!(funding_outpoint.to_channel_id(), chan_id); + assert_eq!(ChannelId::v1_from_funding_outpoint(funding_outpoint), chan_id); let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0; let failed_payment_hash = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1; @@ -1705,7 +1759,7 @@ fn do_test_revoked_counterparty_aggregated_claims(anchors: bool) { let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000); let funding_outpoint = OutPoint { txid: funding_tx.txid(), index: 0 }; - assert_eq!(funding_outpoint.to_channel_id(), chan_id); + assert_eq!(ChannelId::v1_from_funding_outpoint(funding_outpoint), chan_id); // We create two HTLCs, one which we will give A the preimage to to generate an HTLC-Success // transaction, and one which we will not, allowing B to claim the HTLC output in an aggregated @@ -1998,6 +2052,11 @@ fn do_test_restored_packages_retry(check_old_monitor_retries_after_upgrade: bool }; mine_transaction(&nodes[0], &commitment_tx); + if nodes[0].connect_style.borrow().updates_best_block_first() { + let txn = nodes[0].tx_broadcaster.txn_broadcast(); + assert_eq!(txn.len(), 1); + assert_eq!(txn[0].txid(), commitment_tx.txid()); + } // Connect blocks until the HTLC's expiration is met, expecting a transaction broadcast. connect_blocks(&nodes[0], TEST_FINAL_CLTV); @@ -2401,26 +2460,12 @@ fn test_anchors_aggregated_revoked_htlc_tx() { nodes[1].node.timer_tick_occurred(); check_added_monitors(&nodes[1], 2); check_closed_event!(&nodes[1], 2, ClosureReason::OutdatedChannelManager, [nodes[0].node.get_our_node_id(); 2], 1000000); - let (revoked_commitment_a, revoked_commitment_b) = { - let txn = nodes[1].tx_broadcaster.unique_txn_broadcast(); - assert_eq!(txn.len(), 2); - assert_eq!(txn[0].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs - assert_eq!(txn[1].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs - if txn[0].input[0].previous_output.txid == chan_a.3.txid() { - check_spends!(&txn[0], &chan_a.3); - check_spends!(&txn[1], &chan_b.3); - (txn[0].clone(), txn[1].clone()) - } else { - check_spends!(&txn[1], &chan_a.3); - check_spends!(&txn[0], &chan_b.3); - (txn[1].clone(), txn[0].clone()) - } - }; // Bob should now receive two events to bump his revoked commitment transaction fees. assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty()); let events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events(); assert_eq!(events.len(), 2); + let mut revoked_commitment_txs = Vec::with_capacity(events.len()); let mut anchor_txs = Vec::with_capacity(events.len()); for (idx, event) in events.into_iter().enumerate() { let utxo_value = Amount::ONE_BTC.to_sat() * (idx + 1) as u64; @@ -2440,13 +2485,21 @@ fn test_anchors_aggregated_revoked_htlc_tx() { }; let txn = nodes[1].tx_broadcaster.txn_broadcast(); assert_eq!(txn.len(), 2); + assert_eq!(txn[0].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs + if txn[0].input[0].previous_output.txid == chan_a.3.txid() { + check_spends!(&txn[0], &chan_a.3); + } else { + check_spends!(&txn[0], &chan_b.3); + } let (commitment_tx, anchor_tx) = (&txn[0], &txn[1]); check_spends!(anchor_tx, coinbase_tx, commitment_tx); + + revoked_commitment_txs.push(commitment_tx.clone()); anchor_txs.push(anchor_tx.clone()); }; for node in &nodes { - mine_transactions(node, &[&revoked_commitment_a, &anchor_txs[0], &revoked_commitment_b, &anchor_txs[1]]); + mine_transactions(node, &[&revoked_commitment_txs[0], &anchor_txs[0], &revoked_commitment_txs[1], &anchor_txs[1]]); } check_added_monitors!(&nodes[0], 2); check_closed_broadcast(&nodes[0], 2, true); @@ -2458,7 +2511,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() { let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0); assert_eq!(txn.len(), 4); - let (revoked_htlc_claim_a, revoked_htlc_claim_b) = if txn[0].input[0].previous_output.txid == revoked_commitment_a.txid() { + let (revoked_htlc_claim_a, revoked_htlc_claim_b) = if txn[0].input[0].previous_output.txid == revoked_commitment_txs[0].txid() { (if txn[0].input.len() == 2 { &txn[0] } else { &txn[1] }, if txn[2].input.len() == 2 { &txn[2] } else { &txn[3] }) } else { (if txn[2].input.len() == 2 { &txn[2] } else { &txn[3] }, if txn[0].input.len() == 2 { &txn[0] } else { &txn[1] }) @@ -2466,10 +2519,10 @@ fn test_anchors_aggregated_revoked_htlc_tx() { assert_eq!(revoked_htlc_claim_a.input.len(), 2); // Spends both HTLC outputs assert_eq!(revoked_htlc_claim_a.output.len(), 1); - check_spends!(revoked_htlc_claim_a, revoked_commitment_a); + check_spends!(revoked_htlc_claim_a, revoked_commitment_txs[0]); assert_eq!(revoked_htlc_claim_b.input.len(), 2); // Spends both HTLC outputs assert_eq!(revoked_htlc_claim_b.output.len(), 1); - check_spends!(revoked_htlc_claim_b, revoked_commitment_b); + check_spends!(revoked_htlc_claim_b, revoked_commitment_txs[1]); } // Since Bob was able to confirm his revoked commitment, he'll now try to claim the HTLCs @@ -2549,7 +2602,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() { sig }; htlc_tx.input[0].witness = Witness::from_slice(&[fee_utxo_sig, public_key.to_bytes()]); - check_spends!(htlc_tx, coinbase_tx, revoked_commitment_a, revoked_commitment_b); + check_spends!(htlc_tx, coinbase_tx, revoked_commitment_txs[0], revoked_commitment_txs[1]); htlc_tx }; @@ -2573,7 +2626,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() { check_spends!(revoked_htlc_claim, htlc_tx); } - let mut revoked_claim_transaction_map = HashMap::new(); + let mut revoked_claim_transaction_map = new_hash_map(); for current_tx in txn.into_iter() { revoked_claim_transaction_map.insert(current_tx.txid(), current_tx); } @@ -2608,7 +2661,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() { ).unwrap(); if let SpendableOutputDescriptor::StaticPaymentOutput(_) = &outputs[0] { - check_spends!(spend_tx, &revoked_commitment_a, &revoked_commitment_b); + check_spends!(spend_tx, &revoked_commitment_txs[0], &revoked_commitment_txs[1]); } else { check_spends!(spend_tx, revoked_claim_transactions.get(&spend_tx.input[0].previous_output.txid).unwrap()); } @@ -2675,14 +2728,14 @@ fn do_test_anchors_monitor_fixes_counterparty_payment_script_on_reload(confirm_c // We should expect our round trip serialization check to fail as we're writing the monitor // with the incorrect P2WPKH script but reading it with the correct P2WSH script. *nodes[1].chain_monitor.expect_monitor_round_trip_fail.lock().unwrap() = Some(chan_id); - let commitment_tx_conf_height = block_from_scid(&mine_transaction(&nodes[1], &commitment_tx)); + let commitment_tx_conf_height = block_from_scid(mine_transaction(&nodes[1], &commitment_tx)); let serialized_monitor = get_monitor!(nodes[1], chan_id).encode(); reload_node!(nodes[1], user_config, &nodes[1].node.encode(), &[&serialized_monitor], persister, chain_monitor, node_deserialized); commitment_tx_conf_height } else { let serialized_monitor = get_monitor!(nodes[1], chan_id).encode(); reload_node!(nodes[1], user_config, &nodes[1].node.encode(), &[&serialized_monitor], persister, chain_monitor, node_deserialized); - let commitment_tx_conf_height = block_from_scid(&mine_transaction(&nodes[1], &commitment_tx)); + let commitment_tx_conf_height = block_from_scid(mine_transaction(&nodes[1], &commitment_tx)); check_added_monitors(&nodes[1], 1); check_closed_broadcast(&nodes[1], 1, true); commitment_tx_conf_height @@ -2756,7 +2809,9 @@ fn do_test_monitor_claims_with_random_signatures(anchors: bool, confirm_counterp (&nodes[0], &nodes[1]) }; - closing_node.node.force_close_broadcasting_latest_txn(&chan_id, &other_node.node.get_our_node_id()).unwrap(); + get_monitor!(closing_node, chan_id).broadcast_latest_holder_commitment_txn( + &closing_node.tx_broadcaster, &closing_node.fee_estimator, &closing_node.logger + ); // The commitment transaction comes first. let commitment_tx = { @@ -2769,7 +2824,7 @@ fn do_test_monitor_claims_with_random_signatures(anchors: bool, confirm_counterp mine_transaction(closing_node, &commitment_tx); check_added_monitors!(closing_node, 1); check_closed_broadcast!(closing_node, true); - check_closed_event!(closing_node, 1, ClosureReason::HolderForceClosed, [other_node.node.get_our_node_id()], 1_000_000); + check_closed_event!(closing_node, 1, ClosureReason::CommitmentTxConfirmed, [other_node.node.get_our_node_id()], 1_000_000); mine_transaction(other_node, &commitment_tx); check_added_monitors!(other_node, 1); @@ -2778,7 +2833,7 @@ fn do_test_monitor_claims_with_random_signatures(anchors: bool, confirm_counterp // If we update the best block to the new height before providing the confirmed transactions, // we'll see another broadcast of the commitment transaction. - if anchors && !confirm_counterparty_commitment && nodes[0].connect_style.borrow().updates_best_block_first() { + if !confirm_counterparty_commitment && nodes[0].connect_style.borrow().updates_best_block_first() { let _ = nodes[0].tx_broadcaster.txn_broadcast(); } @@ -2796,11 +2851,7 @@ fn do_test_monitor_claims_with_random_signatures(anchors: bool, confirm_counterp let htlc_timeout_tx = { let mut txn = nodes[0].tx_broadcaster.txn_broadcast(); assert_eq!(txn.len(), 1); - let tx = if txn[0].input[0].previous_output.txid == commitment_tx.txid() { - txn[0].clone() - } else { - txn[1].clone() - }; + let tx = txn.pop().unwrap(); check_spends!(tx, commitment_tx, coinbase_tx); tx };