Skip to content

Commit 0843c9a

Browse files
Antoine RiardTheBlueMatt
Antoine Riard
authored andcommitted
Add test_bump_penalty_txn_on_remote_commitment
1 parent ec8e3bf commit 0843c9a

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6558,3 +6558,111 @@ fn test_bump_penalty_txn_on_revoked_htlcs() {
65586558
}
65596559
check_closed_broadcast!(nodes[0]);
65606560
}
6561+
6562+
#[test]
6563+
fn test_bump_penalty_txn_on_remote_commitment() {
6564+
// In case of claim txn with too low feerates for getting into mempools, RBF-bump them to be sure
6565+
// we're able to claim outputs on remote commitment transaction before timelocks expiration
6566+
6567+
// Create 2 HTLCs
6568+
// Provide preimage for one
6569+
// Check aggregation
6570+
6571+
let nodes = create_network(2, &[None, None]);
6572+
6573+
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, LocalFeatures::new(), LocalFeatures::new());
6574+
let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
6575+
route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000).0;
6576+
6577+
// Remote commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC
6578+
let remote_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
6579+
assert_eq!(remote_txn[0].output.len(), 4);
6580+
assert_eq!(remote_txn[0].input.len(), 1);
6581+
assert_eq!(remote_txn[0].input[0].previous_output.txid, chan.3.txid());
6582+
6583+
// Claim a HTLC without revocation (provide B monitor with preimage)
6584+
nodes[1].node.claim_funds(payment_preimage, 3_000_000);
6585+
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
6586+
nodes[1].block_notifier.block_connected(&Block { header, txdata: vec![remote_txn[0].clone()] }, 1);
6587+
check_added_monitors!(nodes[1], 1);
6588+
6589+
// One or more claim tx should have been broadcast, check it
6590+
let timeout;
6591+
let preimage;
6592+
let feerate_timeout;
6593+
let feerate_preimage;
6594+
{
6595+
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
6596+
assert_eq!(node_txn.len(), 6); // 2 * claim tx (broadcasted from ChannelMonitor) * 2 (block-reparsing) + local commitment tx + local HTLC-timeout (broadcasted from ChannelManager)
6597+
assert_eq!(node_txn[0], node_txn[4]);
6598+
assert_eq!(node_txn[1], node_txn[5]);
6599+
assert_eq!(node_txn[0].input.len(), 1);
6600+
assert_eq!(node_txn[1].input.len(), 1);
6601+
check_spends!(node_txn[0], remote_txn[0].clone());
6602+
check_spends!(node_txn[1], remote_txn[0].clone());
6603+
if node_txn[0].input[0].witness.last().unwrap().len() == ACCEPTED_HTLC_SCRIPT_WEIGHT {
6604+
timeout = node_txn[0].txid();
6605+
let index = node_txn[0].input[0].previous_output.vout;
6606+
let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
6607+
feerate_timeout = fee * 1000 / node_txn[0].get_weight() as u64;
6608+
6609+
preimage = node_txn[1].txid();
6610+
let index = node_txn[1].input[0].previous_output.vout;
6611+
let fee = remote_txn[0].output[index as usize].value - node_txn[1].output[0].value;
6612+
feerate_preimage = fee * 1000 / node_txn[1].get_weight() as u64;
6613+
} else {
6614+
timeout = node_txn[1].txid();
6615+
let index = node_txn[1].input[0].previous_output.vout;
6616+
let fee = remote_txn[0].output[index as usize].value - node_txn[1].output[0].value;
6617+
feerate_timeout = fee * 1000 / node_txn[1].get_weight() as u64;
6618+
6619+
preimage = node_txn[0].txid();
6620+
let index = node_txn[0].input[0].previous_output.vout;
6621+
let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
6622+
feerate_preimage = fee * 1000 / node_txn[0].get_weight() as u64;
6623+
}
6624+
node_txn.clear();
6625+
};
6626+
assert_ne!(feerate_timeout, 0);
6627+
assert_ne!(feerate_preimage, 0);
6628+
6629+
// After exhaustion of height timer, new bumped claim txn should have been broadcast, check it
6630+
connect_blocks(&nodes[1].block_notifier, 15, 1, true, header.bitcoin_hash());
6631+
{
6632+
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
6633+
assert_eq!(node_txn.len(), 2);
6634+
assert_eq!(node_txn[0].input.len(), 1);
6635+
assert_eq!(node_txn[1].input.len(), 1);
6636+
check_spends!(node_txn[0], remote_txn[0].clone());
6637+
check_spends!(node_txn[1], remote_txn[0].clone());
6638+
if node_txn[0].input[0].witness.last().unwrap().len() == ACCEPTED_HTLC_SCRIPT_WEIGHT {
6639+
let index = node_txn[0].input[0].previous_output.vout;
6640+
let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
6641+
let new_feerate = fee * 1000 / node_txn[0].get_weight() as u64;
6642+
assert!(new_feerate * 100 > feerate_timeout * 125);
6643+
assert_ne!(timeout, node_txn[0].txid());
6644+
6645+
let index = node_txn[1].input[0].previous_output.vout;
6646+
let fee = remote_txn[0].output[index as usize].value - node_txn[1].output[0].value;
6647+
let new_feerate = fee * 1000 / node_txn[1].get_weight() as u64;
6648+
assert!(new_feerate * 100 > feerate_preimage * 125);
6649+
assert_ne!(preimage, node_txn[1].txid());
6650+
} else {
6651+
let index = node_txn[1].input[0].previous_output.vout;
6652+
let fee = remote_txn[0].output[index as usize].value - node_txn[1].output[0].value;
6653+
let new_feerate = fee * 1000 / node_txn[1].get_weight() as u64;
6654+
assert!(new_feerate * 100 > feerate_timeout * 125);
6655+
assert_ne!(timeout, node_txn[1].txid());
6656+
6657+
let index = node_txn[0].input[0].previous_output.vout;
6658+
let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
6659+
let new_feerate = fee * 1000 / node_txn[0].get_weight() as u64;
6660+
assert!(new_feerate * 100 > feerate_preimage * 125);
6661+
assert_ne!(preimage, node_txn[0].txid());
6662+
}
6663+
node_txn.clear();
6664+
}
6665+
6666+
nodes[1].node.get_and_clear_pending_events();
6667+
nodes[1].node.get_and_clear_pending_msg_events();
6668+
}

0 commit comments

Comments
 (0)