10
10
11
11
pub ( crate ) mod logging;
12
12
13
+ use std:: boxed:: Box ;
13
14
use std:: collections:: { HashMap , HashSet } ;
14
15
use std:: env;
16
+ use std:: future:: Future ;
15
17
use std:: path:: PathBuf ;
18
+ use std:: pin:: Pin ;
16
19
use std:: sync:: { Arc , RwLock } ;
17
20
use std:: time:: Duration ;
18
21
@@ -31,9 +34,10 @@ use ldk_node::payment::{PaymentDirection, PaymentKind, PaymentStatus};
31
34
use ldk_node:: {
32
35
Builder , CustomTlvRecord , Event , LightningBalance , Node , NodeError , PendingSweepBalance ,
33
36
} ;
37
+ use lightning:: io;
34
38
use lightning:: ln:: msgs:: SocketAddress ;
35
39
use lightning:: routing:: gossip:: NodeAlias ;
36
- use lightning:: util:: persist:: KVStoreSync ;
40
+ use lightning:: util:: persist:: { KVStore , KVStoreSync } ;
37
41
use lightning:: util:: test_utils:: TestStore ;
38
42
use lightning_invoice:: { Bolt11InvoiceDescription , Description } ;
39
43
use lightning_persister:: fs_store:: FilesystemStore ;
@@ -1200,6 +1204,76 @@ impl TestSyncStore {
1200
1204
}
1201
1205
}
1202
1206
1207
+ impl KVStore for TestSyncStore {
1208
+ fn read (
1209
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
1210
+ ) -> Pin < Box < dyn Future < Output = Result < Vec < u8 > , io:: Error > > + Send > > {
1211
+ let primary_namespace = primary_namespace. to_string ( ) ;
1212
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1213
+ let key = key. to_string ( ) ;
1214
+ let inner = Arc :: clone ( & self . inner ) ;
1215
+ let fut = tokio:: task:: spawn_blocking ( move || {
1216
+ inner. read_internal ( & primary_namespace, & secondary_namespace, & key)
1217
+ } ) ;
1218
+ Box :: pin ( async move {
1219
+ fut. await . unwrap_or_else ( |e| {
1220
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1221
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1222
+ } )
1223
+ } )
1224
+ }
1225
+ fn write (
1226
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : Vec < u8 > ,
1227
+ ) -> Pin < Box < dyn Future < Output = Result < ( ) , io:: Error > > + Send > > {
1228
+ let primary_namespace = primary_namespace. to_string ( ) ;
1229
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1230
+ let key = key. to_string ( ) ;
1231
+ let inner = Arc :: clone ( & self . inner ) ;
1232
+ let fut = tokio:: task:: spawn_blocking ( move || {
1233
+ inner. write_internal ( & primary_namespace, & secondary_namespace, & key, buf)
1234
+ } ) ;
1235
+ Box :: pin ( async move {
1236
+ fut. await . unwrap_or_else ( |e| {
1237
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1238
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1239
+ } )
1240
+ } )
1241
+ }
1242
+ fn remove (
1243
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
1244
+ ) -> Pin < Box < dyn Future < Output = Result < ( ) , io:: Error > > + Send > > {
1245
+ let primary_namespace = primary_namespace. to_string ( ) ;
1246
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1247
+ let key = key. to_string ( ) ;
1248
+ let inner = Arc :: clone ( & self . inner ) ;
1249
+ let fut = tokio:: task:: spawn_blocking ( move || {
1250
+ inner. remove_internal ( & primary_namespace, & secondary_namespace, & key, lazy)
1251
+ } ) ;
1252
+ Box :: pin ( async move {
1253
+ fut. await . unwrap_or_else ( |e| {
1254
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1255
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1256
+ } )
1257
+ } )
1258
+ }
1259
+ fn list (
1260
+ & self , primary_namespace : & str , secondary_namespace : & str ,
1261
+ ) -> Pin < Box < dyn Future < Output = Result < Vec < String > , io:: Error > > + Send > > {
1262
+ let primary_namespace = primary_namespace. to_string ( ) ;
1263
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1264
+ let inner = Arc :: clone ( & self . inner ) ;
1265
+ let fut = tokio:: task:: spawn_blocking ( move || {
1266
+ inner. list_internal ( & primary_namespace, & secondary_namespace)
1267
+ } ) ;
1268
+ Box :: pin ( async move {
1269
+ fut. await . unwrap_or_else ( |e| {
1270
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1271
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1272
+ } )
1273
+ } )
1274
+ }
1275
+ }
1276
+
1203
1277
impl KVStoreSync for TestSyncStore {
1204
1278
fn read (
1205
1279
& self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
@@ -1254,9 +1328,10 @@ impl TestSyncStoreInner {
1254
1328
fn do_list (
1255
1329
& self , primary_namespace : & str , secondary_namespace : & str ,
1256
1330
) -> lightning:: io:: Result < Vec < String > > {
1257
- let fs_res = self . fs_store . list ( primary_namespace, secondary_namespace) ;
1258
- let sqlite_res = self . sqlite_store . list ( primary_namespace, secondary_namespace) ;
1259
- let test_res = self . test_store . list ( primary_namespace, secondary_namespace) ;
1331
+ let fs_res = KVStoreSync :: list ( & self . fs_store , primary_namespace, secondary_namespace) ;
1332
+ let sqlite_res =
1333
+ KVStoreSync :: list ( & self . sqlite_store , primary_namespace, secondary_namespace) ;
1334
+ let test_res = KVStoreSync :: list ( & self . test_store , primary_namespace, secondary_namespace) ;
1260
1335
1261
1336
match fs_res {
1262
1337
Ok ( mut list) => {
@@ -1285,9 +1360,11 @@ impl TestSyncStoreInner {
1285
1360
) -> lightning:: io:: Result < Vec < u8 > > {
1286
1361
let _guard = self . serializer . read ( ) . unwrap ( ) ;
1287
1362
1288
- let fs_res = self . fs_store . read ( primary_namespace, secondary_namespace, key) ;
1289
- let sqlite_res = self . sqlite_store . read ( primary_namespace, secondary_namespace, key) ;
1290
- let test_res = self . test_store . read ( primary_namespace, secondary_namespace, key) ;
1363
+ let fs_res = KVStoreSync :: read ( & self . fs_store , primary_namespace, secondary_namespace, key) ;
1364
+ let sqlite_res =
1365
+ KVStoreSync :: read ( & self . sqlite_store , primary_namespace, secondary_namespace, key) ;
1366
+ let test_res =
1367
+ KVStoreSync :: read ( & self . test_store , primary_namespace, secondary_namespace, key) ;
1291
1368
1292
1369
match fs_res {
1293
1370
Ok ( read) => {
@@ -1309,11 +1386,27 @@ impl TestSyncStoreInner {
1309
1386
& self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : Vec < u8 > ,
1310
1387
) -> lightning:: io:: Result < ( ) > {
1311
1388
let _guard = self . serializer . write ( ) . unwrap ( ) ;
1312
- let fs_res = self . fs_store . write ( primary_namespace, secondary_namespace, key, buf. clone ( ) ) ;
1313
- let sqlite_res =
1314
- self . sqlite_store . write ( primary_namespace, secondary_namespace, key, buf. clone ( ) ) ;
1315
- let test_res =
1316
- self . test_store . write ( primary_namespace, secondary_namespace, key, buf. clone ( ) ) ;
1389
+ let fs_res = KVStoreSync :: write (
1390
+ & self . fs_store ,
1391
+ primary_namespace,
1392
+ secondary_namespace,
1393
+ key,
1394
+ buf. clone ( ) ,
1395
+ ) ;
1396
+ let sqlite_res = KVStoreSync :: write (
1397
+ & self . sqlite_store ,
1398
+ primary_namespace,
1399
+ secondary_namespace,
1400
+ key,
1401
+ buf. clone ( ) ,
1402
+ ) ;
1403
+ let test_res = KVStoreSync :: write (
1404
+ & self . test_store ,
1405
+ primary_namespace,
1406
+ secondary_namespace,
1407
+ key,
1408
+ buf. clone ( ) ,
1409
+ ) ;
1317
1410
1318
1411
assert ! ( self
1319
1412
. do_list( primary_namespace, secondary_namespace)
@@ -1338,10 +1431,22 @@ impl TestSyncStoreInner {
1338
1431
& self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
1339
1432
) -> lightning:: io:: Result < ( ) > {
1340
1433
let _guard = self . serializer . write ( ) . unwrap ( ) ;
1341
- let fs_res = self . fs_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
1342
- let sqlite_res =
1343
- self . sqlite_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
1344
- let test_res = self . test_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
1434
+ let fs_res =
1435
+ KVStoreSync :: remove ( & self . fs_store , primary_namespace, secondary_namespace, key, lazy) ;
1436
+ let sqlite_res = KVStoreSync :: remove (
1437
+ & self . sqlite_store ,
1438
+ primary_namespace,
1439
+ secondary_namespace,
1440
+ key,
1441
+ lazy,
1442
+ ) ;
1443
+ let test_res = KVStoreSync :: remove (
1444
+ & self . test_store ,
1445
+ primary_namespace,
1446
+ secondary_namespace,
1447
+ key,
1448
+ lazy,
1449
+ ) ;
1345
1450
1346
1451
assert ! ( !self
1347
1452
. do_list( primary_namespace, secondary_namespace)
0 commit comments