Skip to content

Commit 293e5f2

Browse files
authored
Merge pull request #1027 from TheBlueMatt/2021-07-check-dust
Check all outputs meet the dust threshold in `check_spends!()`
2 parents 8d886ee + e81ec4a commit 293e5f2

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

lightning/src/chain/onchaintx.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
388388
let new_timer = Some(cached_request.get_height_timer(cur_height));
389389
if cached_request.is_malleable() {
390390
let predicted_weight = cached_request.package_weight(&self.destination_script);
391-
if let Some((output_value, new_feerate)) = cached_request.compute_package_output(predicted_weight, fee_estimator, logger) {
391+
if let Some((output_value, new_feerate)) =
392+
cached_request.compute_package_output(predicted_weight, self.destination_script.dust_value().as_sat(), fee_estimator, logger) {
392393
assert!(new_feerate != 0);
393394

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

lightning/src/chain/package.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -636,26 +636,25 @@ impl PackageTemplate {
636636
}
637637
current_height + LOW_FREQUENCY_BUMP_INTERVAL
638638
}
639-
/// Returns value in satoshis to be included as package outgoing output amount and feerate with which package finalization should be done.
640-
pub(crate) fn compute_package_output<F: Deref, L: Deref>(&self, predicted_weight: usize, fee_estimator: &F, logger: &L) -> Option<(u64, u64)>
639+
640+
/// Returns value in satoshis to be included as package outgoing output amount and feerate
641+
/// which was used to generate the value. Will not return less than `dust_limit_sats` for the
642+
/// value.
643+
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)>
641644
where F::Target: FeeEstimator,
642645
L::Target: Logger,
643646
{
644647
debug_assert!(self.malleability == PackageMalleability::Malleable, "The package output is fixed for non-malleable packages");
645648
let input_amounts = self.package_amount();
649+
assert!(dust_limit_sats as i64 > 0, "Output script must be broadcastable/have a 'real' dust limit.");
646650
// If old feerate is 0, first iteration of this claim, use normal fee calculation
647651
if self.feerate_previous != 0 {
648652
if let Some((new_fee, feerate)) = feerate_bump(predicted_weight, input_amounts, self.feerate_previous, fee_estimator, logger) {
649-
// If new computed fee is superior at the whole claimable amount burn all in fees
650-
if new_fee > input_amounts {
651-
return Some((0, feerate));
652-
} else {
653-
return Some((input_amounts - new_fee, feerate));
654-
}
653+
return Some((cmp::max(input_amounts as i64 - new_fee as i64, dust_limit_sats as i64) as u64, feerate));
655654
}
656655
} else {
657656
if let Some((new_fee, feerate)) = compute_fee_from_spent_amounts(input_amounts, predicted_weight, fee_estimator, logger) {
658-
return Some((input_amounts - new_fee, feerate));
657+
return Some((cmp::max(input_amounts as i64 - new_fee as i64, dust_limit_sats as i64) as u64, feerate));
659658
}
660659
}
661660
None

lightning/src/ln/functional_test_utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,14 @@ pub fn update_nodes_with_chan_announce<'a, 'b, 'c, 'd>(nodes: &'a Vec<Node<'b, '
707707
macro_rules! check_spends {
708708
($tx: expr, $($spends_txn: expr),*) => {
709709
{
710+
$(
711+
for outp in $spends_txn.output.iter() {
712+
assert!(outp.value >= outp.script_pubkey.dust_value().as_sat(), "Input tx output didn't meet dust limit");
713+
}
714+
)*
715+
for outp in $tx.output.iter() {
716+
assert!(outp.value >= outp.script_pubkey.dust_value().as_sat(), "Spending tx output didn't meet dust limit");
717+
}
710718
let get_output = |out_point: &bitcoin::blockdata::transaction::OutPoint| {
711719
$(
712720
if out_point.txid == $spends_txn.txid() {

0 commit comments

Comments
 (0)