X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Ffunctional_tests.rs;h=4e5d726392186e8a18c9cf452f5ae8d61d316558;hb=edab29e8d834db2b2015b5aaff1c6ff3828ab048;hp=1648538fac81382f77a15310a907523560030e4e;hpb=fd1d5fd62c748ced131c3c7f8c0d9334c49884cc;p=rust-lightning diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 1648538f..4e5d7263 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -4,8 +4,7 @@ use chain::transaction::OutPoint; use chain::chaininterface::{ChainListener, ChainWatchInterfaceUtil}; -use chain::keysinterface::{KeysInterface, SpendableOutputDescriptor, KeysManager}; -use chain::keysinterface; +use chain::keysinterface::{KeysInterface, SpendableOutputDescriptor}; use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC}; use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,HTLCForwardInfo,RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT}; use ln::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ManyChannelMonitor, ANTI_REORG_DELAY}; @@ -14,6 +13,7 @@ use ln::onion_utils; use ln::router::{Route, RouteHop}; use ln::msgs; use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler,HTLCFailChannelUpdate, LocalFeatures, ErrorAction}; +use util::enforcing_trait_impls::EnforcingChannelKeys; use util::test_utils; use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider}; use util::errors::APIError; @@ -57,7 +57,7 @@ fn test_insane_channel_opens() { // Instantiate channel parameters where we push the maximum msats given our // funding satoshis let channel_value_sat = 31337; // same as funding satoshis - let channel_reserve_satoshis = Channel::get_our_channel_reserve_satoshis(channel_value_sat); + let channel_reserve_satoshis = Channel::::get_our_channel_reserve_satoshis(channel_value_sat); let push_msat = (channel_value_sat - channel_reserve_satoshis) * 1000; // Have node0 initiate a channel to node1 with aforementioned parameters @@ -1783,8 +1783,10 @@ fn channel_monitor_network_test() { // nodes[3] gets the preimage, but nodes[2] already disconnected, resulting in a nodes[2] // HTLC-Timeout and a nodes[3] claim against it (+ its own announces) nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id(), true); + let node2_commitment_txid; { let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT); + node2_commitment_txid = node_txn[0].txid(); // Claim the payment on nodes[3], giving it knowledge of the preimage claim_funds!(nodes[3], nodes[2], payment_preimage_1, 3_000_000); @@ -1818,6 +1820,16 @@ fn channel_monitor_network_test() { nodes[3].block_notifier.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]); } + // Clear bumped claiming txn spending node 2 commitment tx. Bumped txn are generated after reaching some height timer. + { + let mut node_txn = nodes[3].tx_broadcaster.txn_broadcasted.lock().unwrap(); + node_txn.retain(|tx| { + if tx.input[0].previous_output.txid == node2_commitment_txid { + false + } else { true } + }); + } + let node_txn = test_txn_broadcast(&nodes[3], &chan_4, None, HTLCType::TIMEOUT); // Claim the payment on nodes[4], giving it knowledge of the preimage @@ -1846,11 +1858,11 @@ fn channel_monitor_network_test() { #[test] fn test_justice_tx() { // Test justice txn built on revoked HTLC-Success tx, against both sides - let mut alice_config = UserConfig::new(); + let mut alice_config = UserConfig::default(); alice_config.channel_options.announced_channel = true; alice_config.peer_channel_config_limits.force_announced_channel_preference = false; alice_config.own_channel_config.our_to_self_delay = 6 * 24 * 5; - let mut bob_config = UserConfig::new(); + let mut bob_config = UserConfig::default(); bob_config.channel_options.announced_channel = true; bob_config.peer_channel_config_limits.force_announced_channel_preference = false; bob_config.own_channel_config.our_to_self_delay = 6 * 24 * 3; @@ -1884,6 +1896,7 @@ fn test_justice_tx() { check_spends!(node_txn[0], revoked_local_txn[0].clone()); node_txn.swap_remove(0); + node_txn.truncate(1); } test_txn_broadcast(&nodes[1], &chan_5, None, HTLCType::NONE); @@ -1891,7 +1904,7 @@ fn test_justice_tx() { let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::TIMEOUT); header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; nodes[1].block_notifier.block_connected(&Block { header, txdata: vec![node_txn[1].clone()] }, 1); - test_revoked_htlc_claim_txn_broadcast(&nodes[1], node_txn[1].clone()); + test_revoked_htlc_claim_txn_broadcast(&nodes[1], node_txn[1].clone(), revoked_local_txn[0].clone()); } get_announce_close_broadcast_events(&nodes, 0, 1); @@ -1901,6 +1914,10 @@ fn test_justice_tx() { // We test justice_tx build by A on B's revoked HTLC-Success tx // Create some new channels: let chan_6 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new()); + { + let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); + node_txn.clear(); + } // A pending HTLC which will be revoked: let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0; @@ -1930,7 +1947,7 @@ fn test_justice_tx() { let node_txn = test_txn_broadcast(&nodes[1], &chan_6, Some(revoked_local_txn[0].clone()), HTLCType::SUCCESS); header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; nodes[0].block_notifier.block_connected(&Block { header, txdata: vec![node_txn[1].clone()] }, 1); - test_revoked_htlc_claim_txn_broadcast(&nodes[0], node_txn[1].clone()); + test_revoked_htlc_claim_txn_broadcast(&nodes[0], node_txn[1].clone(), revoked_local_txn[0].clone()); } get_announce_close_broadcast_events(&nodes, 0, 1); assert_eq!(nodes[0].node.list_channels().len(), 0); @@ -2078,7 +2095,7 @@ fn claim_htlc_outputs_single_tx() { } let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); - assert_eq!(node_txn.len(), 22); // ChannelManager : 2, ChannelMontitor: 8 (1 standard revoked output, 2 revocation htlc tx, 1 local commitment tx + 1 htlc timeout tx) * 2 (block-rescan) + 5 * (1 local commitment tx + 1 htlc timeout tx) + assert_eq!(node_txn.len(), 29); // ChannelManager : 2, ChannelMontitor: 8 (1 standard revoked output, 2 revocation htlc tx, 1 local commitment tx + 1 htlc timeout tx) * 2 (block-rescan) + 5 * (1 local commitment tx + 1 htlc timeout tx) assert_eq!(node_txn[0], node_txn[7]); assert_eq!(node_txn[1], node_txn[8]); @@ -2088,10 +2105,6 @@ fn claim_htlc_outputs_single_tx() { assert_eq!(node_txn[3], node_txn[5]); //local commitment tx + htlc timeout tx broadcasted by ChannelManger assert_eq!(node_txn[4], node_txn[6]); - for i in 12..22 { - if i % 2 == 0 { assert_eq!(node_txn[3], node_txn[i]); } else { assert_eq!(node_txn[4], node_txn[i]); } - } - assert_eq!(node_txn[0].input.len(), 1); assert_eq!(node_txn[1].input.len(), 1); assert_eq!(node_txn[2].input.len(), 1); @@ -3311,8 +3324,8 @@ fn test_invalid_channel_announcement() { let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } ); - let as_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &as_chan.get_local_keys().funding_key); - let bs_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &bs_chan.get_local_keys().funding_key); + let as_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &as_chan.get_local_keys().inner.funding_key); + let bs_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &bs_chan.get_local_keys().inner.funding_key); let as_network_key = nodes[0].node.get_our_node_id(); let bs_network_key = nodes[1].node.get_our_node_id(); @@ -3339,8 +3352,8 @@ fn test_invalid_channel_announcement() { macro_rules! sign_msg { ($unsigned_msg: expr) => { let msghash = Message::from_slice(&Sha256dHash::hash(&$unsigned_msg.encode()[..])[..]).unwrap(); - let as_bitcoin_sig = secp_ctx.sign(&msghash, &as_chan.get_local_keys().funding_key); - let bs_bitcoin_sig = secp_ctx.sign(&msghash, &bs_chan.get_local_keys().funding_key); + let as_bitcoin_sig = secp_ctx.sign(&msghash, &as_chan.get_local_keys().inner.funding_key); + let bs_bitcoin_sig = secp_ctx.sign(&msghash, &bs_chan.get_local_keys().inner.funding_key); let as_node_sig = secp_ctx.sign(&msghash, &nodes[0].keys_manager.get_node_secret()); let bs_node_sig = secp_ctx.sign(&msghash, &nodes[1].keys_manager.get_node_secret()); chan_announcement = msgs::ChannelAnnouncement { @@ -3388,12 +3401,12 @@ fn test_no_txn_manager_serialize_deserialize() { assert!(chan_0_monitor_read.is_empty()); let mut nodes_0_read = &nodes_0_serialized[..]; - let config = UserConfig::new(); + let config = UserConfig::default(); let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new()))); let (_, nodes_0_deserialized) = { let mut channel_monitors = HashMap::new(); channel_monitors.insert(chan_0_monitor.get_funding_txo().unwrap(), &chan_0_monitor); - <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs { + <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs { default_config: config, keys_manager, fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }), @@ -3457,8 +3470,8 @@ fn test_simple_manager_serialize_deserialize() { let (_, nodes_0_deserialized) = { let mut channel_monitors = HashMap::new(); channel_monitors.insert(chan_0_monitor.get_funding_txo().unwrap(), &chan_0_monitor); - <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs { - default_config: UserConfig::new(), + <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs { + default_config: UserConfig::default(), keys_manager, fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }), monitor: nodes[0].chan_monitor.clone(), @@ -3517,8 +3530,8 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() { let mut nodes_0_read = &nodes_0_serialized[..]; let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new()))); - let (_, nodes_0_deserialized) = <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs { - default_config: UserConfig::new(), + let (_, nodes_0_deserialized) = <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs { + default_config: UserConfig::default(), keys_manager, fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }), monitor: nodes[0].chan_monitor.clone(), @@ -3841,7 +3854,7 @@ fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() { check_closed_broadcast!(nodes[1]); let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); - assert_eq!(node_txn.len(), 4); + assert_eq!(node_txn.len(), 5); assert_eq!(node_txn[3].input.len(), 1); check_spends!(node_txn[3], revoked_htlc_txn[0].clone()); @@ -5948,7 +5961,7 @@ fn test_upfront_shutdown_script() { // BOLT 2 : Option upfront shutdown script, if peer commit its closing_script at channel opening // enforce it at shutdown message - let mut config = UserConfig::new(); + let mut config = UserConfig::default(); config.channel_options.announced_channel = true; config.peer_channel_config_limits.force_announced_channel_preference = false; config.channel_options.commit_upfront_shutdown_pubkey = false; @@ -6046,15 +6059,15 @@ fn test_upfront_shutdown_script() { fn test_user_configurable_csv_delay() { // We test our channel constructors yield errors when we pass them absurd csv delay - let mut low_our_to_self_config = UserConfig::new(); + let mut low_our_to_self_config = UserConfig::default(); low_our_to_self_config.own_channel_config.our_to_self_delay = 6; - let mut high_their_to_self_config = UserConfig::new(); + let mut high_their_to_self_config = UserConfig::default(); high_their_to_self_config.peer_channel_config_limits.their_to_self_delay = 100; let cfgs = [Some(high_their_to_self_config.clone()), None]; let nodes = create_network(2, &cfgs); // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_outbound() - let keys_manager: Arc = Arc::new(KeysManager::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new()), 10, 20)); + let keys_manager: Arc> = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new()))); if let Err(error) = Channel::new_outbound(&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), 1000000, 1000000, 0, Arc::new(test_utils::TestLogger::new()), &low_our_to_self_config) { match error { APIError::APIMisuseError { err } => { assert_eq!(err, "Configured with an unreasonable our_to_self_delay putting user funds at risks"); }, @@ -6129,13 +6142,13 @@ fn test_data_loss_protect() { let monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone())); let mut channel_monitors = HashMap::new(); channel_monitors.insert(OutPoint { txid: chan.3.txid(), index: 0 }, &chan_monitor); - let node_state_0 = <(Sha256dHash, ChannelManager)>::read(&mut ::std::io::Cursor::new(previous_node_state), ChannelManagerReadArgs { - keys_manager: Arc::new(keysinterface::KeysManager::new(&nodes[0].node_seed, Network::Testnet, Arc::clone(&logger), 42, 21)), + let node_state_0 = <(Sha256dHash, ChannelManager)>::read(&mut ::std::io::Cursor::new(previous_node_state), ChannelManagerReadArgs { + keys_manager: Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::clone(&logger))), fee_estimator: feeest.clone(), monitor: monitor.clone(), logger: Arc::clone(&logger), tx_broadcaster, - default_config: UserConfig::new(), + default_config: UserConfig::default(), channel_monitors: &channel_monitors }).unwrap().1; nodes[0].node = Arc::new(node_state_0); @@ -6316,3 +6329,482 @@ fn test_announce_disable_channels() { let msg_events = nodes[0].node.get_and_clear_pending_msg_events(); assert_eq!(msg_events.len(), 0); } + +#[test] +fn test_bump_penalty_txn_on_revoked_commitment() { + // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to be sure + // we're able to claim outputs on revoked commitment transaction before timelocks expiration + + let nodes = create_network(2, &[None, None]); + + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, LocalFeatures::new(), LocalFeatures::new()); + let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0; + let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 3000000, 30).unwrap(); + send_along_route(&nodes[1], route, &vec!(&nodes[0])[..], 3000000); + + let revoked_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone(); + // Revoked commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC + assert_eq!(revoked_txn[0].output.len(), 4); + assert_eq!(revoked_txn[0].input.len(), 1); + assert_eq!(revoked_txn[0].input[0].previous_output.txid, chan.3.txid()); + let revoked_txid = revoked_txn[0].txid(); + + let mut penalty_sum = 0; + for outp in revoked_txn[0].output.iter() { + if outp.script_pubkey.is_v0_p2wsh() { + penalty_sum += outp.value; + } + } + + // Connect blocks to change height_timer range to see if we use right soonest_timelock + let header_114 = connect_blocks(&nodes[1].block_notifier, 114, 0, false, Default::default()); + + // Actually revoke tx by claiming a HTLC + claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage, 3_000_000); + let header = BlockHeader { version: 0x20000000, prev_blockhash: header_114, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + nodes[1].block_notifier.block_connected(&Block { header, txdata: vec![revoked_txn[0].clone()] }, 115); + + // One or more justice tx should have been broadcast, check it + let penalty_1; + let feerate_1; + { + let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 4); // justice tx (broadcasted from ChannelMonitor) * 2 (block-reparsing) + local commitment tx + local HTLC-timeout (broadcasted from ChannelManager) + assert_eq!(node_txn[0], node_txn[3]); + assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs + assert_eq!(node_txn[0].output.len(), 1); + check_spends!(node_txn[0], revoked_txn[0].clone()); + let fee_1 = penalty_sum - node_txn[0].output[0].value; + feerate_1 = fee_1 * 1000 / node_txn[0].get_weight() as u64; + penalty_1 = node_txn[0].txid(); + node_txn.clear(); + }; + + // After exhaustion of height timer, a new bumped justice tx should have been broadcast, check it + let header = connect_blocks(&nodes[1].block_notifier, 3, 115, true, header.bitcoin_hash()); + let mut penalty_2 = penalty_1; + let mut feerate_2 = 0; + { + let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 1); + if node_txn[0].input[0].previous_output.txid == revoked_txid { + assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs + assert_eq!(node_txn[0].output.len(), 1); + check_spends!(node_txn[0], revoked_txn[0].clone()); + penalty_2 = node_txn[0].txid(); + // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast + assert_ne!(penalty_2, penalty_1); + let fee_2 = penalty_sum - node_txn[0].output[0].value; + feerate_2 = fee_2 * 1000 / node_txn[0].get_weight() as u64; + // Verify 25% bump heuristic + assert!(feerate_2 * 100 >= feerate_1 * 125); + node_txn.clear(); + } + } + assert_ne!(feerate_2, 0); + + // After exhaustion of height timer for a 2nd time, a new bumped justice tx should have been broadcast, check it + connect_blocks(&nodes[1].block_notifier, 3, 118, true, header); + let penalty_3; + let mut feerate_3 = 0; + { + let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 1); + if node_txn[0].input[0].previous_output.txid == revoked_txid { + assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs + assert_eq!(node_txn[0].output.len(), 1); + check_spends!(node_txn[0], revoked_txn[0].clone()); + penalty_3 = node_txn[0].txid(); + // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast + assert_ne!(penalty_3, penalty_2); + let fee_3 = penalty_sum - node_txn[0].output[0].value; + feerate_3 = fee_3 * 1000 / node_txn[0].get_weight() as u64; + // Verify 25% bump heuristic + assert!(feerate_3 * 100 >= feerate_2 * 125); + node_txn.clear(); + } + } + assert_ne!(feerate_3, 0); + + nodes[1].node.get_and_clear_pending_events(); + nodes[1].node.get_and_clear_pending_msg_events(); +} + +#[test] +fn test_bump_penalty_txn_on_revoked_htlcs() { + // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to sure + // we're able to claim outputs on revoked HTLC transactions before timelocks expiration + + let nodes = create_network(2, &[None, None]); + + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, LocalFeatures::new(), LocalFeatures::new()); + // Lock HTLC in both directions + let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3_000_000).0; + route_payment(&nodes[1], &vec!(&nodes[0])[..], 3_000_000).0; + + let revoked_local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone(); + assert_eq!(revoked_local_txn[0].input.len(), 1); + assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid()); + + // Revoke local commitment tx + claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage, 3_000_000); + + let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + // B will generate both revoked HTLC-timeout/HTLC-preimage txn from revoked commitment tx + nodes[1].block_notifier.block_connected(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1); + check_closed_broadcast!(nodes[1]); + + let mut received = ::std::usize::MAX; + let mut offered = ::std::usize::MAX; + let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(revoked_htlc_txn.len(), 6); + if revoked_htlc_txn[0].input[0].witness.last().unwrap().len() == ACCEPTED_HTLC_SCRIPT_WEIGHT { + assert_eq!(revoked_htlc_txn[0].input.len(), 1); + check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone()); + assert_eq!(revoked_htlc_txn[1].input.len(), 1); + assert_eq!(revoked_htlc_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); + check_spends!(revoked_htlc_txn[1], revoked_local_txn[0].clone()); + received = 0; + offered = 1; + } else if revoked_htlc_txn[1].input[0].witness.last().unwrap().len() == ACCEPTED_HTLC_SCRIPT_WEIGHT { + assert_eq!(revoked_htlc_txn[1].input.len(), 1); + check_spends!(revoked_htlc_txn[1], revoked_local_txn[0].clone()); + assert_eq!(revoked_htlc_txn[0].input.len(), 1); + assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); + check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone()); + received = 1; + offered = 0; + } + + // Broadcast set of revoked txn on A + let header_128 = connect_blocks(&nodes[0].block_notifier, 128, 0, true, header.bitcoin_hash()); + let header_129 = BlockHeader { version: 0x20000000, prev_blockhash: header_128, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + nodes[0].block_notifier.block_connected(&Block { header: header_129, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone(), revoked_htlc_txn[1].clone()] }, 129); + let first; + let second; + let feerate_1; + let feerate_2; + { + let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 9); // 3 penalty txn on revoked commitment tx * 2 (block-rescan) + A commitment tx + 2 penalty tnx on revoked HTLC txn + // Verify claim tx are spending revoked HTLC txn + assert_eq!(node_txn[7].input.len(), 1); + assert_eq!(node_txn[7].output.len(), 1); + check_spends!(node_txn[7], revoked_htlc_txn[0].clone()); + first = node_txn[7].txid(); + assert_eq!(node_txn[8].input.len(), 1); + assert_eq!(node_txn[8].output.len(), 1); + check_spends!(node_txn[8], revoked_htlc_txn[1].clone()); + second = node_txn[8].txid(); + // Store both feerates for later comparison + let fee_1 = revoked_htlc_txn[0].output[0].value - node_txn[7].output[0].value; + feerate_1 = fee_1 * 1000 / node_txn[7].get_weight() as u64; + let fee_2 = revoked_htlc_txn[1].output[0].value - node_txn[8].output[0].value; + feerate_2 = fee_2 * 1000 / node_txn[8].get_weight() as u64; + node_txn.clear(); + } + + // Connect three more block to see if bumped penalty are issued for HTLC txn + let header_132 = connect_blocks(&nodes[0].block_notifier, 3, 129, true, header_129.bitcoin_hash()); + let node_txn = { + let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 5); // 2 bumped penalty txn on offered/received HTLC outputs of revoked commitment tx + 1 penalty tx on to_local of revoked commitment tx + 2 bumped penalty tx on revoked HTLC txn + + check_spends!(node_txn[0], revoked_local_txn[0].clone()); + check_spends!(node_txn[1], revoked_local_txn[0].clone()); + + let mut penalty_local = ::std::usize::MAX; + let mut penalty_offered = ::std::usize::MAX; + let mut penalty_received = ::std::usize::MAX; + + { + let iter_txn = node_txn[2..].iter(); + for (i, tx) in iter_txn.enumerate() { + if tx.input[0].previous_output.txid == revoked_local_txn[0].txid() { + penalty_local = 2 + i; + } else if tx.input[0].previous_output.txid == revoked_htlc_txn[offered].txid() { + penalty_offered = 2+ i; + } else if tx.input[0].previous_output.txid == revoked_htlc_txn[received].txid() { + penalty_received = 2 + i; + } + } + } + check_spends!(node_txn[penalty_local], revoked_local_txn[0].clone()); + + assert_eq!(node_txn[penalty_received].input.len(), 1); + assert_eq!(node_txn[penalty_received].output.len(), 1); + assert_eq!(node_txn[penalty_offered].input.len(), 1); + assert_eq!(node_txn[penalty_offered].output.len(), 1); + // Verify bumped tx is different and 25% bump heuristic + check_spends!(node_txn[penalty_offered], revoked_htlc_txn[offered].clone()); + assert_ne!(first, node_txn[penalty_offered].txid()); + let fee = revoked_htlc_txn[offered].output[0].value - node_txn[penalty_offered].output[0].value; + let new_feerate = fee * 1000 / node_txn[penalty_offered].get_weight() as u64; + assert!(new_feerate * 100 > feerate_1 * 125); + + check_spends!(node_txn[penalty_received], revoked_htlc_txn[received].clone()); + assert_ne!(second, node_txn[penalty_received].txid()); + let fee = revoked_htlc_txn[received].output[0].value - node_txn[penalty_received].output[0].value; + let new_feerate = fee * 1000 / node_txn[penalty_received].get_weight() as u64; + assert!(new_feerate * 100 > feerate_2 * 125); + let txn = vec![node_txn[2].clone(), node_txn[3].clone(), node_txn[4].clone()]; + node_txn.clear(); + txn + }; + // Broadcast claim txn and confirm blocks to avoid further bumps on this outputs + let header_133 = BlockHeader { version: 0x20000000, prev_blockhash: header_132, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + nodes[0].block_notifier.block_connected(&Block { header: header_133, txdata: node_txn }, 133); + let header_140 = connect_blocks(&nodes[0].block_notifier, 6, 134, true, header_133.bitcoin_hash()); + { + let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); + node_txn.clear(); + } + + // Connect few more blocks and check only penalty transaction for to_local output have been issued + connect_blocks(&nodes[0].block_notifier, 7, 140, true, header_140); + { + let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 2); //TODO: should be zero when we fix check_spend_remote_htlc + node_txn.clear(); + } + check_closed_broadcast!(nodes[0]); +} + +#[test] +fn test_bump_penalty_txn_on_remote_commitment() { + // In case of claim txn with too low feerates for getting into mempools, RBF-bump them to be sure + // we're able to claim outputs on remote commitment transaction before timelocks expiration + + // Create 2 HTLCs + // Provide preimage for one + // Check aggregation + + let nodes = create_network(2, &[None, None]); + + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, LocalFeatures::new(), LocalFeatures::new()); + let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0; + route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000).0; + + // Remote commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC + let remote_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone(); + assert_eq!(remote_txn[0].output.len(), 4); + assert_eq!(remote_txn[0].input.len(), 1); + assert_eq!(remote_txn[0].input[0].previous_output.txid, chan.3.txid()); + + // Claim a HTLC without revocation (provide B monitor with preimage) + nodes[1].node.claim_funds(payment_preimage, 3_000_000); + let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + nodes[1].block_notifier.block_connected(&Block { header, txdata: vec![remote_txn[0].clone()] }, 1); + check_added_monitors!(nodes[1], 1); + + // One or more claim tx should have been broadcast, check it + let timeout; + let preimage; + let feerate_timeout; + let feerate_preimage; + { + let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 6); // 2 * claim tx (broadcasted from ChannelMonitor) * 2 (block-reparsing) + local commitment tx + local HTLC-timeout (broadcasted from ChannelManager) + assert_eq!(node_txn[0], node_txn[4]); + assert_eq!(node_txn[1], node_txn[5]); + assert_eq!(node_txn[0].input.len(), 1); + assert_eq!(node_txn[1].input.len(), 1); + check_spends!(node_txn[0], remote_txn[0].clone()); + check_spends!(node_txn[1], remote_txn[0].clone()); + if node_txn[0].input[0].witness.last().unwrap().len() == ACCEPTED_HTLC_SCRIPT_WEIGHT { + timeout = node_txn[0].txid(); + let index = node_txn[0].input[0].previous_output.vout; + let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value; + feerate_timeout = fee * 1000 / node_txn[0].get_weight() as u64; + + preimage = node_txn[1].txid(); + let index = node_txn[1].input[0].previous_output.vout; + let fee = remote_txn[0].output[index as usize].value - node_txn[1].output[0].value; + feerate_preimage = fee * 1000 / node_txn[1].get_weight() as u64; + } else { + timeout = node_txn[1].txid(); + let index = node_txn[1].input[0].previous_output.vout; + let fee = remote_txn[0].output[index as usize].value - node_txn[1].output[0].value; + feerate_timeout = fee * 1000 / node_txn[1].get_weight() as u64; + + preimage = node_txn[0].txid(); + let index = node_txn[0].input[0].previous_output.vout; + let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value; + feerate_preimage = fee * 1000 / node_txn[0].get_weight() as u64; + } + node_txn.clear(); + }; + assert_ne!(feerate_timeout, 0); + assert_ne!(feerate_preimage, 0); + + // After exhaustion of height timer, new bumped claim txn should have been broadcast, check it + connect_blocks(&nodes[1].block_notifier, 15, 1, true, header.bitcoin_hash()); + { + let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 2); + assert_eq!(node_txn[0].input.len(), 1); + assert_eq!(node_txn[1].input.len(), 1); + check_spends!(node_txn[0], remote_txn[0].clone()); + check_spends!(node_txn[1], remote_txn[0].clone()); + if node_txn[0].input[0].witness.last().unwrap().len() == ACCEPTED_HTLC_SCRIPT_WEIGHT { + let index = node_txn[0].input[0].previous_output.vout; + let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value; + let new_feerate = fee * 1000 / node_txn[0].get_weight() as u64; + assert!(new_feerate * 100 > feerate_timeout * 125); + assert_ne!(timeout, node_txn[0].txid()); + + let index = node_txn[1].input[0].previous_output.vout; + let fee = remote_txn[0].output[index as usize].value - node_txn[1].output[0].value; + let new_feerate = fee * 1000 / node_txn[1].get_weight() as u64; + assert!(new_feerate * 100 > feerate_preimage * 125); + assert_ne!(preimage, node_txn[1].txid()); + } else { + let index = node_txn[1].input[0].previous_output.vout; + let fee = remote_txn[0].output[index as usize].value - node_txn[1].output[0].value; + let new_feerate = fee * 1000 / node_txn[1].get_weight() as u64; + assert!(new_feerate * 100 > feerate_timeout * 125); + assert_ne!(timeout, node_txn[1].txid()); + + let index = node_txn[0].input[0].previous_output.vout; + let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value; + let new_feerate = fee * 1000 / node_txn[0].get_weight() as u64; + assert!(new_feerate * 100 > feerate_preimage * 125); + assert_ne!(preimage, node_txn[0].txid()); + } + node_txn.clear(); + } + + nodes[1].node.get_and_clear_pending_events(); + nodes[1].node.get_and_clear_pending_msg_events(); +} + +#[test] +fn test_set_outpoints_partial_claiming() { + // - remote party claim tx, new bump tx + // - disconnect remote claiming tx, new bump + // - disconnect tx, see no tx anymore + let nodes = create_network(2, &[None, None]); + + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, LocalFeatures::new(), LocalFeatures::new()); + let payment_preimage_1 = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3_000_000).0; + let payment_preimage_2 = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3_000_000).0; + + // Remote commitment txn with 4 outputs: to_local, to_remote, 2 outgoing HTLC + let remote_txn = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone(); + assert_eq!(remote_txn[0].output.len(), 4); + assert_eq!(remote_txn[0].input.len(), 1); + assert_eq!(remote_txn[0].input[0].previous_output.txid, chan.3.txid()); + + // Connect blocks on node A to advance height towards TEST_FINAL_CLTV + let prev_header_100 = connect_blocks(&nodes[1].block_notifier, 100, 0, false, Default::default()); + // Provide node A with both preimage + nodes[0].node.claim_funds(payment_preimage_1, 3_000_000); + nodes[0].node.claim_funds(payment_preimage_2, 3_000_000); + 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 + let header = BlockHeader { version: 0x20000000, prev_blockhash: prev_header_100, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + nodes[0].block_notifier.block_connected(&Block { header, txdata: vec![remote_txn[0].clone()] }, 101); + // Verify node A broadcast tx claiming both HTLCs + { + let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 3); + check_spends!(node_txn[0], remote_txn[0].clone()); + assert_eq!(node_txn[0].input.len(), 2); + node_txn.clear(); + } + nodes[0].node.get_and_clear_pending_msg_events(); + + // Connect blocks on node B + connect_blocks(&nodes[1].block_notifier, 135, 0, false, Default::default()); + // Verify node B broadcast 2 HTLC-timeout txn + let partial_claim_tx = { + let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 3); + check_spends!(node_txn[1], node_txn[0].clone()); + check_spends!(node_txn[2], node_txn[0].clone()); + assert_eq!(node_txn[1].input.len(), 1); + assert_eq!(node_txn[2].input.len(), 1); + node_txn[1].clone() + }; + nodes[1].node.get_and_clear_pending_msg_events(); + + // Broadcast partial claim on node A, should regenerate a claiming tx with HTLC dropped + let header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + nodes[0].block_notifier.block_connected(&Block { header, txdata: vec![partial_claim_tx.clone()] }, 102); + { + let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 1); + check_spends!(node_txn[0], remote_txn[0].clone()); + assert_eq!(node_txn[0].input.len(), 1); //dropped HTLC + node_txn.clear(); + } + nodes[0].node.get_and_clear_pending_msg_events(); + + // Disconnect last block on node A, should regenerate a claiming tx with HTLC dropped + nodes[0].block_notifier.block_disconnected(&header, 102); + { + let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 1); + check_spends!(node_txn[0], remote_txn[0].clone()); + assert_eq!(node_txn[0].input.len(), 2); //resurrected HTLC + node_txn.clear(); + } + + //// Disconnect one more block and then reconnect multiple no transaction should be generated + nodes[0].block_notifier.block_disconnected(&header, 101); + connect_blocks(&nodes[1].block_notifier, 15, 101, false, prev_header_100); + { + let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 0); + node_txn.clear(); + } +} + +#[test] +fn test_bump_txn_sanitize_tracking_maps() { + // Sanitizing pendning_claim_request and claimable_outpoints used to be buggy, + // verify we clean then right after expiration of ANTI_REORG_DELAY. + + let nodes = create_network(2, &[None, None]); + + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, LocalFeatures::new(), LocalFeatures::new()); + // Lock HTLC in both directions + let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000).0; + route_payment(&nodes[1], &vec!(&nodes[0])[..], 9_000_000).0; + + let revoked_local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone(); + assert_eq!(revoked_local_txn[0].input.len(), 1); + assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid()); + + // Revoke local commitment tx + claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage, 9_000_000); + + // Broadcast set of revoked txn on A + let header_128 = connect_blocks(&nodes[0].block_notifier, 128, 0, false, Default::default()); + let header_129 = BlockHeader { version: 0x20000000, prev_blockhash: header_128, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + nodes[0].block_notifier.block_connected(&Block { header: header_129, txdata: vec![revoked_local_txn[0].clone()] }, 129); + check_closed_broadcast!(nodes[0]); + let penalty_txn = { + let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 7); + check_spends!(node_txn[0], revoked_local_txn[0].clone()); + check_spends!(node_txn[1], revoked_local_txn[0].clone()); + check_spends!(node_txn[2], revoked_local_txn[0].clone()); + let penalty_txn = vec![node_txn[0].clone(), node_txn[1].clone(), node_txn[2].clone()]; + node_txn.clear(); + penalty_txn + }; + let header_130 = BlockHeader { version: 0x20000000, prev_blockhash: header_129.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + nodes[0].block_notifier.block_connected(&Block { header: header_130, txdata: penalty_txn }, 130); + connect_blocks(&nodes[0].block_notifier, 5, 130, false, header_130.bitcoin_hash()); + { + let monitors = nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap(); + if let Some(monitor) = monitors.get(&OutPoint::new(chan.3.txid(), 0)) { + assert!(monitor.pending_claim_requests.is_empty()); + assert!(monitor.claimable_outpoints.is_empty()); + } + } +}