X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fmonitor_tests.rs;h=761d4b5316c69b4a728c60f654733ccf5330ab3b;hb=960dd658db6e66edd5255a5a4bddcbeb89a1e238;hp=d5f0dc153fc1a1ba9c544c4cfeb552708f860e04;hpb=b8b1ef3149f26992625a03d45c0307bfad70e8bd;p=rust-lightning diff --git a/lightning/src/ln/monitor_tests.rs b/lightning/src/ln/monitor_tests.rs index d5f0dc15..761d4b53 100644 --- a/lightning/src/ln/monitor_tests.rs +++ b/lightning/src/ln/monitor_tests.rs @@ -9,16 +9,16 @@ //! 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, ChannelId}; +use crate::ln::channel; +use crate::ln::types::ChannelId; use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, PaymentId, RecipientOnionFields}; use crate::ln::msgs::ChannelMessageHandler; -use crate::util::config::UserConfig; use crate::crypto::utils::sign; use crate::util::ser::Writeable; use crate::util::scid_utils::block_from_scid; @@ -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 @@ -2195,7 +2249,7 @@ fn test_yield_anchors_events() { // emitted by LDK, such that the consumer can attach fees to the zero fee HTLC transactions. let mut chanmon_cfgs = create_chanmon_cfgs(2); let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); - let mut anchors_config = UserConfig::default(); + let mut anchors_config = test_default_channel_config(); anchors_config.channel_handshake_config.announced_channel = true; anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true; anchors_config.manually_accept_inbound_channels = true; @@ -2346,7 +2400,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() { let bob_persister; let bob_chain_monitor; - let mut anchors_config = UserConfig::default(); + let mut anchors_config = test_default_channel_config(); anchors_config.channel_handshake_config.announced_channel = true; anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true; anchors_config.manually_accept_inbound_channels = true; @@ -2823,3 +2877,40 @@ fn test_monitor_claims_with_random_signatures() { do_test_monitor_claims_with_random_signatures(true, false); do_test_monitor_claims_with_random_signatures(true, true); } + +#[test] +fn test_event_replay_causing_monitor_replay() { + // In LDK 0.0.121 there was a bug where if a `PaymentSent` event caused an RAA + // `ChannelMonitorUpdate` hold and then the node was restarted after the `PaymentSent` event + // and `ChannelMonitorUpdate` both completed but without persisting the `ChannelManager` we'd + // replay the `ChannelMonitorUpdate` on restart (which is fine, but triggered a safety panic). + let chanmon_cfgs = create_chanmon_cfgs(2); + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); + let persister; + let new_chain_monitor; + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); + let node_deserialized; + let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); + + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000); + + let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 1_000_000).0; + + do_claim_payment_along_route(&nodes[0], &[&[&nodes[1]]], false, payment_preimage); + + // At this point the `PaymentSent` event has not been processed but the full commitment signed + // dance has completed. + let serialized_channel_manager = nodes[0].node.encode(); + + // Now process the `PaymentSent` to get the final RAA `ChannelMonitorUpdate`, checking that it + // resulted in a `ChannelManager` persistence request. + nodes[0].node.get_and_clear_needs_persistence(); + expect_payment_sent(&nodes[0], payment_preimage, None, true, true /* expected post-event monitor update*/); + assert!(nodes[0].node.get_and_clear_needs_persistence()); + + let serialized_monitor = get_monitor!(nodes[0], chan.2).encode(); + reload_node!(nodes[0], &serialized_channel_manager, &[&serialized_monitor], persister, new_chain_monitor, node_deserialized); + + // Expect the `PaymentSent` to get replayed, this time without the duplicate monitor update + expect_payment_sent(&nodes[0], payment_preimage, None, false, false /* expected post-event monitor update*/); +}