Skip to content

Commit fb86fc2

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 157af6e commit fb86fc2

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
@@ -1623,6 +1623,36 @@ macro_rules! handle_new_monitor_update {
16231623
}
16241624
}
16251625

1626+
macro_rules! process_events_body {
1627+
($self: expr, $event_to_handle: expr, $handle_event: expr) => {
1628+
// We'll acquire our total consistency lock until the returned future completes so that
1629+
// we can be sure no other persists happen while processing events.
1630+
let _read_guard = $self.total_consistency_lock.read().unwrap();
1631+
1632+
let mut result = NotifyOption::SkipPersist;
1633+
1634+
// TODO: This behavior should be documented. It's unintuitive that we query
1635+
// ChannelMonitors when clearing other events.
1636+
if $self.process_pending_monitor_events() {
1637+
result = NotifyOption::DoPersist;
1638+
}
1639+
1640+
let pending_events = mem::replace(&mut *$self.pending_events.lock().unwrap(), vec![]);
1641+
if !pending_events.is_empty() {
1642+
result = NotifyOption::DoPersist;
1643+
}
1644+
1645+
for event in pending_events {
1646+
$event_to_handle = event;
1647+
$handle_event;
1648+
}
1649+
1650+
if result == NotifyOption::DoPersist {
1651+
$self.persistence_notifier.notify();
1652+
}
1653+
}
1654+
}
1655+
16261656
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>
16271657
where
16281658
M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
@@ -5720,30 +5750,8 @@ where
57205750
pub async fn process_pending_events_async<Future: core::future::Future, H: Fn(Event) -> Future>(
57215751
&self, handler: H
57225752
) {
5723-
// We'll acquire our total consistency lock until the returned future completes so that
5724-
// we can be sure no other persists happen while processing events.
5725-
let _read_guard = self.total_consistency_lock.read().unwrap();
5726-
5727-
let mut result = NotifyOption::SkipPersist;
5728-
5729-
// TODO: This behavior should be documented. It's unintuitive that we query
5730-
// ChannelMonitors when clearing other events.
5731-
if self.process_pending_monitor_events() {
5732-
result = NotifyOption::DoPersist;
5733-
}
5734-
5735-
let pending_events = mem::replace(&mut *self.pending_events.lock().unwrap(), vec![]);
5736-
if !pending_events.is_empty() {
5737-
result = NotifyOption::DoPersist;
5738-
}
5739-
5740-
for event in pending_events {
5741-
handler(event).await;
5742-
}
5743-
5744-
if result == NotifyOption::DoPersist {
5745-
self.persistence_notifier.notify();
5746-
}
5753+
let mut ev;
5754+
process_events_body!(self, ev, { handler(ev).await });
57475755
}
57485756
}
57495757

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

0 commit comments

Comments
 (0)