Skip to content

Commit 4debd21

Browse files
committed
f Use ReplayEvent unit struct error type
1 parent b06cfc3 commit 4debd21

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

lightning-background-processor/src/lib.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use lightning::events::{Event, PathFailure};
2929
use lightning::events::EventHandler;
3030
#[cfg(feature = "std")]
3131
use lightning::events::EventsProvider;
32+
#[cfg(feature = "futures")]
33+
use lightning::events::ReplayEvent;
3234

3335
use lightning::ln::channelmanager::AChannelManager;
3436
use lightning::ln::msgs::OnionMessageHandler;
@@ -539,6 +541,7 @@ use core::task;
539541
/// could setup `process_events_async` like this:
540542
/// ```
541543
/// # use lightning::io;
544+
/// # use lightning::events::ReplayEvent;
542545
/// # use std::sync::{Arc, RwLock};
543546
/// # use std::sync::atomic::{AtomicBool, Ordering};
544547
/// # use std::time::SystemTime;
@@ -556,7 +559,7 @@ use core::task;
556559
/// # }
557560
/// # struct EventHandler {}
558561
/// # impl EventHandler {
559-
/// # async fn handle_event(&self, _: lightning::events::Event) -> Result<(), ()> { Ok(()) }
562+
/// # async fn handle_event(&self, _: lightning::events::Event) -> Result<(), ReplayEvent> { Ok(()) }
560563
/// # }
561564
/// # #[derive(Eq, PartialEq, Clone, Hash)]
562565
/// # struct SocketDescriptor {}
@@ -654,7 +657,7 @@ pub async fn process_events_async<
654657
G: 'static + Deref<Target = NetworkGraph<L>> + Send + Sync,
655658
L: 'static + Deref + Send + Sync,
656659
P: 'static + Deref + Send + Sync,
657-
EventHandlerFuture: core::future::Future<Output = Result<(),()>>,
660+
EventHandlerFuture: core::future::Future<Output = Result<(), ReplayEvent>>,
658661
EventHandler: Fn(Event) -> EventHandlerFuture,
659662
PS: 'static + Deref + Send,
660663
M: 'static + Deref<Target = ChainMonitor<<CM::Target as AChannelManager>::Signer, CF, T, F, L, P>> + Send + Sync,
@@ -704,7 +707,7 @@ where
704707
log_trace!(logger, "Persisting scorer after update");
705708
if let Err(e) = persister.persist_scorer(&scorer) {
706709
log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e);
707-
return Err(());
710+
return Err(ReplayEvent());
708711
}
709712
}
710713
}
@@ -743,7 +746,7 @@ where
743746

744747
#[cfg(feature = "futures")]
745748
async fn process_onion_message_handler_events_async<
746-
EventHandlerFuture: core::future::Future<Output = Result<(), ()>>,
749+
EventHandlerFuture: core::future::Future<Output = Result<(), ReplayEvent>>,
747750
EventHandler: Fn(Event) -> EventHandlerFuture,
748751
PM: 'static + Deref + Send + Sync,
749752
>(

lightning/src/chain/chainmonitor.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, Balance
3333
use crate::chain::transaction::{OutPoint, TransactionData};
3434
use crate::ln::types::ChannelId;
3535
use crate::sign::ecdsa::EcdsaChannelSigner;
36-
use crate::events;
37-
use crate::events::{Event, EventHandler};
36+
use crate::events::{self, Event, EventHandler, ReplayEvent};
3837
use crate::util::logger::{Logger, WithContext};
3938
use crate::util::errors::APIError;
4039
use crate::util::wakers::{Future, Notifier};
@@ -527,7 +526,7 @@ where C::Target: chain::Filter,
527526
/// See the trait-level documentation of [`EventsProvider`] for requirements.
528527
///
529528
/// [`EventsProvider`]: crate::events::EventsProvider
530-
pub async fn process_pending_events_async<Future: core::future::Future<Output = Result<(),()>>, H: Fn(Event) -> Future>(
529+
pub async fn process_pending_events_async<Future: core::future::Future<Output = Result<(),ReplayEvent>>, H: Fn(Event) -> Future>(
531530
&self, handler: H
532531
) {
533532
// Sadly we can't hold the monitors read lock through an async call. Thus we have to do a

lightning/src/chain/channelmonitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ macro_rules! _process_events_body {
11771177
$event_to_handle = event;
11781178
match $handle_event {
11791179
Ok(()) => num_handled_events += 1,
1180-
Err(()) => {
1180+
Err(_) => {
11811181
// If we encounter an error we stop handling events and make sure to replay
11821182
// any unhandled events on the next invocation.
11831183
handling_failed = true;

lightning/src/events/mod.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -2205,8 +2205,8 @@ pub trait MessageSendEventsProvider {
22052205
/// and replay any unhandled events on startup. An [`Event`] is considered handled when
22062206
/// [`process_pending_events`] returns `Ok(())`, thus handlers MUST fully handle [`Event`]s and
22072207
/// persist any relevant changes to disk *before* returning `Ok(())`. In case of a (e.g.,
2208-
/// persistence failure) implementors should return `Err(())`, signalling to the [`EventsProvider`]
2209-
/// to replay unhandled events on the next invocation.
2208+
/// persistence failure) implementors should return `Err(ReplayEvent())`, signalling to the
2209+
/// [`EventsProvider`] to replay unhandled events on the next invocation.
22102210
///
22112211
/// Further, because an application may crash between an [`Event`] being handled and the
22122212
/// implementor of this trait being re-serialized, [`Event`] handling must be idempotent - in
@@ -2233,26 +2233,32 @@ pub trait EventsProvider {
22332233
fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler;
22342234
}
22352235

2236+
/// An error type that may be returned to LDK in order to safely abort event handling if it can't
2237+
/// currently succeed (e.g., due to a persistence failure).
2238+
///
2239+
/// LDK will ensure the event is persisted and will eventually be replayed.
2240+
pub struct ReplayEvent();
2241+
22362242
/// A trait implemented for objects handling events from [`EventsProvider`].
22372243
///
22382244
/// An async variation also exists for implementations of [`EventsProvider`] that support async
22392245
/// event handling. The async event handler should satisfy the generic bounds: `F:
2240-
/// core::future::Future<Output = Result<(), ()>>, H: Fn(Event) -> F`.
2246+
/// core::future::Future<Output = Result<(), ReplayEvent>>, H: Fn(Event) -> F`.
22412247
pub trait EventHandler {
22422248
/// Handles the given [`Event`].
22432249
///
22442250
/// See [`EventsProvider`] for details that must be considered when implementing this method.
2245-
fn handle_event(&self, event: Event) -> Result<(), ()>;
2251+
fn handle_event(&self, event: Event) -> Result<(), ReplayEvent>;
22462252
}
22472253

2248-
impl<F> EventHandler for F where F: Fn(Event) -> Result<(), ()> {
2249-
fn handle_event(&self, event: Event) -> Result<(), ()> {
2254+
impl<F> EventHandler for F where F: Fn(Event) -> Result<(), ReplayEvent> {
2255+
fn handle_event(&self, event: Event) -> Result<(), ReplayEvent> {
22502256
self(event)
22512257
}
22522258
}
22532259

22542260
impl<T: EventHandler> EventHandler for Arc<T> {
2255-
fn handle_event(&self, event: Event) -> Result<(), ()> {
2261+
fn handle_event(&self, event: Event) -> Result<(), ReplayEvent> {
22562262
self.deref().handle_event(event)
22572263
}
22582264
}

lightning/src/ln/channelmanager.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, Fee
4040
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, WithChannelMonitor, ChannelMonitorUpdateStep, HTLC_FAIL_BACK_BUFFER, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY, MonitorEvent, CLOSED_CHANNEL_UPDATE_ID};
4141
use crate::chain::transaction::{OutPoint, TransactionData};
4242
use crate::events;
43-
use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination, PaymentFailureReason};
43+
use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination, PaymentFailureReason, ReplayEvent};
4444
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
4545
// construct one themselves.
4646
use crate::ln::inbound_payment;
@@ -2780,7 +2780,7 @@ macro_rules! process_events_body {
27802780
}
27812781
num_handled_events += 1;
27822782
}
2783-
Err(()) => {
2783+
Err(_e) => {
27842784
// If we encounter an error we stop handling events and make sure to replay
27852785
// any unhandled events on the next invocation.
27862786
handling_failed = true;
@@ -8950,7 +8950,7 @@ where
89508950
/// using the given event handler.
89518951
///
89528952
/// See the trait-level documentation of [`EventsProvider`] for requirements.
8953-
pub async fn process_pending_events_async<Future: core::future::Future<Output = Result<(),()>>, H: Fn(Event) -> Future>(
8953+
pub async fn process_pending_events_async<Future: core::future::Future<Output = Result<(), ReplayEvent>>, H: Fn(Event) -> Future>(
89548954
&self, handler: H
89558955
) {
89568956
let mut ev;

0 commit comments

Comments
 (0)