X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Freorg_tests.rs;h=012d480f2186500914e4296b390674f7986ee639;hb=6e5cf5e8d4c51d16dea27910c41fa17879724dee;hp=c04d28c06048e481bf80d2cf43de8a4a144a8878;hpb=f25a46cf42f9a057dfe0bf840cc9efb2e4f8740b;p=rust-lightning diff --git a/lightning/src/ln/reorg_tests.rs b/lightning/src/ln/reorg_tests.rs index c04d28c0..012d480f 100644 --- a/lightning/src/ln/reorg_tests.rs +++ b/lightning/src/ln/reorg_tests.rs @@ -10,11 +10,10 @@ //! Further functional tests which test blockchain reorganizations. use chain::channelmonitor::{ANTI_REORG_DELAY, ChannelMonitor}; -use chain::Watch; +use chain::{Confirm, Watch}; use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs}; use ln::features::InitFeatures; use ln::msgs::{ChannelMessageHandler, ErrorAction, HTLCFailChannelUpdate}; -use util::config::UserConfig; use util::enforcing_trait_impls::EnforcingSigner; use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider}; use util::test_utils; @@ -57,7 +56,7 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { 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]], 1000000); + let (our_payment_preimage, our_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000); // Provide preimage to node 2 by claiming payment nodes[2].node.claim_funds(our_payment_preimage, &None, 1000000); @@ -74,9 +73,9 @@ 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], &Block { header, txdata: node_1_commitment_txn.clone() }, CHAN_CONFIRM_DEPTH*2 + 2); + connect_block(&nodes[2], &Block { header, txdata: node_1_commitment_txn.clone() }); check_added_monitors!(nodes[2], 1); - check_closed_broadcast!(nodes[2], false); // We should get a BroadcastChannelUpdate (and *only* a BroadcstChannelUpdate) + check_closed_broadcast!(nodes[2], true); // We should get a BroadcastChannelUpdate (and *only* a BroadcstChannelUpdate) let node_2_commitment_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_2_commitment_txn.len(), 3); // ChannelMonitor: 1 offered HTLC-Claim, ChannelManger: 1 local commitment tx, 1 Received HTLC-Claim assert_eq!(node_2_commitment_txn[1].output.len(), 2); // to-remote and Received HTLC (to-self is dust) @@ -88,7 +87,7 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { connect_blocks(&nodes[1], 1); // Confirm node 1's commitment txn (and HTLC-Timeout) on node 1 header.prev_blockhash = nodes[1].best_block_hash(); - connect_block(&nodes[1], &Block { header, txdata: node_1_commitment_txn.clone() }, CHAN_CONFIRM_DEPTH*2 + 3); + connect_block(&nodes[1], &Block { header, txdata: node_1_commitment_txn.clone() }); // ...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()] @@ -111,12 +110,12 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { // Confirm node 2's commitment txn (and node 1's HTLC-Timeout) on node 1 header.prev_blockhash = nodes[1].best_block_hash(); - connect_block(&nodes[1], &Block { header, txdata: vec![node_2_commitment_txn[0].clone(), node_1_commitment_txn[0].clone()] }, CHAN_CONFIRM_DEPTH*2 + 3); + connect_block(&nodes[1], &Block { header, txdata: vec![node_2_commitment_txn[0].clone(), node_1_commitment_txn[0].clone()] }); // ...but return node 2's commitment tx (and claim) in case claim is set and we're preparing to reorg node_2_commitment_txn }; check_added_monitors!(nodes[1], 1); - check_closed_broadcast!(nodes[1], false); // We should get a BroadcastChannelUpdate (and *only* a BroadcstChannelUpdate) + check_closed_broadcast!(nodes[1], true); // We should get a BroadcastChannelUpdate (and *only* a BroadcstChannelUpdate) // 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); @@ -129,7 +128,7 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { header: BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }, txdata: claim_txn, }; - connect_block(&nodes[1], &block, CHAN_CONFIRM_DEPTH*2 + 4); + connect_block(&nodes[1], &block); // ChannelManager only polls chain::Watch::release_pending_monitor_events when we // probe it for events, so we probe non-message events here (which should still end up empty): @@ -140,7 +139,7 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { header: BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }, txdata: vec![], }; - connect_block(&nodes[1], &block, CHAN_CONFIRM_DEPTH*2 + 3 + ANTI_REORG_DELAY - 1); + connect_block(&nodes[1], &block); expect_pending_htlcs_forwardable!(nodes[1]); } @@ -184,7 +183,7 @@ fn test_onchain_htlc_timeout_delay_remote_commitment() { do_test_onchain_htlc_reorg(false, false); } -fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool) { +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. @@ -195,7 +194,9 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool) { let new_chain_monitor: test_utils::TestChainMonitor; let nodes_0_deserialized: ChannelManager; let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); - let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; + *nodes[0].connect_style.borrow_mut() = connect_style; + + let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); let channel_state = nodes[0].node.channel_state.lock().unwrap(); assert_eq!(channel_state.by_id.len(), 1); @@ -203,8 +204,19 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool) { mem::drop(channel_state); if !reorg_after_reload { - disconnect_all_blocks(&nodes[0]); - check_closed_broadcast!(nodes[0], false); + if use_funding_unconfirmed { + let relevant_txids = nodes[0].node.get_relevant_txids(); + assert_eq!(&relevant_txids[..], &[chan.3.txid()]); + nodes[0].node.transaction_unconfirmed(&relevant_txids[0]); + } else { + disconnect_all_blocks(&nodes[0]); + } + if connect_style == ConnectStyle::FullBlockViaListen && !use_funding_unconfirmed { + handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Funding transaction was un-confirmed. Locked at 6 confs, now have 2 confs."); + } else { + handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs."); + } + check_added_monitors!(nodes[1], 1); { let channel_state = nodes[0].node.channel_state.lock().unwrap(); assert_eq!(channel_state.by_id.len(), 0); @@ -231,14 +243,13 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool) { assert!(chan_0_monitor_read.is_empty()); let mut nodes_0_read = &nodes_0_serialized[..]; - let config = UserConfig::default(); nodes_0_deserialized = { let mut channel_monitors = HashMap::new(); channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor); <(BlockHash, ChannelManager)>::read( &mut nodes_0_read, ChannelManagerReadArgs { - default_config: config, + default_config: *nodes[0].node.get_current_default_configuration(), keys_manager, fee_estimator: node_cfgs[0].fee_estimator, chain_monitor: nodes[0].chain_monitor, @@ -249,14 +260,31 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool) { }; nodes[0].node = &nodes_0_deserialized; assert!(nodes_0_read.is_empty()); + if !reorg_after_reload { + // If the channel is already closed when we reload the node, we'll broadcast a closing + // transaction via the ChannelMonitor which is missing a corresponding channel. + assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1); + nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear(); + } nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0.clone(), chan_0_monitor).unwrap(); check_added_monitors!(nodes[0], 1); } if reorg_after_reload { - disconnect_all_blocks(&nodes[0]); - check_closed_broadcast!(nodes[0], false); + if use_funding_unconfirmed { + let relevant_txids = nodes[0].node.get_relevant_txids(); + assert_eq!(&relevant_txids[..], &[chan.3.txid()]); + nodes[0].node.transaction_unconfirmed(&relevant_txids[0]); + } else { + disconnect_all_blocks(&nodes[0]); + } + if connect_style == ConnectStyle::FullBlockViaListen && !use_funding_unconfirmed { + handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Funding transaction was un-confirmed. Locked at 6 confs, now have 2 confs."); + } else { + handle_announce_close_broadcast_events(&nodes, 0, 1, true, "Funding transaction was un-confirmed. Locked at 6 confs, now have 0 confs."); + } + check_added_monitors!(nodes[1], 1); { let channel_state = nodes[0].node.channel_state.lock().unwrap(); assert_eq!(channel_state.by_id.len(), 0); @@ -265,17 +293,44 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool) { } // With expect_channel_force_closed set the TestChainMonitor will enforce that the next update // is a ChannelForcClosed on the right channel with should_broadcast set. - *nodes[0].chain_monitor.expect_channel_force_closed.lock().unwrap() = Some((chan_id, true)); + *nodes[0].chain_monitor.expect_channel_force_closed.lock().unwrap() = Some((chan.2, true)); nodes[0].node.test_process_background_events(); // Required to free the pending background monitor update check_added_monitors!(nodes[0], 1); + assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1); + nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear(); + + // Now check that we can create a new channel + create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); + send_payment(&nodes[0], &[&nodes[1]], 8000000, 8_000_000); } #[test] fn test_unconf_chan() { - do_test_unconf_chan(true, true); - do_test_unconf_chan(false, true); - do_test_unconf_chan(true, false); - do_test_unconf_chan(false, false); + do_test_unconf_chan(true, true, false, ConnectStyle::BestBlockFirstSkippingBlocks); + do_test_unconf_chan(false, true, false, ConnectStyle::BestBlockFirstSkippingBlocks); + do_test_unconf_chan(true, false, false, ConnectStyle::BestBlockFirstSkippingBlocks); + do_test_unconf_chan(false, false, false, ConnectStyle::BestBlockFirstSkippingBlocks); +} + +#[test] +fn test_unconf_chan_via_listen() { + do_test_unconf_chan(true, true, false, ConnectStyle::FullBlockViaListen); + do_test_unconf_chan(false, true, false, ConnectStyle::FullBlockViaListen); + do_test_unconf_chan(true, false, false, ConnectStyle::FullBlockViaListen); + do_test_unconf_chan(false, false, false, ConnectStyle::FullBlockViaListen); +} + +#[test] +fn test_unconf_chan_via_funding_unconfirmed() { + do_test_unconf_chan(true, true, true, ConnectStyle::BestBlockFirstSkippingBlocks); + do_test_unconf_chan(false, true, true, ConnectStyle::BestBlockFirstSkippingBlocks); + do_test_unconf_chan(true, false, true, ConnectStyle::BestBlockFirstSkippingBlocks); + do_test_unconf_chan(false, false, true, ConnectStyle::BestBlockFirstSkippingBlocks); + + do_test_unconf_chan(true, true, true, ConnectStyle::FullBlockViaListen); + do_test_unconf_chan(false, true, true, ConnectStyle::FullBlockViaListen); + do_test_unconf_chan(true, false, true, ConnectStyle::FullBlockViaListen); + do_test_unconf_chan(false, false, true, ConnectStyle::FullBlockViaListen); } #[test] @@ -311,7 +366,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], false); + check_closed_broadcast!(nodes[0], true); check_added_monitors!(nodes[0], 1); // Verify node A broadcast tx claiming both HTLCs { @@ -328,7 +383,7 @@ fn test_set_outpoints_partial_claiming() { // Connect blocks on node B connect_blocks(&nodes[1], 135); - check_closed_broadcast!(nodes[1], false); + check_closed_broadcast!(nodes[1], true); check_added_monitors!(nodes[1], 1); // Verify node B broadcast 2 HTLC-timeout txn let partial_claim_tx = {