Skip to content

Commit a66ab5d

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 783e818 commit a66ab5d

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
@@ -1601,6 +1601,36 @@ macro_rules! handle_new_monitor_update {
16011601
}
16021602
}
16031603

1604+
macro_rules! process_events_body {
1605+
($self: expr, $event_to_handle: expr, $handle_event: expr) => {
1606+
// We'll acquire our total consistency lock until the returned future completes so that
1607+
// we can be sure no other persists happen while processing events.
1608+
let _read_guard = $self.total_consistency_lock.read().unwrap();
1609+
1610+
let mut result = NotifyOption::SkipPersist;
1611+
1612+
// TODO: This behavior should be documented. It's unintuitive that we query
1613+
// ChannelMonitors when clearing other events.
1614+
if $self.process_pending_monitor_events() {
1615+
result = NotifyOption::DoPersist;
1616+
}
1617+
1618+
let pending_events = mem::replace(&mut *$self.pending_events.lock().unwrap(), vec![]);
1619+
if !pending_events.is_empty() {
1620+
result = NotifyOption::DoPersist;
1621+
}
1622+
1623+
for event in pending_events {
1624+
$event_to_handle = event;
1625+
$handle_event;
1626+
}
1627+
1628+
if result == NotifyOption::DoPersist {
1629+
$self.persistence_notifier.notify();
1630+
}
1631+
}
1632+
}
1633+
16041634
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>
16051635
where
16061636
M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
@@ -5724,30 +5754,8 @@ where
57245754
pub async fn process_pending_events_async<Future: core::future::Future, H: Fn(Event) -> Future>(
57255755
&self, handler: H
57265756
) {
5727-
// We'll acquire our total consistency lock until the returned future completes so that
5728-
// we can be sure no other persists happen while processing events.
5729-
let _read_guard = self.total_consistency_lock.read().unwrap();
5730-
5731-
let mut result = NotifyOption::SkipPersist;
5732-
5733-
// TODO: This behavior should be documented. It's unintuitive that we query
5734-
// ChannelMonitors when clearing other events.
5735-
if self.process_pending_monitor_events() {
5736-
result = NotifyOption::DoPersist;
5737-
}
5738-
5739-
let pending_events = mem::replace(&mut *self.pending_events.lock().unwrap(), vec![]);
5740-
if !pending_events.is_empty() {
5741-
result = NotifyOption::DoPersist;
5742-
}
5743-
5744-
for event in pending_events {
5745-
handler(event).await;
5746-
}
5747-
5748-
if result == NotifyOption::DoPersist {
5749-
self.persistence_notifier.notify();
5750-
}
5757+
let mut ev;
5758+
process_events_body!(self, ev, { handler(ev).await });
57515759
}
57525760
}
57535761

@@ -5829,26 +5837,8 @@ where
58295837
/// An [`EventHandler`] may safely call back to the provider in order to handle an event.
58305838
/// However, it must not call [`Writeable::write`] as doing so would result in a deadlock.
58315839
fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {
5832-
PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
5833-
let mut result = NotifyOption::SkipPersist;
5834-
5835-
// TODO: This behavior should be documented. It's unintuitive that we query
5836-
// ChannelMonitors when clearing other events.
5837-
if self.process_pending_monitor_events() {
5838-
result = NotifyOption::DoPersist;
5839-
}
5840-
5841-
let pending_events = mem::replace(&mut *self.pending_events.lock().unwrap(), vec![]);
5842-
if !pending_events.is_empty() {
5843-
result = NotifyOption::DoPersist;
5844-
}
5845-
5846-
for event in pending_events {
5847-
handler.handle_event(event);
5848-
}
5849-
5850-
result
5851-
});
5840+
let mut ev;
5841+
process_events_body!(self, ev, handler.handle_event(ev));
58525842
}
58535843
}
58545844

0 commit comments

Comments
 (0)