@@ -32,6 +32,8 @@ use crate::sign::{ecdsa::EcdsaChannelSigner, EntropySource, SignerProvider};
3232use crate :: util:: logger:: Logger ;
3333use crate :: util:: ser:: { Readable , ReadableArgs , Writeable } ;
3434
35+ use super :: async_poll:: { AsyncResult , AsyncResultError , AsyncResultNo } ;
36+
3537/// The alphabet of characters allowed for namespaces and keys.
3638pub const KVSTORE_NAMESPACE_KEY_ALPHABET : & str =
3739 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" ;
@@ -127,9 +129,9 @@ pub trait KVStore {
127129 /// `primary_namespace` and `secondary_namespace`.
128130 ///
129131 /// [`ErrorKind::NotFound`]: io::ErrorKind::NotFound
130- fn read (
131- & self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
132- ) -> Result < Vec < u8 > , io:: Error > ;
132+ fn read < ' a > (
133+ & ' a self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
134+ ) -> AsyncResultError < ' a , Vec < u8 > , io:: Error > ;
133135 /// Persists the given data under the given `key`.
134136 ///
135137 /// Will create the given `primary_namespace` and `secondary_namespace` if not already present
@@ -186,13 +188,13 @@ pub trait MigratableKVStore: KVStore {
186188///
187189/// Will abort and return an error if any IO operation fails. Note that in this case the
188190/// `target_store` might get left in an intermediate state.
189- pub fn migrate_kv_store_data < S : MigratableKVStore , T : MigratableKVStore > (
191+ pub async fn migrate_kv_store_data < S : MigratableKVStore , T : MigratableKVStore > (
190192 source_store : & mut S , target_store : & mut T ,
191193) -> Result < ( ) , io:: Error > {
192194 let keys_to_migrate = source_store. list_all_keys ( ) ?;
193195
194196 for ( primary_namespace, secondary_namespace, key) in & keys_to_migrate {
195- let data = source_store. read ( primary_namespace, secondary_namespace, key) ?;
197+ let data = source_store. read ( primary_namespace, secondary_namespace, key) . await ?;
196198 target_store. write ( primary_namespace, secondary_namespace, key, & data) ?;
197199 }
198200
@@ -254,7 +256,7 @@ where
254256 }
255257}
256258
257- impl < ChannelSigner : EcdsaChannelSigner , K : KVStore + ?Sized > Persist < ChannelSigner > for K {
259+ impl < ChannelSigner : EcdsaChannelSigner , K : KVStore + Sync + ?Sized > Persist < ChannelSigner > for K {
258260 // TODO: We really need a way for the persister to inform the user that its time to crash/shut
259261 // down once these start returning failure.
260262 // Then we should return InProgress rather than UnrecoverableError, implying we should probably
@@ -289,36 +291,38 @@ impl<ChannelSigner: EcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSign
289291 }
290292 }
291293
292- fn archive_persisted_channel ( & self , monitor_name : MonitorName ) {
293- let monitor_key = monitor_name. to_string ( ) ;
294- let monitor = match self . read (
295- CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
296- CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
297- monitor_key. as_str ( ) ,
298- ) {
299- Ok ( monitor) => monitor,
300- Err ( _) => return ,
301- } ;
302- match self . write (
303- ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
304- ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
305- monitor_key. as_str ( ) ,
306- & monitor,
307- ) {
308- Ok ( ( ) ) => { } ,
309- Err ( _e) => return ,
310- } ;
311- let _ = self . remove (
312- CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
313- CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
314- monitor_key. as_str ( ) ,
315- true ,
316- ) ;
294+ fn archive_persisted_channel < ' a > ( & ' a self , monitor_name : MonitorName ) -> AsyncResultNo < ' a > {
295+ Box :: pin ( async move {
296+ let monitor_key = monitor_name. to_string ( ) ;
297+ let monitor = match self . read (
298+ CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
299+ CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
300+ monitor_key. as_str ( ) ,
301+ ) . await {
302+ Ok ( monitor) => monitor,
303+ Err ( _) => return ,
304+ } ;
305+ match self . write (
306+ ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
307+ ARCHIVED_CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
308+ monitor_key. as_str ( ) ,
309+ & monitor,
310+ ) {
311+ Ok ( ( ) ) => { } ,
312+ Err ( _e) => return ,
313+ } ;
314+ let _ = self . remove (
315+ CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
316+ CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
317+ monitor_key. as_str ( ) ,
318+ true ,
319+ ) ;
320+ } )
317321 }
318322}
319323
320324/// Read previously persisted [`ChannelMonitor`]s from the store.
321- pub fn read_channel_monitors < K : Deref , ES : Deref , SP : Deref > (
325+ pub async fn read_channel_monitors < K : Deref , ES : Deref , SP : Deref > (
322326 kv_store : K , entropy_source : ES , signer_provider : SP ,
323327) -> Result < Vec < ( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: EcdsaSigner > ) > , io:: Error >
324328where
@@ -337,7 +341,7 @@ where
337341 CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
338342 CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
339343 & stored_key,
340- ) ?) ,
344+ ) . await ?) ,
341345 ( & * entropy_source, & * signer_provider) ,
342346 ) {
343347 Ok ( ( block_hash, channel_monitor) ) => {
@@ -586,15 +590,15 @@ where
586590 }
587591
588592 /// Read a channel monitor.
589- fn read_monitor (
593+ async fn read_monitor (
590594 & self , monitor_name : & MonitorName , monitor_key : & str ,
591595 ) -> Result < ( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: EcdsaSigner > ) , io:: Error >
592596 {
593597 let mut monitor_cursor = io:: Cursor :: new ( self . kv_store . read (
594598 CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
595599 CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
596600 monitor_key,
597- ) ?) ;
601+ ) . await ?) ;
598602 // Discard the sentinel bytes if found.
599603 if monitor_cursor. get_ref ( ) . starts_with ( MONITOR_UPDATING_PERSISTER_PREPEND_SENTINEL ) {
600604 monitor_cursor. set_position ( MONITOR_UPDATING_PERSISTER_PREPEND_SENTINEL . len ( ) as u64 ) ;
0 commit comments