@@ -32,6 +32,8 @@ use crate::sign::{ecdsa::EcdsaChannelSigner, EntropySource, SignerProvider};
32
32
use crate :: util:: logger:: Logger ;
33
33
use crate :: util:: ser:: { Readable , ReadableArgs , Writeable } ;
34
34
35
+ use super :: async_poll:: { AsyncResult , AsyncResultError , AsyncResultNo } ;
36
+
35
37
/// The alphabet of characters allowed for namespaces and keys.
36
38
pub const KVSTORE_NAMESPACE_KEY_ALPHABET : & str =
37
39
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" ;
@@ -127,9 +129,9 @@ pub trait KVStore {
127
129
/// `primary_namespace` and `secondary_namespace`.
128
130
///
129
131
/// [`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 > ;
133
135
/// Persists the given data under the given `key`.
134
136
///
135
137
/// Will create the given `primary_namespace` and `secondary_namespace` if not already present
@@ -186,13 +188,13 @@ pub trait MigratableKVStore: KVStore {
186
188
///
187
189
/// Will abort and return an error if any IO operation fails. Note that in this case the
188
190
/// `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 > (
190
192
source_store : & mut S , target_store : & mut T ,
191
193
) -> Result < ( ) , io:: Error > {
192
194
let keys_to_migrate = source_store. list_all_keys ( ) ?;
193
195
194
196
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 ?;
196
198
target_store. write ( primary_namespace, secondary_namespace, key, & data) ?;
197
199
}
198
200
@@ -254,7 +256,7 @@ where
254
256
}
255
257
}
256
258
257
- impl < ChannelSigner : EcdsaChannelSigner , K : KVStore + ?Sized > Persist < ChannelSigner > for K {
259
+ impl < ChannelSigner : EcdsaChannelSigner , K : KVStore + Sync + ?Sized > Persist < ChannelSigner > for K {
258
260
// TODO: We really need a way for the persister to inform the user that its time to crash/shut
259
261
// down once these start returning failure.
260
262
// Then we should return InProgress rather than UnrecoverableError, implying we should probably
@@ -289,36 +291,38 @@ impl<ChannelSigner: EcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSign
289
291
}
290
292
}
291
293
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
+ } )
317
321
}
318
322
}
319
323
320
324
/// 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 > (
322
326
kv_store : K , entropy_source : ES , signer_provider : SP ,
323
327
) -> Result < Vec < ( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: EcdsaSigner > ) > , io:: Error >
324
328
where
@@ -337,7 +341,7 @@ where
337
341
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
338
342
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
339
343
& stored_key,
340
- ) ?) ,
344
+ ) . await ?) ,
341
345
( & * entropy_source, & * signer_provider) ,
342
346
) {
343
347
Ok ( ( block_hash, channel_monitor) ) => {
@@ -586,15 +590,15 @@ where
586
590
}
587
591
588
592
/// Read a channel monitor.
589
- fn read_monitor (
593
+ async fn read_monitor (
590
594
& self , monitor_name : & MonitorName , monitor_key : & str ,
591
595
) -> Result < ( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: EcdsaSigner > ) , io:: Error >
592
596
{
593
597
let mut monitor_cursor = io:: Cursor :: new ( self . kv_store . read (
594
598
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE ,
595
599
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE ,
596
600
monitor_key,
597
- ) ?) ;
601
+ ) . await ?) ;
598
602
// Discard the sentinel bytes if found.
599
603
if monitor_cursor. get_ref ( ) . starts_with ( MONITOR_UPDATING_PERSISTER_PREPEND_SENTINEL ) {
600
604
monitor_cursor. set_position ( MONITOR_UPDATING_PERSISTER_PREPEND_SENTINEL . len ( ) as u64 ) ;
0 commit comments