@@ -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,77 @@ use prelude::*;
42
41
use sync:: { RwLock , Mutex } ;
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 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
+
45
115
/// An implementation of [`chain::Watch`] for monitoring channels.
46
116
///
47
117
/// 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
56
126
T :: Target : BroadcasterInterface ,
57
127
F :: Target : FeeEstimator ,
58
128
L :: Target : Logger ,
59
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
129
+ P :: Target : Persist < ChannelSigner > ,
60
130
{
61
131
/// The monitors
62
132
pub monitors : RwLock < HashMap < OutPoint , ChannelMonitor < ChannelSigner > > > ,
@@ -84,7 +154,7 @@ where C::Target: chain::Filter,
84
154
T :: Target : BroadcasterInterface ,
85
155
F :: Target : FeeEstimator ,
86
156
L :: Target : Logger ,
87
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
157
+ P :: Target : Persist < ChannelSigner > ,
88
158
{
89
159
/// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
90
160
/// of a channel and reacting accordingly based on transactions in the given chain data. See
@@ -213,7 +283,7 @@ where
213
283
T :: Target : BroadcasterInterface ,
214
284
F :: Target : FeeEstimator ,
215
285
L :: Target : Logger ,
216
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
286
+ P :: Target : Persist < ChannelSigner > ,
217
287
{
218
288
fn block_connected ( & self , block : & Block , height : u32 ) {
219
289
let header = & block. header ;
@@ -242,7 +312,7 @@ where
242
312
T :: Target : BroadcasterInterface ,
243
313
F :: Target : FeeEstimator ,
244
314
L :: Target : Logger ,
245
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
315
+ P :: Target : Persist < ChannelSigner > ,
246
316
{
247
317
fn transactions_confirmed ( & self , header : & BlockHeader , txdata : & TransactionData , height : u32 ) {
248
318
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,
290
360
T :: Target : BroadcasterInterface ,
291
361
F :: Target : FeeEstimator ,
292
362
L :: Target : Logger ,
293
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
363
+ P :: Target : Persist < ChannelSigner > ,
294
364
{
295
365
/// Adds the monitor that watches the channel referred to by the given outpoint.
296
366
///
@@ -381,7 +451,7 @@ impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> even
381
451
T :: Target : BroadcasterInterface ,
382
452
F :: Target : FeeEstimator ,
383
453
L :: Target : Logger ,
384
- P :: Target : channelmonitor :: Persist < ChannelSigner > ,
454
+ P :: Target : Persist < ChannelSigner > ,
385
455
{
386
456
/// Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
387
457
///
0 commit comments