Skip to content

Commit e084ab2

Browse files
committed
Stop storing last_chain_persist_height
We only used to store last_chain_persist_height to release events held for more than LATENCY_GRACE_PERIOD_BLOCKS due to pending monitor update with UpdateOrigin::ChainSync. Since we no longer pause events for ChainSync persistence, we no longer need to store last_chain_persist_height.
1 parent cb86399 commit e084ab2

File tree

1 file changed

+5
-43
lines changed

1 file changed

+5
-43
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -210,26 +210,13 @@ struct MonitorHolder<ChannelSigner: WriteableEcdsaChannelSigner> {
210210
/// [`ChannelMonitorUpdateStatus::InProgress`], and then calls channel_monitor_updated
211211
/// immediately, racing our insertion of the pending update into the contained Vec.
212212
pending_monitor_updates: Mutex<Vec<MonitorUpdateId>>,
213-
/// The last block height at which no [`UpdateOrigin::ChainSync`] monitor updates were present
214-
/// in `pending_monitor_updates`.
215-
/// If it's been more than [`LATENCY_GRACE_PERIOD_BLOCKS`] since we started waiting on a chain
216-
/// sync event, we let monitor events return to `ChannelManager` because we cannot hold them up
217-
/// forever or we'll end up with HTLC preimages waiting to feed back into an upstream channel
218-
/// forever, risking funds loss.
219-
///
220-
/// [`LATENCY_GRACE_PERIOD_BLOCKS`]: crate::util::ser::Writeable::write
221-
last_chain_persist_height: AtomicUsize,
222213
}
223214

224215
impl<ChannelSigner: WriteableEcdsaChannelSigner> MonitorHolder<ChannelSigner> {
225216
fn has_pending_offchain_updates(&self, pending_monitor_updates_lock: &MutexGuard<Vec<MonitorUpdateId>>) -> bool {
226217
pending_monitor_updates_lock.iter().any(|update_id|
227218
if let UpdateOrigin::OffChain(_) = update_id.contents { true } else { false })
228219
}
229-
fn has_pending_chainsync_updates(&self, pending_monitor_updates_lock: &MutexGuard<Vec<MonitorUpdateId>>) -> bool {
230-
pending_monitor_updates_lock.iter().any(|update_id|
231-
if let UpdateOrigin::ChainSync(_) = update_id.contents { true } else { false })
232-
}
233220
}
234221

235222
/// A read-only reference to a current ChannelMonitor.
@@ -317,7 +304,7 @@ where C::Target: chain::Filter,
317304
for funding_outpoint in funding_outpoints.iter() {
318305
let monitor_lock = self.monitors.read().unwrap();
319306
if let Some(monitor_state) = monitor_lock.get(funding_outpoint) {
320-
if self.update_monitor_with_chain_data(header, best_height, txdata, &process, funding_outpoint, &monitor_state).is_err() {
307+
if self.update_monitor_with_chain_data(header, txdata, &process, funding_outpoint, &monitor_state).is_err() {
321308
// Take the monitors lock for writing so that we poison it and any future
322309
// operations going forward fail immediately.
323310
core::mem::drop(monitor_lock);
@@ -332,7 +319,7 @@ where C::Target: chain::Filter,
332319
let monitor_states = self.monitors.write().unwrap();
333320
for (funding_outpoint, monitor_state) in monitor_states.iter() {
334321
if !funding_outpoints.contains(funding_outpoint) {
335-
if self.update_monitor_with_chain_data(header, best_height, txdata, &process, funding_outpoint, &monitor_state).is_err() {
322+
if self.update_monitor_with_chain_data(header, txdata, &process, funding_outpoint, &monitor_state).is_err() {
336323
log_error!(self.logger, "{}", err_str);
337324
panic!("{}", err_str);
338325
}
@@ -351,8 +338,8 @@ where C::Target: chain::Filter,
351338
}
352339

353340
fn update_monitor_with_chain_data<FN>(
354-
&self, header: &Header, best_height: Option<u32>, txdata: &TransactionData,
355-
process: FN, funding_outpoint: &OutPoint, monitor_state: &MonitorHolder<ChannelSigner>
341+
&self, header: &Header, txdata: &TransactionData, process: FN, funding_outpoint: &OutPoint,
342+
monitor_state: &MonitorHolder<ChannelSigner>
356343
) -> Result<(), ()> where FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<TransactionOutputs> {
357344
let monitor = &monitor_state.monitor;
358345
let logger = WithChannelMonitor::from(&self.logger, &monitor);
@@ -364,14 +351,6 @@ where C::Target: chain::Filter,
364351
contents: UpdateOrigin::ChainSync(chain_sync_update_id),
365352
};
366353
let mut pending_monitor_updates = monitor_state.pending_monitor_updates.lock().unwrap();
367-
if let Some(height) = best_height {
368-
if !monitor_state.has_pending_chainsync_updates(&pending_monitor_updates) {
369-
// If there are not ChainSync persists awaiting completion, go ahead and
370-
// set last_chain_persist_height here - we wouldn't want the first
371-
// InProgress to always immediately be considered "overly delayed".
372-
monitor_state.last_chain_persist_height.store(height as usize, Ordering::Release);
373-
}
374-
}
375354

376355
log_trace!(logger, "Syncing Channel Monitor for channel {} for block-data update_id {}",
377356
log_funding_info!(monitor),
@@ -562,23 +541,7 @@ where C::Target: chain::Filter,
562541
monitor_update_id: monitor_data.monitor.get_latest_update_id(),
563542
}], monitor_data.monitor.get_counterparty_node_id()));
564543
},
565-
MonitorUpdateId { contents: UpdateOrigin::ChainSync(completed_update_id) } => {
566-
let monitor_has_pending_updates =
567-
monitor_data.has_pending_chainsync_updates(&pending_monitor_updates);
568-
log_debug!(self.logger, "Completed chain sync monitor update {} for channel with funding outpoint {:?}, {}",
569-
completed_update_id,
570-
funding_txo,
571-
if monitor_has_pending_updates {
572-
"still have pending chain sync updates"
573-
} else {
574-
"all chain sync updates complete, releasing pending MonitorEvents"
575-
});
576-
if !monitor_has_pending_updates {
577-
monitor_data.last_chain_persist_height.store(self.highest_chain_height.load(Ordering::Acquire), Ordering::Release);
578-
// The next time release_pending_monitor_events is called, any events for this
579-
// ChannelMonitor will be returned.
580-
}
581-
},
544+
MonitorUpdateId { contents: UpdateOrigin::ChainSync(_) } => {},
582545
}
583546
self.event_notifier.notify();
584547
Ok(())
@@ -833,7 +796,6 @@ where C::Target: chain::Filter,
833796
entry.insert(MonitorHolder {
834797
monitor,
835798
pending_monitor_updates: Mutex::new(pending_monitor_updates),
836-
last_chain_persist_height: AtomicUsize::new(self.highest_chain_height.load(Ordering::Acquire)),
837799
});
838800
Ok(persist_res)
839801
}

0 commit comments

Comments
 (0)