Skip to content

Commit

Permalink
f Filter once, simplify spend_spendable_outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Nov 1, 2023
1 parent 80dffe9 commit 9b46601
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,29 @@ where
Self { outputs, wallet, keys_manager, kv_store, best_block, logger }
}

pub(crate) fn add_outputs(&self, output_descriptors: Vec<SpendableOutputDescriptor>) {
pub(crate) fn add_outputs(&self, mut output_descriptors: Vec<SpendableOutputDescriptor>) {
let non_static_outputs = output_descriptors
.drain(..)
.filter(|desc| !matches!(desc, SpendableOutputDescriptor::StaticOutput { .. }))
.collect::<Vec<_>>();

let mut locked_outputs = self.outputs.lock().unwrap();

let cur_height = self.best_block.lock().unwrap().height();

let (spending_tx, broadcast_height) =
match self.get_spending_tx(&output_descriptors, cur_height) {
Ok(Some(spending_tx)) => {
match self.get_spending_tx(&non_static_outputs, cur_height) {
Ok(spending_tx) => {
self.wallet.broadcast_transactions(&[&spending_tx]);
(Some(spending_tx), Some(cur_height))
}
Ok(None) => {
log_debug!(
self.logger,
"Omitted spending static outputs: {:?}",
output_descriptors
);
(None, None)
}
Err(e) => {
log_error!(self.logger, "Error spending outputs: {:?}", e);
(None, None)
}
};

for descriptor in output_descriptors {
for descriptor in non_static_outputs {
let id = self.keys_manager.get_secure_random_bytes();
let output_info = SpendableOutputInfo {
id,
Expand All @@ -109,7 +106,7 @@ where

fn get_spending_tx(
&self, output_descriptors: &Vec<SpendableOutputDescriptor>, cur_height: u32,
) -> Result<Option<Transaction>, ()> {
) -> Result<Transaction, ()> {
let tx_feerate =
self.wallet.get_est_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);

Expand Down

0 comments on commit 9b46601

Please sign in to comment.