@@ -1236,21 +1236,32 @@ struct PersistenceNotifierGuard<'a, F: Fn() -> NotifyOption> {
1236
1236
1237
1237
impl<'a> PersistenceNotifierGuard<'a, fn() -> NotifyOption> { // We don't care what the concrete F is here, it's unused
1238
1238
fn notify_on_drop<C: AChannelManager>(cm: &'a C) -> PersistenceNotifierGuard<'a, impl Fn() -> NotifyOption> {
1239
+ Self::optionally_notify(cm, || -> NotifyOption { NotifyOption::DoPersist })
1240
+ }
1241
+
1242
+ fn optionally_notify<F: Fn() -> NotifyOption, C: AChannelManager>(cm: &'a C, persist_check: F)
1243
+ -> PersistenceNotifierGuard<'a, impl Fn() -> NotifyOption> {
1239
1244
let read_guard = cm.get_cm().total_consistency_lock.read().unwrap();
1240
- let _ = cm.get_cm().process_background_events(); // We always persist
1245
+ let force_notify = cm.get_cm().process_background_events();
1241
1246
1242
1247
PersistenceNotifierGuard {
1243
1248
event_persist_notifier: &cm.get_cm().event_persist_notifier,
1244
- should_persist: || -> NotifyOption { NotifyOption::DoPersist },
1249
+ should_persist: move || {
1250
+ // Pick the "most" action between `persist_check` and the background events
1251
+ // processing and return that.
1252
+ let notify = persist_check();
1253
+ if force_notify == NotifyOption::DoPersist { NotifyOption::DoPersist }
1254
+ else { notify }
1255
+ },
1245
1256
_read_guard: read_guard,
1246
1257
}
1247
-
1248
1258
}
1249
1259
1250
1260
/// Note that if any [`ChannelMonitorUpdate`]s are possibly generated,
1251
- /// [`ChannelManager::process_background_events`] MUST be called first.
1252
- fn optionally_notify<F: Fn() -> NotifyOption, C: AChannelManager>(cm: &'a C, persist_check: F)
1253
- -> PersistenceNotifierGuard<'a, F> {
1261
+ /// [`ChannelManager::process_background_events`] MUST be called first (or
1262
+ /// [`Self::optionally_notify`] used).
1263
+ fn optionally_notify_skipping_background_events<F: Fn() -> NotifyOption, C: AChannelManager>
1264
+ (cm: &'a C, persist_check: F) -> PersistenceNotifierGuard<'a, F> {
1254
1265
let read_guard = cm.get_cm().total_consistency_lock.read().unwrap();
1255
1266
1256
1267
PersistenceNotifierGuard {
@@ -4424,7 +4435,7 @@ where
4424
4435
/// it wants to detect). Thus, we have a variant exposed here for its benefit.
4425
4436
pub fn maybe_update_chan_fees(&self) {
4426
4437
PersistenceNotifierGuard::optionally_notify(self, || {
4427
- let mut should_persist = self.process_background_events() ;
4438
+ let mut should_persist = NotifyOption::SkipPersist ;
4428
4439
4429
4440
let normal_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4430
4441
let min_mempool_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::MempoolMinimum);
@@ -4469,7 +4480,7 @@ where
4469
4480
/// [`ChannelConfig`]: crate::util::config::ChannelConfig
4470
4481
pub fn timer_tick_occurred(&self) {
4471
4482
PersistenceNotifierGuard::optionally_notify(self, || {
4472
- let mut should_persist = self.process_background_events() ;
4483
+ let mut should_persist = NotifyOption::SkipPersist ;
4473
4484
4474
4485
let normal_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::Normal);
4475
4486
let min_mempool_feerate = self.fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::MempoolMinimum);
@@ -7002,7 +7013,7 @@ where
7002
7013
fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
7003
7014
let events = RefCell::new(Vec::new());
7004
7015
PersistenceNotifierGuard::optionally_notify(self, || {
7005
- let mut result = self.process_background_events() ;
7016
+ let mut result = NotifyOption::SkipPersist ;
7006
7017
7007
7018
// TODO: This behavior should be documented. It's unintuitive that we query
7008
7019
// ChannelMonitors when clearing other events.
@@ -7083,8 +7094,9 @@ where
7083
7094
}
7084
7095
7085
7096
fn block_disconnected(&self, header: &BlockHeader, height: u32) {
7086
- let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self,
7087
- || -> NotifyOption { NotifyOption::DoPersist });
7097
+ let _persistence_guard =
7098
+ PersistenceNotifierGuard::optionally_notify_skipping_background_events(
7099
+ self, || -> NotifyOption { NotifyOption::DoPersist });
7088
7100
let new_height = height - 1;
7089
7101
{
7090
7102
let mut best_block = self.best_block.write().unwrap();
@@ -7118,8 +7130,9 @@ where
7118
7130
let block_hash = header.block_hash();
7119
7131
log_trace!(self.logger, "{} transactions included in block {} at height {} provided", txdata.len(), block_hash, height);
7120
7132
7121
- let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self,
7122
- || -> NotifyOption { NotifyOption::DoPersist });
7133
+ let _persistence_guard =
7134
+ PersistenceNotifierGuard::optionally_notify_skipping_background_events(
7135
+ self, || -> NotifyOption { NotifyOption::DoPersist });
7123
7136
self.do_chain_event(Some(height), |channel| channel.transactions_confirmed(&block_hash, height, txdata, self.genesis_hash.clone(), &self.node_signer, &self.default_configuration, &self.logger)
7124
7137
.map(|(a, b)| (a, Vec::new(), b)));
7125
7138
@@ -7138,8 +7151,9 @@ where
7138
7151
let block_hash = header.block_hash();
7139
7152
log_trace!(self.logger, "New best block: {} at height {}", block_hash, height);
7140
7153
7141
- let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self,
7142
- || -> NotifyOption { NotifyOption::DoPersist });
7154
+ let _persistence_guard =
7155
+ PersistenceNotifierGuard::optionally_notify_skipping_background_events(
7156
+ self, || -> NotifyOption { NotifyOption::DoPersist });
7143
7157
*self.best_block.write().unwrap() = BestBlock::new(block_hash, height);
7144
7158
7145
7159
self.do_chain_event(Some(height), |channel| channel.best_block_updated(height, header.time, self.genesis_hash.clone(), &self.node_signer, &self.default_configuration, &self.logger));
@@ -7182,8 +7196,9 @@ where
7182
7196
}
7183
7197
7184
7198
fn transaction_unconfirmed(&self, txid: &Txid) {
7185
- let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self,
7186
- || -> NotifyOption { NotifyOption::DoPersist });
7199
+ let _persistence_guard =
7200
+ PersistenceNotifierGuard::optionally_notify_skipping_background_events(
7201
+ self, || -> NotifyOption { NotifyOption::DoPersist });
7187
7202
self.do_chain_event(None, |channel| {
7188
7203
if let Some(funding_txo) = channel.context.get_funding_txo() {
7189
7204
if funding_txo.txid == *txid {
@@ -7522,9 +7537,8 @@ where
7522
7537
7523
7538
fn handle_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) {
7524
7539
PersistenceNotifierGuard::optionally_notify(self, || {
7525
- let force_persist = self.process_background_events();
7526
7540
if let Ok(persist) = handle_error!(self, self.internal_channel_update(counterparty_node_id, msg), *counterparty_node_id) {
7527
- if force_persist == NotifyOption::DoPersist { NotifyOption::DoPersist } else { persist }
7541
+ persist
7528
7542
} else {
7529
7543
NotifyOption::SkipPersist
7530
7544
}
0 commit comments