Skip to content

Commit b429f30

Browse files
committed
f auto-detect if we need persistence
1 parent e45bfed commit b429f30

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -837,11 +837,20 @@ where
837837
/// changed due to a [`ChannelMonitorUpdate`] such that it may be different after another
838838
/// restart).
839839
///
840-
/// This method is only safe for [`ChannelMonitor`]s which have been loaded (in conjunction
841-
/// with a `ChannelManager`) at least once by LDK 0.1 or later.
842-
pub fn load_post_0_1_existing_monitor(
840+
/// For [`ChannelMonitor`]s which were last serialized by an LDK version prior to 0.1 this will
841+
/// fall back to calling [`chain::Watch::watch_channel`] and persisting the [`ChannelMonitor`].
842+
/// See the release notes for LDK 0.1 for more information on this requirement.
843+
///
844+
/// [`ChannelMonitor`]s which do not need to be persisted (i.e. were last written by LDK 0.1 or
845+
/// later) will be loaded without persistence and this method will return
846+
/// [`ChannelMonitorUpdateStatus::Completed`].
847+
pub fn load_existing_monitor(
843848
&self, channel_id: ChannelId, monitor: ChannelMonitor<ChannelSigner>,
844-
) -> Result<(), ()> {
849+
) -> Result<ChannelMonitorUpdateStatus, ()> {
850+
if !monitor.written_by_0_1_or_later() {
851+
return chain::Watch::watch_channel(self, channel_id, monitor);
852+
}
853+
845854
let logger = WithChannelMonitor::from(&self.logger, &monitor, None);
846855
let mut monitors = self.monitors.write().unwrap();
847856
let entry = match monitors.entry(channel_id) {
@@ -860,7 +869,8 @@ where
860869
monitor.load_outputs_to_watch(chain_source, &self.logger);
861870
}
862871
entry.insert(MonitorHolder { monitor, pending_monitor_updates: Mutex::new(Vec::new()) });
863-
Ok(())
872+
873+
Ok(ChannelMonitorUpdateStatus::Completed)
864874
}
865875
}
866876

lightning/src/chain/channelmonitor.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,11 @@ pub(crate) struct ChannelMonitorImpl<Signer: EcdsaChannelSigner> {
13001300
// found at `Self::funding`. We don't use the term "renegotiated", as the currently locked
13011301
// `FundingScope` could be one that was renegotiated.
13021302
alternative_funding_confirmed: Option<(Txid, u32)>,
1303+
1304+
/// [`ChannelMonitor`]s written by LDK prior to 0.1 need to be re-persisted after startup. To
1305+
/// make deciding whether to do so simple, here we track whether this monitor was last written
1306+
/// prior to 0.1.
1307+
written_by_0_1_or_later: bool,
13031308
}
13041309

13051310
// Macro helper to access holder commitment HTLC data (including both non-dust and dust) while
@@ -1803,6 +1808,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
18031808
prev_holder_htlc_data: None,
18041809

18051810
alternative_funding_confirmed: None,
1811+
1812+
written_by_0_1_or_later: true,
18061813
})
18071814
}
18081815

@@ -1936,6 +1943,10 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
19361943
self.inner.lock().unwrap().get_funding_txo()
19371944
}
19381945

1946+
pub(crate) fn written_by_0_1_or_later(&self) -> bool {
1947+
self.inner.lock().unwrap().written_by_0_1_or_later
1948+
}
1949+
19391950
/// Gets the funding script of the channel this ChannelMonitor is monitoring for.
19401951
pub fn get_funding_script(&self) -> ScriptBuf {
19411952
self.inner.lock().unwrap().get_funding_script()
@@ -6080,6 +6091,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
60806091
(32, pending_funding, optional_vec),
60816092
(34, alternative_funding_confirmed, option),
60826093
});
6094+
let written_by_0_1_or_later = payment_preimages_with_info.is_some();
60836095
if let Some(payment_preimages_with_info) = payment_preimages_with_info {
60846096
if payment_preimages_with_info.len() != payment_preimages.len() {
60856097
return Err(DecodeError::InvalidValue);
@@ -6250,6 +6262,8 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
62506262
prev_holder_htlc_data,
62516263

62526264
alternative_funding_confirmed,
6265+
6266+
written_by_0_1_or_later,
62536267
})))
62546268
}
62556269
}

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15402,7 +15402,7 @@ impl Readable for VecDeque<(Event, Option<EventCompletionAction>)> {
1540215402
/// the next step.
1540315403
///
1540415404
/// If you wish to avoid this for performance reasons, use
15405-
/// [`ChainMonitor::load_post_0_1_existing_monitor`].
15405+
/// [`ChainMonitor::load_existing_monitor`].
1540615406
/// 7) Move the [`ChannelMonitor`]s into your local [`chain::Watch`]. If you're using a
1540715407
/// [`ChainMonitor`], this is done by calling [`chain::Watch::watch_channel`].
1540815408
///
@@ -15417,7 +15417,7 @@ impl Readable for VecDeque<(Event, Option<EventCompletionAction>)> {
1541715417
/// which you've already broadcasted the transaction.
1541815418
///
1541915419
/// [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
15420-
/// [`ChainMonitor::load_post_0_1_existing_monitor`]: crate::chain::chainmonitor::ChainMonitor::load_post_0_1_existing_monitor
15420+
/// [`ChainMonitor::load_existing_monitor`]: crate::chain::chainmonitor::ChainMonitor::load_existing_monitor
1542115421
pub struct ChannelManagerReadArgs<
1542215422
'a,
1542315423
M: Deref,

lightning/src/ln/functional_test_utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,10 @@ pub fn _reload_node<'a, 'b, 'c>(
13231323

13241324
for monitor in monitors_read.drain(..) {
13251325
let channel_id = monitor.channel_id();
1326-
assert_eq!(node.chain_monitor.load_post_0_1_existing_monitor(channel_id, monitor), Ok(()));
1326+
assert_eq!(
1327+
node.chain_monitor.load_existing_monitor(channel_id, monitor),
1328+
Ok(ChannelMonitorUpdateStatus::Completed),
1329+
);
13271330
check_added_monitors!(node, 1);
13281331
}
13291332

lightning/src/util/test_utils.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ impl<'a> TestChainMonitor<'a> {
516516
self.chain_monitor.channel_monitor_updated(*channel_id, latest_update).unwrap();
517517
}
518518

519-
pub fn load_post_0_1_existing_monitor(
519+
pub fn load_existing_monitor(
520520
&self, channel_id: ChannelId, monitor: ChannelMonitor<TestChannelSigner>,
521-
) -> Result<(), ()> {
521+
) -> Result<chain::ChannelMonitorUpdateStatus, ()> {
522522
#[cfg(feature = "std")]
523523
if let Some(blocker) = &*self.write_blocker.lock().unwrap() {
524524
blocker.recv().unwrap();
@@ -534,13 +534,15 @@ impl<'a> TestChainMonitor<'a> {
534534
)
535535
.unwrap()
536536
.1;
537-
assert!(new_monitor == monitor);
537+
// Note that a ChannelMonitor might not round-trip exactly here as we have tests that were
538+
// serialized prior to LDK 0.1 and re-serializing them will flip the "written after LDK
539+
// 0.1" flag.
538540
self.latest_monitor_update_id
539541
.lock()
540542
.unwrap()
541543
.insert(channel_id, (monitor.get_latest_update_id(), monitor.get_latest_update_id()));
542544
self.added_monitors.lock().unwrap().push((channel_id, monitor));
543-
self.chain_monitor.load_post_0_1_existing_monitor(channel_id, new_monitor)
545+
self.chain_monitor.load_existing_monitor(channel_id, new_monitor)
544546
}
545547
}
546548
impl<'a> chain::Watch<TestChannelSigner> for TestChainMonitor<'a> {

0 commit comments

Comments
 (0)