Skip to content

Commit 034c3e2

Browse files
author
Antoine Riard
committed
Add block_disconnecting tests to cancel HTLC failure updates
Add test_sweep_outbound_htlc_failure_update
1 parent c0d1400 commit 034c3e2

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/ln/functional_test_utils.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use util::logger::Logger;
1515
use util::config::UserConfig;
1616

1717
use bitcoin::util::hash::BitcoinHash;
18-
use bitcoin::blockdata::block::BlockHeader;
18+
use bitcoin::blockdata::block::{BlockHeader, Block};
1919
use bitcoin::blockdata::transaction::{Transaction, TxOut};
2020
use bitcoin::network::constants::Network;
2121

@@ -56,6 +56,20 @@ pub fn connect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u3
5656
}
5757
}
5858

59+
pub fn disconnect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) {
60+
let mut header = BlockHeader { version: 0x2000000, prev_blockhash: if parent { prev_blockhash } else { Default::default() }, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
61+
let mut blocks = Vec::new();
62+
for _ in 0..depth {
63+
blocks.push(Block { header, txdata: Vec::new() });
64+
header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
65+
}
66+
let mut height = height;
67+
for block in blocks.pop() {
68+
chain.block_disconnected(&block.header, height);
69+
height -= 1;
70+
}
71+
}
72+
5973
pub struct Node {
6074
pub chain_monitor: Arc<chaininterface::ChainWatchInterfaceUtil>,
6175
pub tx_broadcaster: Arc<test_utils::TestBroadcaster>,

src/ln/functional_tests.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5682,3 +5682,71 @@ fn test_no_failure_dust_htlc_local_commitment() {
56825682
claim_payment(&nodes[0], &vec!(&nodes[1])[..], preimage_1);
56835683
claim_payment(&nodes[1], &vec!(&nodes[0])[..], preimage_2);
56845684
}
5685+
5686+
fn do_test_sweep_outbound_htlc_failure_update(revoked: bool, local: bool) {
5687+
// Outbound HTLC-failure updates must be cancelled if we get a reorg before we reach HTLC_FAIL_ANTI_REORG_DELAY.
5688+
// Broadcast of revoked remote commitment tx, trigger failure-update of dust/non-dust HTLCs
5689+
// Broadcast of remote commitment tx, trigger failure-update of dust-HTLCs
5690+
// Broadcast of timeout tx on remote commitment tx, trigger failure-udate of non-dust HTLCs
5691+
// Broadcast of local commitment tx, trigger failure-update of dust-HTLCs
5692+
// Broadcast of HTLC-timeout tx on local commitment tx, trigger failure-update of non-dust HTLCs
5693+
5694+
let nodes = create_network(2);
5695+
let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5696+
5697+
let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
5698+
5699+
let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
5700+
let (payment_preimage_2, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
5701+
5702+
let as_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
5703+
let bs_commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
5704+
5705+
// We revoked bs_commitment_tx
5706+
if revoked {
5707+
let (payment_preimage_3, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
5708+
claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
5709+
}
5710+
5711+
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5712+
let header_2 = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5713+
let mut timeout_tx = Vec::new();
5714+
if local {
5715+
// We fail dust-HTLC 1 by broadcast of local commitment tx
5716+
nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_commitment_tx[0]], &[1; 1]);
5717+
timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].clone());
5718+
assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5719+
// We fail dust-HTLC 2 by broadcast of local HTLC-timeout tx on local commitment tx
5720+
nodes[0].chain_monitor.block_connected_checked(&header_2, 1, &[&timeout_tx[0]], &[1; 1]);
5721+
} else {
5722+
// We fail dust-HTLC 1 by broadcast of remote commitment tx. If revoked, fail also non-dust HTLC
5723+
nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&bs_commitment_tx[0]], &[1; 1]);
5724+
if !revoked {
5725+
timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].clone());
5726+
assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5727+
// We fail non-dust-HTLC 2 by broadcast of local timeout tx on remote commitment tx
5728+
nodes[0].chain_monitor.block_connected_checked(&header_2, 1, &[&timeout_tx[0]], &[1; 1]);
5729+
}
5730+
}
5731+
5732+
assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
5733+
// We connect 3 blocks, not enough to reach HTLC_FAIL_ANTI_REORG_DELAY
5734+
connect_blocks(&nodes[0].chain_monitor, 3, 2, true, header.bitcoin_hash());
5735+
assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
5736+
5737+
// We disconnect 5 blocks, updates should have been cancelled and HTLC still claimable
5738+
disconnect_blocks(&nodes[0].chain_monitor, 3, 5, true, header.bitcoin_hash());
5739+
nodes[0].chain_monitor.block_disconnected(&header, 2);
5740+
nodes[0].chain_monitor.block_disconnected(&header, 1);
5741+
assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
5742+
5743+
claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
5744+
claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_2);
5745+
}
5746+
5747+
#[test]
5748+
fn test_sweep_outbound_htlc_failure_update() {
5749+
do_test_sweep_outbound_htlc_failure_update(false, true);
5750+
do_test_sweep_outbound_htlc_failure_update(false, false);
5751+
do_test_sweep_outbound_htlc_failure_update(true, false);
5752+
}

0 commit comments

Comments
 (0)