Skip to content

Commit bb0e33f

Browse files
author
Antoine Riard
committed
Move get_height_timer out of OnchainTxHandler
1 parent 4363f96 commit bb0e33f

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

lightning/src/chain/onchaintx.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -332,20 +332,6 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
332332
}
333333
}
334334

335-
/// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
336-
/// output detection, we generate a first version of a claim tx and associate to it a height timer. A height timer is an absolute block
337-
/// height than once reached we should generate a new bumped "version" of the claim tx to be sure than we safely claim outputs before
338-
/// than our counterparty can do it too. If timelock expires soon, height timer is going to be scale down in consequence to increase
339-
/// frequency of the bump and so increase our bets of success.
340-
fn get_height_timer(current_height: u32, timelock_expiration: u32) -> u32 {
341-
if timelock_expiration <= current_height + 3 {
342-
return current_height + 1
343-
} else if timelock_expiration - current_height <= 15 {
344-
return current_height + 3
345-
}
346-
current_height + 15
347-
}
348-
349335
/// Lightning security model (i.e being able to redeem/timeout HTLC or penalize coutnerparty onchain) lays on the assumption of claim transactions getting confirmed before timelock expiration
350336
/// (CSV or CLTV following cases). In case of high-fee spikes, claim tx may stuck in the mempool, so you need to bump its feerate quickly using Replace-By-Fee or Child-Pay-For-Parent.
351337
/// Panics if there are signing errors, because signing operations in reaction to on-chain events
@@ -358,7 +344,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
358344

359345
// Compute new height timer to decide when we need to regenerate a new bumped version of the claim tx (if we
360346
// didn't receive confirmation of it before, or not enough reorg-safe depth on top of it).
361-
let new_timer = Some(Self::get_height_timer(height, cached_request.timelock()));
347+
let new_timer = Some(cached_request.get_height_timer(height));
362348
let amt = cached_request.package_amount();
363349
if cached_request.is_malleable() {
364350
let predicted_weight = cached_request.package_weight(&self.destination_script);

lightning/src/chain/package.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ pub(crate) const WEIGHT_RECEIVED_HTLC: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 139;
4949
// number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
5050
pub(crate) const WEIGHT_REVOKED_OUTPUT: u64 = 1 + 1 + 73 + 1 + 1 + 1 + 77;
5151

52+
/// Height delay at which transactions are fee-bumped/rebroadcasted with a low priority.
53+
const LOW_FREQUENCY_BUMP_INTERVAL: u32 = 15;
54+
/// Height delay at which transactions are fee-bumped/rebroadcasted with a middle priority.
55+
const MIDDLE_FREQUENCY_BUMP_INTERVAL: u32 = 3;
56+
/// Height delay at which transactions are fee-bumped/rebroadcasted with a high priority.
57+
const HIGH_FREQUENCY_BUMP_INTERVAL: u32 = 1;
58+
5259
/// A struct to describe a revoked output and corresponding information to generate a solving
5360
/// witness spending a commitment `to_local` output or a second-stage HTLC transaction output.
5461
///
@@ -634,6 +641,19 @@ impl PackageTemplate {
634641
},
635642
}
636643
}
644+
/// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
645+
/// output detection, we generate a first version of a claim tx and associate to it a height timer. A height timer is an absolute block
646+
/// height that once reached we should generate a new bumped "version" of the claim tx to be sure that we safely claim outputs before
647+
/// that our counterparty can do so. If timelock expires soon, height timer is going to be scaled down in consequence to increase
648+
/// frequency of the bump and so increase our bets of success.
649+
pub(crate) fn get_height_timer(&self, current_height: u32) -> u32 {
650+
if self.soonest_conf_deadline <= current_height + MIDDLE_FREQUENCY_BUMP_INTERVAL {
651+
return current_height + HIGH_FREQUENCY_BUMP_INTERVAL
652+
} else if self.soonest_conf_deadline - current_height <= LOW_FREQUENCY_BUMP_INTERVAL {
653+
return current_height + MIDDLE_FREQUENCY_BUMP_INTERVAL
654+
}
655+
current_height + LOW_FREQUENCY_BUMP_INTERVAL
656+
}
637657
pub (crate) fn build_package(txid: Txid, vout: u32, input_solving_data: PackageSolvingData, soonest_conf_deadline: u32, aggregable: bool, height_original: u32) -> Self {
638658
let malleability = match input_solving_data {
639659
PackageSolvingData::RevokedOutput(..) => { PackageMalleability::Malleable },
@@ -810,3 +830,4 @@ pub(crate) fn compute_output_value<F: Deref, L: Deref>(predicted_weight: usize,
810830
}
811831
None
812832
}
833+

0 commit comments

Comments
 (0)