Skip to content

Commit e5dd416

Browse files
committed
Do not always persist ChannelManager on channel_update messages
If we receive a `channel_update` message for a channel unrelated to our own, we shouldn't trigger a persistence of our `ChannelManager`. This avoids significant persistence traffic during initial node startup.
1 parent f472907 commit e5dd416

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3346,14 +3346,15 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33463346
Ok(())
33473347
}
33483348

3349-
fn internal_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) -> Result<(), MsgHandleErrInternal> {
3349+
/// Returns ShouldPersist if anything changed, otherwise either SkipPersist or an Err.
3350+
fn internal_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) -> Result<NotifyOption, MsgHandleErrInternal> {
33503351
let mut channel_state_lock = self.channel_state.lock().unwrap();
33513352
let channel_state = &mut *channel_state_lock;
33523353
let chan_id = match channel_state.short_to_id.get(&msg.contents.short_channel_id) {
33533354
Some(chan_id) => chan_id.clone(),
33543355
None => {
33553356
// It's not a local channel
3356-
return Ok(())
3357+
return Ok(NotifyOption::SkipPersist)
33573358
}
33583359
};
33593360
match channel_state.by_id.entry(chan_id) {
@@ -3363,15 +3364,15 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33633364
// If the announcement is about a channel of ours which is public, some
33643365
// other peer may simply be forwarding all its gossip to us. Don't provide
33653366
// a scary-looking error message and return Ok instead.
3366-
return Ok(());
3367+
return Ok(NotifyOption::SkipPersist);
33673368
}
33683369
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a channel_update for a channel from the wrong node - it shouldn't know about our private channels!".to_owned(), chan_id));
33693370
}
33703371
try_chan_entry!(self, chan.get_mut().channel_update(&msg), channel_state, chan);
33713372
},
33723373
hash_map::Entry::Vacant(_) => unreachable!()
33733374
}
3374-
Ok(())
3375+
Ok(NotifyOption::DoPersist)
33753376
}
33763377

33773378
fn internal_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) -> Result<(), MsgHandleErrInternal> {
@@ -4116,8 +4117,13 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
41164117
}
41174118

41184119
fn handle_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) {
4119-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
4120-
let _ = handle_error!(self, self.internal_channel_update(counterparty_node_id, msg), *counterparty_node_id);
4120+
PersistenceNotifierGuard::optionally_notify(&self.total_consistency_lock, &self.persistence_notifier, || {
4121+
if let Ok(persist) = handle_error!(self, self.internal_channel_update(counterparty_node_id, msg), *counterparty_node_id) {
4122+
persist
4123+
} else {
4124+
NotifyOption::SkipPersist
4125+
}
4126+
});
41214127
}
41224128

41234129
fn handle_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) {

0 commit comments

Comments
 (0)