Skip to content

Commit 8d81193

Browse files
committed
Update ChannelMonitorUpdateStatus documentation with async support
Since we now (almost) support async monitor update persistence, the documentation on `ChannelMonitorUpdateStatus` can be updated to no longer suggest users must keep a local copy that persists before returning. However, because there are still a few remaining issues, we note that async support is currently beta and explicily warn of potential for funds-loss. Fixes lightningdevkit#1684
1 parent b3260e1 commit 8d81193

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

lightning/src/chain/chainmonitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ impl MonitorUpdateId {
8282
/// * If persistence (including any relevant `fsync()` calls) happens immediately, the
8383
/// implementation should return [`ChannelMonitorUpdateStatus::Completed`], indicating normal
8484
/// channel operation should continue.
85-
/// * If persistence happens asynchronously, implementations should first ensure the
86-
/// [`ChannelMonitor`] or [`ChannelMonitorUpdate`] are written durably to disk, and then return
85+
///
86+
/// * If persistence happens asynchronously, implementations can return
8787
/// [`ChannelMonitorUpdateStatus::InProgress`] while the update continues in the background.
8888
/// Once the update completes, [`ChainMonitor::channel_monitor_updated`] should be called with
8989
/// the corresponding [`MonitorUpdateId`].

lightning/src/chain/mod.rs

+27-17
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ pub trait Confirm {
176176
}
177177

178178
/// An enum representing the status of a channel monitor update persistence.
179+
///
180+
/// Note that there is no error variant - any failure to persist a [`ChannelMonitor`] should be
181+
/// retried indefinitely, the node shut down (as if we cannot update stored data we can't do much
182+
/// of anything useful).
183+
///
184+
/// Note that channels should generally *not* be force-closed after a persistence failure.
185+
/// Force-closing with the latest [`ChannelMonitorUpdate`] applied may result in a transaction
186+
/// being broadcast which can only be spent by the latest [`ChannelMonitor`]! Thus, if the
187+
/// latest [`ChannelMonitor`] is not durably persisted anywhere and exists only in memory, naively
188+
/// calling [`ChannelManager::force_close_broadcasting_latest_txn`] *may result in loss of funds*!
189+
///
190+
/// [`ChannelManager::force_close_broadcasting_latest_txn`]: crate::ln::channelmanager::ChannelManager::force_close_broadcasting_latest_txn
179191
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
180192
pub enum ChannelMonitorUpdateStatus {
181193
/// The update has been durably persisted and all copies of the relevant [`ChannelMonitor`]
@@ -184,13 +196,13 @@ pub enum ChannelMonitorUpdateStatus {
184196
/// This includes performing any `fsync()` calls required to ensure the update is guaranteed to
185197
/// be available on restart even if the application crashes.
186198
Completed,
187-
/// Used to indicate a temporary failure (eg connection to a watchtower or remote backup of
188-
/// our state failed, but is expected to succeed at some point in the future).
199+
/// Indicates that the update will happen asynchronously in the background or that a transient
200+
/// failure occurred which is being retried in the background and will eventually complete.
189201
///
190-
/// Such a failure will "freeze" a channel, preventing us from revoking old states or
191-
/// submitting new commitment transactions to the counterparty. Once the update(s) which failed
192-
/// have been successfully applied, a [`MonitorEvent::Completed`] can be used to restore the
193-
/// channel to an operational state.
202+
/// This will "freeze" a channel, preventing us from revoking old states or submitting a new
203+
/// commitment transaction to the counterparty. Once the update(s) which are `InProgress` have
204+
/// been completed, a [`MonitorEvent::Completed`] can be used to restore the channel to an
205+
/// operational state.
194206
///
195207
/// Even when a channel has been "frozen", updates to the [`ChannelMonitor`] can continue to
196208
/// occur (e.g. if an inbound HTLC which we forwarded was claimed upstream, resulting in us
@@ -204,6 +216,10 @@ pub enum ChannelMonitorUpdateStatus {
204216
/// remote location (with local copies persisted immediately), it is anticipated that all
205217
/// updates will return [`InProgress`] until the remote copies could be updated.
206218
///
219+
/// Note that while fully asynchronous persistence of [`ChannelMonitor`] data is generally
220+
/// reliable, this feature is considered beta, and a handful of edge-cases remain. Until the
221+
/// remaining cases are fixed, in rare cases, *using this feature may lead to funds loss*.
222+
///
207223
/// [`InProgress`]: ChannelMonitorUpdateStatus::InProgress
208224
InProgress,
209225
}
@@ -212,18 +228,12 @@ pub enum ChannelMonitorUpdateStatus {
212228
/// blocks are connected and disconnected.
213229
///
214230
/// Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
215-
/// responsible for maintaining a set of monitors such that they can be updated accordingly as
216-
/// channel state changes and HTLCs are resolved. See method documentation for specific
217-
/// requirements.
218-
///
219-
/// Implementations **must** ensure that updates are successfully applied and persisted upon method
220-
/// completion. If an update will not succeed, then it must immediately shut down.
231+
/// responsible for maintaining a set of monitors such that they can be updated as channel state
232+
/// changes. On each update, *all copies* of a [`ChannelMonitor`] must be updated and the update
233+
/// persisted to disk to ensure that the latest [`ChannelMonitor`] state can be reloaded if the
234+
/// application crashes.
221235
///
222-
/// If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
223-
/// backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
224-
/// could result in a revoked transaction being broadcast, allowing the counterparty to claim all
225-
/// funds in the channel. See [`ChannelMonitorUpdateStatus`] for more details about how to handle
226-
/// multiple instances.
236+
/// See method documentation and [`ChannelMonitorUpdateStatus`] for specific requirements.
227237
pub trait Watch<ChannelSigner: WriteableEcdsaChannelSigner> {
228238
/// Watches a channel identified by `funding_txo` using `monitor`.
229239
///

lightning/src/ln/channelmanager.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -923,12 +923,14 @@ where
923923
/// called [`funding_transaction_generated`] for outbound channels) being closed.
924924
///
925925
/// Note that you can be a bit lazier about writing out `ChannelManager` than you can be with
926-
/// [`ChannelMonitor`]. With [`ChannelMonitor`] you MUST write each monitor update out to disk before
927-
/// returning from [`chain::Watch::watch_channel`]/[`update_channel`], with ChannelManagers, writing updates
928-
/// happens out-of-band (and will prevent any other `ChannelManager` operations from occurring during
929-
/// the serialization process). If the deserialized version is out-of-date compared to the
930-
/// [`ChannelMonitor`] passed by reference to [`read`], those channels will be force-closed based on the
931-
/// `ChannelMonitor` state and no funds will be lost (mod on-chain transaction fees).
926+
/// [`ChannelMonitor`]. With [`ChannelMonitor`] you MUST durably write each
927+
/// [`ChannelMonitorUpdate`] before returning from
928+
/// [`chain::Watch::watch_channel`]/[`update_channel`] or before completing async writes. With
929+
/// `ChannelManager`s, writing updates happens out-of-band (and will prevent any other
930+
/// `ChannelManager` operations from occurring during the serialization process). If the
931+
/// deserialized version is out-of-date compared to the [`ChannelMonitor`] passed by reference to
932+
/// [`read`], those channels will be force-closed based on the `ChannelMonitor` state and no funds
933+
/// will be lost (modulo on-chain transaction fees).
932934
///
933935
/// Note that the deserializer is only implemented for `(`[`BlockHash`]`, `[`ChannelManager`]`)`, which
934936
/// tells you the last block hash which was connected. You should get the best block tip before using the manager.

0 commit comments

Comments
 (0)