1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Further functional tests which test blockchain reorganizations.
12 use chain::channelmonitor::ANTI_REORG_DELAY;
13 use ln::{PaymentPreimage, PaymentHash};
14 use ln::features::InitFeatures;
15 use ln::msgs::{ChannelMessageHandler, ErrorAction};
16 use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
17 use routing::network_graph::NetworkUpdate;
18 use routing::router::get_route;
20 use bitcoin::hashes::sha256::Hash as Sha256;
21 use bitcoin::hashes::Hash;
25 use ln::functional_test_utils::*;
28 fn chanmon_fail_from_stale_commitment() {
29 // If we forward an HTLC to our counterparty, but we force-closed the channel before our
30 // counterparty provides us an updated commitment transaction, we'll end up with a commitment
31 // transaction that does not contain the HTLC which we attempted to forward. In this case, we
32 // need to wait `ANTI_REORG_DELAY` blocks and then fail back the HTLC as there is no way for us
33 // to learn the preimage and the confirmed commitment transaction paid us the value of the
36 // However, previously, we did not do this, ignoring the HTLC entirely.
38 // This could lead to channel closure if the sender we received the HTLC from decides to go on
39 // chain to get their HTLC back before it times out.
41 // Here, we check exactly this case, forwarding a payment from A, through B, to C, before B
42 // broadcasts its latest commitment transaction, which should result in it eventually failing
43 // the HTLC back off-chain to A.
44 let chanmon_cfgs = create_chanmon_cfgs(3);
45 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
46 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
47 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
49 create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
50 let (update_a, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
52 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
53 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
54 check_added_monitors!(nodes[0], 1);
56 let bs_txn = get_local_commitment_txn!(nodes[1], chan_id_2);
58 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
59 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
60 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false);
62 expect_pending_htlcs_forwardable!(nodes[1]);
63 get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
64 check_added_monitors!(nodes[1], 1);
66 // Don't bother delivering the new HTLC add/commits, instead confirming the pre-HTLC commitment
67 // transaction for nodes[1].
68 mine_transaction(&nodes[1], &bs_txn[0]);
69 check_added_monitors!(nodes[1], 1);
70 check_closed_broadcast!(nodes[1], true);
71 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
73 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
74 expect_pending_htlcs_forwardable!(nodes[1]);
75 check_added_monitors!(nodes[1], 1);
76 let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
78 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
79 commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, true, true);
80 expect_payment_failed_with_update!(nodes[0], payment_hash, false, update_a.contents.short_channel_id, true);