Skip to content

Commit 89b0ddf

Browse files
committed
Implement async versions of process_pending_events
1 parent c7b1efd commit 89b0ddf

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

lightning/src/chain/chainmonitor.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::util::atomic_counter::AtomicCounter;
3636
use crate::util::logger::Logger;
3737
use crate::util::errors::APIError;
3838
use crate::util::events;
39-
use crate::util::events::EventHandler;
39+
use crate::util::events::{Event, EventHandler};
4040
use crate::ln::channelmanager::ChannelDetails;
4141

4242
use crate::prelude::*;
@@ -479,6 +479,22 @@ where C::Target: chain::Filter,
479479
self.process_pending_events(&event_handler);
480480
events.into_inner()
481481
}
482+
483+
/// Processes any events asynchronously in the order they were generated since the last call
484+
/// using the given event handler.
485+
///
486+
/// See the trait-level documentation of [`EventsProvider`] for requirements.
487+
///
488+
/// [`EventsProvider`]: crate::util::events::EventsProvider
489+
pub async fn process_pending_events_async<Future: core::future::Future, H: Fn(Event) -> Future>(&self, handler: H) {
490+
let mut pending_events = Vec::new();
491+
for monitor_state in self.monitors.read().unwrap().values() {
492+
pending_events.append(&mut monitor_state.monitor.get_and_clear_pending_events());
493+
}
494+
for event in pending_events {
495+
handler(event).await;
496+
}
497+
}
482498
}
483499

484500
impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>

lightning/src/ln/channelmanager.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, MAX_VA
5353
use crate::ln::wire::Encode;
5454
use crate::chain::keysinterface::{Sign, KeysInterface, KeysManager, Recipient};
5555
use crate::util::config::{UserConfig, ChannelConfig};
56-
use crate::util::events::{EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
56+
use crate::util::events::{Event, EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
5757
use crate::util::{byte_utils, events};
5858
use crate::util::wakers::{Future, Notifier};
5959
use crate::util::scid_utils::fake_scid;
@@ -5675,6 +5675,37 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
56755675
pub fn clear_pending_payments(&self) {
56765676
self.pending_outbound_payments.lock().unwrap().clear()
56775677
}
5678+
5679+
/// Processes any events asynchronously in the order they were generated since the last call
5680+
/// using the given event handler.
5681+
///
5682+
/// See the trait-level documentation of [`EventsProvider`] for requirements.
5683+
pub async fn process_pending_events_async<Future: core::future::Future, H: Fn(Event) -> Future>(&self, handler: H) {
5684+
// We'll acquire our total consistency lock until the returned future completes so that
5685+
// we can be sure no other persists happen while processing events.
5686+
let _read_guard = self.total_consistency_lock.read().unwrap();
5687+
5688+
let mut result = NotifyOption::SkipPersist;
5689+
5690+
// TODO: This behavior should be documented. It's unintuitive that we query
5691+
// ChannelMonitors when clearing other events.
5692+
if self.process_pending_monitor_events() {
5693+
result = NotifyOption::DoPersist;
5694+
}
5695+
5696+
let pending_events = mem::replace(&mut *self.pending_events.lock().unwrap(), vec![]);
5697+
if !pending_events.is_empty() {
5698+
result = NotifyOption::DoPersist;
5699+
}
5700+
5701+
for event in pending_events {
5702+
handler(event).await;
5703+
}
5704+
5705+
if result == NotifyOption::DoPersist {
5706+
self.persistence_notifier.notify();
5707+
}
5708+
}
56785709
}
56795710

56805711
impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> MessageSendEventsProvider for ChannelManager<M, T, K, F, L>

0 commit comments

Comments
 (0)