Skip to content

Commit 8fa2dfc

Browse files
author
Antoine Riard
committed
Move compute_output_value as part of package member functions
1 parent 7248744 commit 8fa2dfc

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

lightning/src/chain/onchain_utils.rs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,6 @@ impl PackageTemplate {
521521
pub(crate) fn aggregable(&self) -> bool {
522522
self.aggregable
523523
}
524-
pub(crate) fn feerate(&self) -> u64 {
525-
self.feerate_previous
526-
}
527524
pub(crate) fn set_feerate(&mut self, new_feerate: u64) {
528525
self.feerate_previous = new_feerate;
529526
}
@@ -662,6 +659,27 @@ impl PackageTemplate {
662659
},
663660
}
664661
}
662+
pub(crate) fn compute_package_output<F: Deref, L: Deref>(&self, predicted_weight: usize, input_amounts: u64, fee_estimator: &F, logger: &L) -> Option<(u64, u64)>
663+
where F::Target: FeeEstimator,
664+
L::Target: Logger,
665+
{
666+
// If old feerate is 0, first iteration of this claim, use normal fee calculation
667+
if self.feerate_previous != 0 {
668+
if let Some((new_fee, feerate)) = feerate_bump(predicted_weight, input_amounts, self.feerate_previous, fee_estimator, logger) {
669+
// If new computed fee is superior at the whole claimable amount burn all in fees
670+
if new_fee > input_amounts {
671+
return Some((0, feerate));
672+
} else {
673+
return Some((input_amounts - new_fee, feerate));
674+
}
675+
}
676+
} else {
677+
if let Some((new_fee, feerate)) = subtract_high_prio_fee(input_amounts, predicted_weight, fee_estimator, logger) {
678+
return Some((input_amounts - new_fee, feerate));
679+
}
680+
}
681+
None
682+
}
665683
pub (crate) fn build_package(txid: Txid, vout: u32, input_solving_data: PackageSolvingData, absolute_timelock: u32, aggregable: bool, height_original: u32) -> Self {
666684
let malleability = match input_solving_data {
667685
PackageSolvingData::RevokedOutput(..) => { PackageMalleability::Malleable },
@@ -813,31 +831,6 @@ fn feerate_bump<F: Deref, L: Deref>(predicted_weight: usize, input_amounts: u64,
813831
Some((new_fee, new_fee * 1000 / (predicted_weight as u64)))
814832
}
815833

816-
/// Deduce a new proposed fee from the claiming transaction output value.
817-
/// If the new proposed fee is superior to the consumed outpoint's value, burn everything in miner's
818-
/// fee to deter counterparties attacker.
819-
pub(crate) fn compute_output_value<F: Deref, L: Deref>(predicted_weight: usize, input_amounts: u64, previous_feerate: u64, fee_estimator: &F, logger: &L) -> Option<(u64, u64)>
820-
where F::Target: FeeEstimator,
821-
L::Target: Logger,
822-
{
823-
// If old feerate is 0, first iteration of this claim, use normal fee calculation
824-
if previous_feerate != 0 {
825-
if let Some((new_fee, feerate)) = feerate_bump(predicted_weight, input_amounts, previous_feerate, fee_estimator, logger) {
826-
// If new computed fee is superior at the whole claimable amount burn all in fees
827-
if new_fee > input_amounts {
828-
return Some((0, feerate));
829-
} else {
830-
return Some((input_amounts - new_fee, feerate));
831-
}
832-
}
833-
} else {
834-
if let Some((new_fee, feerate)) = subtract_high_prio_fee(input_amounts, predicted_weight, fee_estimator, logger) {
835-
return Some((input_amounts - new_fee, feerate));
836-
}
837-
}
838-
None
839-
}
840-
841834
/// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
842835
/// 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
843836
/// height than once reached we should generate a new bumped "version" of the claim tx to be sure than we safely claim outputs before

lightning/src/chain/onchaintx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
337337
let amt = cached_request.package_amount();
338338
if cached_request.is_malleable() {
339339
let predicted_weight = cached_request.package_weight(&self.destination_script);
340-
if let Some((output_value, new_feerate)) = onchain_utils::compute_output_value(predicted_weight, amt, cached_request.feerate(), fee_estimator, logger) {
340+
if let Some((output_value, new_feerate)) = cached_request.compute_package_output(predicted_weight, amt, fee_estimator, logger) {
341341
assert!(new_feerate != 0);
342342

343343
let transaction = cached_request.finalize_package(self, output_value, self.destination_script.clone(), logger).unwrap();

0 commit comments

Comments
 (0)