Skip to content

Commit 305df1d

Browse files
committed
Update channelmanager::NotifyOption to indicate persist or event
As we now signal events-available from persistence-needed separately, the `NotifyOption` enum should include a separate variant for events-but-no-persistence, which we add here.
1 parent 7fa499c commit 305df1d

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,8 @@ pub struct ChainParameters {
12151215
#[must_use]
12161216
enum NotifyOption {
12171217
DoPersist,
1218-
SkipPersist,
1218+
SkipPersistHandleEvents,
1219+
SkipPersistNoEvents,
12191220
}
12201221

12211222
/// Whenever we release the `ChannelManager`'s `total_consistency_lock`, from read mode, it is
@@ -1253,8 +1254,13 @@ impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> { // We don't care w
12531254
// Pick the "most" action between `persist_check` and the background events
12541255
// processing and return that.
12551256
let notify = persist_check();
1256-
if force_notify == NotifyOption::DoPersist { NotifyOption::DoPersist }
1257-
else { notify }
1257+
match (notify, force_notify) {
1258+
(NotifyOption::DoPersist, _) => NotifyOption::DoPersist,
1259+
(_, NotifyOption::DoPersist) => NotifyOption::DoPersist,
1260+
(NotifyOption::SkipPersistHandleEvents, _) => NotifyOption::SkipPersistHandleEvents,
1261+
(_, NotifyOption::SkipPersistHandleEvents) => NotifyOption::SkipPersistHandleEvents,
1262+
_ => NotifyOption::SkipPersistNoEvents,
1263+
}
12581264
},
12591265
_read_guard: read_guard,
12601266
}
@@ -1278,9 +1284,14 @@ impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> { // We don't care w
12781284

12791285
impl<'a, F: Fn() -> NotifyOption> Drop for PersistenceNotifierGuard<'a, F> {
12801286
fn drop(&mut self) {
1281-
if (self.should_persist)() == NotifyOption::DoPersist {
1282-
self.needs_persist_flag.store(true, Ordering::Release);
1283-
self.event_persist_notifier.notify();
1287+
match (self.should_persist)() {
1288+
NotifyOption::DoPersist => {
1289+
self.needs_persist_flag.store(true, Ordering::Release);
1290+
self.event_persist_notifier.notify()
1291+
},
1292+
NotifyOption::SkipPersistHandleEvents =>
1293+
self.event_persist_notifier.notify(),
1294+
NotifyOption::SkipPersistNoEvents => {},
12841295
}
12851296
}
12861297
}
@@ -2092,7 +2103,7 @@ macro_rules! process_events_body {
20922103
return;
20932104
}
20942105

2095-
let mut result = NotifyOption::SkipPersist;
2106+
let mut result;
20962107

20972108
{
20982109
// We'll acquire our total consistency lock so that we can be sure no other
@@ -2101,7 +2112,7 @@ macro_rules! process_events_body {
21012112

21022113
// Because `handle_post_event_actions` may send `ChannelMonitorUpdate`s to the user we must
21032114
// ensure any startup-generated background events are handled first.
2104-
if $self.process_background_events() == NotifyOption::DoPersist { result = NotifyOption::DoPersist; }
2115+
result = $self.process_background_events();
21052116

21062117
// TODO: This behavior should be documented. It's unintuitive that we query
21072118
// ChannelMonitors when clearing other events.
@@ -4348,7 +4359,7 @@ where
43484359
let mut background_events = Vec::new();
43494360
mem::swap(&mut *self.pending_background_events.lock().unwrap(), &mut background_events);
43504361
if background_events.is_empty() {
4351-
return NotifyOption::SkipPersist;
4362+
return NotifyOption::SkipPersistNoEvents;
43524363
}
43534364

43544365
for event in background_events.drain(..) {
@@ -4417,17 +4428,17 @@ where
44174428
}
44184429

44194430
fn update_channel_fee(&self, chan_id: &ChannelId, chan: &mut Channel<SP>, new_feerate: u32) -> NotifyOption {
4420-
if !chan.context.is_outbound() { return NotifyOption::SkipPersist; }
4431+
if !chan.context.is_outbound() { return NotifyOption::SkipPersistNoEvents; }
44214432
// If the feerate has decreased by less than half, don't bother
44224433
if new_feerate <= chan.context.get_feerate_sat_per_1000_weight() && new_feerate * 2 > chan.context.get_feerate_sat_per_1000_weight() {
44234434
log_trace!(self.logger, "Channel {} does not qualify for a feerate change from {} to {}.",
4424-
&chan_id, chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
4425-
return NotifyOption::SkipPersist;
4435+
chan_id, chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
4436+
return NotifyOption::SkipPersistNoEvents;
44264437
}
44274438
if !chan.context.is_live() {
44284439
log_trace!(self.logger, "Channel {} does not qualify for a feerate change from {} to {} as it cannot currently be updated (probably the peer is disconnected).",
4429-
&chan_id, chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
4430-
return NotifyOption::SkipPersist;
4440+
chan_id, chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
4441+
return NotifyOption::SkipPersistNoEvents;
44314442
}
44324443
log_trace!(self.logger, "Channel {} qualifies for a feerate change from {} to {}.",
44334444
&chan_id, chan.context.get_feerate_sat_per_1000_weight(), new_feerate);
@@ -4443,7 +4454,7 @@ where
44434454
/// it wants to detect). Thus, we have a variant exposed here for its benefit.
44444455
pub fn maybe_update_chan_fees(&self) {
44454456
PersistenceNotifierGuard::optionally_notify(self, || {
4446-
let mut should_persist = NotifyOption::SkipPersist;
4457+
let mut should_persist = NotifyOption::SkipPersistNoEvents;
44474458

44484459
let normal_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
44494460
let min_mempool_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::MempoolMinimum);
@@ -4488,7 +4499,7 @@ where
44884499
/// [`ChannelConfig`]: crate::util::config::ChannelConfig
44894500
pub fn timer_tick_occurred(&self) {
44904501
PersistenceNotifierGuard::optionally_notify(self, || {
4491-
let mut should_persist = NotifyOption::SkipPersist;
4502+
let mut should_persist = NotifyOption::SkipPersistNoEvents;
44924503

44934504
let normal_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
44944505
let min_mempool_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::MempoolMinimum);
@@ -6361,19 +6372,19 @@ where
63616372
Ok(())
63626373
}
63636374

6364-
/// Returns ShouldPersist if anything changed, otherwise either SkipPersist or an Err.
6375+
/// Returns DoPersist if anything changed, otherwise either SkipPersistNoEvents or an Err.
63656376
fn internal_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) -> Result<NotifyOption, MsgHandleErrInternal> {
63666377
let (chan_counterparty_node_id, chan_id) = match self.short_to_chan_info.read().unwrap().get(&msg.contents.short_channel_id) {
63676378
Some((cp_id, chan_id)) => (cp_id.clone(), chan_id.clone()),
63686379
None => {
63696380
// It's not a local channel
6370-
return Ok(NotifyOption::SkipPersist)
6381+
return Ok(NotifyOption::SkipPersistNoEvents)
63716382
}
63726383
};
63736384
let per_peer_state = self.per_peer_state.read().unwrap();
63746385
let peer_state_mutex_opt = per_peer_state.get(&chan_counterparty_node_id);
63756386
if peer_state_mutex_opt.is_none() {
6376-
return Ok(NotifyOption::SkipPersist)
6387+
return Ok(NotifyOption::SkipPersistNoEvents)
63776388
}
63786389
let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
63796390
let peer_state = &mut *peer_state_lock;
@@ -6385,14 +6396,14 @@ where
63856396
// If the announcement is about a channel of ours which is public, some
63866397
// other peer may simply be forwarding all its gossip to us. Don't provide
63876398
// a scary-looking error message and return Ok instead.
6388-
return Ok(NotifyOption::SkipPersist);
6399+
return Ok(NotifyOption::SkipPersistNoEvents);
63896400
}
63906401
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));
63916402
}
63926403
let were_node_one = self.get_our_node_id().serialize()[..] < chan.context.get_counterparty_node_id().serialize()[..];
63936404
let msg_from_node_one = msg.contents.flags & 1 == 0;
63946405
if were_node_one == msg_from_node_one {
6395-
return Ok(NotifyOption::SkipPersist);
6406+
return Ok(NotifyOption::SkipPersistNoEvents);
63966407
} else {
63976408
log_debug!(self.logger, "Received channel_update for channel {}.", chan_id);
63986409
try_chan_phase_entry!(self, chan.channel_update(&msg), chan_phase_entry);
@@ -6402,7 +6413,7 @@ where
64026413
"Got a channel_update for an unfunded channel!".into())), chan_phase_entry);
64036414
}
64046415
},
6405-
hash_map::Entry::Vacant(_) => return Ok(NotifyOption::SkipPersist)
6416+
hash_map::Entry::Vacant(_) => return Ok(NotifyOption::SkipPersistNoEvents)
64066417
}
64076418
Ok(NotifyOption::DoPersist)
64086419
}
@@ -7021,7 +7032,7 @@ where
70217032
fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
70227033
let events = RefCell::new(Vec::new());
70237034
PersistenceNotifierGuard::optionally_notify(self, || {
7024-
let mut result = NotifyOption::SkipPersist;
7035+
let mut result = NotifyOption::SkipPersistNoEvents;
70257036

70267037
// TODO: This behavior should be documented. It's unintuitive that we query
70277038
// ChannelMonitors when clearing other events.
@@ -7556,7 +7567,7 @@ where
75567567
if let Ok(persist) = handle_error!(self, self.internal_channel_update(counterparty_node_id, msg), *counterparty_node_id) {
75577568
persist
75587569
} else {
7559-
NotifyOption::SkipPersist
7570+
NotifyOption::SkipPersistNoEvents
75607571
}
75617572
});
75627573
}

0 commit comments

Comments
 (0)