@@ -47,46 +47,6 @@ use core::ops::Deref;
47
47
use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
48
48
use bitcoin:: secp256k1:: PublicKey ;
49
49
50
- mod update_origin {
51
- #[ derive( Debug , Clone , Copy , Hash , PartialEq , Eq ) ]
52
- /// A specific update's ID stored in a `MonitorUpdateId`, separated out to make the contents
53
- /// entirely opaque.
54
- pub ( crate ) enum UpdateOrigin {
55
- /// An update that was generated by the `ChannelManager` (via our [`crate::chain::Watch`]
56
- /// implementation). This corresponds to an actual [ChannelMonitorUpdate::update_id] field
57
- /// and [ChannelMonitor::get_latest_update_id].
58
- ///
59
- /// [ChannelMonitor::get_latest_update_id]: crate::chain::channelmonitor::ChannelMonitor::get_latest_update_id
60
- /// [ChannelMonitorUpdate::update_id]: crate::chain::channelmonitor::ChannelMonitorUpdate::update_id
61
- OffChain ( u64 ) ,
62
- /// An update that was generated during blockchain processing. The ID here is specific to the
63
- /// generating [ChannelMonitor] and does *not* correspond to any on-disk IDs.
64
- ///
65
- /// [ChannelMonitor]: crate::chain::channelmonitor::ChannelMonitor
66
- ChainSync ( u64 ) ,
67
- }
68
- }
69
-
70
- #[ cfg( any( feature = "_test_utils" , test) ) ]
71
- pub ( crate ) use update_origin:: UpdateOrigin ;
72
- #[ cfg( not( any( feature = "_test_utils" , test) ) ) ]
73
- use update_origin:: UpdateOrigin ;
74
-
75
- /// An opaque identifier describing a specific [`Persist`] method call.
76
- #[ derive( Debug , Clone , Copy , Hash , PartialEq , Eq ) ]
77
- pub struct MonitorUpdateId {
78
- pub ( crate ) contents : UpdateOrigin ,
79
- }
80
-
81
- impl MonitorUpdateId {
82
- pub ( crate ) fn from_monitor_update ( update : & ChannelMonitorUpdate ) -> Self {
83
- Self { contents : UpdateOrigin :: OffChain ( update. update_id ) }
84
- }
85
- pub ( crate ) fn from_new_monitor < ChannelSigner : WriteableEcdsaChannelSigner > ( monitor : & ChannelMonitor < ChannelSigner > ) -> Self {
86
- Self { contents : UpdateOrigin :: OffChain ( monitor. get_latest_update_id ( ) ) }
87
- }
88
- }
89
-
90
50
/// `Persist` defines behavior for persisting channel monitors: this could mean
91
51
/// writing once to disk, and/or uploading to one or more backup services.
92
52
///
@@ -105,7 +65,9 @@ impl MonitorUpdateId {
105
65
/// [`ChainMonitor::get_monitor`].
106
66
///
107
67
/// Once a full [`ChannelMonitor`] has been persisted, all pending updates for that channel can
108
- /// be marked as complete via [`ChainMonitor::channel_monitor_updated`].
68
+ /// be marked as complete via [`ChainMonitor::channel_monitor_updated`]. It is only necessary to
69
+ /// call [`ChainMonitor::channel_monitor_updated`] when an `update_id` is provided and when you
70
+ /// return [`ChannelMonitorUpdateStatus::InProgress`].
109
71
///
110
72
/// If at some point no further progress can be made towards persisting the pending updates, the
111
73
/// node should simply shut down.
@@ -117,9 +79,9 @@ impl MonitorUpdateId {
117
79
/// # For those implementing asynchronous persistence
118
80
///
119
81
/// All calls should generally spawn a background task and immediately return
120
- /// [`ChannelMonitorUpdateStatus::InProgress`]. Once the update completes,
121
- /// [`ChainMonitor::channel_monitor_updated`] should be called with the corresponding
122
- /// [`MonitorUpdateId `].
82
+ /// [`ChannelMonitorUpdateStatus::InProgress`]. Once the update completes, it is only necessary to
83
+ /// call [`ChainMonitor::channel_monitor_updated`] when an `update_id` is provided and when you
84
+ /// return [`ChannelMonitorUpdateStatus::InProgress `].
123
85
///
124
86
/// Note that unlike the direct [`chain::Watch`] interface,
125
87
/// [`ChainMonitor::channel_monitor_updated`] must be called once for *each* update which occurs.
@@ -150,15 +112,16 @@ pub trait Persist<ChannelSigner: WriteableEcdsaChannelSigner> {
150
112
/// channel's outpoint (and it is up to you to maintain a correct mapping between the outpoint
151
113
/// and the stored channel data). Note that you **must** persist every new monitor to disk.
152
114
///
153
- /// The `update_id` is used to identify this call to [`ChainMonitor::channel_monitor_updated`],
154
- /// if you return [`ChannelMonitorUpdateStatus::InProgress`].
115
+ /// The `update_id` uniquely identifies a call to [`ChainMonitor::channel_monitor_updated`].
116
+ /// It is only necessary to call [`ChainMonitor::channel_monitor_updated`] when an `update_id` is
117
+ /// provided and when you return [`ChannelMonitorUpdateStatus::InProgress`].
155
118
///
156
119
/// See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`
157
120
/// and [`ChannelMonitorUpdateStatus`] for requirements when returning errors.
158
121
///
159
122
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
160
123
/// [`Writeable::write`]: crate::util::ser::Writeable::write
161
- fn persist_new_channel ( & self , channel_funding_outpoint : OutPoint , data : & ChannelMonitor < ChannelSigner > , update_id : MonitorUpdateId ) -> ChannelMonitorUpdateStatus ;
124
+ fn persist_new_channel ( & self , channel_funding_outpoint : OutPoint , data : & ChannelMonitor < ChannelSigner > ) -> ChannelMonitorUpdateStatus ;
162
125
163
126
/// Update one channel's data. The provided [`ChannelMonitor`] has already applied the given
164
127
/// update.
@@ -185,15 +148,16 @@ pub trait Persist<ChannelSigner: WriteableEcdsaChannelSigner> {
185
148
/// them in batches. The size of each monitor grows `O(number of state updates)`
186
149
/// whereas updates are small and `O(1)`.
187
150
///
188
- /// The `update_id` is used to identify this call to [`ChainMonitor::channel_monitor_updated`],
189
- /// if you return [`ChannelMonitorUpdateStatus::InProgress`].
151
+ /// The `update_id` uniquely identifies a call to [`ChainMonitor::channel_monitor_updated`].
152
+ /// It is only necessary to call [`ChainMonitor::channel_monitor_updated`] when an `update_id` is
153
+ /// provided and when you return [`ChannelMonitorUpdateStatus::InProgress`].
190
154
///
191
155
/// See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`,
192
156
/// [`Writeable::write`] on [`ChannelMonitorUpdate`] for writing out an update, and
193
157
/// [`ChannelMonitorUpdateStatus`] for requirements when returning errors.
194
158
///
195
159
/// [`Writeable::write`]: crate::util::ser::Writeable::write
196
- fn update_persisted_channel ( & self , channel_funding_outpoint : OutPoint , update : Option < & ChannelMonitorUpdate > , data : & ChannelMonitor < ChannelSigner > , update_id : MonitorUpdateId ) -> ChannelMonitorUpdateStatus ;
160
+ fn update_persisted_channel ( & self , channel_funding_outpoint : OutPoint , update : Option < & ChannelMonitorUpdate > , data : & ChannelMonitor < ChannelSigner > ) -> ChannelMonitorUpdateStatus ;
197
161
/// Prevents the channel monitor from being loaded on startup.
198
162
///
199
163
/// Archiving the data in a backup location (rather than deleting it fully) is useful for
@@ -209,13 +173,12 @@ struct MonitorHolder<ChannelSigner: WriteableEcdsaChannelSigner> {
209
173
/// update_persisted_channel, the user returns a
210
174
/// [`ChannelMonitorUpdateStatus::InProgress`], and then calls channel_monitor_updated
211
175
/// immediately, racing our insertion of the pending update into the contained Vec.
212
- pending_monitor_updates : Mutex < Vec < MonitorUpdateId > > ,
176
+ pending_monitor_updates : Mutex < Vec < u64 > > ,
213
177
}
214
178
215
179
impl < ChannelSigner : WriteableEcdsaChannelSigner > MonitorHolder < ChannelSigner > {
216
- fn has_pending_offchain_updates ( & self , pending_monitor_updates_lock : & MutexGuard < Vec < MonitorUpdateId > > ) -> bool {
217
- pending_monitor_updates_lock. iter ( ) . any ( |update_id|
218
- if let UpdateOrigin :: OffChain ( _) = update_id. contents { true } else { false } )
180
+ fn has_pending_updates ( & self , pending_monitor_updates_lock : & MutexGuard < Vec < u64 > > ) -> bool {
181
+ !pending_monitor_updates_lock. is_empty ( )
219
182
}
220
183
}
221
184
@@ -346,20 +309,11 @@ where C::Target: chain::Filter,
346
309
let mut txn_outputs;
347
310
{
348
311
txn_outputs = process ( monitor, txdata) ;
349
- let chain_sync_update_id = self . sync_persistence_id . get_increment ( ) ;
350
- let update_id = MonitorUpdateId {
351
- contents : UpdateOrigin :: ChainSync ( chain_sync_update_id) ,
352
- } ;
353
-
354
- log_trace ! ( logger, "Syncing Channel Monitor for channel {} for block-data update_id {}" ,
355
- log_funding_info!( monitor) ,
356
- chain_sync_update_id
357
- ) ;
358
- match self . persister . update_persisted_channel ( * funding_outpoint, None , monitor, update_id) {
312
+ log_trace ! ( logger, "Syncing Channel Monitor for channel {}" , log_funding_info!( monitor) ) ;
313
+ match self . persister . update_persisted_channel ( * funding_outpoint, None , monitor) {
359
314
ChannelMonitorUpdateStatus :: Completed =>
360
- log_trace ! ( logger, "Finished syncing Channel Monitor for channel {} for block-data update_id {}" ,
361
- log_funding_info!( monitor) ,
362
- chain_sync_update_id
315
+ log_trace ! ( logger, "Finished syncing Channel Monitor for channel {} for block-data" ,
316
+ log_funding_info!( monitor)
363
317
) ,
364
318
ChannelMonitorUpdateStatus :: InProgress => {
365
319
log_debug ! ( logger, "Channel Monitor sync for channel {} in progress." , log_funding_info!( monitor) ) ;
@@ -464,15 +418,15 @@ where C::Target: chain::Filter,
464
418
465
419
#[ cfg( not( c_bindings) ) ]
466
420
/// Lists the pending updates for each [`ChannelMonitor`] (by `OutPoint` being monitored).
467
- pub fn list_pending_monitor_updates ( & self ) -> HashMap < OutPoint , Vec < MonitorUpdateId > > {
421
+ pub fn list_pending_monitor_updates ( & self ) -> HashMap < OutPoint , Vec < u64 > > {
468
422
hash_map_from_iter ( self . monitors . read ( ) . unwrap ( ) . iter ( ) . map ( |( outpoint, holder) | {
469
423
( * outpoint, holder. pending_monitor_updates . lock ( ) . unwrap ( ) . clone ( ) )
470
424
} ) )
471
425
}
472
426
473
427
#[ cfg( c_bindings) ]
474
428
/// Lists the pending updates for each [`ChannelMonitor`] (by `OutPoint` being monitored).
475
- pub fn list_pending_monitor_updates ( & self ) -> Vec < ( OutPoint , Vec < MonitorUpdateId > ) > {
429
+ pub fn list_pending_monitor_updates ( & self ) -> Vec < ( OutPoint , Vec < u64 > ) > {
476
430
self . monitors . read ( ) . unwrap ( ) . iter ( ) . map ( |( outpoint, holder) | {
477
431
( * outpoint, holder. pending_monitor_updates . lock ( ) . unwrap ( ) . clone ( ) )
478
432
} ) . collect ( )
@@ -500,47 +454,43 @@ where C::Target: chain::Filter,
500
454
///
501
455
/// Returns an [`APIError::APIMisuseError`] if `funding_txo` does not match any currently
502
456
/// registered [`ChannelMonitor`]s.
503
- pub fn channel_monitor_updated ( & self , funding_txo : OutPoint , completed_update_id : MonitorUpdateId ) -> Result < ( ) , APIError > {
457
+ pub fn channel_monitor_updated ( & self , funding_txo : OutPoint , completed_update_id : u64 ) -> Result < ( ) , APIError > {
504
458
let monitors = self . monitors . read ( ) . unwrap ( ) ;
505
459
let monitor_data = if let Some ( mon) = monitors. get ( & funding_txo) { mon } else {
506
460
return Err ( APIError :: APIMisuseError { err : format ! ( "No ChannelMonitor matching funding outpoint {:?} found" , funding_txo) } ) ;
507
461
} ;
508
462
let mut pending_monitor_updates = monitor_data. pending_monitor_updates . lock ( ) . unwrap ( ) ;
509
463
pending_monitor_updates. retain ( |update_id| * update_id != completed_update_id) ;
510
464
511
- match completed_update_id {
512
- MonitorUpdateId { contents : UpdateOrigin :: OffChain ( completed_update_id) } => {
513
- // Note that we only check for `UpdateOrigin::OffChain` failures here - if
514
- // we're being told that a `UpdateOrigin::OffChain` monitor update completed,
515
- // we only care about ensuring we don't tell the `ChannelManager` to restore
516
- // the channel to normal operation until all `UpdateOrigin::OffChain` updates
517
- // complete.
518
- // If there's some `UpdateOrigin::ChainSync` update still pending that's okay
519
- // - we can still update our channel state, just as long as we don't return
520
- // `MonitorEvent`s from the monitor back to the `ChannelManager` until they
521
- // complete.
522
- let monitor_is_pending_updates = monitor_data. has_pending_offchain_updates ( & pending_monitor_updates) ;
523
- log_debug ! ( self . logger, "Completed off-chain monitor update {} for channel with funding outpoint {:?}, {}" ,
524
- completed_update_id,
525
- funding_txo,
526
- if monitor_is_pending_updates {
527
- "still have pending off-chain updates"
528
- } else {
529
- "all off-chain updates complete, returning a MonitorEvent"
530
- } ) ;
531
- if monitor_is_pending_updates {
532
- // If there are still monitor updates pending, we cannot yet construct a
533
- // Completed event.
534
- return Ok ( ( ) ) ;
535
- }
536
- let channel_id = monitor_data. monitor . channel_id ( ) ;
537
- self . pending_monitor_events . lock ( ) . unwrap ( ) . push ( ( funding_txo, channel_id, vec ! [ MonitorEvent :: Completed {
538
- funding_txo, channel_id,
539
- monitor_update_id: monitor_data. monitor. get_latest_update_id( ) ,
540
- } ] , monitor_data. monitor . get_counterparty_node_id ( ) ) ) ;
541
- } ,
542
- MonitorUpdateId { contents : UpdateOrigin :: ChainSync ( _) } => { } ,
465
+ // Note that we only check for `UpdateOrigin::OffChain` failures here - if
466
+ // we're being told that a `UpdateOrigin::OffChain` monitor update completed,
467
+ // we only care about ensuring we don't tell the `ChannelManager` to restore
468
+ // the channel to normal operation until all `UpdateOrigin::OffChain` updates
469
+ // complete.
470
+ // If there's some `UpdateOrigin::ChainSync` update still pending that's okay
471
+ // - we can still update our channel state, just as long as we don't return
472
+ // `MonitorEvent`s from the monitor back to the `ChannelManager` until they
473
+ // complete.
474
+ let monitor_is_pending_updates = monitor_data. has_pending_updates ( & pending_monitor_updates) ;
475
+ log_debug ! ( self . logger, "Completed off-chain monitor update {} for channel with funding outpoint {:?}, {}" ,
476
+ completed_update_id,
477
+ funding_txo,
478
+ if monitor_is_pending_updates {
479
+ "still have pending off-chain updates"
480
+ } else {
481
+ "all off-chain updates complete, returning a MonitorEvent"
482
+ } ) ;
483
+ if monitor_is_pending_updates {
484
+ // If there are still monitor updates pending, we cannot yet construct a
485
+ // Completed event.
486
+ return Ok ( ( ) ) ;
543
487
}
488
+ let channel_id = monitor_data. monitor . channel_id ( ) ;
489
+ self . pending_monitor_events . lock ( ) . unwrap ( ) . push ( ( funding_txo, channel_id, vec ! [ MonitorEvent :: Completed {
490
+ funding_txo, channel_id,
491
+ monitor_update_id: monitor_data. monitor. get_latest_update_id( ) ,
492
+ } ] , monitor_data. monitor . get_counterparty_node_id ( ) ) ) ;
493
+
544
494
self . event_notifier . notify ( ) ;
545
495
Ok ( ( ) )
546
496
}
@@ -771,9 +721,9 @@ where C::Target: chain::Filter,
771
721
hash_map:: Entry :: Vacant ( e) => e,
772
722
} ;
773
723
log_trace ! ( logger, "Got new ChannelMonitor for channel {}" , log_funding_info!( monitor) ) ;
774
- let update_id = MonitorUpdateId :: from_new_monitor ( & monitor) ;
724
+ let update_id = monitor. get_latest_update_id ( ) ;
775
725
let mut pending_monitor_updates = Vec :: new ( ) ;
776
- let persist_res = self . persister . persist_new_channel ( funding_outpoint, & monitor, update_id ) ;
726
+ let persist_res = self . persister . persist_new_channel ( funding_outpoint, & monitor) ;
777
727
match persist_res {
778
728
ChannelMonitorUpdateStatus :: InProgress => {
779
729
log_info ! ( logger, "Persistence of new ChannelMonitor for channel {} in progress" , log_funding_info!( monitor) ) ;
@@ -823,7 +773,7 @@ where C::Target: chain::Filter,
823
773
log_trace ! ( logger, "Updating ChannelMonitor to id {} for channel {}" , update. update_id, log_funding_info!( monitor) ) ;
824
774
let update_res = monitor. update_monitor ( update, & self . broadcaster , & self . fee_estimator , & self . logger ) ;
825
775
826
- let update_id = MonitorUpdateId :: from_monitor_update ( update) ;
776
+ let update_id = update. update_id ;
827
777
let mut pending_monitor_updates = monitor_state. pending_monitor_updates . lock ( ) . unwrap ( ) ;
828
778
let persist_res = if update_res. is_err ( ) {
829
779
// Even if updating the monitor returns an error, the monitor's state will
@@ -832,9 +782,9 @@ where C::Target: chain::Filter,
832
782
// while reading `channel_monitor` with updates from storage. Instead, we should persist
833
783
// the entire `channel_monitor` here.
834
784
log_warn ! ( logger, "Failed to update ChannelMonitor for channel {}. Going ahead and persisting the entire ChannelMonitor" , log_funding_info!( monitor) ) ;
835
- self . persister . update_persisted_channel ( funding_txo, None , monitor, update_id )
785
+ self . persister . update_persisted_channel ( funding_txo, None , monitor)
836
786
} else {
837
- self . persister . update_persisted_channel ( funding_txo, Some ( update) , monitor, update_id )
787
+ self . persister . update_persisted_channel ( funding_txo, Some ( update) , monitor)
838
788
} ;
839
789
match persist_res {
840
790
ChannelMonitorUpdateStatus :: InProgress => {
0 commit comments