X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Freorg_tests.rs;h=fb316caf2ef1008012ca57ffc8722ee3e9e412ef;hb=8cc9410ae176f415d90e8ed8afe590455db63dc9;hp=46400641bc178ba21d6e41366f62fe0cbaaef4f9;hpb=4fc05af870b9598adccb39ef96a79cd1635baf84;p=rust-lightning diff --git a/lightning/src/ln/reorg_tests.rs b/lightning/src/ln/reorg_tests.rs index 46400641..fb316caf 100644 --- a/lightning/src/ln/reorg_tests.rs +++ b/lightning/src/ln/reorg_tests.rs @@ -10,21 +10,21 @@ //! 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::events::{Event, MessageSendEvent, MessageSendEventsProvider}; use util::test_utils; use util::ser::{ReadableArgs, Writeable}; use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::hash_types::BlockHash; +use prelude::*; use std::collections::HashMap; -use std::mem; +use core::mem; use ln::functional_test_utils::*; @@ -57,10 +57,10 @@ 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); + nodes[2].node.claim_funds(our_payment_preimage); check_added_monitors!(nodes[2], 1); get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id()); @@ -76,7 +76,7 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { // 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() }); 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) @@ -102,21 +102,22 @@ fn do_test_onchain_htlc_reorg(local_commitment: bool, claim: bool) { // Give node 1 node 2's commitment transaction and get its response (timing the HTLC out) mine_transaction(&nodes[1], &node_2_commitment_txn[0]); - let node_1_commitment_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); - assert_eq!(node_1_commitment_txn.len(), 3); // ChannelMonitor: 1 offered HTLC-Timeout, ChannelManger: 1 local commitment tx, 1 Offered HTLC-Timeout - assert_eq!(node_1_commitment_txn[1].output.len(), 2); // to-local and Offered HTLC (to-remote is dust) - check_spends!(node_1_commitment_txn[1], chan_2.3); - check_spends!(node_1_commitment_txn[2], node_1_commitment_txn[1]); - check_spends!(node_1_commitment_txn[0], node_2_commitment_txn[0]); + connect_blocks(&nodes[1], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires + let node_1_commitment_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); + assert_eq!(node_1_commitment_txn.len(), 2); // ChannelMonitor: 1 offered HTLC-Timeout, ChannelManger: 1 local commitment tx + assert_eq!(node_1_commitment_txn[0].output.len(), 2); // to-local and Offered HTLC (to-remote is dust) + check_spends!(node_1_commitment_txn[0], chan_2.3); + check_spends!(node_1_commitment_txn[1], node_2_commitment_txn[0]); // 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()] }); + let block = Block { header, txdata: vec![node_2_commitment_txn[0].clone(), node_1_commitment_txn[1].clone()] }; + connect_block(&nodes[1], &block); // ...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); @@ -184,7 +185,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 +196,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 +206,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 +245,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 +262,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 +295,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); } #[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] @@ -303,15 +360,15 @@ fn test_set_outpoints_partial_claiming() { // Connect blocks on node A to advance height towards TEST_FINAL_CLTV // Provide node A with both preimage - nodes[0].node.claim_funds(payment_preimage_1, &None, 3_000_000); - nodes[0].node.claim_funds(payment_preimage_2, &None, 3_000_000); + nodes[0].node.claim_funds(payment_preimage_1); + nodes[0].node.claim_funds(payment_preimage_2); check_added_monitors!(nodes[0], 2); nodes[0].node.get_and_clear_pending_events(); nodes[0].node.get_and_clear_pending_msg_events(); // 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 +385,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 = {