@@ -29,8 +29,7 @@ use bitcoin::hash_types::Txid;
29
29
use chain;
30
30
use chain:: { Filter , WatchedOutput } ;
31
31
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 } ;
34
33
use chain:: transaction:: { OutPoint , TransactionData } ;
35
34
use chain:: keysinterface:: Sign ;
36
35
use util:: logger:: Logger ;
@@ -42,6 +41,57 @@ use prelude::*;
42
41
use sync:: RwLock ;
43
42
use core:: ops:: Deref ;
44
43
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 an update, and
89
+ /// [`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
+
45
95
/// An implementation of [`chain::Watch`] for monitoring channels.
46
96
///
47
97
/// 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
56
106
T :: Target : BroadcasterInterface ,
57
107
F :: Target : FeeEstimator ,
58
108
L :: Target : Logger ,
59
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
109
+ P :: Target : Persist < ChannelSigner > ,
60
110
{
61
111
/// The monitors
62
112
pub monitors : RwLock < HashMap < OutPoint , ChannelMonitor < ChannelSigner > > > ,
@@ -72,7 +122,7 @@ where C::Target: chain::Filter,
72
122
T :: Target : BroadcasterInterface ,
73
123
F :: Target : FeeEstimator ,
74
124
L :: Target : Logger ,
75
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
125
+ P :: Target : Persist < ChannelSigner > ,
76
126
{
77
127
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
78
128
/// of a channel and reacting accordingly based on transactions in the given chain data. See
@@ -183,7 +233,7 @@ where
183
233
T :: Target : BroadcasterInterface ,
184
234
F :: Target : FeeEstimator ,
185
235
L :: Target : Logger ,
186
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
236
+ P :: Target : Persist < ChannelSigner > ,
187
237
{
188
238
fn block_connected ( & self , block : & Block , height : u32 ) {
189
239
let header = & block. header ;
@@ -212,7 +262,7 @@ where
212
262
T :: Target : BroadcasterInterface ,
213
263
F :: Target : FeeEstimator ,
214
264
L :: Target : Logger ,
215
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
265
+ P :: Target : Persist < ChannelSigner > ,
216
266
{
217
267
fn transactions_confirmed ( & self , header : & BlockHeader , txdata : & TransactionData , height : u32 ) {
218
268
log_debug ! ( self . logger, "{} provided transactions confirmed at height {} in block {}" , txdata. len( ) , height, header. block_hash( ) ) ;
@@ -260,7 +310,7 @@ where C::Target: chain::Filter,
260
310
T :: Target : BroadcasterInterface ,
261
311
F :: Target : FeeEstimator ,
262
312
L :: Target : Logger ,
263
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
313
+ P :: Target : Persist < ChannelSigner > ,
264
314
{
265
315
/// Adds the monitor that watches the channel referred to by the given outpoint.
266
316
///
@@ -344,7 +394,7 @@ impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> even
344
394
T :: Target : BroadcasterInterface ,
345
395
F :: Target : FeeEstimator ,
346
396
L :: Target : Logger ,
347
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
397
+ P :: Target : Persist < ChannelSigner > ,
348
398
{
349
399
/// Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
350
400
///
0 commit comments