Skip to content

Commit 41acae8

Browse files
committed
Move Persist trait to chainmonitor as that's the only reference
1 parent cdb2132 commit 41acae8

File tree

7 files changed

+70
-67
lines changed

7 files changed

+70
-67
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-block-sync/src/init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ BlockSourceResult<ValidatedBlockHeader> {
4040
///
4141
/// use lightning::chain;
4242
/// use lightning::chain::Watch;
43+
/// use lightning::chain::chainmonitor;
4344
/// use lightning::chain::chainmonitor::ChainMonitor;
44-
/// use lightning::chain::channelmonitor;
4545
/// use lightning::chain::channelmonitor::ChannelMonitor;
4646
/// use lightning::chain::chaininterface::BroadcasterInterface;
4747
/// use lightning::chain::chaininterface::FeeEstimator;
@@ -65,7 +65,7 @@ BlockSourceResult<ValidatedBlockHeader> {
6565
/// F: FeeEstimator,
6666
/// L: Logger,
6767
/// C: chain::Filter,
68-
/// P: channelmonitor::Persist<S>,
68+
/// P: chainmonitor::Persist<S>,
6969
/// >(
7070
/// block_source: &mut B,
7171
/// chain_monitor: &ChainMonitor<S, &C, &T, &F, &L, &P>,

lightning-net-tokio/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
//! type Logger = dyn lightning::util::logger::Logger + Send + Sync;
3535
//! type ChainAccess = dyn lightning::chain::Access + Send + Sync;
3636
//! type ChainFilter = dyn lightning::chain::Filter + Send + Sync;
37-
//! type DataPersister = dyn lightning::chain::channelmonitor::Persist<lightning::chain::keysinterface::InMemorySigner> + Send + Sync;
37+
//! type DataPersister = dyn lightning::chain::chainmonitor::Persist<lightning::chain::keysinterface::InMemorySigner> + Send + Sync;
3838
//! type ChainMonitor = lightning::chain::chainmonitor::ChainMonitor<lightning::chain::keysinterface::InMemorySigner, Arc<ChainFilter>, Arc<TxBroadcaster>, Arc<FeeEstimator>, Arc<Logger>, Arc<DataPersister>>;
3939
//! type ChannelManager = Arc<lightning::ln::channelmanager::SimpleArcChannelManager<ChainMonitor, TxBroadcaster, FeeEstimator, Logger>>;
4040
//! type PeerManager = Arc<lightning::ln::peer_handler::SimpleArcPeerManager<lightning_net_tokio::SocketDescriptor, ChainMonitor, TxBroadcaster, FeeEstimator, ChainAccess, Logger>>;

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
@@ -185,7 +185,8 @@ mod tests {
185185
use bitcoin::blockdata::block::{Block, BlockHeader};
186186
use bitcoin::hashes::hex::FromHex;
187187
use bitcoin::Txid;
188-
use lightning::chain::channelmonitor::{Persist, ChannelMonitorUpdateErr};
188+
use lightning::chain::chainmonitor::Persist;
189+
use lightning::chain::channelmonitor::ChannelMonitorUpdateErr;
189190
use lightning::chain::transaction::OutPoint;
190191
use lightning::{check_closed_broadcast, check_closed_event, check_added_monitors};
191192
use lightning::ln::features::InitFeatures;

lightning/src/chain/chainmonitor.rs

Lines changed: 58 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, MonitorUpdated, Persist, TransactionOutputs};
32+
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, Balance, MonitorEvent, MonitorUpdated, TransactionOutputs};
3433
use chain::transaction::{OutPoint, TransactionData};
3534
use chain::keysinterface::Sign;
3635
use util::logger::Logger;
@@ -42,6 +41,57 @@ 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: &Option<ChannelMonitorUpdate>, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
93+
}
94+
4595
/// An implementation of [`chain::Watch`] for monitoring channels.
4696
///
4797
/// Connected and disconnected blocks must be provided to `ChainMonitor` as documented by
@@ -56,7 +106,7 @@ pub struct ChainMonitor<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: De
56106
T::Target: BroadcasterInterface,
57107
F::Target: FeeEstimator,
58108
L::Target: Logger,
59-
P::Target: channelmonitor::Persist<ChannelSigner>,
109+
P::Target: Persist<ChannelSigner>,
60110
{
61111
/// The monitors
62112
pub monitors: RwLock<HashMap<OutPoint, ChannelMonitor<ChannelSigner>>>,
@@ -87,7 +137,7 @@ where C::Target: chain::Filter,
87137
T::Target: BroadcasterInterface,
88138
F::Target: FeeEstimator,
89139
L::Target: Logger,
90-
P::Target: channelmonitor::Persist<ChannelSigner>,
140+
P::Target: Persist<ChannelSigner>,
91141
{
92142
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
93143
/// of a channel and reacting accordingly based on transactions in the given chain data. See
@@ -249,7 +299,7 @@ where
249299
T::Target: BroadcasterInterface,
250300
F::Target: FeeEstimator,
251301
L::Target: Logger,
252-
P::Target: channelmonitor::Persist<ChannelSigner>,
302+
P::Target: Persist<ChannelSigner>,
253303
{
254304
fn block_connected(&self, block: &Block, height: u32) {
255305
let header = &block.header;
@@ -278,7 +328,7 @@ where
278328
T::Target: BroadcasterInterface,
279329
F::Target: FeeEstimator,
280330
L::Target: Logger,
281-
P::Target: channelmonitor::Persist<ChannelSigner>,
331+
P::Target: Persist<ChannelSigner>,
282332
{
283333
fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
284334
log_debug!(self.logger, "{} provided transactions confirmed at height {} in block {}", txdata.len(), height, header.block_hash());
@@ -326,7 +376,7 @@ where C::Target: chain::Filter,
326376
T::Target: BroadcasterInterface,
327377
F::Target: FeeEstimator,
328378
L::Target: Logger,
329-
P::Target: channelmonitor::Persist<ChannelSigner>,
379+
P::Target: Persist<ChannelSigner>,
330380
{
331381
/// Adds the monitor that watches the channel referred to by the given outpoint.
332382
///
@@ -421,7 +471,7 @@ impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> even
421471
T::Target: BroadcasterInterface,
422472
F::Target: FeeEstimator,
423473
L::Target: Logger,
424-
P::Target: channelmonitor::Persist<ChannelSigner>,
474+
P::Target: Persist<ChannelSigner>,
425475
{
426476
/// Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
427477
///

lightning/src/chain/channelmonitor.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,53 +2918,6 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29182918
}
29192919
}
29202920

2921-
/// `Persist` defines behavior for persisting channel monitors: this could mean
2922-
/// writing once to disk, and/or uploading to one or more backup services.
2923-
///
2924-
/// Note that for every new monitor, you **must** persist the new `ChannelMonitor`
2925-
/// to disk/backups. And, on every update, you **must** persist either the
2926-
/// `ChannelMonitorUpdate` or the updated monitor itself. Otherwise, there is risk
2927-
/// of situations such as revoking a transaction, then crashing before this
2928-
/// revocation can be persisted, then unintentionally broadcasting a revoked
2929-
/// transaction and losing money. This is a risk because previous channel states
2930-
/// are toxic, so it's important that whatever channel state is persisted is
2931-
/// kept up-to-date.
2932-
pub trait Persist<ChannelSigner: Sign> {
2933-
/// Persist a new channel's data. The data can be stored any way you want, but
2934-
/// the identifier provided by Rust-Lightning is the channel's outpoint (and
2935-
/// it is up to you to maintain a correct mapping between the outpoint and the
2936-
/// stored channel data). Note that you **must** persist every new monitor to
2937-
/// disk. See the `Persist` trait documentation for more details.
2938-
///
2939-
/// See [`ChannelMonitor::write`] for writing out a `ChannelMonitor`,
2940-
/// and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
2941-
fn persist_new_channel(&self, id: OutPoint, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
2942-
2943-
/// Update one channel's data. The provided `ChannelMonitor` has already
2944-
/// applied the given update.
2945-
///
2946-
/// Note that on every update, you **must** persist either the
2947-
/// `ChannelMonitorUpdate` or the updated monitor itself to disk/backups. See
2948-
/// the `Persist` trait documentation for more details.
2949-
///
2950-
/// If an implementer chooses to persist the updates only, they need to make
2951-
/// sure that all the updates are applied to the `ChannelMonitors` *before*
2952-
/// the set of channel monitors is given to the `ChannelManager`
2953-
/// deserialization routine. See [`ChannelMonitor::update_monitor`] for
2954-
/// applying a monitor update to a monitor. If full `ChannelMonitors` are
2955-
/// persisted, then there is no need to persist individual updates.
2956-
///
2957-
/// Note that there could be a performance tradeoff between persisting complete
2958-
/// channel monitors on every update vs. persisting only updates and applying
2959-
/// them in batches. The size of each monitor grows `O(number of state updates)`
2960-
/// whereas updates are small and `O(1)`.
2961-
///
2962-
/// See [`ChannelMonitor::write`] for writing out a `ChannelMonitor`,
2963-
/// [`ChannelMonitorUpdate::write`] for writing out an update, and
2964-
/// [`ChannelMonitorUpdateErr`] for requirements when returning errors.
2965-
fn update_persisted_channel(&self, id: OutPoint, update: &Option<ChannelMonitorUpdate>, data: &ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
2966-
}
2967-
29682921
impl<Signer: Sign, T: Deref, F: Deref, L: Deref> chain::Listen for (ChannelMonitor<Signer>, T, F, L)
29692922
where
29702923
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)