]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Move compute_output_value as part of package member functions
authorAntoine Riard <dev@ariard.me>
Tue, 18 May 2021 18:09:02 +0000 (14:09 -0400)
committerAntoine Riard <dev@ariard.me>
Tue, 25 May 2021 23:59:40 +0000 (19:59 -0400)
lightning/src/chain/onchaintx.rs
lightning/src/chain/package.rs

index ca86864e0d8430efcbd23cec04570a4b7dcdf1e0..4e6a067fc72c25e311b8526700dc80410f2ac3e0 100644 (file)
@@ -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<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                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();
index 008da5d265e5e4c5d38d188d025e12328d7d7db5..2cad133a8e14c5d9947637c5db7709374daf9653 100644 (file)
@@ -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<F: Deref, L: Deref>(&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<F: Deref, L: Deref>(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<F: Deref, L: Deref>(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
-}
-