Make OutputSweeper::track_spendable_outputs fallible

.. as otherwise we might only log an error and continue if we fail to
persist the sweeper state.
This commit is contained in:
Elias Rohrer 2024-04-24 09:30:16 +02:00
parent 0e22b1245a
commit afb452a813
No known key found for this signature in database
GPG Key ID: 36153082BDF676FD
2 changed files with 10 additions and 8 deletions

View File

@ -1665,7 +1665,7 @@ mod tests {
.expect("Events not handled within deadline"); .expect("Events not handled within deadline");
match event { match event {
Event::SpendableOutputs { outputs, channel_id } => { Event::SpendableOutputs { outputs, channel_id } => {
nodes[0].sweeper.track_spendable_outputs(outputs, channel_id, false, Some(153)); nodes[0].sweeper.track_spendable_outputs(outputs, channel_id, false, Some(153)).unwrap();
}, },
_ => panic!("Unexpected event: {:?}", event), _ => panic!("Unexpected event: {:?}", event),
} }

View File

@ -397,11 +397,13 @@ where
/// If `delay_until_height` is set, we will delay the spending until the respective block /// If `delay_until_height` is set, we will delay the spending until the respective block
/// height is reached. This can be used to batch spends, e.g., to reduce on-chain fees. /// height is reached. This can be used to batch spends, e.g., to reduce on-chain fees.
/// ///
/// Returns `Err` on persistence failure, in which case the call may be safely retried.
///
/// [`Event::SpendableOutputs`]: crate::events::Event::SpendableOutputs /// [`Event::SpendableOutputs`]: crate::events::Event::SpendableOutputs
pub fn track_spendable_outputs( pub fn track_spendable_outputs(
&self, output_descriptors: Vec<SpendableOutputDescriptor>, channel_id: Option<ChannelId>, &self, output_descriptors: Vec<SpendableOutputDescriptor>, channel_id: Option<ChannelId>,
exclude_static_outputs: bool, delay_until_height: Option<u32>, exclude_static_outputs: bool, delay_until_height: Option<u32>,
) { ) -> Result<(), ()> {
let mut relevant_descriptors = output_descriptors let mut relevant_descriptors = output_descriptors
.into_iter() .into_iter()
.filter(|desc| { .filter(|desc| {
@ -411,10 +413,10 @@ where
.peekable(); .peekable();
if relevant_descriptors.peek().is_none() { if relevant_descriptors.peek().is_none() {
return; return Ok(());
} }
let mut spending_tx_opt; let spending_tx_opt;
{ {
let mut state_lock = self.sweeper_state.lock().unwrap(); let mut state_lock = self.sweeper_state.lock().unwrap();
for descriptor in relevant_descriptors { for descriptor in relevant_descriptors {
@ -438,16 +440,16 @@ where
state_lock.outputs.push(output_info); state_lock.outputs.push(output_info);
} }
spending_tx_opt = self.regenerate_spend_if_necessary(&mut *state_lock); spending_tx_opt = self.regenerate_spend_if_necessary(&mut *state_lock);
self.persist_state(&*state_lock).unwrap_or_else(|e| { self.persist_state(&*state_lock).map_err(|e| {
log_error!(self.logger, "Error persisting OutputSweeper: {:?}", e); log_error!(self.logger, "Error persisting OutputSweeper: {:?}", e);
// Skip broadcasting if the persist failed. })?;
spending_tx_opt = None;
});
} }
if let Some(spending_tx) = spending_tx_opt { if let Some(spending_tx) = spending_tx_opt {
self.broadcaster.broadcast_transactions(&[&spending_tx]); self.broadcaster.broadcast_transactions(&[&spending_tx]);
} }
Ok(())
} }
/// Returns a list of the currently tracked spendable outputs. /// Returns a list of the currently tracked spendable outputs.