X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Ffunctional_tests.rs;h=6dd09e1ee4c073a6a8394b0dd56163785ac4681f;hb=90ce97198cf37c589c11eb2091d09c7805b2c026;hp=3a8e3b8619d94bc2eba717fc0f754b2ac4bebf9d;hpb=93fd3e8bb95b23fa6929dc201cff928892f34d55;p=rust-lightning diff --git a/src/ln/functional_tests.rs b/src/ln/functional_tests.rs index 3a8e3b86..6dd09e1e 100644 --- a/src/ln/functional_tests.rs +++ b/src/ln/functional_tests.rs @@ -2648,14 +2648,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); @@ -2669,6 +2670,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 @@ -2677,7 +2679,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()); @@ -2686,22 +2689,28 @@ fn test_htlc_on_chain_success() { nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1); check_closed_broadcast!(nodes[2]); - 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]); + 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); @@ -2719,25 +2728,45 @@ 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 @@ -2749,7 +2778,9 @@ fn test_htlc_on_chain_success() { 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()); @@ -2761,26 +2792,22 @@ fn test_htlc_on_chain_success() { nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone(), node_txn[0].clone()] }, 1); 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] @@ -2951,7 +2978,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. @@ -2973,15 +3000,22 @@ 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]); @@ -3043,8 +3077,8 @@ 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. + // 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); @@ -3152,9 +3186,19 @@ 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_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]