From: Antoine Riard Date: Tue, 18 May 2021 18:09:02 +0000 (-0400) Subject: Move compute_output_value as part of package member functions X-Git-Tag: v0.0.98~17^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=881c0f58e16e3e178f55c8cae17215c1b8d4e7aa;p=rust-lightning Move compute_output_value as part of package member functions --- diff --git a/lightning/src/chain/onchaintx.rs b/lightning/src/chain/onchaintx.rs index ca86864e0..4e6a067fc 100644 --- a/lightning/src/chain/onchaintx.rs +++ b/lightning/src/chain/onchaintx.rs @@ -28,7 +28,6 @@ use chain::chaininterface::{FeeEstimator, BroadcasterInterface}; use chain::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER}; use chain::keysinterface::{Sign, KeysInterface}; use chain::package::PackageTemplate; -use chain::package; use util::logger::Logger; use util::ser::{Readable, ReadableArgs, Writer, Writeable, VecWriter}; use util::byte_utils; @@ -348,7 +347,7 @@ impl OnchainTxHandler { let amt = cached_request.package_amount(); if cached_request.is_malleable() { let predicted_weight = cached_request.package_weight(&self.destination_script); - if let Some((output_value, new_feerate)) = package::compute_output_value(predicted_weight, amt, cached_request.feerate(), fee_estimator, logger) { + if let Some((output_value, new_feerate)) = cached_request.compute_package_output(predicted_weight, amt, fee_estimator, logger) { assert!(new_feerate != 0); let transaction = cached_request.finalize_package(self, output_value, self.destination_script.clone(), logger).unwrap(); diff --git a/lightning/src/chain/package.rs b/lightning/src/chain/package.rs index 008da5d26..2cad133a8 100644 --- a/lightning/src/chain/package.rs +++ b/lightning/src/chain/package.rs @@ -500,9 +500,6 @@ impl PackageTemplate { pub(crate) fn aggregable(&self) -> bool { self.aggregable } - pub(crate) fn feerate(&self) -> u64 { - self.feerate_previous - } pub(crate) fn set_feerate(&mut self, new_feerate: u64) { self.feerate_previous = new_feerate; } @@ -654,6 +651,28 @@ impl PackageTemplate { } current_height + LOW_FREQUENCY_BUMP_INTERVAL } + /// Returns value in satoshis to be included as package outgoing output amount and feerate with which package finalization should be done. + pub(crate) fn compute_package_output(&self, predicted_weight: usize, input_amounts: u64, fee_estimator: &F, logger: &L) -> Option<(u64, u64)> + where F::Target: FeeEstimator, + L::Target: Logger, + { + // If old feerate is 0, first iteration of this claim, use normal fee calculation + if self.feerate_previous != 0 { + if let Some((new_fee, feerate)) = feerate_bump(predicted_weight, input_amounts, self.feerate_previous, fee_estimator, logger) { + // If new computed fee is superior at the whole claimable amount burn all in fees + if new_fee > input_amounts { + return Some((0, feerate)); + } else { + return Some((input_amounts - new_fee, feerate)); + } + } + } else { + if let Some((new_fee, feerate)) = compute_fee_from_spent_amounts(input_amounts, predicted_weight, fee_estimator, logger) { + return Some((input_amounts - new_fee, feerate)); + } + } + None + } pub (crate) fn build_package(txid: Txid, vout: u32, input_solving_data: PackageSolvingData, soonest_conf_deadline: u32, aggregable: bool, height_original: u32) -> Self { let malleability = match input_solving_data { PackageSolvingData::RevokedOutput(..) => { PackageMalleability::Malleable }, @@ -805,29 +824,3 @@ fn feerate_bump(predicted_weight: usize, input_amounts: u64, }; Some((new_fee, new_fee * 1000 / (predicted_weight as u64))) } - -/// Deduce a new proposed fee from the claiming transaction output value. -/// If the new proposed fee is superior to the consumed outpoint's value, burn everything in miner's -/// fee to deter counterparties attacker. -pub(crate) fn compute_output_value(predicted_weight: usize, input_amounts: u64, previous_feerate: u64, fee_estimator: &F, logger: &L) -> Option<(u64, u64)> - where F::Target: FeeEstimator, - L::Target: Logger, -{ - // If old feerate is 0, first iteration of this claim, use normal fee calculation - if previous_feerate != 0 { - if let Some((new_fee, feerate)) = feerate_bump(predicted_weight, input_amounts, previous_feerate, fee_estimator, logger) { - // If new computed fee is superior at the whole claimable amount burn all in fees - if new_fee > input_amounts { - return Some((0, feerate)); - } else { - return Some((input_amounts - new_fee, feerate)); - } - } - } else { - if let Some((new_fee, feerate)) = compute_fee_from_spent_amounts(input_amounts, predicted_weight, fee_estimator, logger) { - return Some((input_amounts - new_fee, feerate)); - } - } - None -} -