|
| 1 | +//! Logic for persisting data from ChannelMonitors on-disk. Sample data |
| 2 | +//! persisters are separated into the lightning-persist-data crate. These |
| 3 | +//! objects mainly interface with the ChainMonitor when a channel monitor is |
| 4 | +//! added or updated, and when they are all synced on startup. |
| 5 | +use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr}; |
| 6 | +use chain::keysinterface::ChannelKeys; |
| 7 | +use chain::transaction::OutPoint; |
| 8 | + |
| 9 | +/// ChannelDataPersister is responsible for persisting channel data: this could |
| 10 | +/// mean writing once to disk, and/or uploading to one or more backup services. |
| 11 | +/// |
| 12 | +/// Note that for every new monitor, you **must** persist the new ChannelMonitor |
| 13 | +/// to disk/backups. And, on every update, you **must** persist either the |
| 14 | +/// ChannelMonitorUpdate or the updated monitor itself. Otherwise, there is risk |
| 15 | +/// of situations such as revoking a transaction, then crashing before this |
| 16 | +/// revocation can be persisted, then unintentionally broadcasting a revoked |
| 17 | +/// transaction and losing money. This is a risk because previous channel states |
| 18 | +/// are toxic, so it's important that whatever channel state is persisted is |
| 19 | +/// kept up-to-date. |
| 20 | +/// |
| 21 | +/// Given multiple backups, situations may arise where one or more backup sources |
| 22 | +/// have fallen behind or disagree on the current state. Because these backup |
| 23 | +/// sources don't have any transaction signing/broadcasting duties and are only |
| 24 | +/// supposed to be persisting bytes of data, backup sources may be considered |
| 25 | +/// "in consensus" if a majority of them agree on the current state and are on |
| 26 | +/// the highest known commitment number. |
| 27 | +pub trait ChannelDataPersister: Send + Sync { |
| 28 | + /// The concrete type which signs for transactions and provides access to our channel public |
| 29 | + /// keys. |
| 30 | + type Keys: ChannelKeys; |
| 31 | + |
| 32 | + /// Persist a new channel's data. The data can be stored with any file |
| 33 | + /// name/path, but the identifier provided is the channel's outpoint. |
| 34 | + /// Note that you **must** persist every new monitor to disk. See the |
| 35 | + /// ChannelDataPersister trait documentation for more details, and for |
| 36 | + /// information on consensus between backups. |
| 37 | + /// |
| 38 | + /// See [`ChannelMonitor::write_for_disk`] for writing out a ChannelMonitor. |
| 39 | + /// |
| 40 | + /// [`update_id`]: struct.ChannelMonitorUpdate.html#structfield.update_id |
| 41 | + /// [`ChannelMonitor::write_for_disk`]: ../../chain/channelmonitor/struct.ChannelMonitor.html#method.write_for_disk |
| 42 | + fn persist_channel_data(&self, id: OutPoint, data: &ChannelMonitor<Self::Keys>) -> Result<(), ChannelMonitorUpdateErr>; |
| 43 | + |
| 44 | + /// Update one channel's data. The provided ChannelMonitor has already |
| 45 | + /// applied the given update. |
| 46 | + /// |
| 47 | + /// Note that on every update, you **must** persist either the |
| 48 | + /// ChannelMonitorUpdate or the updated monitor itself to disk/backups. |
| 49 | + /// See the ChannelDataPersister trait documentation for more details, and |
| 50 | + /// for information on consensus between backups. |
| 51 | + /// |
| 52 | + /// If an implementer chooses to persist the updates only, they need to make |
| 53 | + /// sure that all the updates are applied to the ChannelMonitors *before* the |
| 54 | + /// set of channel monitors is given to the ChainMonitor at startup time |
| 55 | + /// (e.g., all monitors returned by [`load_channel_data`] must be up-to-date). |
| 56 | + /// If full ChannelMonitors are persisted, then there is no need to persist |
| 57 | + /// individual updates. |
| 58 | + /// |
| 59 | + /// Note that there could be a performance tradeoff between persisting complete |
| 60 | + /// channel monitors on every update vs. persisting only updates and applying |
| 61 | + /// them in batches. |
| 62 | + /// |
| 63 | + /// See [`ChannelMonitor::write_for_disk`] for writing out a ChannelMonitor |
| 64 | + /// and [`ChannelMonitorUpdate::write`] for writing out an update. |
| 65 | + /// |
| 66 | + /// [`load_channel_data`]: trait.ChannelDataPersister.html#tymethod.load_channel_data |
| 67 | + /// [`ChannelMonitor::write_for_disk`]: ../../chain/channelmonitor/struct.ChannelMonitor.html#method.write_for_disk |
| 68 | + /// [`ChannelMonitorUpdate::write`]: ../../chain/channelmonitor/struct.ChannelMonitorUpdate.html#method.write |
| 69 | + fn update_channel_data(&self, id: OutPoint, update: &ChannelMonitorUpdate, data: &ChannelMonitor<Self::Keys>) -> Result<(), ChannelMonitorUpdateErr>; |
| 70 | +} |
0 commit comments