+#[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, &[None, None]);
+
+ // Create some initial channels
+ let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+ route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000).0;
+ let local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
+ assert_eq!(local_txn[0].input.len(), 1);
+ check_spends!(local_txn[0], chan_1.3.clone());
+
+ // 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);
+ 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);
+ check_spends!(node_txn[0], local_txn[0].clone());
+
+ // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
+ let spend_txn = check_spendable_outputs!(nodes[0], 1);
+ assert_eq!(spend_txn.len(), 8);
+ assert_eq!(spend_txn[0], spend_txn[2]);
+ assert_eq!(spend_txn[0], spend_txn[4]);
+ assert_eq!(spend_txn[0], spend_txn[6]);
+ assert_eq!(spend_txn[1], spend_txn[3]);
+ assert_eq!(spend_txn[1], spend_txn[5]);
+ assert_eq!(spend_txn[1], spend_txn[7]);
+ check_spends!(spend_txn[0], local_txn[0].clone());
+ check_spends!(spend_txn[1], node_txn[0].clone());
+}
+
+#[test]
+fn test_static_output_closing_tx() {
+ let nodes = create_network(2, &[None, None]);
+
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+ send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
+ let closing_tx = close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true).2;
+
+ 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![closing_tx.clone()] }, 1);
+ let spend_txn = check_spendable_outputs!(nodes[0], 2);
+ assert_eq!(spend_txn.len(), 1);
+ check_spends!(spend_txn[0], closing_tx.clone());
+
+ nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![closing_tx.clone()] }, 1);
+ let spend_txn = check_spendable_outputs!(nodes[1], 2);
+ assert_eq!(spend_txn.len(), 1);
+ check_spends!(spend_txn[0], closing_tx);
+}
+
+fn do_htlc_claim_local_commitment_only(use_dust: bool) {
+ let nodes = create_network(2, &[None, None]);
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+ 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, &[None, None]);
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+ 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 concerned, 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 + LATENCY_GRACE_PERIOD_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, &[None, None, None]);
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
+
+ // 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));
+ 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 + LATENCY_GRACE_PERIOD_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<F1,F2>(_name: &str, test_case: u8, nodes: &Vec<Node>, route: &Route, payment_hash: &PaymentHash, callback_msg: F1, callback_node: F2, expected_retryable: bool, expected_error_code: Option<u16>, expected_channel_update: Option<HTLCFailChannelUpdate>)
+ where F1: for <'a> FnMut(&'a mut msgs::UpdateAddHTLC),
+ F2: FnMut(),
+{
+ run_onion_failure_test_with_fail_intercept(_name, test_case, nodes, route, payment_hash, callback_msg, |_|{}, callback_node, expected_retryable, expected_error_code, expected_channel_update);
+}
+
+// test_case
+// 0: node1 fails backward
+// 1: final node fails backward
+// 2: payment completed but the user rejects the payment
+// 3: final node fails backward (but tamper onion payloads from node0)
+// 100: trigger error in the intermediate node and tamper returning fail_htlc
+// 200: trigger error in the final node and tamper returning fail_htlc
+fn run_onion_failure_test_with_fail_intercept<F1,F2,F3>(_name: &str, test_case: u8, nodes: &Vec<Node>, route: &Route, payment_hash: &PaymentHash, mut callback_msg: F1, mut callback_fail: F2, mut callback_node: F3, expected_retryable: bool, expected_error_code: Option<u16>, expected_channel_update: Option<HTLCFailChannelUpdate>)
+ where F1: for <'a> FnMut(&'a mut msgs::UpdateAddHTLC),
+ F2: for <'a> FnMut(&'a mut msgs::UpdateFailHTLC),
+ F3: FnMut(),
+{
+
+ // reset block height
+ let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+ for ix in 0..nodes.len() {
+ nodes[ix].chain_monitor.block_connected_checked(&header, 1, &Vec::new()[..], &[0; 0]);
+ }
+
+ macro_rules! expect_event {
+ ($node: expr, $event_type: path) => {{
+ let events = $node.node.get_and_clear_pending_events();
+ assert_eq!(events.len(), 1);
+ match events[0] {
+ $event_type { .. } => {},
+ _ => panic!("Unexpected event"),
+ }
+ }}
+ }
+
+ macro_rules! expect_htlc_forward {
+ ($node: expr) => {{
+ expect_event!($node, Event::PendingHTLCsForwardable);
+ $node.node.process_pending_htlc_forwards();
+ }}
+ }
+
+ // 0 ~~> 2 send payment
+ nodes[0].node.send_payment(route.clone(), payment_hash.clone()).unwrap();
+ check_added_monitors!(nodes[0], 1);
+ let update_0 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
+ // temper update_add (0 => 1)
+ let mut update_add_0 = update_0.update_add_htlcs[0].clone();
+ if test_case == 0 || test_case == 3 || test_case == 100 {
+ callback_msg(&mut update_add_0);
+ callback_node();
+ }
+ // 0 => 1 update_add & CS
+ nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add_0).unwrap();
+ commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
+
+ let update_1_0 = match test_case {
+ 0|100 => { // intermediate node failure; fail backward to 0
+ let update_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+ assert!(update_1_0.update_fail_htlcs.len()+update_1_0.update_fail_malformed_htlcs.len()==1 && (update_1_0.update_fail_htlcs.len()==1 || update_1_0.update_fail_malformed_htlcs.len()==1));
+ update_1_0
+ },
+ 1|2|3|200 => { // final node failure; forwarding to 2
+ assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+ // forwarding on 1
+ if test_case != 200 {
+ callback_node();
+ }
+ expect_htlc_forward!(&nodes[1]);
+
+ let update_1 = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
+ check_added_monitors!(&nodes[1], 1);
+ assert_eq!(update_1.update_add_htlcs.len(), 1);
+ // tamper update_add (1 => 2)
+ let mut update_add_1 = update_1.update_add_htlcs[0].clone();
+ if test_case != 3 && test_case != 200 {
+ callback_msg(&mut update_add_1);
+ }
+
+ // 1 => 2
+ nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_1).unwrap();
+ commitment_signed_dance!(nodes[2], nodes[1], update_1.commitment_signed, false, true);
+
+ if test_case == 2 || test_case == 200 {
+ 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());
+ if test_case == 2 || test_case == 200 {
+ check_added_monitors!(&nodes[2], 1);
+ }
+ assert!(update_2_1.update_fail_htlcs.len() == 1);
+
+ let mut fail_msg = update_2_1.update_fail_htlcs[0].clone();
+ if test_case == 200 {
+ callback_fail(&mut fail_msg);
+ }
+
+ // 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);
+
+ // backward fail on 1
+ let update_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+ assert!(update_1_0.update_fail_htlcs.len() == 1);
+ update_1_0
+ },
+ _ => unreachable!(),
+ };
+
+ // 1 => 0 commitment_signed_dance
+ if update_1_0.update_fail_htlcs.len() > 0 {
+ let mut fail_msg = update_1_0.update_fail_htlcs[0].clone();
+ if test_case == 100 {
+ callback_fail(&mut fail_msg);
+ }
+ nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg).unwrap();
+ } else {
+ nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_1_0.update_fail_malformed_htlcs[0]).unwrap();
+ };
+
+ commitment_signed_dance!(nodes[0], nodes[1], update_1_0.commitment_signed, false, true);
+
+ let events = nodes[0].node.get_and_clear_pending_events();
+ assert_eq!(events.len(), 1);
+ if let &Event::PaymentFailed { payment_hash:_, ref rejected_by_dest, ref error_code } = &events[0] {
+ assert_eq!(*rejected_by_dest, !expected_retryable);
+ assert_eq!(*error_code, expected_error_code);
+ } else {
+ panic!("Uexpected event");
+ }
+
+ let events = nodes[0].node.get_and_clear_pending_msg_events();
+ if expected_channel_update.is_some() {
+ assert_eq!(events.len(), 1);
+ match events[0] {
+ MessageSendEvent::PaymentFailureNetworkUpdate { ref update } => {
+ match update {
+ &HTLCFailChannelUpdate::ChannelUpdateMessage { .. } => {
+ if let HTLCFailChannelUpdate::ChannelUpdateMessage { .. } = expected_channel_update.unwrap() {} else {
+ panic!("channel_update not found!");