@@ -36,7 +36,7 @@ use crate::ln::types::ChannelId;
36
36
use crate :: sign:: { ecdsa:: EcdsaChannelSigner , EntropySource , SignerProvider } ;
37
37
use crate :: sync:: Mutex ;
38
38
use crate :: util:: async_poll:: {
39
- dummy_waker, MaybeSend , MaybeSync , MultiResultFuturePoller , ResultFuture ,
39
+ dummy_waker, MaybeSend , MaybeSync , MultiResultFuturePoller , ResultFuture , TwoFutureJoiner ,
40
40
} ;
41
41
use crate :: util:: logger:: Logger ;
42
42
use crate :: util:: native_async:: FutureSpawner ;
@@ -493,15 +493,6 @@ fn poll_sync_future<F: Future>(future: F) -> F::Output {
493
493
/// list channel monitors themselves and load channels individually using
494
494
/// [`MonitorUpdatingPersister::read_channel_monitor_with_updates`].
495
495
///
496
- /// ## EXTREMELY IMPORTANT
497
- ///
498
- /// It is extremely important that your [`KVStoreSync::read`] implementation uses the
499
- /// [`io::ErrorKind::NotFound`] variant correctly: that is, when a file is not found, and _only_ in
500
- /// that circumstance (not when there is really a permissions error, for example). This is because
501
- /// neither channel monitor reading function lists updates. Instead, either reads the monitor, and
502
- /// using its stored `update_id`, synthesizes update storage keys, and tries them in sequence until
503
- /// one is not found. All _other_ errors will be bubbled up in the function's [`Result`].
504
- ///
505
496
/// # Pruning stale channel updates
506
497
///
507
498
/// Stale updates are pruned when the consolidation threshold is reached according to `maximum_pending_updates`.
@@ -569,10 +560,6 @@ where
569
560
}
570
561
571
562
/// Reads all stored channel monitors, along with any stored updates for them.
572
- ///
573
- /// It is extremely important that your [`KVStoreSync::read`] implementation uses the
574
- /// [`io::ErrorKind::NotFound`] variant correctly. For more information, please see the
575
- /// documentation for [`MonitorUpdatingPersister`].
576
563
pub fn read_all_channel_monitors_with_updates (
577
564
& self ,
578
565
) -> Result <
@@ -584,10 +571,6 @@ where
584
571
585
572
/// Read a single channel monitor, along with any stored updates for it.
586
573
///
587
- /// It is extremely important that your [`KVStoreSync::read`] implementation uses the
588
- /// [`io::ErrorKind::NotFound`] variant correctly. For more information, please see the
589
- /// documentation for [`MonitorUpdatingPersister`].
590
- ///
591
574
/// For `monitor_key`, channel storage keys can be the channel's funding [`OutPoint`], with an
592
575
/// underscore `_` between txid and index for v1 channels. For example, given:
593
576
///
@@ -781,10 +764,6 @@ where
781
764
/// While the reads themselves are performend in parallel, deserializing the
782
765
/// [`ChannelMonitor`]s is not. For large [`ChannelMonitor`]s actively used for forwarding,
783
766
/// this may substantially limit the parallelism of this method.
784
- ///
785
- /// It is extremely important that your [`KVStore::read`] implementation uses the
786
- /// [`io::ErrorKind::NotFound`] variant correctly. For more information, please see the
787
- /// documentation for [`MonitorUpdatingPersister`].
788
767
pub async fn read_all_channel_monitors_with_updates (
789
768
& self ,
790
769
) -> Result <
@@ -819,10 +798,6 @@ where
819
798
/// Because [`FutureSpawner`] requires that the spawned future be `'static` (matching `tokio`
820
799
/// and other multi-threaded runtime requirements), this method requires that `self` be an
821
800
/// `Arc` that can live for `'static` and be sent and accessed across threads.
822
- ///
823
- /// It is extremely important that your [`KVStore::read`] implementation uses the
824
- /// [`io::ErrorKind::NotFound`] variant correctly. For more information, please see the
825
- /// documentation for [`MonitorUpdatingPersister`].
826
801
pub async fn read_all_channel_monitors_with_updates_parallel (
827
802
self : & Arc < Self > ,
828
803
) -> Result <
@@ -862,10 +837,6 @@ where
862
837
863
838
/// Read a single channel monitor, along with any stored updates for it.
864
839
///
865
- /// It is extremely important that your [`KVStoreSync::read`] implementation uses the
866
- /// [`io::ErrorKind::NotFound`] variant correctly. For more information, please see the
867
- /// documentation for [`MonitorUpdatingPersister`].
868
- ///
869
840
/// For `monitor_key`, channel storage keys can be the channel's funding [`OutPoint`], with an
870
841
/// underscore `_` between txid and index for v1 channels. For example, given:
871
842
///
@@ -1011,40 +982,37 @@ where
1011
982
io:: Error ,
1012
983
> {
1013
984
let monitor_name = MonitorName :: from_str ( monitor_key) ?;
1014
- let read_res = self . maybe_read_monitor ( & monitor_name, monitor_key) . await ?;
1015
- let ( block_hash, monitor) = match read_res {
985
+ // TODO: After an MSRV bump we should be able to use the pin macro rather than Box::pin
986
+ let read_future = Box :: pin ( self . maybe_read_monitor ( & monitor_name, monitor_key) ) ;
987
+ let list_future =
988
+ Box :: pin ( self . kv_store . list ( CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE , monitor_key) ) ;
989
+ let ( read_res, list_res) = TwoFutureJoiner :: new ( read_future, list_future) . await ;
990
+ let ( block_hash, monitor) = match read_res? {
1016
991
Some ( res) => res,
1017
992
None => return Ok ( None ) ,
1018
993
} ;
1019
994
let mut current_update_id = monitor. get_latest_update_id ( ) ;
1020
- // TODO: Parallelize this loop by speculatively reading a batch of updates
1021
- loop {
1022
- current_update_id = match current_update_id. checked_add ( 1 ) {
1023
- Some ( next_update_id) => next_update_id,
1024
- None => break ,
1025
- } ;
1026
- let update_name = UpdateName :: from ( current_update_id) ;
1027
- let update = match self . read_monitor_update ( monitor_key, & update_name) . await {
1028
- Ok ( update) => update,
1029
- Err ( err) if err. kind ( ) == io:: ErrorKind :: NotFound => {
1030
- // We can't find any more updates, so we are done.
1031
- break ;
1032
- } ,
1033
- Err ( err) => return Err ( err) ,
1034
- } ;
1035
-
1036
- monitor
1037
- . update_monitor ( & update, & self . broadcaster , & self . fee_estimator , & self . logger )
1038
- . map_err ( |e| {
1039
- log_error ! (
1040
- self . logger,
1041
- "Monitor update failed. monitor: {} update: {} reason: {:?}" ,
1042
- monitor_key,
1043
- update_name. as_str( ) ,
1044
- e
1045
- ) ;
1046
- io:: Error :: new ( io:: ErrorKind :: Other , "Monitor update failed" )
1047
- } ) ?;
995
+ let updates: Result < Vec < _ > , _ > =
996
+ list_res?. into_iter ( ) . map ( |name| UpdateName :: new ( name) ) . collect ( ) ;
997
+ let mut updates = updates?;
998
+ updates. sort_unstable ( ) ;
999
+ // TODO: Parallelize this loop
1000
+ for update_name in updates {
1001
+ if update_name. 0 > current_update_id {
1002
+ let update = self . read_monitor_update ( monitor_key, & update_name) . await ?;
1003
+ monitor
1004
+ . update_monitor ( & update, & self . broadcaster , & self . fee_estimator , & self . logger )
1005
+ . map_err ( |e| {
1006
+ log_error ! (
1007
+ self . logger,
1008
+ "Monitor update failed. monitor: {} update: {} reason: {:?}" ,
1009
+ monitor_key,
1010
+ update_name. as_str( ) ,
1011
+ e
1012
+ ) ;
1013
+ io:: Error :: new ( io:: ErrorKind :: Other , "Monitor update failed" )
1014
+ } ) ?;
1015
+ }
1048
1016
}
1049
1017
Ok ( Some ( ( block_hash, monitor) ) )
1050
1018
}
@@ -1416,7 +1384,7 @@ impl core::fmt::Display for MonitorName {
1416
1384
/// let monitor_name = "some_monitor_name";
1417
1385
/// let storage_key = format!("channel_monitor_updates/{}/{}", monitor_name, update_name.as_str());
1418
1386
/// ```
1419
- #[ derive( Debug ) ]
1387
+ #[ derive( Debug , PartialEq , Eq , PartialOrd , Ord ) ]
1420
1388
pub struct UpdateName ( pub u64 , String ) ;
1421
1389
1422
1390
impl UpdateName {
0 commit comments