X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fchain%2Fpackage.rs;h=8a17f726cf030a23b3fbda31248b791cf5b51b28;hb=0b072834ab92081bba4ec907b39f1aa78953016c;hp=bda924d5565a0dcd15e7850854024d4ee582c2d9;hpb=74717d390c3ac654978d1a22123d9085729529cd;p=rust-lightning diff --git a/lightning/src/chain/package.rs b/lightning/src/chain/package.rs index bda924d5..8a17f726 100644 --- a/lightning/src/chain/package.rs +++ b/lightning/src/chain/package.rs @@ -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; @@ -87,14 +89,14 @@ impl RevokedOutput { } impl_writeable_tlv_based!(RevokedOutput, { - (0, per_commitment_point), - (2, counterparty_delayed_payment_base_key), - (4, counterparty_htlc_base_key), - (6, per_commitment_key), - (8, weight), - (10, amount), - (12, on_counterparty_tx_csv), -}, {}, {}); + (0, per_commitment_point, required), + (2, counterparty_delayed_payment_base_key, required), + (4, counterparty_htlc_base_key, required), + (6, per_commitment_key, required), + (8, weight, required), + (10, amount, required), + (12, on_counterparty_tx_csv, required), +}); /// A struct to describe a revoked offered output and corresponding information to generate a /// solving witness. @@ -131,14 +133,14 @@ impl RevokedHTLCOutput { } impl_writeable_tlv_based!(RevokedHTLCOutput, { - (0, per_commitment_point), - (2, counterparty_delayed_payment_base_key), - (4, counterparty_htlc_base_key), - (6, per_commitment_key), - (8, weight), - (10, amount), - (12, htlc), -}, {}, {}); + (0, per_commitment_point, required), + (2, counterparty_delayed_payment_base_key, required), + (4, counterparty_htlc_base_key, required), + (6, per_commitment_key, required), + (8, weight, required), + (10, amount, required), + (12, htlc, required), +}); /// A struct to describe a HTLC output on a counterparty commitment transaction. /// @@ -168,12 +170,12 @@ impl CounterpartyOfferedHTLCOutput { } impl_writeable_tlv_based!(CounterpartyOfferedHTLCOutput, { - (0, per_commitment_point), - (2, counterparty_delayed_payment_base_key), - (4, counterparty_htlc_base_key), - (6, preimage), - (8, htlc), -}, {}, {}); + (0, per_commitment_point, required), + (2, counterparty_delayed_payment_base_key, required), + (4, counterparty_htlc_base_key, required), + (6, preimage, required), + (8, htlc, required), +}); /// A struct to describe a HTLC output on a counterparty commitment transaction. /// @@ -199,11 +201,11 @@ impl CounterpartyReceivedHTLCOutput { } impl_writeable_tlv_based!(CounterpartyReceivedHTLCOutput, { - (0, per_commitment_point), - (2, counterparty_delayed_payment_base_key), - (4, counterparty_htlc_base_key), - (6, htlc), -}, {}, {}); + (0, per_commitment_point, required), + (2, counterparty_delayed_payment_base_key, required), + (4, counterparty_htlc_base_key, required), + (6, htlc, required), +}); /// A struct to describe a HTLC output on holder commitment transaction. /// @@ -236,11 +238,10 @@ impl HolderHTLCOutput { } impl_writeable_tlv_based!(HolderHTLCOutput, { - (0, amount), - (2, cltv_expiry), -}, { - (4, preimage), -}, {}); + (0, amount, required), + (2, cltv_expiry, required), + (4, preimage, option) +}); /// A struct to describe the channel output on the funding transaction. /// @@ -259,8 +260,8 @@ impl HolderFundingOutput { } impl_writeable_tlv_based!(HolderFundingOutput, { - (0, funding_redeemscript), -}, {}, {}); + (0, funding_redeemscript, required), +}); /// A wrapper encapsulating all in-protocol differing outputs types. /// @@ -396,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 @@ -635,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(&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(&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 @@ -683,23 +683,24 @@ impl PackageTemplate { } impl Writeable for PackageTemplate { - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + fn write(&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)?; rev_outp.write(writer)?; } write_tlv_fields!(writer, { - (0, self.soonest_conf_deadline), - (2, self.feerate_previous), - (4, self.height_original), - }, { (6, self.height_timer) }); + (0, self.soonest_conf_deadline, required), + (2, self.feerate_previous, required), + (4, self.height_original, required), + (6, self.height_timer, option) + }); Ok(()) } } impl Readable for PackageTemplate { - fn read(reader: &mut R) -> Result { + fn read(reader: &mut R) -> Result { let inputs_count = ::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 { @@ -722,10 +723,11 @@ impl Readable for PackageTemplate { let mut height_timer = None; let mut height_original = 0; read_tlv_fields!(reader, { - (0, soonest_conf_deadline), - (2, feerate_previous), - (4, height_original) - }, { (6, height_timer) }); + (0, soonest_conf_deadline, required), + (2, feerate_previous, required), + (4, height_original, required), + (6, height_timer, option), + }); Ok(PackageTemplate { inputs, malleability,