Skip to content

Commit 996f65b

Browse files
committed
Persist unresolved ChannelMonitors on empty height change
1 parent 726dd5c commit 996f65b

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -639,25 +639,30 @@ where C::Target: chain::Filter,
639639
/// Depending on the implementation of [`Persist::archive_persisted_channel`] the monitor
640640
/// data could be moved to an archive location or removed entirely.
641641
pub fn archive_fully_resolved_channel_monitors(&self) {
642-
let mut have_monitors_to_prune = false;
642+
let mut have_monitors_to_persist_or_prune = false;
643643
for (_, monitor_holder) in self.monitors.read().unwrap().iter() {
644644
let logger = WithChannelMonitor::from(&self.logger, &monitor_holder.monitor, None);
645-
if monitor_holder.monitor.is_fully_resolved(&logger) {
646-
have_monitors_to_prune = true;
645+
let (is_fully_resolved, needs_persistence) = monitor_holder.monitor.process_claim_resolution_status(&logger);
646+
if is_fully_resolved || needs_persistence {
647+
have_monitors_to_persist_or_prune = true;
647648
}
648649
}
649-
if have_monitors_to_prune {
650+
if have_monitors_to_persist_or_prune {
650651
let mut monitors = self.monitors.write().unwrap();
651652
monitors.retain(|funding_txo, monitor_holder| {
652653
let logger = WithChannelMonitor::from(&self.logger, &monitor_holder.monitor, None);
653-
if monitor_holder.monitor.is_fully_resolved(&logger) {
654+
let (is_fully_resolved, needs_persistence) = monitor_holder.monitor.process_claim_resolution_status(&logger);
655+
if is_fully_resolved {
654656
log_info!(logger,
655657
"Archiving fully resolved ChannelMonitor for funding txo {}",
656658
funding_txo
657659
);
658660
self.persister.archive_persisted_channel(*funding_txo);
659661
false
660662
} else {
663+
if needs_persistence {
664+
self.persister.update_persisted_channel(*funding_txo, None, &monitor_holder.monitor);
665+
}
661666
true
662667
}
663668
});

lightning/src/chain/channelmonitor.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,10 +1994,14 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
19941994

19951995
/// Checks if the monitor is fully resolved. Resolved monitor is one that has claimed all of
19961996
/// its outputs and balances (i.e. [`Self::get_claimable_balances`] returns an empty set).
1997+
/// Additionally updates the empty balances height if necessary.
19971998
///
1998-
/// This function returns true only if [`Self::get_claimable_balances`] has been empty for at least
1999+
/// This function returns a tuple of two booleans, the first indicating whether the monitor is
2000+
/// fully resolved, and the second whether the monitor needs persistence as a consequence of
2001+
/// checking its claim resolution status.
2002+
/// The first boolean is true only if [`Self::get_claimable_balances`] has been empty for at least
19992003
/// 4032 blocks as an additional protection against any bugs resulting in spuriously empty balance sets.
2000-
pub fn is_fully_resolved<L: Logger>(&self, logger: &L) -> bool {
2004+
pub fn process_claim_resolution_status<L: Logger>(&self, logger: &L) -> (bool, bool) {
20012005
let mut is_all_funds_claimed = self.get_claimable_balances().is_empty();
20022006
let current_height = self.current_best_block().height;
20032007
let mut inner = self.inner.lock().unwrap();
@@ -2011,7 +2015,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
20112015
match (inner.balances_empty_height, is_all_funds_claimed) {
20122016
(Some(balances_empty_height), true) => {
20132017
// Claimed all funds, check if reached the blocks threshold.
2014-
current_height >= balances_empty_height + BLOCKS_THRESHOLD
2018+
(current_height >= balances_empty_height + BLOCKS_THRESHOLD, false)
20152019
},
20162020
(Some(_), false) => {
20172021
// previously assumed we claimed all funds, but we have new funds to claim.
@@ -2021,7 +2025,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
20212025
"WARNING: LDK thought it was done claiming all the available funds in the ChannelMonitor for channel {}, but later decided it had more to claim. This is potentially an important bug in LDK, please report it at https://github.com/lightningdevkit/rust-lightning/issues/new",
20222026
inner.get_funding_txo().0);
20232027
inner.balances_empty_height = None;
2024-
false
2028+
(false, true)
20252029
},
20262030
(None, true) => {
20272031
// Claimed all funds but `balances_empty_height` is None. It is set to the
@@ -2030,11 +2034,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
20302034
"ChannelMonitor funded at {} is now fully resolved. It will become archivable in {} blocks",
20312035
inner.get_funding_txo().0, BLOCKS_THRESHOLD);
20322036
inner.balances_empty_height = Some(current_height);
2033-
false
2037+
(false, true)
20342038
},
20352039
(None, false) => {
20362040
// Have funds to claim.
2037-
false
2041+
(false, false)
20382042
},
20392043
}
20402044
}

0 commit comments

Comments
 (0)