From 8a84ed09fbb7d0b192e8cb9593253a9ecefd6455 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Thu, 28 Aug 2025 18:58:15 -0500 Subject: [PATCH] Randomize order of inputs from `OutputSweeper` For a marginal increase in privacy, we can randomize the inputs from the `OutputSweeper`. Since we don't depend on `rand` to randomize the order I just put the elements into a hashset, then back into a Vec. This should give us enough randomness without having to introduce a new dep or make the `OutputSweeper` depend on the `EntropySource`. --- lightning/src/util/sweep.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lightning/src/util/sweep.rs b/lightning/src/util/sweep.rs index b72dddbcc7c..6268a051d56 100644 --- a/lightning/src/util/sweep.rs +++ b/lightning/src/util/sweep.rs @@ -574,13 +574,19 @@ where let cur_height = sweeper_state.best_block.height; let cur_hash = sweeper_state.best_block.block_hash; - let respend_descriptors: Vec<&SpendableOutputDescriptor> = sweeper_state + let respend_descriptors_set: HashSet<&SpendableOutputDescriptor> = sweeper_state .outputs .iter() .filter(|o| filter_fn(*o, cur_height)) .map(|o| &o.descriptor) .collect(); + // we first collect into a set to avoid duplicates and to "randomize" the order + // in which outputs are spent. Then we collect into a vec as that is what + // `spend_outputs` requires. + let respend_descriptors: Vec<&SpendableOutputDescriptor> = + respend_descriptors_set.into_iter().collect(); + // Generate the spending transaction and broadcast it. if !respend_descriptors.is_empty() { let spending_tx = self