Merge pull request #1503 from valentinewallace/2022-05-onion-msgs
[rust-lightning] / lightning / src / ln / monitor_tests.rs
index 7e1eec96b1ebd6e53d1aefc9c714d111694c1c9e..4f36b9a88810aa5122c3347953028c6b31943a87 100644 (file)
@@ -15,7 +15,7 @@ use ln::channel;
 use ln::channelmanager::BREAKDOWN_TIMEOUT;
 use ln::features::InitFeatures;
 use ln::msgs::ChannelMessageHandler;
-use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
+use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
 
 use bitcoin::blockdata::script::Builder;
 use bitcoin::blockdata::opcodes;
@@ -74,7 +74,7 @@ fn chanmon_fail_from_stale_commitment() {
        assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
 
        connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
-       expect_pending_htlcs_forwardable!(nodes[1]);
+       expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_id_2 }]);
        check_added_monitors!(nodes[1], 1);
        let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
 
@@ -94,6 +94,55 @@ fn test_spendable_output<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, spendable_t
        } else { panic!(); }
 }
 
+#[test]
+fn revoked_output_htlc_resolution_timing() {
+       // Tests that HTLCs which were present in a broadcasted remote revoked commitment transaction
+       // are resolved only after a spend of the HTLC output reaches six confirmations. Preivously
+       // they would resolve after the revoked commitment transaction itself reaches six
+       // confirmations.
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known());
+
+       let payment_hash_1 = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1;
+
+       // Get a commitment transaction which contains the HTLC we care about, but which we'll revoke
+       // before forwarding.
+       let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
+       assert_eq!(revoked_local_txn.len(), 1);
+
+       // Route a dust payment to revoke the above commitment transaction
+       route_payment(&nodes[0], &[&nodes[1]], 1_000);
+
+       // Confirm the revoked commitment transaction, closing the channel.
+       mine_transaction(&nodes[1], &revoked_local_txn[0]);
+       check_added_monitors!(nodes[1], 1);
+       check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
+       check_closed_broadcast!(nodes[1], true);
+
+       let bs_spend_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
+       assert_eq!(bs_spend_txn.len(), 2);
+       check_spends!(bs_spend_txn[0], revoked_local_txn[0]);
+       check_spends!(bs_spend_txn[1], chan.3);
+
+       // After the commitment transaction confirms, we should still wait on the HTLC spend
+       // transaction to confirm before resolving the HTLC.
+       connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+
+       // Spend the HTLC output, generating a HTLC failure event after ANTI_REORG_DELAY confirmations.
+       mine_transaction(&nodes[1], &bs_spend_txn[0]);
+       assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+       assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+
+       connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
+       expect_payment_failed!(nodes[1], payment_hash_1, true);
+}
+
 #[test]
 fn chanmon_claim_value_coop_close() {
        // Tests `get_claimable_balances` returns the correct values across a simple cooperative claim.
@@ -201,7 +250,7 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
        assert_eq!(funding_outpoint.to_channel_id(), chan_id);
 
        // This HTLC is immediately claimed, giving node B the preimage
-       let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
+       let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
        // This HTLC is allowed to time out, letting A claim it. However, in order to test claimable
        // balances more fully we also give B the preimage for this HTLC.
        let (timeout_payment_preimage, timeout_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 4_000_000);
@@ -234,13 +283,18 @@ fn do_test_claim_value_force_close(prev_commitment_tx: bool) {
 
        nodes[1].node.claim_funds(payment_preimage);
        check_added_monitors!(nodes[1], 1);
+       expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
+
        let b_htlc_msgs = get_htlc_update_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
        // We claim the dust payment here as well, but it won't impact our claimable balances as its
        // dust and thus doesn't appear on chain at all.
        nodes[1].node.claim_funds(dust_payment_preimage);
        check_added_monitors!(nodes[1], 1);
+       expect_payment_claimed!(nodes[1], dust_payment_hash, 3_000);
+
        nodes[1].node.claim_funds(timeout_payment_preimage);
        check_added_monitors!(nodes[1], 1);
+       expect_payment_claimed!(nodes[1], timeout_payment_hash, 4_000_000);
 
        if prev_commitment_tx {
                // To build a previous commitment transaction, deliver one round of commitment messages.
@@ -547,9 +601,10 @@ fn test_balances_on_local_commitment_htlcs() {
 
        expect_pending_htlcs_forwardable!(nodes[1]);
        expect_payment_received!(nodes[1], payment_hash_2, payment_secret_2, 20_000_000);
-       assert!(nodes[1].node.claim_funds(payment_preimage_2));
+       nodes[1].node.claim_funds(payment_preimage_2);
        get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
        check_added_monitors!(nodes[1], 1);
+       expect_payment_claimed!(nodes[1], payment_hash_2, 20_000_000);
 
        let chan_feerate = get_feerate!(nodes[0], chan_id) as u64;
        let opt_anchors = get_opt_anchors!(nodes[0], chan_id);