X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Ffunctional_tests.rs;h=c98ef2e2e1471580cbb80f88b1540fceb2961636;hb=4cc7d5d5274edc2e28ffc5e83228dbcaa4a935bb;hp=b83b9d6fab635f708e4bc82f257f4e0accc8094d;hpb=3bfea5b659ff912011d030cc4ccb1a53d7a8a7c9;p=rust-lightning diff --git a/src/ln/functional_tests.rs b/src/ln/functional_tests.rs index b83b9d6f..c98ef2e2 100644 --- a/src/ln/functional_tests.rs +++ b/src/ln/functional_tests.rs @@ -8,7 +8,7 @@ use chain::chaininterface::{ChainListener, ChainWatchInterface}; use chain::keysinterface::{KeysInterface, SpendableOutputDescriptor}; use chain::keysinterface; use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC}; -use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,RAACommitmentOrder, PaymentPreimage, PaymentHash}; +use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,HTLCForwardInfo,RAACommitmentOrder, PaymentPreimage, PaymentHash}; use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, CLTV_CLAIM_BUFFER, HTLC_FAIL_TIMEOUT_BLOCKS, ManyChannelMonitor}; use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT}; use ln::onion_utils; @@ -21,6 +21,7 @@ use util::errors::APIError; use util::logger::Logger; use util::ser::{Writeable, Writer, ReadableArgs}; use util::config::UserConfig; +use util::rng; use bitcoin::util::hash::{BitcoinHash, Sha256dHash}; use bitcoin::util::bip143; @@ -50,11 +51,12 @@ use std::sync::atomic::Ordering; use std::time::Instant; use std::mem; +const CHAN_CONFIRM_DEPTH: u32 = 100; fn confirm_transaction(chain: &chaininterface::ChainWatchInterfaceUtil, tx: &Transaction, chan_id: u32) { assert!(chain.does_match_tx(tx)); let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; chain.block_connected_checked(&header, 1, &[tx; 1], &[chan_id; 1]); - for i in 2..100 { + for i in 2..CHAN_CONFIRM_DEPTH { header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; chain.block_connected_checked(&header, i, &[tx; 0], &[0; 0]); } @@ -75,9 +77,9 @@ impl Drop for Node { fn drop(&mut self) { if !::std::thread::panicking() { // Check that we processed all pending events - assert_eq!(self.node.get_and_clear_pending_msg_events().len(), 0); - assert_eq!(self.node.get_and_clear_pending_events().len(), 0); - assert_eq!(self.chan_monitor.added_monitors.lock().unwrap().len(), 0); + assert!(self.node.get_and_clear_pending_msg_events().is_empty()); + assert!(self.node.get_and_clear_pending_events().is_empty()); + assert!(self.chan_monitor.added_monitors.lock().unwrap().is_empty()); } } } @@ -333,6 +335,19 @@ macro_rules! get_closing_signed_broadcast { } } +macro_rules! check_closed_broadcast { + ($node: expr) => {{ + let events = $node.node.get_and_clear_pending_msg_events(); + assert_eq!(events.len(), 1); + match events[0] { + MessageSendEvent::BroadcastChannelUpdate { ref msg } => { + assert_eq!(msg.contents.flags & 2, 2); + }, + _ => panic!("Unexpected event"), + } + }} +} + fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &[u8; 32], funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, Transaction) { let (node_a, broadcaster_a, struct_a) = if close_inbound_first { (&inbound_node.node, &inbound_node.tx_broadcaster, inbound_node) } else { (&outbound_node.node, &outbound_node.tx_broadcaster, outbound_node) }; let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) }; @@ -490,16 +505,7 @@ macro_rules! commitment_signed_dance { { let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true); $node_a.node.handle_revoke_and_ack(&$node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap(); - { - let mut added_monitors = $node_a.chan_monitor.added_monitors.lock().unwrap(); - if $fail_backwards { - assert_eq!(added_monitors.len(), 2); - assert!(added_monitors[0].0 != added_monitors[1].0); - } else { - assert_eq!(added_monitors.len(), 1); - } - added_monitors.clear(); - } + check_added_monitors!($node_a, 1); extra_msg_option } }; @@ -512,6 +518,9 @@ macro_rules! commitment_signed_dance { { commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true); if $fail_backwards { + expect_pending_htlcs_forwardable!($node_a); + check_added_monitors!($node_a, 1); + let channel_state = $node_a.node.channel_state.lock().unwrap(); assert_eq!(channel_state.pending_msg_events.len(), 1); if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = channel_state.pending_msg_events[0] { @@ -535,9 +544,21 @@ macro_rules! get_payment_preimage_hash { } } -fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) { - let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node); +macro_rules! expect_pending_htlcs_forwardable { + ($node: expr) => {{ + let events = $node.node.get_and_clear_pending_events(); + assert_eq!(events.len(), 1); + match events[0] { + Event::PendingHTLCsForwardable { .. } => { }, + _ => panic!("Unexpected event"), + }; + let node_ref: &Node = &$node; + node_ref.node.channel_state.lock().unwrap().next_forward = Instant::now(); + $node.node.process_pending_htlc_forwards(); + }} +} +fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) { let mut payment_event = { origin_node.node.send_payment(route, our_payment_hash).unwrap(); check_added_monitors!(origin_node, 1); @@ -555,15 +576,7 @@ fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], check_added_monitors!(node, 0); commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false); - let events_1 = node.node.get_and_clear_pending_events(); - assert_eq!(events_1.len(), 1); - match events_1[0] { - Event::PendingHTLCsForwardable { .. } => { }, - _ => panic!("Unexpected event"), - }; - - node.node.channel_state.lock().unwrap().next_forward = Instant::now(); - node.node.process_pending_htlc_forwards(); + expect_pending_htlcs_forwardable!(node); if idx == expected_route.len() - 1 { let events_2 = node.node.get_and_clear_pending_events(); @@ -585,7 +598,11 @@ fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], prev_node = node; } +} +fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) { + let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node); + send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash); (our_payment_preimage, our_payment_hash) } @@ -713,6 +730,7 @@ fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64) { fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) { assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash, 0)); + expect_pending_htlcs_forwardable!(expected_route.last().unwrap()); check_added_monitors!(expected_route.last().unwrap(), 1); let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None; @@ -721,6 +739,9 @@ fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_l { $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap(); commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node); + if skip_last && $last_node { + expect_pending_htlcs_forwardable!($node); + } } } } @@ -1244,14 +1265,7 @@ fn test_update_fee_with_fundee_update_add_htlc() { check_added_monitors!(nodes[0], 1); assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); - let events = nodes[0].node.get_and_clear_pending_events(); - assert_eq!(events.len(), 1); - match events[0] { - Event::PendingHTLCsForwardable { .. } => { }, - _ => panic!("Unexpected event"), - }; - nodes[0].node.channel_state.lock().unwrap().next_forward = Instant::now(); - nodes[0].node.process_pending_htlc_forwards(); + expect_pending_htlcs_forwardable!(nodes[0]); let events = nodes[0].node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); @@ -1679,14 +1693,7 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) { // get_closing_signed_broadcast usually eats the BroadcastChannelUpdate for us and // checks it, but in this case nodes[0] didn't ever get a chance to receive a // closing_signed so we do it ourselves - let events = nodes[0].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 1); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { ref msg } => { - assert_eq!(msg.contents.flags & 2, 2); - }, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[0]); } assert!(nodes[0].node.list_channels().is_empty()); @@ -1962,16 +1969,25 @@ fn get_announce_close_broadcast_events(nodes: &Vec, a: usize, b: usize) { } } -macro_rules! expect_pending_htlcs_forwardable { - ($node: expr) => {{ +macro_rules! expect_payment_received { + ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => { let events = $node.node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); match events[0] { - Event::PendingHTLCsForwardable { .. } => { }, + Event::PaymentReceived { ref payment_hash, amt } => { + assert_eq!($expected_payment_hash, *payment_hash); + assert_eq!($expected_recv_value, amt); + }, _ => panic!("Unexpected event"), - }; - $node.node.channel_state.lock().unwrap().next_forward = Instant::now(); - $node.node.process_pending_htlc_forwards(); + } + } +} + +macro_rules! get_channel_value_stat { + ($node: expr, $channel_id: expr) => {{ + let chan_lock = $node.node.channel_state.lock().unwrap(); + let chan = chan_lock.by_id.get(&$channel_id).unwrap(); + chan.get_value_stat() }} } @@ -1980,14 +1996,6 @@ fn do_channel_reserve_test(test_recv: bool) { use std::sync::atomic::Ordering; use ln::msgs::HandleError; - macro_rules! get_channel_value_stat { - ($node: expr, $channel_id: expr) => {{ - let chan_lock = $node.node.channel_state.lock().unwrap(); - let chan = chan_lock.by_id.get(&$channel_id).unwrap(); - chan.get_value_stat() - }} - } - let mut nodes = create_network(3); let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1900, 1001); let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1900, 1001); @@ -2016,20 +2024,6 @@ fn do_channel_reserve_test(test_recv: bool) { }} } - macro_rules! expect_payment_received { - ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => { - let events = $node.node.get_and_clear_pending_events(); - assert_eq!(events.len(), 1); - match events[0] { - Event::PaymentReceived { ref payment_hash, amt } => { - assert_eq!($expected_payment_hash, *payment_hash); - assert_eq!($expected_recv_value, amt); - }, - _ => panic!("Unexpected event"), - } - } - }; - let feemsat = 239; // somehow we know? let total_fee_msat = (nodes.len() - 2) as u64 * 239; @@ -2113,7 +2107,7 @@ fn do_channel_reserve_test(test_recv: bool) { // Need to manually create update_add_htlc message to go around the channel reserve check in send_htlc() let secp_ctx = Secp256k1::new(); - let session_priv = SecretKey::from_slice(&secp_ctx, &{ + let session_priv = SecretKey::from_slice(&{ let mut session_key = [0; 32]; rng::fill_bytes(&mut session_key); session_key @@ -2140,14 +2134,7 @@ fn do_channel_reserve_test(test_recv: bool) { // If we send a garbage message, the channel should get closed, making the rest of this test case fail. assert_eq!(nodes[1].node.list_channels().len(), 1); assert_eq!(nodes[1].node.list_channels().len(), 1); - let channel_close_broadcast = nodes[1].node.get_and_clear_pending_msg_events(); - assert_eq!(channel_close_broadcast.len(), 1); - match channel_close_broadcast[0] { - MessageSendEvent::BroadcastChannelUpdate { ref msg } => { - assert_eq!(msg.contents.flags & 2, 2); - }, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[1]); return; } } @@ -2663,14 +2650,15 @@ fn test_htlc_on_chain_success() { // Test that in case of an unilateral close onchain, we detect the state of output thanks to // ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is // broadcasting the right event to other nodes in payment path. + // We test with two HTLCs simultaneously as that was not handled correctly in the past. // A --------------------> B ----------------------> C (preimage) - // First, C should claim the HTLC output via HTLC-Success when its own latest local + // First, C should claim the HTLC outputs via HTLC-Success when its own latest local // commitment transaction was broadcast. // Then, B should learn the preimage from said transactions, attempting to claim backwards // towards B. // B should be able to claim via preimage if A then broadcasts its local tx. // Finally, when A sees B's latest local commitment transaction it should be able to claim - // the HTLC output via the preimage it learned (which, once confirmed should generate a + // the HTLC outputs via the preimage it learned (which, once confirmed should generate a // PaymentSent event). let nodes = create_network(3); @@ -2684,6 +2672,7 @@ fn test_htlc_on_chain_success() { send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000); let (our_payment_preimage, _payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000); + let (our_payment_preimage_2, _payment_hash_2) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000); let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42}; // Broadcast legit commitment tx from C on B's chain @@ -2692,7 +2681,8 @@ fn test_htlc_on_chain_success() { assert_eq!(commitment_tx.len(), 1); check_spends!(commitment_tx[0], chan_2.3.clone()); nodes[2].node.claim_funds(our_payment_preimage); - check_added_monitors!(nodes[2], 1); + nodes[2].node.claim_funds(our_payment_preimage_2); + check_added_monitors!(nodes[2], 2); let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id()); assert!(updates.update_add_htlcs.is_empty()); assert!(updates.update_fail_htlcs.is_empty()); @@ -2700,28 +2690,29 @@ fn test_htlc_on_chain_success() { assert_eq!(updates.update_fulfill_htlcs.len(), 1); nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1); - let events = nodes[2].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 1); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } - let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 2 (2 * HTLC-Success tx) - assert_eq!(node_txn.len(), 3); - assert_eq!(node_txn[1], commitment_tx[0]); - assert_eq!(node_txn[0], node_txn[2]); + check_closed_broadcast!(nodes[2]); + let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 4 (2*2 * HTLC-Success tx) + assert_eq!(node_txn.len(), 5); + assert_eq!(node_txn[0], node_txn[3]); + assert_eq!(node_txn[1], node_txn[4]); + assert_eq!(node_txn[2], commitment_tx[0]); check_spends!(node_txn[0], commitment_tx[0].clone()); + check_spends!(node_txn[1], commitment_tx[0].clone()); assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); + assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output + assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output assert_eq!(node_txn[0].lock_time, 0); + assert_eq!(node_txn[1].lock_time, 0); // Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn}, 1); let events = nodes[1].node.get_and_clear_pending_msg_events(); { let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap(); - assert_eq!(added_monitors.len(), 1); + assert_eq!(added_monitors.len(), 2); assert_eq!(added_monitors[0].0.txid, chan_1.3.txid()); + assert_eq!(added_monitors[1].0.txid, chan_1.3.txid()); added_monitors.clear(); } assert_eq!(events.len(), 2); @@ -2739,42 +2730,59 @@ fn test_htlc_on_chain_success() { }, _ => panic!("Unexpected event"), }; - { - // nodes[1] now broadcasts its own local state as a fallback, suggesting an alternate - // commitment transaction with a corresponding HTLC-Timeout transaction, as well as a - // timeout-claim of the output that nodes[2] just claimed via success. - let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 1 (timeout tx) * 2 (block-rescan) - assert_eq!(node_txn.len(), 4); - assert_eq!(node_txn[0], node_txn[3]); - check_spends!(node_txn[0], commitment_tx[0].clone()); - assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); - assert_ne!(node_txn[0].lock_time, 0); - assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment - check_spends!(node_txn[1], chan_2.3.clone()); - check_spends!(node_txn[2], node_txn[1].clone()); - assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71); - assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); - assert!(node_txn[2].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output - assert_ne!(node_txn[2].lock_time, 0); - node_txn.clear(); - } + macro_rules! check_tx_local_broadcast { + ($node: expr, $htlc_offered: expr, $commitment_tx: expr, $chan_tx: expr) => { { + // ChannelManager : 3 (commitment tx, 2*HTLC-Timeout tx), ChannelMonitor : 2 (timeout tx) * 2 (block-rescan) + let mut node_txn = $node.tx_broadcaster.txn_broadcasted.lock().unwrap(); + assert_eq!(node_txn.len(), 7); + assert_eq!(node_txn[0], node_txn[5]); + assert_eq!(node_txn[1], node_txn[6]); + check_spends!(node_txn[0], $commitment_tx.clone()); + check_spends!(node_txn[1], $commitment_tx.clone()); + assert_ne!(node_txn[0].lock_time, 0); + assert_ne!(node_txn[1].lock_time, 0); + if $htlc_offered { + assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); + assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); + assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output + assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output + } else { + assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); + assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); + assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment + assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment + } + check_spends!(node_txn[2], $chan_tx.clone()); + check_spends!(node_txn[3], node_txn[2].clone()); + check_spends!(node_txn[4], node_txn[2].clone()); + assert_eq!(node_txn[2].input[0].witness.last().unwrap().len(), 71); + assert_eq!(node_txn[3].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); + assert_eq!(node_txn[4].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); + assert!(node_txn[3].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output + assert!(node_txn[4].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output + assert_ne!(node_txn[3].lock_time, 0); + assert_ne!(node_txn[4].lock_time, 0); + node_txn.clear(); + } } + } + // nodes[1] now broadcasts its own local state as a fallback, suggesting an alternate + // commitment transaction with a corresponding HTLC-Timeout transactions, as well as a + // timeout-claim of the output that nodes[2] just claimed via success. + check_tx_local_broadcast!(nodes[1], false, commitment_tx[0], chan_2.3); // Broadcast legit commitment tx from A on B's chain // Broadcast preimage tx by B on offered output from A commitment tx on A's chain let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone(); check_spends!(commitment_tx[0], chan_1.3.clone()); nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1); - let events = nodes[1].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 1); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[1]); let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 1 (HTLC-Success) * 2 (block-rescan) assert_eq!(node_txn.len(), 3); assert_eq!(node_txn[0], node_txn[2]); check_spends!(node_txn[0], commitment_tx[0].clone()); - assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); + assert_eq!(node_txn[0].input.len(), 2); + assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); + assert_eq!(node_txn[0].input[1].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); assert_eq!(node_txn[0].lock_time, 0); assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment check_spends!(node_txn[1], chan_1.3.clone()); @@ -2784,33 +2792,24 @@ fn test_htlc_on_chain_success() { // Verify that A's ChannelManager is able to extract preimage from preimage tx and generate PaymentSent nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone(), node_txn[0].clone()] }, 1); - let events = nodes[0].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 1); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[0]); let events = nodes[0].node.get_and_clear_pending_events(); - assert_eq!(events.len(), 1); - match events[0] { - Event::PaymentSent { payment_preimage } => { - assert_eq!(payment_preimage, our_payment_preimage); - }, - _ => panic!("Unexpected event"), + assert_eq!(events.len(), 2); + let mut first_claimed = false; + for event in events { + match event { + Event::PaymentSent { payment_preimage } => { + if payment_preimage == our_payment_preimage { + assert!(!first_claimed); + first_claimed = true; + } else { + assert_eq!(payment_preimage, our_payment_preimage_2); + } + }, + _ => panic!("Unexpected event"), + } } - let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 1 (HTLC-Timeout tx) * 2 (block-rescan) - assert_eq!(node_txn.len(), 4); - assert_eq!(node_txn[0], node_txn[3]); - check_spends!(node_txn[0], commitment_tx[0].clone()); - assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); - assert_ne!(node_txn[0].lock_time, 0); - assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output - check_spends!(node_txn[1], chan_1.3.clone()); - check_spends!(node_txn[2], node_txn[1].clone()); - assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71); - assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); - assert!(node_txn[2].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output - assert_ne!(node_txn[2].lock_time, 0); + check_tx_local_broadcast!(nodes[0], true, commitment_tx[0], chan_1.3); } #[test] @@ -2840,11 +2839,10 @@ fn test_htlc_on_chain_timeout() { let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone(); check_spends!(commitment_tx[0], chan_2.3.clone()); nodes[2].node.fail_htlc_backwards(&payment_hash, 0); - { - let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap(); - assert_eq!(added_monitors.len(), 1); - added_monitors.clear(); - } + check_added_monitors!(nodes[2], 0); + expect_pending_htlcs_forwardable!(nodes[2]); + check_added_monitors!(nodes[2], 1); + let events = nodes[2].node.get_and_clear_pending_msg_events(); assert_eq!(events.len(), 1); match events[0] { @@ -2858,12 +2856,7 @@ fn test_htlc_on_chain_timeout() { _ => panic!("Unexpected event"), }; nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1); - let events = nodes[2].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 1); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[2]); let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx) assert_eq!(node_txn.len(), 1); check_spends!(node_txn[0], chan_2.3.clone()); @@ -2894,14 +2887,14 @@ fn test_htlc_on_chain_timeout() { } nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![timeout_tx]}, 1); - let events = nodes[1].node.get_and_clear_pending_msg_events(); + check_added_monitors!(nodes[1], 0); + check_closed_broadcast!(nodes[1]); + + expect_pending_htlcs_forwardable!(nodes[1]); check_added_monitors!(nodes[1], 1); - assert_eq!(events.len(), 2); + let events = nodes[1].node.get_and_clear_pending_msg_events(); + assert_eq!(events.len(), 1); match events[0] { - MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {}, - _ => panic!("Unexpected event"), - } - match events[1] { MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => { assert!(update_add_htlcs.is_empty()); assert!(!update_fail_htlcs.is_empty()); @@ -2919,12 +2912,7 @@ fn test_htlc_on_chain_timeout() { check_spends!(commitment_tx[0], chan_1.3.clone()); nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200); - let events = nodes[0].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 1); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[0]); let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 2 (timeout tx) * 2 block-rescan assert_eq!(node_txn.len(), 4); assert_eq!(node_txn[0], node_txn[3]); @@ -2957,14 +2945,14 @@ fn test_simple_commitment_revoked_fail_backward() { let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42}; nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1); - let events = nodes[1].node.get_and_clear_pending_msg_events(); + check_added_monitors!(nodes[1], 0); + check_closed_broadcast!(nodes[1]); + + expect_pending_htlcs_forwardable!(nodes[1]); check_added_monitors!(nodes[1], 1); - assert_eq!(events.len(), 2); + let events = nodes[1].node.get_and_clear_pending_msg_events(); + assert_eq!(events.len(), 1); match events[0] { - MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {}, - _ => panic!("Unexpected event"), - } - match events[1] { MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => { assert!(update_add_htlcs.is_empty()); assert_eq!(update_fail_htlcs.len(), 1); @@ -2992,7 +2980,7 @@ fn test_simple_commitment_revoked_fail_backward() { } } -fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) { +fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use_dust: bool, no_to_remote: bool) { // Test that if our counterparty broadcasts a revoked commitment transaction we fail all // pending HTLCs on that channel backwards even if the HTLCs aren't present in our latest // commitment transaction anymore. @@ -3014,17 +3002,25 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) { create_announced_chan_between_nodes(&nodes, 0, 1); let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2); - let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000); + let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], if no_to_remote { 10_000 } else { 3_000_000 }); // Get the will-be-revoked local txn from nodes[2] let revoked_local_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone(); + assert_eq!(revoked_local_txn[0].output.len(), if no_to_remote { 1 } else { 2 }); // Revoke the old state claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage); - let (_, first_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000); - let (_, second_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000); - let (_, third_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000); + let value = if use_dust { + // The dust limit applied to HTLC outputs considers the fee of the HTLC transaction as + // well, so HTLCs at exactly the dust limit will not be included in commitment txn. + nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().our_dust_limit_satoshis * 1000 + } else { 3000000 }; + + let (_, first_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value); + let (_, second_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value); + let (_, third_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value); assert!(nodes[2].node.fail_htlc_backwards(&first_payment_hash, 0)); + expect_pending_htlcs_forwardable!(nodes[2]); check_added_monitors!(nodes[2], 1); let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id()); assert!(updates.update_add_htlcs.is_empty()); @@ -3037,6 +3033,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) { // Drop the last RAA from 3 -> 2 assert!(nodes[2].node.fail_htlc_backwards(&second_payment_hash, 0)); + expect_pending_htlcs_forwardable!(nodes[2]); check_added_monitors!(nodes[2], 1); let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id()); assert!(updates.update_add_htlcs.is_empty()); @@ -3053,6 +3050,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) { check_added_monitors!(nodes[2], 1); assert!(nodes[2].node.fail_htlc_backwards(&third_payment_hash, 0)); + expect_pending_htlcs_forwardable!(nodes[2]); check_added_monitors!(nodes[2], 1); let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id()); assert!(updates.update_add_htlcs.is_empty()); @@ -3081,9 +3079,17 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) { if deliver_bs_raa { nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_raa).unwrap(); - // One monitor for the new revocation preimage, one as we generate a commitment for - // nodes[0] to fail first_payment_hash backwards. - check_added_monitors!(nodes[1], 2); + // One monitor for the new revocation preimage, no second on as we won't generate a new + // commitment transaction for nodes[0] until process_pending_htlc_forwards(). + check_added_monitors!(nodes[1], 1); + let events = nodes[1].node.get_and_clear_pending_events(); + assert_eq!(events.len(), 1); + match events[0] { + Event::PendingHTLCsForwardable { .. } => { }, + _ => panic!("Unexpected event"), + }; + // Deliberately don't process the pending fail-back so they all fail back at once after + // block connection just like the !deliver_bs_raa case } let mut failed_htlcs = HashSet::new(); @@ -3093,22 +3099,26 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) { nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1); let events = nodes[1].node.get_and_clear_pending_events(); - assert_eq!(events.len(), 1); + assert_eq!(events.len(), if deliver_bs_raa { 1 } else { 2 }); match events[0] { Event::PaymentFailed { ref payment_hash, .. } => { assert_eq!(*payment_hash, fourth_payment_hash); }, _ => panic!("Unexpected event"), } - if !deliver_bs_raa { - // If we delivered the RAA already then we already failed first_payment_hash backwards. - check_added_monitors!(nodes[1], 1); + match events[1] { + Event::PendingHTLCsForwardable { .. } => { }, + _ => panic!("Unexpected event"), + }; } + nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now(); + nodes[1].node.process_pending_htlc_forwards(); + check_added_monitors!(nodes[1], 1); let events = nodes[1].node.get_and_clear_pending_msg_events(); assert_eq!(events.len(), if deliver_bs_raa { 3 } else { 2 }); - match events[if deliver_bs_raa { 2 } else { 0 }] { + match events[if deliver_bs_raa { 1 } else { 0 }] { MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {}, _ => panic!("Unexpected event"), } @@ -3124,80 +3134,50 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) { _ => panic!("Unexpected event"), } } - // Due to the way backwards-failing occurs we do the updates in two steps. - let updates = match events[1] { + match events[if deliver_bs_raa { 2 } else { 1 }] { MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => { assert!(update_add_htlcs.is_empty()); - assert_eq!(update_fail_htlcs.len(), 1); + assert_eq!(update_fail_htlcs.len(), 3); assert!(update_fulfill_htlcs.is_empty()); assert!(update_fail_malformed_htlcs.is_empty()); assert_eq!(nodes[0].node.get_our_node_id(), *node_id); nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]).unwrap(); - nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed).unwrap(); - check_added_monitors!(nodes[0], 1); - let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id()); - nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap(); - check_added_monitors!(nodes[1], 1); - let bs_second_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()); - nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed).unwrap(); - check_added_monitors!(nodes[1], 1); - let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id()); - nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap(); - check_added_monitors!(nodes[0], 1); + nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[1]).unwrap(); + nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[2]).unwrap(); - if !deliver_bs_raa { - // If we delievered B's RAA we got an unknown preimage error, not something - // that we should update our routing table for. - let events = nodes[0].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 1); - match events[0] { + commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true); + + let events = nodes[0].node.get_and_clear_pending_msg_events(); + // If we delievered B's RAA we got an unknown preimage error, not something + // that we should update our routing table for. + assert_eq!(events.len(), if deliver_bs_raa { 2 } else { 3 }); + for event in events { + match event { MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {}, _ => panic!("Unexpected event"), } } let events = nodes[0].node.get_and_clear_pending_events(); - assert_eq!(events.len(), 1); + assert_eq!(events.len(), 3); match events[0] { Event::PaymentFailed { ref payment_hash, .. } => { assert!(failed_htlcs.insert(payment_hash.0)); }, _ => panic!("Unexpected event"), } - - bs_second_update - }, - _ => panic!("Unexpected event"), - }; - - assert!(updates.update_add_htlcs.is_empty()); - assert_eq!(updates.update_fail_htlcs.len(), 2); - assert!(updates.update_fulfill_htlcs.is_empty()); - assert!(updates.update_fail_malformed_htlcs.is_empty()); - nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap(); - nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[1]).unwrap(); - commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false, true); - - let events = nodes[0].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 2); - for event in events { - match event { - MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } - } - - let events = nodes[0].node.get_and_clear_pending_events(); - assert_eq!(events.len(), 2); - match events[0] { - Event::PaymentFailed { ref payment_hash, .. } => { - assert!(failed_htlcs.insert(payment_hash.0)); - }, - _ => panic!("Unexpected event"), - } - match events[1] { - Event::PaymentFailed { ref payment_hash, .. } => { - assert!(failed_htlcs.insert(payment_hash.0)); + match events[1] { + Event::PaymentFailed { ref payment_hash, .. } => { + assert!(failed_htlcs.insert(payment_hash.0)); + }, + _ => panic!("Unexpected event"), + } + match events[2] { + Event::PaymentFailed { ref payment_hash, .. } => { + assert!(failed_htlcs.insert(payment_hash.0)); + }, + _ => panic!("Unexpected event"), + } }, _ => panic!("Unexpected event"), } @@ -3208,47 +3188,38 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool) { } #[test] -fn test_commitment_revoked_fail_backward_exhaustive() { - do_test_commitment_revoked_fail_backward_exhaustive(false); - do_test_commitment_revoked_fail_backward_exhaustive(true); +fn test_commitment_revoked_fail_backward_exhaustive_a() { + do_test_commitment_revoked_fail_backward_exhaustive(false, true, false); + do_test_commitment_revoked_fail_backward_exhaustive(true, true, false); + do_test_commitment_revoked_fail_backward_exhaustive(false, false, false); + do_test_commitment_revoked_fail_backward_exhaustive(true, false, false); } #[test] -fn test_htlc_ignore_latest_remote_commitment() { - // Test that HTLC transactions spending the latest remote commitment transaction are simply +fn test_commitment_revoked_fail_backward_exhaustive_b() { + do_test_commitment_revoked_fail_backward_exhaustive(false, true, true); + do_test_commitment_revoked_fail_backward_exhaustive(true, true, true); + do_test_commitment_revoked_fail_backward_exhaustive(false, false, true); + do_test_commitment_revoked_fail_backward_exhaustive(true, false, true); +} + +#[test] +fn test_htlc_ignore_latest_remote_commitment() { + // Test that HTLC transactions spending the latest remote commitment transaction are simply // ignored if we cannot claim them. This originally tickled an invalid unwrap(). let nodes = create_network(2); create_announced_chan_between_nodes(&nodes, 0, 1); route_payment(&nodes[0], &[&nodes[1]], 10000000); nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id); - { - let events = nodes[0].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 1); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { contents: msgs::UnsignedChannelUpdate { flags, .. }, .. } } => { - assert_eq!(flags & 0b10, 0b10); - }, - _ => panic!("Unexpected event"), - } - } + check_closed_broadcast!(nodes[0]); let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_txn.len(), 2); let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0], &node_txn[1]], &[1; 2]); - - { - let events = nodes[1].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 1); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { contents: msgs::UnsignedChannelUpdate { flags, .. }, .. } } => { - assert_eq!(flags & 0b10, 0b10); - }, - _ => panic!("Unexpected event"), - } - } + check_closed_broadcast!(nodes[1]); // Duplicate the block_connected call since this may happen due to other listeners // registering new transactions @@ -3278,15 +3249,7 @@ fn test_force_close_fail_back() { nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap(); commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false); - let events_1 = nodes[1].node.get_and_clear_pending_events(); - assert_eq!(events_1.len(), 1); - match events_1[0] { - Event::PendingHTLCsForwardable { .. } => { }, - _ => panic!("Unexpected event"), - }; - - nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now(); - nodes[1].node.process_pending_htlc_forwards(); + expect_pending_htlcs_forwardable!(nodes[1]); let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events(); assert_eq!(events_2.len(), 1); @@ -3304,15 +3267,7 @@ fn test_force_close_fail_back() { // transaction and ensure nodes[1] doesn't fail-backwards (this was originally a bug!). nodes[2].node.force_close_channel(&payment_event.commitment_msg.channel_id); - let events_3 = nodes[2].node.get_and_clear_pending_msg_events(); - assert_eq!(events_3.len(), 1); - match events_3[0] { - MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { contents: msgs::UnsignedChannelUpdate { flags, .. }, .. } } => { - assert_eq!(flags & 0b10, 0b10); - }, - _ => panic!("Unexpected event"), - } - + check_closed_broadcast!(nodes[2]); let tx = { let mut node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap(); // Note that we don't bother broadcasting the HTLC-Success transaction here as we don't @@ -3325,15 +3280,8 @@ fn test_force_close_fail_back() { let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&tx], &[1]); - let events_4 = nodes[1].node.get_and_clear_pending_msg_events(); // Note no UpdateHTLCs event here from nodes[1] to nodes[0]! - assert_eq!(events_4.len(), 1); - match events_4[0] { - MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { contents: msgs::UnsignedChannelUpdate { flags, .. }, .. } } => { - assert_eq!(flags & 0b10, 0b10); - }, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[1]); // Now check that if we add the preimage to ChannelMonitor it broadcasts our HTLC-Success.. { @@ -3373,16 +3321,7 @@ fn test_unconf_chan() { while !headers.is_empty() { nodes[0].node.block_disconnected(&headers.pop().unwrap()); } - { - let events = nodes[0].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 1); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { contents: msgs::UnsignedChannelUpdate { flags, .. }, .. } } => { - assert_eq!(flags & 0b10, 0b10); - }, - _ => panic!("Unexpected event"), - } - } + check_closed_broadcast!(nodes[0]); let channel_state = nodes[0].node.channel_state.lock().unwrap(); assert_eq!(channel_state.by_id.len(), 0); assert_eq!(channel_state.short_to_id.len(), 0); @@ -4063,15 +4002,7 @@ fn test_drop_messages_peer_disconnect_dual_htlc() { assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty()); check_added_monitors!(nodes[1], 1); - let events_4 = nodes[1].node.get_and_clear_pending_events(); - assert_eq!(events_4.len(), 1); - match events_4[0] { - Event::PendingHTLCsForwardable { .. } => { }, - _ => panic!("Unexpected event"), - }; - - nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now(); - nodes[1].node.process_pending_htlc_forwards(); + expect_pending_htlcs_forwardable!(nodes[1]); let events_5 = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events_5.len(), 1); @@ -4187,13 +4118,7 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) { *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::PermanentFailure); nodes[0].node.test_restore_channel_monitor(); check_added_monitors!(nodes[0], 1); - - let events_5 = nodes[0].node.get_and_clear_pending_msg_events(); - assert_eq!(events_5.len(), 1); - match events_5[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[0]); // TODO: Once we hit the chain with the failure transaction we should check that we get a // PaymentFailed event @@ -4612,16 +4537,9 @@ fn test_monitor_update_fail_cs() { nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &final_raa).unwrap(); check_added_monitors!(nodes[1], 1); - let mut events = nodes[1].node.get_and_clear_pending_events(); - assert_eq!(events.len(), 1); - match events[0] { - Event::PendingHTLCsForwardable { .. } => { }, - _ => panic!("Unexpected event"), - }; - nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now(); - nodes[1].node.process_pending_htlc_forwards(); + expect_pending_htlcs_forwardable!(nodes[1]); - events = nodes[1].node.get_and_clear_pending_events(); + let events = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); match events[0] { Event::PaymentReceived { payment_hash, amt } => { @@ -4648,6 +4566,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) { // Fail the payment backwards, failing the monitor update on nodes[1]'s receipt of the RAA assert!(nodes[2].node.fail_htlc_backwards(&payment_hash_1, 0)); + expect_pending_htlcs_forwardable!(nodes[2]); check_added_monitors!(nodes[2], 1); let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id()); @@ -4672,15 +4591,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) { nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]).unwrap(); commitment_signed_dance!(nodes[1], nodes[0], send_event.commitment_msg, false); - let events_1 = nodes[1].node.get_and_clear_pending_events(); - assert_eq!(events_1.len(), 1); - match events_1[0] { - Event::PendingHTLCsForwardable { .. } => { }, - _ => panic!("Unexpected event"), - }; - - nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now(); - nodes[1].node.process_pending_htlc_forwards(); + expect_pending_htlcs_forwardable!(nodes[1]); check_added_monitors!(nodes[1], 0); assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty()); @@ -4762,7 +4673,9 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) { // update_add update. *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(()); nodes[1].node.test_restore_channel_monitor(); - check_added_monitors!(nodes[1], 2); + check_added_monitors!(nodes[1], 1); + expect_pending_htlcs_forwardable!(nodes[1]); + check_added_monitors!(nodes[1], 1); let mut events_3 = nodes[1].node.get_and_clear_pending_msg_events(); if test_ignore_second_cs { @@ -4850,15 +4763,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) { commitment_signed_dance!(nodes[2], nodes[1], send_event_b.commitment_msg, false); } - let events_5 = nodes[2].node.get_and_clear_pending_events(); - assert_eq!(events_5.len(), 1); - match events_5[0] { - Event::PendingHTLCsForwardable { .. } => { }, - _ => panic!("Unexpected event"), - }; - - nodes[2].node.channel_state.lock().unwrap().next_forward = Instant::now(); - nodes[2].node.process_pending_htlc_forwards(); + expect_pending_htlcs_forwardable!(nodes[2]); let events_6 = nodes[2].node.get_and_clear_pending_events(); assert_eq!(events_6.len(), 1); @@ -4868,15 +4773,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) { }; if test_ignore_second_cs { - let events_7 = nodes[1].node.get_and_clear_pending_events(); - assert_eq!(events_7.len(), 1); - match events_7[0] { - Event::PendingHTLCsForwardable { .. } => { }, - _ => panic!("Unexpected event"), - }; - - nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now(); - nodes[1].node.process_pending_htlc_forwards(); + expect_pending_htlcs_forwardable!(nodes[1]); check_added_monitors!(nodes[1], 1); send_event = SendEvent::from_node(&nodes[1]); @@ -4885,15 +4782,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) { nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event.msgs[0]).unwrap(); commitment_signed_dance!(nodes[0], nodes[1], send_event.commitment_msg, false); - let events_8 = nodes[0].node.get_and_clear_pending_events(); - assert_eq!(events_8.len(), 1); - match events_8[0] { - Event::PendingHTLCsForwardable { .. } => { }, - _ => panic!("Unexpected event"), - }; - - nodes[0].node.channel_state.lock().unwrap().next_forward = Instant::now(); - nodes[0].node.process_pending_htlc_forwards(); + expect_pending_htlcs_forwardable!(nodes[0]); let events_9 = nodes[0].node.get_and_clear_pending_events(); assert_eq!(events_9.len(), 1); @@ -5281,7 +5170,7 @@ macro_rules! check_spendable_outputs { witness: Vec::new(), }; let outp = TxOut { - script_pubkey: Builder::new().push_opcode(opcodes::All::OP_RETURN).into_script(), + script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), value: output.value, }; let mut spend_tx = Transaction { @@ -5295,7 +5184,7 @@ macro_rules! check_spendable_outputs { let witness_script = Address::p2pkh(&remotepubkey, Network::Testnet).script_pubkey(); let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap(); let remotesig = secp_ctx.sign(&sighash, key); - spend_tx.input[0].witness.push(remotesig.serialize_der(&secp_ctx).to_vec()); + spend_tx.input[0].witness.push(remotesig.serialize_der().to_vec()); spend_tx.input[0].witness[0].push(SigHashType::All as u8); spend_tx.input[0].witness.push(remotepubkey.serialize().to_vec()); txn.push(spend_tx); @@ -5308,7 +5197,7 @@ macro_rules! check_spendable_outputs { witness: Vec::new(), }; let outp = TxOut { - script_pubkey: Builder::new().push_opcode(opcodes::All::OP_RETURN).into_script(), + script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), value: output.value, }; let mut spend_tx = Transaction { @@ -5320,7 +5209,7 @@ macro_rules! check_spendable_outputs { let secp_ctx = Secp256k1::new(); let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], witness_script, output.value)[..]).unwrap(); let local_delaysig = secp_ctx.sign(&sighash, key); - spend_tx.input[0].witness.push(local_delaysig.serialize_der(&secp_ctx).to_vec()); + spend_tx.input[0].witness.push(local_delaysig.serialize_der().to_vec()); spend_tx.input[0].witness[0].push(SigHashType::All as u8); spend_tx.input[0].witness.push(vec!(0)); spend_tx.input[0].witness.push(witness_script.clone().into_bytes()); @@ -5335,7 +5224,7 @@ macro_rules! check_spendable_outputs { witness: Vec::new(), }; let outp = TxOut { - script_pubkey: Builder::new().push_opcode(opcodes::All::OP_RETURN).into_script(), + script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), value: output.value, }; let mut spend_tx = Transaction { @@ -5345,7 +5234,7 @@ macro_rules! check_spendable_outputs { output: vec![outp.clone()], }; let secret = { - match ExtendedPrivKey::new_master(&secp_ctx, Network::Testnet, &$node.node_seed) { + match ExtendedPrivKey::new_master(Network::Testnet, &$node.node_seed) { Ok(master_key) => { match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx($der_idx)) { Ok(key) => key, @@ -5359,7 +5248,7 @@ macro_rules! check_spendable_outputs { let witness_script = Address::p2pkh(&pubkey, Network::Testnet).script_pubkey(); let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap(); let sig = secp_ctx.sign(&sighash, &secret.secret_key); - spend_tx.input[0].witness.push(sig.serialize_der(&secp_ctx).to_vec()); + spend_tx.input[0].witness.push(sig.serialize_der().to_vec()); spend_tx.input[0].witness[0].push(SigHashType::All as u8); spend_tx.input[0].witness.push(pubkey.serialize().to_vec()); txn.push(spend_tx); @@ -5382,11 +5271,7 @@ fn test_claim_sizeable_push_msat() { let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 99000000); nodes[1].node.force_close_channel(&chan.2); - let events = nodes[1].node.get_and_clear_pending_msg_events(); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[1]); let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_txn.len(), 1); check_spends!(node_txn[0], chan.3.clone()); @@ -5408,11 +5293,8 @@ fn test_claim_on_remote_sizeable_push_msat() { let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 99000000); nodes[0].node.force_close_channel(&chan.2); - let events = nodes[0].node.get_and_clear_pending_msg_events(); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[0]); + let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_txn.len(), 1); check_spends!(node_txn[0], chan.3.clone()); @@ -5420,11 +5302,7 @@ fn test_claim_on_remote_sizeable_push_msat() { let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 0); - let events = nodes[1].node.get_and_clear_pending_msg_events(); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[1]); let spend_txn = check_spendable_outputs!(nodes[1], 1); assert_eq!(spend_txn.len(), 2); assert_eq!(spend_txn[0], spend_txn[1]); @@ -5447,11 +5325,8 @@ fn test_claim_on_remote_revoked_sizeable_push_msat() { claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage); let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1); - let events = nodes[1].node.get_and_clear_pending_msg_events(); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[1]); + let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); let spend_txn = check_spendable_outputs!(nodes[1], 1); assert_eq!(spend_txn.len(), 4); @@ -5518,11 +5393,8 @@ fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() { let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1); - let events = nodes[1].node.get_and_clear_pending_msg_events(); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[1]); + let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_txn.len(), 3); assert_eq!(node_txn.pop().unwrap(), node_txn[0]); @@ -5552,11 +5424,8 @@ fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() { let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; // A will generate HTLC-Timeout from revoked commitment tx nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1); - let events = nodes[0].node.get_and_clear_pending_msg_events(); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[0]); + let revoked_htlc_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(revoked_htlc_txn.len(), 3); assert_eq!(revoked_htlc_txn[0], revoked_htlc_txn[2]); @@ -5567,11 +5436,7 @@ fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() { // B will generate justice tx from A's revoked commitment/HTLC tx nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()] }, 1); - let events = nodes[1].node.get_and_clear_pending_msg_events(); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[1]); let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_txn.len(), 4); @@ -5603,11 +5468,7 @@ fn test_static_spendable_outputs_justice_tx_revoked_htlc_success_tx() { let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; // B will generate HTLC-Success from revoked commitment tx nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1); - let events = nodes[1].node.get_and_clear_pending_msg_events(); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[1]); let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(revoked_htlc_txn.len(), 3); @@ -5618,11 +5479,7 @@ fn test_static_spendable_outputs_justice_tx_revoked_htlc_success_tx() { // A will generate justice tx from B's revoked commitment/HTLC tx nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()] }, 1); - let events = nodes[0].node.get_and_clear_pending_msg_events(); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[0]); let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_txn.len(), 4); @@ -5672,12 +5529,7 @@ fn test_onchain_to_onchain_claim() { assert!(updates.update_fail_malformed_htlcs.is_empty()); nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1); - let events = nodes[2].node.get_and_clear_pending_msg_events(); - assert_eq!(events.len(), 1); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + check_closed_broadcast!(nodes[2]); let c_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx) assert_eq!(c_txn.len(), 3); @@ -5734,11 +5586,8 @@ fn test_onchain_to_onchain_claim() { assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment assert_eq!(b_txn[2].lock_time, 0); // Success tx - let msg_events = nodes[1].node.get_and_clear_pending_msg_events(); - match msg_events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexpected event"), - } + + check_closed_broadcast!(nodes[1]); } #[test] @@ -5760,6 +5609,8 @@ fn test_duplicate_payment_hash_one_failure_one_success() { let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_txn[0].clone()] }, 1); + check_closed_broadcast!(nodes[1]); + let htlc_timeout_tx; { // Extract one of the two HTLC-Timeout transaction let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); @@ -5777,12 +5628,6 @@ fn test_duplicate_payment_hash_one_failure_one_success() { htlc_timeout_tx = node_txn[1].clone(); } - let events = nodes[1].node.get_and_clear_pending_msg_events(); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexepected event"), - } - nodes[2].node.claim_funds(our_payment_preimage); nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_txn[0].clone()] }, 1); check_added_monitors!(nodes[2], 2); @@ -5809,6 +5654,7 @@ fn test_duplicate_payment_hash_one_failure_one_success() { check_spends!(htlc_success_txn[1], commitment_txn[0].clone()); nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![htlc_timeout_tx] }, 200); + expect_pending_htlcs_forwardable!(nodes[1]); let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()); assert!(htlc_updates.update_add_htlcs.is_empty()); assert_eq!(htlc_updates.update_fail_htlcs.len(), 1); @@ -5897,6 +5743,255 @@ fn test_dynamic_spendable_outputs_local_htlc_success_tx() { check_spends!(spend_txn[1], node_txn[2].clone()); } +fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, announce_latest: bool) { + // Test that we fail backwards the full set of HTLCs we need to when remote broadcasts an + // unrevoked commitment transaction. + // This includes HTLCs which were below the dust threshold as well as HTLCs which were awaiting + // a remote RAA before they could be failed backwards (and combinations thereof). + // We also test duplicate-hash HTLCs by adding two nodes on each side of the target nodes which + // use the same payment hashes. + // Thus, we use a six-node network: + // + // A \ / E + // - C - D - + // B / \ F + // And test where C fails back to A/B when D announces its latest commitment transaction + let nodes = create_network(6); + + create_announced_chan_between_nodes(&nodes, 0, 2); + create_announced_chan_between_nodes(&nodes, 1, 2); + let chan = create_announced_chan_between_nodes(&nodes, 2, 3); + create_announced_chan_between_nodes(&nodes, 3, 4); + create_announced_chan_between_nodes(&nodes, 3, 5); + + // Rebalance and check output sanity... + send_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 500000); + send_payment(&nodes[1], &[&nodes[2], &nodes[3], &nodes[5]], 500000); + assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn[0].output.len(), 2); + + let ds_dust_limit = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis; + // 0th HTLC: + let (_, payment_hash_1) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee + // 1st HTLC: + let (_, payment_hash_2) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee + let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV).unwrap(); + // 2nd HTLC: + send_along_route_with_hash(&nodes[1], route.clone(), &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_1); // not added < dust limit + HTLC tx fee + // 3rd HTLC: + send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_2); // not added < dust limit + HTLC tx fee + // 4th HTLC: + let (_, payment_hash_3) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000); + // 5th HTLC: + let (_, payment_hash_4) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000); + let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); + // 6th HTLC: + send_along_route_with_hash(&nodes[1], route.clone(), &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_3); + // 7th HTLC: + send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_4); + + // 8th HTLC: + let (_, payment_hash_5) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000); + // 9th HTLC: + let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV).unwrap(); + send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_5); // not added < dust limit + HTLC tx fee + + // 10th HTLC: + let (_, payment_hash_6) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee + // 11th HTLC: + let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); + send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_6); + + // Double-check that six of the new HTLC were added + // We now have six HTLCs pending over the dust limit and six HTLCs under the dust limit (ie, + // with to_local and to_remote outputs, 8 outputs and 6 HTLCs not included). + assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.len(), 1); + assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn[0].output.len(), 8); + + // Now fail back three of the over-dust-limit and three of the under-dust-limit payments in one go. + // Fail 0th below-dust, 4th above-dust, 8th above-dust, 10th below-dust HTLCs + assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_1, ds_dust_limit*1000)); + assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_3, 1000000)); + assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_5, 1000000)); + assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_6, ds_dust_limit*1000)); + check_added_monitors!(nodes[4], 0); + expect_pending_htlcs_forwardable!(nodes[4]); + check_added_monitors!(nodes[4], 1); + + let four_removes = get_htlc_update_msgs!(nodes[4], nodes[3].node.get_our_node_id()); + nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[0]).unwrap(); + nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[1]).unwrap(); + nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[2]).unwrap(); + nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[3]).unwrap(); + commitment_signed_dance!(nodes[3], nodes[4], four_removes.commitment_signed, false); + + // Fail 3rd below-dust and 7th above-dust HTLCs + assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_2, ds_dust_limit*1000)); + assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_4, 1000000)); + check_added_monitors!(nodes[5], 0); + expect_pending_htlcs_forwardable!(nodes[5]); + check_added_monitors!(nodes[5], 1); + + let two_removes = get_htlc_update_msgs!(nodes[5], nodes[3].node.get_our_node_id()); + nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[0]).unwrap(); + nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[1]).unwrap(); + commitment_signed_dance!(nodes[3], nodes[5], two_removes.commitment_signed, false); + + let ds_prev_commitment_tx = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone(); + + expect_pending_htlcs_forwardable!(nodes[3]); + check_added_monitors!(nodes[3], 1); + let six_removes = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id()); + nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[0]).unwrap(); + nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[1]).unwrap(); + nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[2]).unwrap(); + nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[3]).unwrap(); + nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[4]).unwrap(); + nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[5]).unwrap(); + if deliver_last_raa { + commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false); + } else { + let _cs_last_raa = commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false, true, false, true); + } + + // D's latest commitment transaction now contains 1st + 2nd + 9th HTLCs (implicitly, they're + // below the dust limit) and the 5th + 6th + 11th HTLCs. It has failed back the 0th, 3rd, 4th, + // 7th, 8th, and 10th, but as we haven't yet delivered the final RAA to C, the fails haven't + // propagated back to A/B yet (and D has two unrevoked commitment transactions). + // + // We now broadcast the latest commitment transaction, which *should* result in failures for + // the 0th, 1st, 2nd, 3rd, 4th, 7th, 8th, 9th, and 10th HTLCs, ie all the below-dust HTLCs and + // the non-broadcast above-dust HTLCs. + // + // Alternatively, we may broadcast the previous commitment transaction, which should only + // result in failures for the below-dust HTLCs, ie the 0th, 1st, 2nd, 3rd, 9th, and 10th HTLCs. + let ds_last_commitment_tx = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone(); + + let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + if announce_latest { + nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&ds_last_commitment_tx[0]], &[1; 1]); + } else { + nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&ds_prev_commitment_tx[0]], &[1; 1]); + } + check_closed_broadcast!(nodes[2]); + expect_pending_htlcs_forwardable!(nodes[2]); + check_added_monitors!(nodes[2], 2); + + let cs_msgs = nodes[2].node.get_and_clear_pending_msg_events(); + assert_eq!(cs_msgs.len(), 2); + let mut a_done = false; + for msg in cs_msgs { + match msg { + MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => { + // Both under-dust HTLCs and the one above-dust HTLC that we had already failed + // should be failed-backwards here. + let target = if *node_id == nodes[0].node.get_our_node_id() { + // If announce_latest, expect 0th, 1st, 4th, 8th, 10th HTLCs, else only 0th, 1st, 10th below-dust HTLCs + for htlc in &updates.update_fail_htlcs { + assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 6 || if announce_latest { htlc.htlc_id == 3 || htlc.htlc_id == 5 } else { false }); + } + assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 5 } else { 3 }); + assert!(!a_done); + a_done = true; + &nodes[0] + } else { + // If announce_latest, expect 2nd, 3rd, 7th, 9th HTLCs, else only 2nd, 3rd, 9th below-dust HTLCs + for htlc in &updates.update_fail_htlcs { + assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 5 || if announce_latest { htlc.htlc_id == 4 } else { false }); + } + assert_eq!(*node_id, nodes[1].node.get_our_node_id()); + assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 4 } else { 3 }); + &nodes[1] + }; + target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap(); + target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[1]).unwrap(); + target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[2]).unwrap(); + if announce_latest { + target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[3]).unwrap(); + if *node_id == nodes[0].node.get_our_node_id() { + target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[4]).unwrap(); + } + } + commitment_signed_dance!(target, nodes[2], updates.commitment_signed, false, true); + }, + _ => panic!("Unexpected event"), + } + } + + let as_events = nodes[0].node.get_and_clear_pending_events(); + assert_eq!(as_events.len(), if announce_latest { 5 } else { 3 }); + let mut as_failds = HashSet::new(); + for event in as_events.iter() { + if let &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } = event { + assert!(as_failds.insert(*payment_hash)); + if *payment_hash != payment_hash_2 { + assert_eq!(*rejected_by_dest, deliver_last_raa); + } else { + assert!(!rejected_by_dest); + } + } else { panic!("Unexpected event"); } + } + assert!(as_failds.contains(&payment_hash_1)); + assert!(as_failds.contains(&payment_hash_2)); + if announce_latest { + assert!(as_failds.contains(&payment_hash_3)); + assert!(as_failds.contains(&payment_hash_5)); + } + assert!(as_failds.contains(&payment_hash_6)); + + let bs_events = nodes[1].node.get_and_clear_pending_events(); + assert_eq!(bs_events.len(), if announce_latest { 4 } else { 3 }); + let mut bs_failds = HashSet::new(); + for event in bs_events.iter() { + if let &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } = event { + assert!(bs_failds.insert(*payment_hash)); + if *payment_hash != payment_hash_1 && *payment_hash != payment_hash_5 { + assert_eq!(*rejected_by_dest, deliver_last_raa); + } else { + assert!(!rejected_by_dest); + } + } else { panic!("Unexpected event"); } + } + assert!(bs_failds.contains(&payment_hash_1)); + assert!(bs_failds.contains(&payment_hash_2)); + if announce_latest { + assert!(bs_failds.contains(&payment_hash_4)); + } + assert!(bs_failds.contains(&payment_hash_5)); + + // For each HTLC which was not failed-back by normal process (ie deliver_last_raa), we should + // get a PaymentFailureNetworkUpdate. A should have gotten 4 HTLCs which were failed-back due + // to unknown-preimage-etc, B should have gotten 2. Thus, in the + // announce_latest && deliver_last_raa case, we should have 5-4=1 and 4-2=2 + // PaymentFailureNetworkUpdates. + let as_msg_events = nodes[0].node.get_and_clear_pending_msg_events(); + assert_eq!(as_msg_events.len(), if deliver_last_raa { 1 } else if !announce_latest { 3 } else { 5 }); + let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events(); + assert_eq!(bs_msg_events.len(), if deliver_last_raa { 2 } else if !announce_latest { 3 } else { 4 }); + for event in as_msg_events.iter().chain(bs_msg_events.iter()) { + match event { + &MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {}, + _ => panic!("Unexpected event"), + } + } +} + +#[test] +fn test_fail_backwards_latest_remote_announce_a() { + do_test_fail_backwards_unrevoked_remote_announce(false, true); +} + +#[test] +fn test_fail_backwards_latest_remote_announce_b() { + do_test_fail_backwards_unrevoked_remote_announce(true, true); +} + +#[test] +fn test_fail_backwards_previous_remote_announce() { + do_test_fail_backwards_unrevoked_remote_announce(false, false); + // Note that true, true doesn't make sense as it implies we announce a revoked state, which is + // tested for in test_commitment_revoked_fail_backward_exhaustive() +} + #[test] fn test_dynamic_spendable_outputs_local_htlc_timeout_tx() { let nodes = create_network(2); @@ -5912,11 +6007,8 @@ fn test_dynamic_spendable_outputs_local_htlc_timeout_tx() { // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![local_txn[0].clone()] }, 200); - let events = nodes[0].node.get_and_clear_pending_msg_events(); - match events[0] { - MessageSendEvent::BroadcastChannelUpdate { .. } => {}, - _ => panic!("Unexepected event"), - } + check_closed_broadcast!(nodes[0]); + let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap(); assert_eq!(node_txn[0].input.len(), 1); assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); @@ -5956,6 +6048,146 @@ fn test_static_output_closing_tx() { check_spends!(spend_txn[0], closing_tx); } +fn do_htlc_claim_local_commitment_only(use_dust: bool) { + let nodes = create_network(2); + let chan = create_announced_chan_between_nodes(&nodes, 0, 1); + + let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1]], if use_dust { 50000 } else { 3000000 }); + + // Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being + // present in B's local commitment transaction, but none of A's commitment transactions. + assert!(nodes[1].node.claim_funds(our_payment_preimage)); + check_added_monitors!(nodes[1], 1); + + let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()); + nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]).unwrap(); + let events = nodes[0].node.get_and_clear_pending_events(); + assert_eq!(events.len(), 1); + match events[0] { + Event::PaymentSent { payment_preimage } => { + assert_eq!(payment_preimage, our_payment_preimage); + }, + _ => panic!("Unexpected event"), + } + + nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed).unwrap(); + check_added_monitors!(nodes[0], 1); + let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id()); + nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0).unwrap(); + check_added_monitors!(nodes[1], 1); + + let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + for i in 1..TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + CHAN_CONFIRM_DEPTH + 1 { + nodes[1].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new()); + header.prev_blockhash = header.bitcoin_hash(); + } + test_txn_broadcast(&nodes[1], &chan, None, if use_dust { HTLCType::NONE } else { HTLCType::SUCCESS }); + check_closed_broadcast!(nodes[1]); +} + +fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) { + let mut nodes = create_network(2); + let chan = create_announced_chan_between_nodes(&nodes, 0, 1); + + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), if use_dust { 50000 } else { 3000000 }, TEST_FINAL_CLTV).unwrap(); + let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]); + nodes[0].node.send_payment(route, payment_hash).unwrap(); + check_added_monitors!(nodes[0], 1); + + let _as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); + + // As far as A is concerened, the HTLC is now present only in the latest remote commitment + // transaction, however it is not in A's latest local commitment, so we can just broadcast that + // to "time out" the HTLC. + + let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + for i in 1..TEST_FINAL_CLTV + HTLC_FAIL_TIMEOUT_BLOCKS + CHAN_CONFIRM_DEPTH + 1 { + nodes[0].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new()); + header.prev_blockhash = header.bitcoin_hash(); + } + test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE); + check_closed_broadcast!(nodes[0]); +} + +fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no_close: bool) { + let nodes = create_network(3); + let chan = create_announced_chan_between_nodes(&nodes, 0, 1); + + // Fail the payment, but don't deliver A's final RAA, resulting in the HTLC only being present + // in B's previous (unrevoked) commitment transaction, but none of A's commitment transactions. + // Also optionally test that we *don't* fail the channel in case the commitment transaction was + // actually revoked. + let htlc_value = if use_dust { 50000 } else { 3000000 }; + let (_, our_payment_hash) = route_payment(&nodes[0], &[&nodes[1]], htlc_value); + assert!(nodes[1].node.fail_htlc_backwards(&our_payment_hash, htlc_value)); + expect_pending_htlcs_forwardable!(nodes[1]); + check_added_monitors!(nodes[1], 1); + + let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()); + nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]).unwrap(); + nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed).unwrap(); + check_added_monitors!(nodes[0], 1); + let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id()); + nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0).unwrap(); + check_added_monitors!(nodes[1], 1); + nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.1).unwrap(); + check_added_monitors!(nodes[1], 1); + let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id()); + + if check_revoke_no_close { + nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap(); + check_added_monitors!(nodes[0], 1); + } + + let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; + for i in 1..TEST_FINAL_CLTV + HTLC_FAIL_TIMEOUT_BLOCKS + CHAN_CONFIRM_DEPTH + 1 { + nodes[0].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new()); + header.prev_blockhash = header.bitcoin_hash(); + } + if !check_revoke_no_close { + test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE); + check_closed_broadcast!(nodes[0]); + } else { + let events = nodes[0].node.get_and_clear_pending_events(); + assert_eq!(events.len(), 1); + match events[0] { + Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => { + assert_eq!(payment_hash, our_payment_hash); + assert!(rejected_by_dest); + }, + _ => panic!("Unexpected event"), + } + } +} + +// Test that we close channels on-chain when broadcastable HTLCs reach their timeout window. +// There are only a few cases to test here: +// * its not really normative behavior, but we test that below-dust HTLCs "included" in +// broadcastable commitment transactions result in channel closure, +// * its included in an unrevoked-but-previous remote commitment transaction, +// * its included in the latest remote or local commitment transactions. +// We test each of the three possible commitment transactions individually and use both dust and +// non-dust HTLCs. +// Note that we don't bother testing both outbound and inbound HTLC failures for each case, and we +// assume they are handled the same across all six cases, as both outbound and inbound failures are +// tested for at least one of the cases in other tests. +#[test] +fn htlc_claim_single_commitment_only_a() { + do_htlc_claim_local_commitment_only(true); + do_htlc_claim_local_commitment_only(false); + + do_htlc_claim_current_remote_commitment_only(true); + do_htlc_claim_current_remote_commitment_only(false); +} + +#[test] +fn htlc_claim_single_commitment_only_b() { + do_htlc_claim_previous_remote_commitment_only(true, false); + do_htlc_claim_previous_remote_commitment_only(false, false); + do_htlc_claim_previous_remote_commitment_only(true, true); + do_htlc_claim_previous_remote_commitment_only(false, true); +} + fn run_onion_failure_test(_name: &str, test_case: u8, nodes: &Vec, route: &Route, payment_hash: &PaymentHash, callback_msg: F1, callback_node: F2, expected_retryable: bool, expected_error_code: Option, expected_channel_update: Option) where F1: for <'a> FnMut(&'a mut msgs::UpdateAddHTLC), F2: FnMut(), @@ -6047,6 +6279,7 @@ fn run_onion_failure_test_with_fail_intercept(_name: &str, test_case: expect_htlc_forward!(&nodes[2]); expect_event!(&nodes[2], Event::PaymentReceived); callback_node(); + expect_pending_htlcs_forwardable!(nodes[2]); } let update_2_1 = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id()); @@ -6062,7 +6295,7 @@ fn run_onion_failure_test_with_fail_intercept(_name: &str, test_case: // 2 => 1 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &fail_msg).unwrap(); - commitment_signed_dance!(nodes[1], nodes[2], update_2_1.commitment_signed, true, true); + commitment_signed_dance!(nodes[1], nodes[2], update_2_1.commitment_signed, true); // backward fail on 1 let update_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()); @@ -6164,7 +6397,7 @@ fn test_onion_failure() { let mut nodes = create_network(3); for node in nodes.iter() { - *node.keys_manager.override_session_priv.lock().unwrap() = Some(SecretKey::from_slice(&Secp256k1::without_caps(), &[3; 32]).unwrap()); + *node.keys_manager.override_session_priv.lock().unwrap() = Some(SecretKey::from_slice(&[3; 32]).unwrap()); } let channels = [create_announced_chan_between_nodes(&nodes, 0, 1), create_announced_chan_between_nodes(&nodes, 1, 2)]; let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]); @@ -6174,7 +6407,7 @@ fn test_onion_failure() { // intermediate node failure run_onion_failure_test("invalid_realm", 0, &nodes, &route, &payment_hash, |msg| { - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1; let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap(); let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap(); @@ -6184,7 +6417,7 @@ fn test_onion_failure() { // final node failure run_onion_failure_test("invalid_realm", 3, &nodes, &route, &payment_hash, |msg| { - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1; let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap(); let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap(); @@ -6200,7 +6433,7 @@ fn test_onion_failure() { msg.amount_msat -= 1; }, |msg| { // and tamper returing error message - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap(); msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], NODE|2, &[0;0]); }, ||{}, true, Some(NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[0].pubkey, is_permanent: false})); @@ -6208,7 +6441,7 @@ fn test_onion_failure() { // final node failure run_onion_failure_test_with_fail_intercept("temporary_node_failure", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| { // and tamper returing error message - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap(); msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], NODE|2, &[0;0]); }, ||{ @@ -6219,14 +6452,14 @@ fn test_onion_failure() { run_onion_failure_test_with_fail_intercept("permanent_node_failure", 100, &nodes, &route, &payment_hash, |msg| { msg.amount_msat -= 1; }, |msg| { - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap(); msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|NODE|2, &[0;0]); }, ||{}, true, Some(PERM|NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[0].pubkey, is_permanent: true})); // final node failure run_onion_failure_test_with_fail_intercept("permanent_node_failure", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| { - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap(); msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], PERM|NODE|2, &[0;0]); }, ||{ @@ -6237,7 +6470,7 @@ fn test_onion_failure() { run_onion_failure_test_with_fail_intercept("required_node_feature_missing", 100, &nodes, &route, &payment_hash, |msg| { msg.amount_msat -= 1; }, |msg| { - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap(); msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|NODE|3, &[0;0]); }, ||{ @@ -6246,7 +6479,7 @@ fn test_onion_failure() { // final node failure run_onion_failure_test_with_fail_intercept("required_node_feature_missing", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| { - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap(); msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], PERM|NODE|3, &[0;0]); }, ||{ @@ -6265,7 +6498,7 @@ fn test_onion_failure() { run_onion_failure_test_with_fail_intercept("temporary_channel_failure", 100, &nodes, &route, &payment_hash, |msg| { msg.amount_msat -= 1; }, |msg| { - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap(); msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], UPDATE|7, &ChannelUpdate::dummy().encode_with_len()[..]); }, ||{}, true, Some(UPDATE|7), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()})); @@ -6273,7 +6506,7 @@ fn test_onion_failure() { run_onion_failure_test_with_fail_intercept("permanent_channel_failure", 100, &nodes, &route, &payment_hash, |msg| { msg.amount_msat -= 1; }, |msg| { - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap(); msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|8, &[0;0]); // short_channel_id from the processing node @@ -6282,7 +6515,7 @@ fn test_onion_failure() { run_onion_failure_test_with_fail_intercept("required_channel_feature_missing", 100, &nodes, &route, &payment_hash, |msg| { msg.amount_msat -= 1; }, |msg| { - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap(); msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|9, &[0;0]); // short_channel_id from the processing node @@ -6329,7 +6562,11 @@ fn test_onion_failure() { run_onion_failure_test("final_incorrect_cltv_expiry", 1, &nodes, &route, &payment_hash, |_| {}, || { for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().borrow_parts().forward_htlcs.iter_mut() { for f in pending_forwards.iter_mut() { - f.forward_info.outgoing_cltv_value += 1; + match f { + &mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } => + forward_info.outgoing_cltv_value += 1, + _ => {}, + } } } }, true, Some(18), None); @@ -6338,7 +6575,11 @@ fn test_onion_failure() { // violate amt_to_forward > msg.amount_msat for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().borrow_parts().forward_htlcs.iter_mut() { for f in pending_forwards.iter_mut() { - f.forward_info.amt_to_forward -= 1; + match f { + &mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } => + forward_info.amt_to_forward -= 1, + _ => {}, + } } } }, true, Some(19), None); @@ -6351,7 +6592,7 @@ fn test_onion_failure() { reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false)); run_onion_failure_test("expiry_too_far", 0, &nodes, &route, &payment_hash, |msg| { - let session_priv = SecretKey::from_slice(&::secp256k1::Secp256k1::without_caps(), &[3; 32]).unwrap(); + let session_priv = SecretKey::from_slice(&[3; 32]).unwrap(); let mut route = route.clone(); let height = 1; route.hops[1].cltv_expiry_delta += CLTV_FAR_FAR_AWAY + route.hops[0].cltv_expiry_delta + 1; @@ -6362,3 +6603,304 @@ fn test_onion_failure() { msg.onion_routing_packet = onion_packet; }, ||{}, true, Some(21), None); } + +// BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message. +// BOLT 2 Requirement: MUST NOT offer amount_msat it cannot pay for in the remote commitment transaction at the current feerate_per_kw (see "Updating Fees") while maintaining its channel reserve. +//TODO: I don't believe this is explicitly enforced when sending an HTLC but as the Fee aspect of the BOLT specs is in flux leaving this as a TODO. + +#[test] +fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() { + //BOLT2 Requirement: MUST offer amount_msat greater than 0. + //BOLT2 Requirement: MUST NOT offer amount_msat below the receiving node's htlc_minimum_msat (same validation check catches both of these) + let mut nodes = create_network(2); + let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000); + let mut route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + + route.hops[0].fee_msat = 0; + + let err = nodes[0].node.send_payment(route, our_payment_hash); + + if let Err(APIError::ChannelUnavailable{err}) = err { + assert_eq!(err, "Cannot send less than their minimum HTLC value"); + } else { + assert!(false); + } +} + +#[test] +fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() { + //BOLT 2 Requirement: MUST set cltv_expiry less than 500000000. + //It is enforced when constructing a route. + let mut nodes = create_network(2); + let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 0); + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000000, 500000001).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + + let err = nodes[0].node.send_payment(route, our_payment_hash); + + if let Err(APIError::RouteError{err}) = err { + assert_eq!(err, "Channel CLTV overflowed?!"); + } else { + assert!(false); + } +} + +#[test] +fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() { + //BOLT 2 Requirement: if result would be offering more than the remote's max_accepted_htlcs HTLCs, in the remote commitment transaction: MUST NOT add an HTLC. + //BOLT 2 Requirement: for the first HTLC it offers MUST set id to 0. + //BOLT 2 Requirement: MUST increase the value of id by 1 for each successive offer. + let mut nodes = create_network(2); + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0); + let max_accepted_htlcs = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().their_max_accepted_htlcs as u64; + + for i in 0..max_accepted_htlcs { + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let mut payment_event = { + nodes[0].node.send_payment(route, our_payment_hash).unwrap(); + check_added_monitors!(nodes[0], 1); + + let mut events = nodes[0].node.get_and_clear_pending_msg_events(); + assert_eq!(events.len(), 1); + if let MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate{ update_add_htlcs: ref htlcs, .. }, } = events[0] { + assert_eq!(htlcs[0].htlc_id, i); + } else { + assert!(false); + } + SendEvent::from_event(events.remove(0)) + }; + nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap(); + check_added_monitors!(nodes[1], 0); + commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false); + + expect_pending_htlcs_forwardable!(nodes[1]); + expect_payment_received!(nodes[1], our_payment_hash, 100000); + } + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let err = nodes[0].node.send_payment(route, our_payment_hash); + + if let Err(APIError::ChannelUnavailable{err}) = err { + assert_eq!(err, "Cannot push more than their max accepted HTLCs"); + } else { + assert!(false); + } +} + +#[test] +fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() { + //BOLT 2 Requirement: if the sum of total offered HTLCs would exceed the remote's max_htlc_value_in_flight_msat: MUST NOT add an HTLC. + let mut nodes = create_network(2); + let channel_value = 100000; + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 0); + let max_in_flight = get_channel_value_stat!(nodes[0], chan.2).their_max_htlc_value_in_flight_msat; + + send_payment(&nodes[0], &vec!(&nodes[1])[..], max_in_flight); + + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], max_in_flight+1, TEST_FINAL_CLTV).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let err = nodes[0].node.send_payment(route, our_payment_hash); + + if let Err(APIError::ChannelUnavailable{err}) = err { + assert_eq!(err, "Cannot send value that would put us over our max HTLC value in flight"); + } else { + assert!(false); + } + + send_payment(&nodes[0], &[&nodes[1]], max_in_flight); +} + +// BOLT 2 Requirements for the Receiver when handling an update_add_htlc message. +#[test] +fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() { + //BOLT2 Requirement: receiving an amount_msat equal to 0, OR less than its own htlc_minimum_msat -> SHOULD fail the channel. + let mut nodes = create_network(2); + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000); + let htlc_minimum_msat: u64; + { + let chan_lock = nodes[0].node.channel_state.lock().unwrap(); + let channel = chan_lock.by_id.get(&chan.2).unwrap(); + htlc_minimum_msat = channel.get_our_htlc_minimum_msat(); + } + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], htlc_minimum_msat, TEST_FINAL_CLTV).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + nodes[0].node.send_payment(route, our_payment_hash).unwrap(); + check_added_monitors!(nodes[0], 1); + let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); + updates.update_add_htlcs[0].amount_msat = htlc_minimum_msat-1; + let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]); + if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err { + assert_eq!(err, "Remote side tried to send less than our minimum HTLC value"); + } else { + assert!(false); + } + assert!(nodes[1].node.list_channels().is_empty()); + check_closed_broadcast!(nodes[1]); +} + +#[test] +fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() { + //BOLT2 Requirement: receiving an amount_msat that the sending node cannot afford at the current feerate_per_kw (while maintaining its channel reserve): SHOULD fail the channel + let mut nodes = create_network(2); + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000); + + let their_channel_reserve = get_channel_value_stat!(nodes[0], chan.2).channel_reserve_msat; + + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 5000000-their_channel_reserve, TEST_FINAL_CLTV).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + nodes[0].node.send_payment(route, our_payment_hash).unwrap(); + check_added_monitors!(nodes[0], 1); + let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); + + updates.update_add_htlcs[0].amount_msat = 5000000-their_channel_reserve+1; + let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]); + + if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err { + assert_eq!(err, "Remote HTLC add would put them over their reserve value"); + } else { + assert!(false); + } + + assert!(nodes[1].node.list_channels().is_empty()); + check_closed_broadcast!(nodes[1]); +} + +#[test] +fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() { + //BOLT 2 Requirement: if a sending node adds more than its max_accepted_htlcs HTLCs to its local commitment transaction: SHOULD fail the channel + //BOLT 2 Requirement: MUST allow multiple HTLCs with the same payment_hash. + let mut nodes = create_network(2); + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000); + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + + let session_priv = SecretKey::from_slice(&{ + let mut session_key = [0; 32]; + rng::fill_bytes(&mut session_key); + session_key + }).expect("RNG is bad!"); + + let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1; + let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route, &session_priv).unwrap(); + let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap(); + let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &our_payment_hash); + + let mut msg = msgs::UpdateAddHTLC { + channel_id: chan.2, + htlc_id: 0, + amount_msat: 1000, + payment_hash: our_payment_hash, + cltv_expiry: htlc_cltv, + onion_routing_packet: onion_packet.clone(), + }; + + for i in 0..super::channel::OUR_MAX_HTLCS { + msg.htlc_id = i as u64; + nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg).unwrap(); + } + msg.htlc_id = (super::channel::OUR_MAX_HTLCS) as u64; + let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg); + + if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err { + assert_eq!(err, "Remote tried to push more than our max accepted HTLCs"); + } else { + assert!(false); + } + + assert!(nodes[1].node.list_channels().is_empty()); + check_closed_broadcast!(nodes[1]); +} + +#[test] +fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() { + //OR adds more than its max_htlc_value_in_flight_msat worth of offered HTLCs to its local commitment transaction: SHOULD fail the channel + let mut nodes = create_network(2); + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000); + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + nodes[0].node.send_payment(route, our_payment_hash).unwrap(); + check_added_monitors!(nodes[0], 1); + let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); + updates.update_add_htlcs[0].amount_msat = get_channel_value_stat!(nodes[1], chan.2).their_max_htlc_value_in_flight_msat + 1; + let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]); + + if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err { + assert_eq!(err,"Remote HTLC add would put them over their max HTLC value in flight"); + } else { + assert!(false); + } + + assert!(nodes[1].node.list_channels().is_empty()); + check_closed_broadcast!(nodes[1]); +} + +#[test] +fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() { + //BOLT2 Requirement: if sending node sets cltv_expiry to greater or equal to 500000000: SHOULD fail the channel. + let mut nodes = create_network(2); + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000); + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + nodes[0].node.send_payment(route, our_payment_hash).unwrap(); + check_added_monitors!(nodes[0], 1); + let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); + updates.update_add_htlcs[0].cltv_expiry = 500000000; + let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]); + + if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err { + assert_eq!(err,"Remote provided CLTV expiry in seconds instead of block height"); + } else { + assert!(false); + } + + assert!(nodes[1].node.list_channels().is_empty()); + check_closed_broadcast!(nodes[1]); +} + +#[test] +fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() { + //BOLT 2 requirement: if the sender did not previously acknowledge the commitment of that HTLC: MUST ignore a repeated id value after a reconnection. + // We test this by first testing that that repeated HTLCs pass commitment signature checks + // after disconnect and that non-sequential htlc_ids result in a channel failure. + let mut nodes = create_network(2); + create_announced_chan_between_nodes(&nodes, 0, 1); + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + nodes[0].node.send_payment(route, our_payment_hash).unwrap(); + check_added_monitors!(nodes[0], 1); + let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); + nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap(); + + //Disconnect and Reconnect + nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); + nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id()); + let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]); + assert_eq!(reestablish_1.len(), 1); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id()); + let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]); + assert_eq!(reestablish_2.len(), 1); + nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap(); + handle_chan_reestablish_msgs!(nodes[0], nodes[1]); + nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap(); + handle_chan_reestablish_msgs!(nodes[1], nodes[0]); + + //Resend HTLC + nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap(); + assert_eq!(updates.commitment_signed.htlc_signatures.len(), 1); + nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed).unwrap(); + check_added_monitors!(nodes[1], 1); + let _bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id()); + + let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]); + if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err { + assert_eq!(err, "Remote skipped HTLC ID"); + } else { + assert!(false); + } + + assert!(nodes[1].node.list_channels().is_empty()); + check_closed_broadcast!(nodes[1]); +}