Skip to content

Commit ce94a5e

Browse files
committed
Skip persistence in the usual case handling channel_reestablish
When we handle an inbound `channel_reestablish` from our peers it generally doesn't change any state and thus doesn't need a `ChannelManager` persistence. Here we avoid said persistence where possible.
1 parent 9078c0d commit ce94a5e

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6446,7 +6446,7 @@ where
64466446
Ok(NotifyOption::DoPersist)
64476447
}
64486448

6449-
fn internal_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(), MsgHandleErrInternal> {
6449+
fn internal_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<NotifyOption, MsgHandleErrInternal> {
64506450
let htlc_forwards;
64516451
let need_lnd_workaround = {
64526452
let per_peer_state = self.per_peer_state.read().unwrap();
@@ -6502,14 +6502,16 @@ where
65026502
}
65036503
};
65046504

6505+
let mut persist = NotifyOption::SkipPersistHandleEvents;
65056506
if let Some(forwards) = htlc_forwards {
65066507
self.forward_htlcs(&mut [forwards][..]);
6508+
persist = NotifyOption::DoPersist;
65076509
}
65086510

65096511
if let Some(channel_ready_msg) = need_lnd_workaround {
65106512
self.internal_channel_ready(counterparty_node_id, &channel_ready_msg)?;
65116513
}
6512-
Ok(())
6514+
Ok(persist)
65136515
}
65146516

65156517
/// Process pending events from the [`chain::Watch`], returning whether any events were processed.
@@ -7674,12 +7676,22 @@ where
76747676
}
76757677

76767678
fn handle_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) {
7677-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
7678-
let _ = handle_error!(self, self.internal_channel_reestablish(counterparty_node_id, msg), *counterparty_node_id);
7679+
let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
7680+
let res = self.internal_channel_reestablish(counterparty_node_id, msg);
7681+
let persist = match &res {
7682+
Err(e) if e.closes_channel() => NotifyOption::DoPersist,
7683+
Err(_) => NotifyOption::SkipPersistHandleEvents,
7684+
Ok(persist) => *persist,
7685+
};
7686+
let _ = handle_error!(self, res, *counterparty_node_id);
7687+
persist
7688+
});
76797689
}
76807690

76817691
fn peer_disconnected(&self, counterparty_node_id: &PublicKey) {
7682-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
7692+
let _persistence_guard = PersistenceNotifierGuard::optionally_notify(
7693+
self, || NotifyOption::SkipPersistHandleEvents);
7694+
76837695
let mut failed_channels = Vec::new();
76847696
let mut per_peer_state = self.per_peer_state.write().unwrap();
76857697
let remove_peer = {

0 commit comments

Comments
 (0)