Skip to content

Commit ec4715c

Browse files
Add Persist trait.
Implementors of Persist are responsible for persisting new ChannelMonitors and ChannelMonitor updates to disk/backups.
1 parent 4350cc6 commit ec4715c

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
@@ -2123,6 +2123,61 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
21232123
}
21242124
}
21252125

2126+
/// `Persist` defines behavior for persisting channel monitors: this could mean
2127+
/// writing once to disk, and/or uploading to one or more backup services.
2128+
///
2129+
/// Note that for every new monitor, you **must** persist the new `ChannelMonitor`
2130+
/// to disk/backups. And, on every update, you **must** persist either the
2131+
/// `ChannelMonitorUpdate` or the updated monitor itself. Otherwise, there is risk
2132+
/// of situations such as revoking a transaction, then crashing before this
2133+
/// revocation can be persisted, then unintentionally broadcasting a revoked
2134+
/// transaction and losing money. This is a risk because previous channel states
2135+
/// are toxic, so it's important that whatever channel state is persisted is
2136+
/// kept up-to-date.
2137+
pub trait Persist<Keys: ChannelKeys>: Send + Sync {
2138+
/// Persist a new channel's data. The data can be stored any way you want, but
2139+
/// the identifier provided by Rust-Lightning is the channel's outpoint (and
2140+
/// it is up to you to maintain a correct mapping between the outpoint and the
2141+
/// stored channel data). Note that you **must** persist every new monitor to
2142+
/// disk. See the `Persist` trait documentation for more details.
2143+
///
2144+
/// See [`ChannelMonitor::write_for_disk`] for writing out a `ChannelMonitor`,
2145+
/// and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
2146+
///
2147+
/// [`ChannelMonitor::write_for_disk`]: struct.ChannelMonitor.html#method.write_for_disk
2148+
/// [`ChannelMonitorUpdateErr`]: enum.ChannelMonitorUpdateErr.html
2149+
fn persist_new_channel(&self, id: OutPoint, data: &ChannelMonitor<Keys>) -> Result<(), ChannelMonitorUpdateErr>;
2150+
2151+
/// Update one channel's data. The provided `ChannelMonitor` has already
2152+
/// applied the given update.
2153+
///
2154+
/// Note that on every update, you **must** persist either the
2155+
/// `ChannelMonitorUpdate` or the updated monitor itself to disk/backups. See
2156+
/// the `Persist` trait documentation for more details.
2157+
///
2158+
/// If an implementer chooses to persist the updates only, they need to make
2159+
/// sure that all the updates are applied to the `ChannelMonitors` *before*
2160+
/// the set of channel monitors is given to the `ChannelManager`
2161+
/// deserialization routine. See [`ChannelMonitor::update_monitor`] for
2162+
/// applying a monitor update to a monitor. If full `ChannelMonitors` are
2163+
/// persisted, then there is no need to persist individual updates.
2164+
///
2165+
/// Note that there could be a performance tradeoff between persisting complete
2166+
/// channel monitors on every update vs. persisting only updates and applying
2167+
/// them in batches. The size of each monitor grows `O(number of state updates)`
2168+
/// whereas updates are small and `O(1)`.
2169+
///
2170+
/// See [`ChannelMonitor::write_for_disk`] for writing out a `ChannelMonitor`,
2171+
/// [`ChannelMonitorUpdate::write`] for writing out an update, and
2172+
/// [`ChannelMonitorUpdateErr`] for requirements when returning errors.
2173+
///
2174+
/// [`ChannelMonitor::update_monitor`]: struct.ChannelMonitor.html#impl-1
2175+
/// [`ChannelMonitor::write_for_disk`]: struct.ChannelMonitor.html#method.write_for_disk
2176+
/// [`ChannelMonitorUpdate::write`]: struct.ChannelMonitorUpdate.html#method.write
2177+
/// [`ChannelMonitorUpdateErr`]: enum.ChannelMonitorUpdateErr.html
2178+
fn update_persisted_channel(&self, id: OutPoint, update: &ChannelMonitorUpdate, data: &ChannelMonitor<Keys>) -> Result<(), ChannelMonitorUpdateErr>;
2179+
}
2180+
21262181
const MAX_ALLOC_SIZE: usize = 64*1024;
21272182

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

0 commit comments

Comments
 (0)