Skip to content

Commit 8edb45b

Browse files
author
Antoine Riard
committed
Add test_bump_penalty_txn_on_revoked_htlcs
1 parent b6f13da commit 8edb45b

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6326,3 +6326,132 @@ fn test_bump_penalty_txn_on_revoked_commitment() {
63266326
nodes[1].node.get_and_clear_pending_events();
63276327
nodes[1].node.get_and_clear_pending_msg_events();
63286328
}
6329+
6330+
#[test]
6331+
fn test_bump_penalty_txn_on_revoked_htlcs() {
6332+
// In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to sure
6333+
// we're able to claim outputs on revoked HTLC transactions before timelocks expiration
6334+
6335+
let nodes = create_network(2, &[None, None]);
6336+
6337+
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, LocalFeatures::new(), LocalFeatures::new());
6338+
// Lock HTLC in both directions
6339+
let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3_000_000).0;
6340+
route_payment(&nodes[1], &vec!(&nodes[0])[..], 3_000_000).0;
6341+
6342+
let revoked_local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
6343+
assert_eq!(revoked_local_txn[0].input.len(), 1);
6344+
assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
6345+
6346+
// Revoke local commitment tx
6347+
claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage, 3_000_000);
6348+
6349+
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
6350+
// B will generate both revoked HTLC-timeout/HTLC-preimage txn from revoked commitment tx
6351+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
6352+
check_closed_broadcast!(nodes[1]);
6353+
6354+
let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
6355+
assert_eq!(revoked_htlc_txn.len(), 6);
6356+
if revoked_htlc_txn[0].input[0].witness.last().unwrap().len() == ACCEPTED_HTLC_SCRIPT_WEIGHT {
6357+
assert_eq!(revoked_htlc_txn[0].input.len(), 1);
6358+
check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone());
6359+
assert_eq!(revoked_htlc_txn[1].input.len(), 1);
6360+
assert_eq!(revoked_htlc_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
6361+
check_spends!(revoked_htlc_txn[1], revoked_local_txn[0].clone());
6362+
} else {
6363+
assert_eq!(revoked_htlc_txn[1].input.len(), 1);
6364+
check_spends!(revoked_htlc_txn[1], revoked_local_txn[0].clone());
6365+
assert_eq!(revoked_htlc_txn[0].input.len(), 1);
6366+
assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
6367+
check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone());
6368+
}
6369+
6370+
// Broadcast set of revoked txn on A
6371+
let header_129 = connect_blocks(&nodes[0].chain_monitor, 128, 1, true, header.bitcoin_hash());
6372+
let header_130 = BlockHeader { version: 0x20000000, prev_blockhash: header_129, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
6373+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header: header_130, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone(), revoked_htlc_txn[1].clone()] }, 130);
6374+
let first;
6375+
let second;
6376+
let feerate_1;
6377+
let feerate_2;
6378+
{
6379+
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
6380+
assert_eq!(node_txn.len(), 9); // 3 penalty txn on revoked commitment tx * 2 (block-rescan) + A commitment tx + 2 penalty tnx on revoked HTLC txn
6381+
// Verify claim tx are spending revoked HTLC txn
6382+
assert_eq!(node_txn[7].input.len(), 1);
6383+
assert_eq!(node_txn[7].output.len(), 1);
6384+
check_spends!(node_txn[7], revoked_htlc_txn[0].clone());
6385+
first = node_txn[7].txid();
6386+
assert_eq!(node_txn[8].input.len(), 1);
6387+
assert_eq!(node_txn[8].output.len(), 1);
6388+
check_spends!(node_txn[8], revoked_htlc_txn[1].clone());
6389+
second = node_txn[8].txid();
6390+
// Store both feerates for later comparison
6391+
let fee_1 = revoked_htlc_txn[0].output[0].value - node_txn[7].output[0].value;
6392+
feerate_1 = fee_1 * 1000 / node_txn[7].get_weight() as u64;
6393+
let fee_2 = revoked_htlc_txn[1].output[0].value - node_txn[8].output[0].value;
6394+
feerate_2 = fee_2 * 1000 / node_txn[8].get_weight() as u64;
6395+
node_txn.clear();
6396+
}
6397+
6398+
// Connect three more block to see if bumped penalty are issued for HTLC txn
6399+
let header_133 = connect_blocks(&nodes[0].chain_monitor, 3, 130, true, header_130.bitcoin_hash());
6400+
let node_txn = {
6401+
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
6402+
assert_eq!(node_txn.len(), 4); // 2 first tx : bumped claiming txn on Revoked Commitment Tx + 2 last tx : bumped claiming txn on Revoked HTLCs Txn
6403+
check_spends!(node_txn[0], revoked_local_txn[0].clone());
6404+
check_spends!(node_txn[1], revoked_local_txn[0].clone());
6405+
assert_eq!(node_txn[2].input.len(), 1);
6406+
assert_eq!(node_txn[2].output.len(), 1);
6407+
assert_eq!(node_txn[3].input.len(), 1);
6408+
assert_eq!(node_txn[3].output.len(), 1);
6409+
// Verify bumped tx is different and 25% bump heuristic
6410+
if node_txn[2].input[0].previous_output.txid == revoked_htlc_txn[0].txid() {
6411+
check_spends!(node_txn[2], revoked_htlc_txn[0].clone());
6412+
assert_ne!(first, node_txn[2].txid());
6413+
let fee = revoked_htlc_txn[0].output[0].value - node_txn[2].output[0].value;
6414+
let new_feerate = fee * 1000 / node_txn[2].get_weight() as u64;
6415+
assert!(new_feerate * 100 > feerate_1 * 125);
6416+
6417+
check_spends!(node_txn[3], revoked_htlc_txn[1].clone());
6418+
assert_ne!(second, node_txn[3].txid());
6419+
let fee = revoked_htlc_txn[1].output[0].value - node_txn[3].output[0].value;
6420+
let new_feerate = fee * 1000 / node_txn[3].get_weight() as u64;
6421+
assert!(new_feerate * 100 > feerate_2 * 125);
6422+
} else if node_txn[2].input[0].previous_output.txid == revoked_htlc_txn[1].txid() {
6423+
check_spends!(node_txn[2], revoked_htlc_txn[1].clone());
6424+
assert_ne!(second, node_txn[2].txid());
6425+
let fee = revoked_htlc_txn[1].output[0].value - node_txn[2].output[0].value;
6426+
let new_feerate = fee * 1000 / node_txn[2].get_weight() as u64;
6427+
assert!(new_feerate * 100 > feerate_2 * 125);
6428+
6429+
check_spends!(node_txn[3], revoked_htlc_txn[0].clone());
6430+
assert_ne!(first, node_txn[3].txid());
6431+
let fee = revoked_htlc_txn[0].output[0].value - node_txn[3].output[0].value;
6432+
let new_feerate = fee * 1000 / node_txn[3].get_weight() as u64;
6433+
assert!(new_feerate * 100 > feerate_1 * 125);
6434+
} else { assert!(false) }
6435+
let txn = vec![node_txn[0].clone(), node_txn[1].clone(), node_txn[2].clone(), node_txn[3].clone()];
6436+
node_txn.clear();
6437+
txn
6438+
};
6439+
// Broadcast claim txn and confirm blocks to avoid further bumps on this outputs
6440+
let header_134 = BlockHeader { version: 0x20000000, prev_blockhash: header_133, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
6441+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header: header_134, txdata: node_txn }, 134);
6442+
connect_blocks(&nodes[0].chain_monitor, 6, 134, true, header_134.bitcoin_hash());
6443+
{
6444+
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
6445+
node_txn.clear();
6446+
}
6447+
6448+
// Connect few more blocks and check only penalty transaction for to_local output have been issued
6449+
connect_blocks(&nodes[0].chain_monitor, 5, 140, true, header_134.bitcoin_hash());
6450+
{
6451+
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
6452+
assert_eq!(node_txn.len(), 1);
6453+
check_spends!(node_txn[0], revoked_local_txn[0].clone());
6454+
node_txn.clear();
6455+
}
6456+
check_closed_broadcast!(nodes[0]);
6457+
}

0 commit comments

Comments
 (0)