Skip to content

Commit 8dbf1da

Browse files
Add Persist trait.
Implementors of Persist are responsible for persisting new ChannelMonitors and ChannelMonitor updates to disk/backups.
1 parent 214a0fb commit 8dbf1da

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,61 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
20922092
}
20932093
}
20942094

2095+
/// `Persist` defines behavior for persisting channel monitors: this could mean
2096+
/// writing once to disk, and/or uploading to one or more backup services.
2097+
///
2098+
/// Note that for every new monitor, you **must** persist the new `ChannelMonitor`
2099+
/// to disk/backups. And, on every update, you **must** persist either the
2100+
/// `ChannelMonitorUpdate` or the updated monitor itself. Otherwise, there is risk
2101+
/// of situations such as revoking a transaction, then crashing before this
2102+
/// revocation can be persisted, then unintentionally broadcasting a revoked
2103+
/// transaction and losing money. This is a risk because previous channel states
2104+
/// are toxic, so it's important that whatever channel state is persisted is
2105+
/// kept up-to-date.
2106+
pub trait Persist<Keys: ChannelKeys>: Send + Sync {
2107+
/// Persist a new channel's data. The data can be stored any way you want, but
2108+
/// the identifier provided by Rust-Lightning is the channel's outpoint (and
2109+
/// it is up to you to maintain a correct mapping between the outpoint and the
2110+
/// stored channel data). Note that you **must** persist every new monitor to
2111+
/// disk. See the `Persist` trait documentation for more details.
2112+
///
2113+
/// See [`ChannelMonitor::write_for_disk`] for writing out a `ChannelMonitor`,
2114+
/// and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
2115+
///
2116+
/// [`ChannelMonitor::write_for_disk`]: struct.ChannelMonitor.html#method.write_for_disk
2117+
/// [`ChannelMonitorUpdateErr`]: enum.ChannelMonitorUpdateErr.html
2118+
fn persist_new_channel(&self, id: OutPoint, data: &ChannelMonitor<Keys>) -> Result<(), ChannelMonitorUpdateErr>;
2119+
2120+
/// Update one channel's data. The provided `ChannelMonitor` has already
2121+
/// applied the given update.
2122+
///
2123+
/// Note that on every update, you **must** persist either the
2124+
/// `ChannelMonitorUpdate` or the updated monitor itself to disk/backups. See
2125+
/// the `Persist` trait documentation for more details.
2126+
///
2127+
/// If an implementer chooses to persist the updates only, they need to make
2128+
/// sure that all the updates are applied to the `ChannelMonitors` *before* the
2129+
/// set of channel monitors is given to the `ChainMonitor` at startup time. See
2130+
/// [`ChannelMonitor::update_monitor`] for applying a monitor update to a
2131+
/// monitor. If full `ChannelMonitors` are persisted, then there is no need to
2132+
/// persist individual updates.
2133+
///
2134+
/// Note that there could be a performance tradeoff between persisting complete
2135+
/// channel monitors on every update vs. persisting only updates and applying
2136+
/// them in batches. The size of each monitor grows `O(number of state updates)`
2137+
/// whereas updates are small and `O(1)`.
2138+
///
2139+
/// See [`ChannelMonitor::write_for_disk`] for writing out a `ChannelMonitor`,
2140+
/// [`ChannelMonitorUpdate::write`] for writing out an update, and
2141+
/// [`ChannelMonitorUpdateErr`] for requirements when returning errors.
2142+
///
2143+
/// [`ChannelMonitor::update_monitor`]: struct.ChannelMonitor.html#impl-1
2144+
/// [`ChannelMonitor::write_for_disk`]: struct.ChannelMonitor.html#method.write_for_disk
2145+
/// [`ChannelMonitorUpdate::write`]: struct.ChannelMonitorUpdate.html#method.write
2146+
/// [`ChannelMonitorUpdateErr`]: enum.ChannelMonitorUpdateErr.html
2147+
fn update_persisted_channel(&self, id: OutPoint, update: &ChannelMonitorUpdate, data: &ChannelMonitor<Keys>) -> Result<(), ChannelMonitorUpdateErr>;
2148+
}
2149+
20952150
const MAX_ALLOC_SIZE: usize = 64*1024;
20962151

20972152
impl<ChanSigner: ChannelKeys + Readable> Readable for (BlockHash, ChannelMonitor<ChanSigner>) {

0 commit comments

Comments
 (0)