Skip to content

Commit 83c53bd

Browse files
committed
Move Persist trait to chainmonitor as that's the only reference
1 parent cce1291 commit 83c53bd

File tree

5 files changed

+87
-85
lines changed

5 files changed

+87
-85
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
use lightning::chain;
1212
use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
13-
use lightning::chain::chainmonitor::ChainMonitor;
14-
use lightning::chain::channelmonitor;
13+
use lightning::chain::chainmonitor::{ChainMonitor, Persist};
1514
use lightning::chain::keysinterface::{Sign, KeysInterface};
1615
use lightning::ln::channelmanager::ChannelManager;
1716
use lightning::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler};
@@ -192,7 +191,7 @@ impl BackgroundProcessor {
192191
K::Target: 'static + KeysInterface<Signer = Signer>,
193192
F::Target: 'static + FeeEstimator,
194193
L::Target: 'static + Logger,
195-
P::Target: 'static + channelmonitor::Persist<Signer>,
194+
P::Target: 'static + Persist<Signer>,
196195
CMH::Target: 'static + ChannelMessageHandler,
197196
RMH::Target: 'static + RoutingMessageHandler,
198197
UMH::Target: 'static + CustomMessageHandler,

lightning-persister/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::util::DiskWriteable;
1818
use lightning::chain;
1919
use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
2020
use lightning::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr};
21-
use lightning::chain::channelmonitor;
21+
use lightning::chain::chainmonitor;
2222
use lightning::chain::keysinterface::{Sign, KeysInterface};
2323
use lightning::chain::transaction::OutPoint;
2424
use lightning::ln::channelmanager::ChannelManager;
@@ -158,7 +158,7 @@ impl FilesystemPersister {
158158
}
159159
}
160160

161-
impl<ChannelSigner: Sign> channelmonitor::Persist<ChannelSigner> for FilesystemPersister {
161+
impl<ChannelSigner: Sign> chainmonitor::Persist<ChannelSigner> for FilesystemPersister {
162162
// TODO: We really need a way for the persister to inform the user that its time to crash/shut
163163
// down once these start returning failure.
164164
// A PermanentFailure implies we need to shut down since we're force-closing channels without
@@ -192,7 +192,8 @@ mod tests {
192192
use bitcoin::blockdata::block::{Block, BlockHeader};
193193
use bitcoin::hashes::hex::FromHex;
194194
use bitcoin::Txid;
195-
use lightning::chain::channelmonitor::{Persist, ChannelMonitorUpdateErr};
195+
use lightning::chain::chainmonitor::Persist;
196+
use lightning::chain::channelmonitor::ChannelMonitorUpdateErr;
196197
use lightning::chain::transaction::OutPoint;
197198
use lightning::{check_closed_broadcast, check_closed_event, check_added_monitors};
198199
use lightning::ln::features::InitFeatures;

lightning/src/chain/chainmonitor.rs

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ use bitcoin::hash_types::Txid;
2929
use chain;
3030
use chain::{Filter, WatchedOutput};
3131
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
32-
use chain::channelmonitor;
33-
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, Balance, MonitorEvent, Persist, TransactionOutputs};
32+
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, Balance, MonitorEvent, TransactionOutputs};
3433
use chain::transaction::{OutPoint, TransactionData};
3534
use chain::keysinterface::Sign;
3635
use util::logger::Logger;
@@ -42,6 +41,77 @@ use prelude::*;
4241
use sync::{RwLock, Mutex};
4342
use core::ops::Deref;
4443

44+
/// `Persist` defines behavior for persisting channel monitors: this could mean
45+
/// writing once to disk, and/or uploading to one or more backup services.
46+
///
47+
/// Note that for every new monitor, you **must** persist the new `ChannelMonitor`
48+
/// to disk/backups. And, on every update, you **must** persist either the
49+
/// `ChannelMonitorUpdate` or the updated monitor itself. Otherwise, there is risk
50+
/// of situations such as revoking a transaction, then crashing before this
51+
/// revocation can be persisted, then unintentionally broadcasting a revoked
52+
/// transaction and losing money. This is a risk because previous channel states
53+
/// are toxic, so it's important that whatever channel state is persisted is
54+
/// kept up-to-date.
55+
pub trait Persist<ChannelSigner: Sign> {
56+
/// Persist a new channel's data. The data can be stored any way you want, but
57+
/// the identifier provided by Rust-Lightning is the channel's outpoint (and
58+
/// it is up to you to maintain a correct mapping between the outpoint and the
59+
/// stored channel data). Note that you **must** persist every new monitor to
60+
/// disk. See the `Persist` trait documentation for more details.
61+
///
62+
/// See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`,
63+
/// and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
64+
///
65+
/// [`Writeable::write`]: crate::util::ser::Writeable::write
66+
fn persist_new_channel(&self, id: OutPoint, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
67+
68+
/// Update one channel's data. The provided `ChannelMonitor` has already
69+
/// applied the given update.
70+
///
71+
/// Note that on every update, you **must** persist either the
72+
/// `ChannelMonitorUpdate` or the updated monitor itself to disk/backups. See
73+
/// the `Persist` trait documentation for more details.
74+
///
75+
/// If an implementer chooses to persist the updates only, they need to make
76+
/// sure that all the updates are applied to the `ChannelMonitors` *before*
77+
/// the set of channel monitors is given to the `ChannelManager`
78+
/// deserialization routine. See [`ChannelMonitor::update_monitor`] for
79+
/// applying a monitor update to a monitor. If full `ChannelMonitors` are
80+
/// persisted, then there is no need to persist individual updates.
81+
///
82+
/// Note that there could be a performance tradeoff between persisting complete
83+
/// channel monitors on every update vs. persisting only updates and applying
84+
/// them in batches. The size of each monitor grows `O(number of state updates)`
85+
/// whereas updates are small and `O(1)`.
86+
///
87+
/// See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`,
88+
/// [`Writeable::write`] on [`ChannelMonitorUpdate`] for writing out a `ChannelMonitorUpdate`,
89+
/// and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
90+
///
91+
/// [`Writeable::write`]: crate::util::ser::Writeable::write
92+
fn update_persisted_channel(&self, id: OutPoint, update: &ChannelMonitorUpdate, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
93+
94+
/// Update one channel's data synchronously without a [`ChannelMonitorUpdate`].
95+
///
96+
/// This is called during block/transaction connection, and is a good time to synchronously
97+
/// remove all pending [`ChannelMonitorUpdate`]s which may have been persisted separately as an
98+
/// intent log.
99+
///
100+
/// Note that returning an error here irrevocably disables some processing in [`ChainMonitor`],
101+
/// preventing continued normal operation. Errors here are largely only useful to continue
102+
/// operation long enough to shut down.
103+
///
104+
/// Failures here do not imply the channel will be force-closed, however any future calls to
105+
/// [`update_persisted_channel`] after an error is returned here MUST either persist the full,
106+
/// updated [`ChannelMonitor`] provided to [`update_persisted_channel`] or return
107+
/// [`ChannelMonitorUpdateErr::PermanentFailure`], force-closing the channel. In other words,
108+
/// any future calls to [`update_persisted_channel`] after an error here MUST NOT persist the
109+
/// [`ChannelMonitorUpdate`] alone.
110+
///
111+
/// [`update_persisted_channel`]: Persist::update_persisted_channel
112+
fn sync_persisted_channel(&self, id: OutPoint, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ()>;
113+
}
114+
45115
/// An implementation of [`chain::Watch`] for monitoring channels.
46116
///
47117
/// Connected and disconnected blocks must be provided to `ChainMonitor` as documented by
@@ -56,7 +126,7 @@ pub struct ChainMonitor<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: De
56126
T::Target: BroadcasterInterface,
57127
F::Target: FeeEstimator,
58128
L::Target: Logger,
59-
P::Target: channelmonitor::Persist<ChannelSigner>,
129+
P::Target: Persist<ChannelSigner>,
60130
{
61131
/// The monitors
62132
pub monitors: RwLock<HashMap<OutPoint, ChannelMonitor<ChannelSigner>>>,
@@ -84,7 +154,7 @@ where C::Target: chain::Filter,
84154
T::Target: BroadcasterInterface,
85155
F::Target: FeeEstimator,
86156
L::Target: Logger,
87-
P::Target: channelmonitor::Persist<ChannelSigner>,
157+
P::Target: Persist<ChannelSigner>,
88158
{
89159
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
90160
/// of a channel and reacting accordingly based on transactions in the given chain data. See
@@ -213,7 +283,7 @@ where
213283
T::Target: BroadcasterInterface,
214284
F::Target: FeeEstimator,
215285
L::Target: Logger,
216-
P::Target: channelmonitor::Persist<ChannelSigner>,
286+
P::Target: Persist<ChannelSigner>,
217287
{
218288
fn block_connected(&self, block: &Block, height: u32) {
219289
let header = &block.header;
@@ -242,7 +312,7 @@ where
242312
T::Target: BroadcasterInterface,
243313
F::Target: FeeEstimator,
244314
L::Target: Logger,
245-
P::Target: channelmonitor::Persist<ChannelSigner>,
315+
P::Target: Persist<ChannelSigner>,
246316
{
247317
fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
248318
log_debug!(self.logger, "{} provided transactions confirmed at height {} in block {}", txdata.len(), height, header.block_hash());
@@ -290,7 +360,7 @@ where C::Target: chain::Filter,
290360
T::Target: BroadcasterInterface,
291361
F::Target: FeeEstimator,
292362
L::Target: Logger,
293-
P::Target: channelmonitor::Persist<ChannelSigner>,
363+
P::Target: Persist<ChannelSigner>,
294364
{
295365
/// Adds the monitor that watches the channel referred to by the given outpoint.
296366
///
@@ -381,7 +451,7 @@ impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> even
381451
T::Target: BroadcasterInterface,
382452
F::Target: FeeEstimator,
383453
L::Target: Logger,
384-
P::Target: channelmonitor::Persist<ChannelSigner>,
454+
P::Target: Persist<ChannelSigner>,
385455
{
386456
/// Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
387457
///

lightning/src/chain/channelmonitor.rs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,74 +2883,6 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
28832883
}
28842884
}
28852885

2886-
/// `Persist` defines behavior for persisting channel monitors: this could mean
2887-
/// writing once to disk, and/or uploading to one or more backup services.
2888-
///
2889-
/// Note that for every new monitor, you **must** persist the new `ChannelMonitor`
2890-
/// to disk/backups. And, on every update, you **must** persist either the
2891-
/// `ChannelMonitorUpdate` or the updated monitor itself. Otherwise, there is risk
2892-
/// of situations such as revoking a transaction, then crashing before this
2893-
/// revocation can be persisted, then unintentionally broadcasting a revoked
2894-
/// transaction and losing money. This is a risk because previous channel states
2895-
/// are toxic, so it's important that whatever channel state is persisted is
2896-
/// kept up-to-date.
2897-
pub trait Persist<ChannelSigner: Sign> {
2898-
/// Persist a new channel's data. The data can be stored any way you want, but
2899-
/// the identifier provided by Rust-Lightning is the channel's outpoint (and
2900-
/// it is up to you to maintain a correct mapping between the outpoint and the
2901-
/// stored channel data). Note that you **must** persist every new monitor to
2902-
/// disk. See the `Persist` trait documentation for more details.
2903-
///
2904-
/// See [`ChannelMonitor::write`] for writing out a `ChannelMonitor`,
2905-
/// and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
2906-
fn persist_new_channel(&self, id: OutPoint, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
2907-
2908-
/// Update one channel's data. The provided `ChannelMonitor` has already
2909-
/// applied the given update.
2910-
///
2911-
/// Note that on every update, you **must** persist either the
2912-
/// `ChannelMonitorUpdate` or the updated monitor itself to disk/backups. See
2913-
/// the `Persist` trait documentation for more details.
2914-
///
2915-
/// If an implementer chooses to persist the updates only, they need to make
2916-
/// sure that all the updates are applied to the `ChannelMonitors` *before*
2917-
/// the set of channel monitors is given to the `ChannelManager`
2918-
/// deserialization routine. See [`ChannelMonitor::update_monitor`] for
2919-
/// applying a monitor update to a monitor. If full `ChannelMonitors` are
2920-
/// persisted, then there is no need to persist individual updates.
2921-
///
2922-
/// Note that there could be a performance tradeoff between persisting complete
2923-
/// channel monitors on every update vs. persisting only updates and applying
2924-
/// them in batches. The size of each monitor grows `O(number of state updates)`
2925-
/// whereas updates are small and `O(1)`.
2926-
///
2927-
/// See [`ChannelMonitor::write`] for writing out a `ChannelMonitor`,
2928-
/// [`ChannelMonitorUpdate::write`] for writing out an update, and
2929-
/// [`ChannelMonitorUpdateErr`] for requirements when returning errors.
2930-
fn update_persisted_channel(&self, id: OutPoint, update: &ChannelMonitorUpdate, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
2931-
2932-
/// Update one channel's data synchronously without a [`ChannelMonitorUpdate`].
2933-
///
2934-
/// This is called during block/transaction connection, and is a good time to synchronously
2935-
/// remove all pending [`ChannelMonitorUpdate`]s which may have been persisted separately as an
2936-
/// intent log.
2937-
///
2938-
/// Note that returning an error here irrevocably disables some processing in [`ChainMonitor`],
2939-
/// preventing continued normal operation. Errors here are largely only useful to continue
2940-
/// operation long enough to shut down.
2941-
///
2942-
/// Failures here do not imply the channel will be force-closed, however any future calls to
2943-
/// [`update_persisted_channel`] after an error is returned here MUST either persist the full,
2944-
/// updated [`ChannelMonitor`] provided to [`update_persisted_channel`] or return
2945-
/// [`ChannelMonitorUpdateErr::PermanentFailure`], force-closing the channel. In other words,
2946-
/// any future calls to [`update_persisted_channel`] after an error here MUST NOT persist the
2947-
/// [`ChannelMonitorUpdate`] alone.
2948-
///
2949-
/// [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
2950-
/// [`update_persisted_channel`]: Persist::update_persisted_channel
2951-
fn sync_persisted_channel(&self, id: OutPoint, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ()>;
2952-
}
2953-
29542886
impl<Signer: Sign, T: Deref, F: Deref, L: Deref> chain::Listen for (ChannelMonitor<Signer>, T, F, L)
29552887
where
29562888
T::Target: BroadcasterInterface,

lightning/src/util/test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl keysinterface::KeysInterface for OnlyReadsKeysInterface {
8989
pub struct TestChainMonitor<'a> {
9090
pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor<EnforcingSigner>)>>,
9191
pub latest_monitor_update_id: Mutex<HashMap<[u8; 32], (OutPoint, u64)>>,
92-
pub chain_monitor: chainmonitor::ChainMonitor<EnforcingSigner, &'a TestChainSource, &'a chaininterface::BroadcasterInterface, &'a TestFeeEstimator, &'a TestLogger, &'a channelmonitor::Persist<EnforcingSigner>>,
92+
pub chain_monitor: chainmonitor::ChainMonitor<EnforcingSigner, &'a TestChainSource, &'a chaininterface::BroadcasterInterface, &'a TestFeeEstimator, &'a TestLogger, &'a chainmonitor::Persist<EnforcingSigner>>,
9393
pub keys_manager: &'a TestKeysInterface,
9494
pub update_ret: Mutex<Option<Result<(), channelmonitor::ChannelMonitorUpdateErr>>>,
9595
/// If this is set to Some(), after the next return, we'll always return this until update_ret
@@ -101,7 +101,7 @@ pub struct TestChainMonitor<'a> {
101101
pub expect_channel_force_closed: Mutex<Option<([u8; 32], bool)>>,
102102
}
103103
impl<'a> TestChainMonitor<'a> {
104-
pub fn new(chain_source: Option<&'a TestChainSource>, broadcaster: &'a chaininterface::BroadcasterInterface, logger: &'a TestLogger, fee_estimator: &'a TestFeeEstimator, persister: &'a channelmonitor::Persist<EnforcingSigner>, keys_manager: &'a TestKeysInterface) -> Self {
104+
pub fn new(chain_source: Option<&'a TestChainSource>, broadcaster: &'a chaininterface::BroadcasterInterface, logger: &'a TestLogger, fee_estimator: &'a TestFeeEstimator, persister: &'a chainmonitor::Persist<EnforcingSigner>, keys_manager: &'a TestKeysInterface) -> Self {
105105
Self {
106106
added_monitors: Mutex::new(Vec::new()),
107107
latest_monitor_update_id: Mutex::new(HashMap::new()),
@@ -195,7 +195,7 @@ impl TestPersister {
195195
*self.update_ret.lock().unwrap() = ret;
196196
}
197197
}
198-
impl<Signer: keysinterface::Sign> channelmonitor::Persist<Signer> for TestPersister {
198+
impl<Signer: keysinterface::Sign> chainmonitor::Persist<Signer> for TestPersister {
199199
fn persist_new_channel(&self, _funding_txo: OutPoint, _data: &channelmonitor::ChannelMonitor<Signer>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
200200
self.update_ret.lock().unwrap().clone()
201201
}

0 commit comments

Comments
 (0)