Limit minimum output size to the dust limit when RBF-bumping
[rust-lightning] / lightning / src / chain / package.rs
index b5c1ffdf9bc4c92557f5c4dde08e9f1754b54d3e..8a17f726cf030a23b3fbda31248b791cf5b51b28 100644 (file)
@@ -31,6 +31,8 @@ use util::byte_utils;
 use util::logger::Logger;
 use util::ser::{Readable, Writer, Writeable};
 
+use io;
+use prelude::*;
 use core::cmp;
 use core::mem;
 use core::ops::Deref;
@@ -395,8 +397,8 @@ impl PackageSolvingData {
                        PackageSolvingData::RevokedOutput(_) => output_conf_height + 1,
                        PackageSolvingData::RevokedHTLCOutput(_) => output_conf_height + 1,
                        PackageSolvingData::CounterpartyOfferedHTLCOutput(_) => output_conf_height + 1,
-                       PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => std::cmp::max(outp.htlc.cltv_expiry, output_conf_height + 1),
-                       PackageSolvingData::HolderHTLCOutput(ref outp) => std::cmp::max(outp.cltv_expiry, output_conf_height + 1),
+                       PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => cmp::max(outp.htlc.cltv_expiry, output_conf_height + 1),
+                       PackageSolvingData::HolderHTLCOutput(ref outp) => cmp::max(outp.cltv_expiry, output_conf_height + 1),
                        PackageSolvingData::HolderFundingOutput(_) => output_conf_height + 1,
                };
                absolute_timelock
@@ -634,26 +636,25 @@ 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, fee_estimator: &F, logger: &L) -> Option<(u64, u64)>
+
+       /// Returns value in satoshis to be included as package outgoing output amount and feerate
+       /// which was used to generate the value. Will not return less than `dust_limit_sats` for the
+       /// value.
+       pub(crate) fn compute_package_output<F: Deref, L: Deref>(&self, predicted_weight: usize, dust_limit_sats: u64, fee_estimator: &F, logger: &L) -> Option<(u64, u64)>
                where F::Target: FeeEstimator,
                      L::Target: Logger,
        {
                debug_assert!(self.malleability == PackageMalleability::Malleable, "The package output is fixed for non-malleable packages");
                let input_amounts = self.package_amount();
+               assert!(dust_limit_sats as i64 > 0, "Output script must be broadcastable/have a 'real' dust limit.");
                // 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));
-                               }
+                               return Some((cmp::max(input_amounts as i64 - new_fee as i64, dust_limit_sats as i64) as u64, 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));
+                               return Some((cmp::max(input_amounts as i64 - new_fee as i64, dust_limit_sats as i64) as u64, feerate));
                        }
                }
                None
@@ -682,7 +683,7 @@ impl PackageTemplate {
 }
 
 impl Writeable for PackageTemplate {
-       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+       fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
                writer.write_all(&byte_utils::be64_to_array(self.inputs.len() as u64))?;
                for (ref outpoint, ref rev_outp) in self.inputs.iter() {
                        outpoint.write(writer)?;
@@ -699,7 +700,7 @@ impl Writeable for PackageTemplate {
 }
 
 impl Readable for PackageTemplate {
-       fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
                let inputs_count = <u64 as Readable>::read(reader)?;
                let mut inputs: Vec<(BitcoinOutPoint, PackageSolvingData)> = Vec::with_capacity(cmp::min(inputs_count as usize, MAX_ALLOC_SIZE / 128));
                for _ in 0..inputs_count {