Skip to content

Commit 814fc7a

Browse files
committed
DRY the event handling in ChannelManager
In the coming commits we'll add some additional complexity to the event handling flows, so best to DRY them up before we get there.
1 parent 8fcbe64 commit 814fc7a

File tree

1 file changed

+34
-44
lines changed

1 file changed

+34
-44
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,36 @@ macro_rules! handle_new_monitor_update {
16141614
}
16151615
}
16161616

1617+
macro_rules! process_events_body {
1618+
($self: expr, $event_to_handle: expr, $handle_event: expr) => {
1619+
// We'll acquire our total consistency lock until the returned future completes so that
1620+
// we can be sure no other persists happen while processing events.
1621+
let _read_guard = $self.total_consistency_lock.read().unwrap();
1622+
1623+
let mut result = NotifyOption::SkipPersist;
1624+
1625+
// TODO: This behavior should be documented. It's unintuitive that we query
1626+
// ChannelMonitors when clearing other events.
1627+
if $self.process_pending_monitor_events() {
1628+
result = NotifyOption::DoPersist;
1629+
}
1630+
1631+
let pending_events = mem::replace(&mut *$self.pending_events.lock().unwrap(), vec![]);
1632+
if !pending_events.is_empty() {
1633+
result = NotifyOption::DoPersist;
1634+
}
1635+
1636+
for event in pending_events {
1637+
$event_to_handle = event;
1638+
$handle_event;
1639+
}
1640+
1641+
if result == NotifyOption::DoPersist {
1642+
$self.persistence_notifier.notify();
1643+
}
1644+
}
1645+
}
1646+
16171647
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref> ChannelManager<M, T, ES, NS, SP, F, R, L>
16181648
where
16191649
M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
@@ -5744,30 +5774,8 @@ where
57445774
pub async fn process_pending_events_async<Future: core::future::Future, H: Fn(Event) -> Future>(
57455775
&self, handler: H
57465776
) {
5747-
// We'll acquire our total consistency lock until the returned future completes so that
5748-
// we can be sure no other persists happen while processing events.
5749-
let _read_guard = self.total_consistency_lock.read().unwrap();
5750-
5751-
let mut result = NotifyOption::SkipPersist;
5752-
5753-
// TODO: This behavior should be documented. It's unintuitive that we query
5754-
// ChannelMonitors when clearing other events.
5755-
if self.process_pending_monitor_events() {
5756-
result = NotifyOption::DoPersist;
5757-
}
5758-
5759-
let pending_events = mem::replace(&mut *self.pending_events.lock().unwrap(), vec![]);
5760-
if !pending_events.is_empty() {
5761-
result = NotifyOption::DoPersist;
5762-
}
5763-
5764-
for event in pending_events {
5765-
handler(event).await;
5766-
}
5767-
5768-
if result == NotifyOption::DoPersist {
5769-
self.persistence_notifier.notify();
5770-
}
5777+
let mut ev;
5778+
process_events_body!(self, ev, { handler(ev).await });
57715779
}
57725780
}
57735781

@@ -5849,26 +5857,8 @@ where
58495857
/// An [`EventHandler`] may safely call back to the provider in order to handle an event.
58505858
/// However, it must not call [`Writeable::write`] as doing so would result in a deadlock.
58515859
fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {
5852-
PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
5853-
let mut result = NotifyOption::SkipPersist;
5854-
5855-
// TODO: This behavior should be documented. It's unintuitive that we query
5856-
// ChannelMonitors when clearing other events.
5857-
if self.process_pending_monitor_events() {
5858-
result = NotifyOption::DoPersist;
5859-
}
5860-
5861-
let pending_events = mem::replace(&mut *self.pending_events.lock().unwrap(), vec![]);
5862-
if !pending_events.is_empty() {
5863-
result = NotifyOption::DoPersist;
5864-
}
5865-
5866-
for event in pending_events {
5867-
handler.handle_event(event);
5868-
}
5869-
5870-
result
5871-
});
5860+
let mut ev;
5861+
process_events_body!(self, ev, handler.handle_event(ev));
58725862
}
58735863
}
58745864

0 commit comments

Comments
 (0)