Skip to content

Commit 9a438ee

Browse files
authored
Merge pull request #3015 from tnull/2024-04-sweeper-error-on-persistence-failure
Make `OutputSweeper::track_spendable_outputs` fallible
2 parents 0e22b12 + afb452a commit 9a438ee

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ mod tests {
16651665
.expect("Events not handled within deadline");
16661666
match event {
16671667
Event::SpendableOutputs { outputs, channel_id } => {
1668-
nodes[0].sweeper.track_spendable_outputs(outputs, channel_id, false, Some(153));
1668+
nodes[0].sweeper.track_spendable_outputs(outputs, channel_id, false, Some(153)).unwrap();
16691669
},
16701670
_ => panic!("Unexpected event: {:?}", event),
16711671
}

lightning/src/util/sweep.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,13 @@ where
397397
/// If `delay_until_height` is set, we will delay the spending until the respective block
398398
/// height is reached. This can be used to batch spends, e.g., to reduce on-chain fees.
399399
///
400+
/// Returns `Err` on persistence failure, in which case the call may be safely retried.
401+
///
400402
/// [`Event::SpendableOutputs`]: crate::events::Event::SpendableOutputs
401403
pub fn track_spendable_outputs(
402404
&self, output_descriptors: Vec<SpendableOutputDescriptor>, channel_id: Option<ChannelId>,
403405
exclude_static_outputs: bool, delay_until_height: Option<u32>,
404-
) {
406+
) -> Result<(), ()> {
405407
let mut relevant_descriptors = output_descriptors
406408
.into_iter()
407409
.filter(|desc| {
@@ -411,10 +413,10 @@ where
411413
.peekable();
412414

413415
if relevant_descriptors.peek().is_none() {
414-
return;
416+
return Ok(());
415417
}
416418

417-
let mut spending_tx_opt;
419+
let spending_tx_opt;
418420
{
419421
let mut state_lock = self.sweeper_state.lock().unwrap();
420422
for descriptor in relevant_descriptors {
@@ -438,16 +440,16 @@ where
438440
state_lock.outputs.push(output_info);
439441
}
440442
spending_tx_opt = self.regenerate_spend_if_necessary(&mut *state_lock);
441-
self.persist_state(&*state_lock).unwrap_or_else(|e| {
443+
self.persist_state(&*state_lock).map_err(|e| {
442444
log_error!(self.logger, "Error persisting OutputSweeper: {:?}", e);
443-
// Skip broadcasting if the persist failed.
444-
spending_tx_opt = None;
445-
});
445+
})?;
446446
}
447447

448448
if let Some(spending_tx) = spending_tx_opt {
449449
self.broadcaster.broadcast_transactions(&[&spending_tx]);
450450
}
451+
452+
Ok(())
451453
}
452454

453455
/// Returns a list of the currently tracked spendable outputs.

0 commit comments

Comments
 (0)