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