Skip to content

Commit 801353e

Browse files
committed
f Filter once, simplify spend_spendable_outputs
1 parent 80dffe9 commit 801353e

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

src/sweep.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,29 @@ where
6262
Self { outputs, wallet, keys_manager, kv_store, best_block, logger }
6363
}
6464

65-
pub(crate) fn add_outputs(&self, output_descriptors: Vec<SpendableOutputDescriptor>) {
65+
pub(crate) fn add_outputs(&self, mut output_descriptors: Vec<SpendableOutputDescriptor>) {
66+
let non_static_outputs = output_descriptors
67+
.drain(..)
68+
.filter(|desc| !matches!(desc, SpendableOutputDescriptor::StaticOutput { .. }))
69+
.collect::<Vec<_>>();
70+
6671
let mut locked_outputs = self.outputs.lock().unwrap();
6772

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

7075
let (spending_tx, broadcast_height) =
71-
match self.get_spending_tx(&output_descriptors, cur_height) {
72-
Ok(Some(spending_tx)) => {
76+
match self.get_spending_tx(&non_static_outputs, cur_height) {
77+
Ok(spending_tx) => {
7378
self.wallet.broadcast_transactions(&[&spending_tx]);
7479
(Some(spending_tx), Some(cur_height))
7580
}
76-
Ok(None) => {
77-
log_debug!(
78-
self.logger,
79-
"Omitted spending static outputs: {:?}",
80-
output_descriptors
81-
);
82-
(None, None)
83-
}
8481
Err(e) => {
8582
log_error!(self.logger, "Error spending outputs: {:?}", e);
8683
(None, None)
8784
}
8885
};
8986

90-
for descriptor in output_descriptors {
87+
for descriptor in non_static_outputs {
9188
let id = self.keys_manager.get_secure_random_bytes();
9289
let output_info = SpendableOutputInfo {
9390
id,
@@ -109,7 +106,7 @@ where
109106

110107
fn get_spending_tx(
111108
&self, output_descriptors: &Vec<SpendableOutputDescriptor>, cur_height: u32,
112-
) -> Result<Option<Transaction>, ()> {
109+
) -> Result<Transaction, ()> {
113110
let tx_feerate =
114111
self.wallet.get_est_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
115112

src/wallet.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -403,25 +403,15 @@ where
403403
&self, descriptors: &[&SpendableOutputDescriptor], outputs: Vec<TxOut>,
404404
change_destination_script: Script, feerate_sat_per_1000_weight: u32,
405405
locktime: Option<PackedLockTime>, secp_ctx: &Secp256k1<C>,
406-
) -> Result<Option<Transaction>, ()> {
407-
let only_non_static = &descriptors
408-
.iter()
409-
.filter(|desc| !matches!(desc, SpendableOutputDescriptor::StaticOutput { .. }))
410-
.copied()
411-
.collect::<Vec<_>>();
412-
if only_non_static.is_empty() {
413-
return Ok(None);
414-
}
415-
self.inner
416-
.spend_spendable_outputs(
417-
only_non_static,
418-
outputs,
419-
change_destination_script,
420-
feerate_sat_per_1000_weight,
421-
locktime,
422-
secp_ctx,
423-
)
424-
.map(Some)
406+
) -> Result<Transaction, ()> {
407+
self.inner.spend_spendable_outputs(
408+
descriptors,
409+
outputs,
410+
change_destination_script,
411+
feerate_sat_per_1000_weight,
412+
locktime,
413+
secp_ctx,
414+
)
425415
}
426416

427417
pub fn sign_message(&self, msg: &[u8]) -> Result<String, Error> {

0 commit comments

Comments
 (0)