Skip to content

Commit e832e36

Browse files
author
Antoine Riard
committed
Fix bumping timer for claiming revoked HTLC outputs
Previously, we were using their_to_self_delay instead of our_to_self_delay which was falsifying test.
1 parent 09d2a71 commit e832e36

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@ impl ChannelMonitor {
20592059
assert!(predicted_weight >= spend_tx.get_weight());
20602060
let outpoint = BitcoinOutPoint { txid: spend_tx.txid(), vout: 0 };
20612061
let output = spend_tx.output[0].clone();
2062-
let height_timer = Self::get_height_timer(height, self.their_to_self_delay.unwrap() as u32); // We can safely unwrap given we are past channel opening
2062+
let height_timer = Self::get_height_timer(height, height + self.our_to_self_delay as u32);
20632063
log_trace!(self, "Outpoint {}:{} is being being claimed, if it doesn't succeed, a bumped claiming txn is going to be broadcast at height {}", spend_tx.input[0].previous_output.txid, spend_tx.input[0].previous_output.vout, height_timer);
20642064
let mut per_input_material = HashMap::with_capacity(1);
20652065
per_input_material.insert(spend_tx.input[0].previous_output, InputMaterial::Revoked { script: redeemscript, pubkey: None, key: revocation_key, is_htlc: false, amount: tx.output[0].value });

lightning/src/ln/functional_tests.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6500,30 +6500,46 @@ fn test_bump_penalty_txn_on_revoked_htlcs() {
65006500

65016501
// Connect three more block to see if bumped penalty are issued for HTLC txn
65026502
let header_132 = connect_blocks(&nodes[0].block_notifier, 3, 129, true, header_129.bitcoin_hash());
6503-
let node_txn = {
6503+
let penalty_local_tx;
6504+
{
65046505
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
6505-
assert_eq!(node_txn.len(), 5); // 2 bumped penalty txn on offered/received HTLC outputs of revoked commitment tx + 1 penalty tx on to_local of revoked commitment tx + 2 bumped penalty tx on revoked HTLC txn
6506+
assert_eq!(node_txn.len(), 3); // 2 bumped penalty txn on offered/received HTLC outputs of revoked commitment tx + 1 penalty tx on to_local of revoked commitment tx + 2 bumped penalty tx on revoked HTLC txn
65066507

65076508
check_spends!(node_txn[0], revoked_local_txn[0].clone());
65086509
check_spends!(node_txn[1], revoked_local_txn[0].clone());
65096510

6510-
let mut penalty_local = ::std::usize::MAX;
6511+
check_spends!(node_txn[2], revoked_local_txn[0].clone());
6512+
6513+
penalty_local_tx = node_txn[2].clone();
6514+
node_txn.clear();
6515+
};
6516+
// Few more blocks to broadcast and confirm penalty_local_tx
6517+
let header_133 = BlockHeader { version: 0x20000000, prev_blockhash: header_132, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
6518+
nodes[0].block_notifier.block_connected(&Block { header: header_133, txdata: vec![penalty_local_tx] }, 133);
6519+
let header_135 = connect_blocks(&nodes[0].block_notifier, 2, 133, true, header_133.bitcoin_hash());
6520+
{
6521+
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
6522+
assert_eq!(node_txn.len(), 1);
6523+
check_spends!(node_txn[0], revoked_local_txn[0].clone());
6524+
node_txn.clear();
6525+
}
6526+
let header_144 = connect_blocks(&nodes[0].block_notifier, 9, 135, true, header_135);
6527+
let node_txn = {
6528+
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
6529+
assert_eq!(node_txn.len(), 2);
6530+
65116531
let mut penalty_offered = ::std::usize::MAX;
65126532
let mut penalty_received = ::std::usize::MAX;
65136533

65146534
{
6515-
let iter_txn = node_txn[2..].iter();
6516-
for (i, tx) in iter_txn.enumerate() {
6517-
if tx.input[0].previous_output.txid == revoked_local_txn[0].txid() {
6518-
penalty_local = 2 + i;
6519-
} else if tx.input[0].previous_output.txid == revoked_htlc_txn[offered].txid() {
6520-
penalty_offered = 2+ i;
6535+
for (i, tx) in node_txn.iter().enumerate() {
6536+
if tx.input[0].previous_output.txid == revoked_htlc_txn[offered].txid() {
6537+
penalty_offered = i;
65216538
} else if tx.input[0].previous_output.txid == revoked_htlc_txn[received].txid() {
6522-
penalty_received = 2 + i;
6539+
penalty_received = i;
65236540
}
65246541
}
65256542
}
6526-
check_spends!(node_txn[penalty_local], revoked_local_txn[0].clone());
65276543

65286544
assert_eq!(node_txn[penalty_received].input.len(), 1);
65296545
assert_eq!(node_txn[penalty_received].output.len(), 1);
@@ -6541,24 +6557,17 @@ fn test_bump_penalty_txn_on_revoked_htlcs() {
65416557
let fee = revoked_htlc_txn[received].output[0].value - node_txn[penalty_received].output[0].value;
65426558
let new_feerate = fee * 1000 / node_txn[penalty_received].get_weight() as u64;
65436559
assert!(new_feerate * 100 > feerate_2 * 125);
6544-
let txn = vec![node_txn[2].clone(), node_txn[3].clone(), node_txn[4].clone()];
6560+
let txn = vec![node_txn[0].clone(), node_txn[1].clone()];
65456561
node_txn.clear();
65466562
txn
65476563
};
65486564
// Broadcast claim txn and confirm blocks to avoid further bumps on this outputs
6549-
let header_133 = BlockHeader { version: 0x20000000, prev_blockhash: header_132, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
6550-
nodes[0].block_notifier.block_connected(&Block { header: header_133, txdata: node_txn }, 133);
6551-
let header_140 = connect_blocks(&nodes[0].block_notifier, 6, 134, true, header_133.bitcoin_hash());
6552-
{
6553-
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
6554-
node_txn.clear();
6555-
}
6556-
6557-
// Connect few more blocks and check only penalty transaction for to_local output have been issued
6558-
connect_blocks(&nodes[0].block_notifier, 7, 140, true, header_140);
6565+
let header_145 = BlockHeader { version: 0x20000000, prev_blockhash: header_144, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
6566+
nodes[0].block_notifier.block_connected(&Block { header: header_145, txdata: node_txn }, 145);
6567+
connect_blocks(&nodes[0].block_notifier, 20, 145, true, header_145.bitcoin_hash());
65596568
{
65606569
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
6561-
assert_eq!(node_txn.len(), 2); //TODO: should be zero when we fix check_spend_remote_htlc
6570+
assert_eq!(node_txn.len(), 2); //TODO: fix check_spend_remote_htlc lack of watch output
65626571
node_txn.clear();
65636572
}
65646573
check_closed_broadcast!(nodes[0], false);

0 commit comments

Comments
 (0)