Skip to content

Commit 772a851

Browse files
committed
f Use ReplayEvent unit struct error type
1 parent 266ef70 commit 772a851

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use lightning::events::{Event, PathFailure};
2929
use lightning::events::EventHandler;
3030
#[cfg(any(feature = "std", feature = "futures"))]
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;
@@ -535,6 +537,7 @@ use core::task;
535537
/// could setup `process_events_async` like this:
536538
/// ```
537539
/// # use lightning::io;
540+
/// # use lightning::events::ReplayEvent;
538541
/// # use std::sync::{Arc, RwLock};
539542
/// # use std::sync::atomic::{AtomicBool, Ordering};
540543
/// # use std::time::SystemTime;
@@ -552,7 +555,7 @@ use core::task;
552555
/// # }
553556
/// # struct EventHandler {}
554557
/// # impl EventHandler {
555-
/// # async fn handle_event(&self, _: lightning::events::Event) -> Result<(), ()> { Ok(()) }
558+
/// # async fn handle_event(&self, _: lightning::events::Event) -> Result<(), ReplayEvent> { Ok(()) }
556559
/// # }
557560
/// # #[derive(Eq, PartialEq, Clone, Hash)]
558561
/// # struct SocketDescriptor {}
@@ -646,7 +649,7 @@ pub async fn process_events_async<
646649
G: 'static + Deref<Target = NetworkGraph<L>> + Send + Sync,
647650
L: 'static + Deref + Send + Sync,
648651
P: 'static + Deref + Send + Sync,
649-
EventHandlerFuture: core::future::Future<Output = Result<(),()>>,
652+
EventHandlerFuture: core::future::Future<Output = Result<(), ReplayEvent>>,
650653
EventHandler: Fn(Event) -> EventHandlerFuture,
651654
PS: 'static + Deref + Send,
652655
M: 'static + Deref<Target = ChainMonitor<<CM::Target as AChannelManager>::Signer, CF, T, F, L, P>> + Send + Sync,
@@ -693,7 +696,7 @@ where
693696
log_trace!(logger, "Persisting scorer after update");
694697
if let Err(e) = persister.persist_scorer(&scorer) {
695698
log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e);
696-
return Err(());
699+
return Err(ReplayEvent());
697700
}
698701
}
699702
}
@@ -732,7 +735,7 @@ where
732735

733736
#[cfg(feature = "futures")]
734737
async fn process_onion_message_handler_events_async<
735-
EventHandlerFuture: core::future::Future<Output = Result<(), ()>>,
738+
EventHandlerFuture: core::future::Future<Output = Result<(), ReplayEvent>>,
736739
EventHandler: Fn(Event) -> EventHandlerFuture,
737740
PM: 'static + Deref + Send + Sync,
738741
>(

lightning/src/chain/chainmonitor.rs

Lines changed: 2 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 13 additions & 7 deletions
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

Lines changed: 3 additions & 3 deletions
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;
@@ -3111,7 +3111,7 @@ macro_rules! process_events_body {
31113111
}
31123112
num_handled_events += 1;
31133113
}
3114-
Err(()) => {
3114+
Err(_e) => {
31153115
// If we encounter an error we stop handling events and make sure to replay
31163116
// any unhandled events on the next invocation.
31173117
handling_failed = true;
@@ -9264,7 +9264,7 @@ where
92649264
/// using the given event handler.
92659265
///
92669266
/// See the trait-level documentation of [`EventsProvider`] for requirements.
9267-
pub async fn process_pending_events_async<Future: core::future::Future<Output = Result<(),()>>, H: Fn(Event) -> Future>(
9267+
pub async fn process_pending_events_async<Future: core::future::Future<Output = Result<(), ReplayEvent>>, H: Fn(Event) -> Future>(
92689268
&self, handler: H
92699269
) {
92709270
let mut ev;

0 commit comments

Comments
 (0)