@@ -19,9 +19,10 @@ use ldk_node::{
19
19
Builder , CustomTlvRecord , Event , LightningBalance , Node , NodeError , PendingSweepBalance ,
20
20
} ;
21
21
22
+ use lightning:: io;
22
23
use lightning:: ln:: msgs:: SocketAddress ;
23
24
use lightning:: routing:: gossip:: NodeAlias ;
24
- use lightning:: util:: persist:: KVStoreSync ;
25
+ use lightning:: util:: persist:: { KVStore , KVStoreSync } ;
25
26
use lightning:: util:: test_utils:: TestStore ;
26
27
27
28
use lightning_invoice:: { Bolt11InvoiceDescription , Description } ;
@@ -44,9 +45,12 @@ use rand::distributions::Alphanumeric;
44
45
use rand:: { thread_rng, Rng } ;
45
46
use serde_json:: { json, Value } ;
46
47
48
+ use std:: boxed:: Box ;
47
49
use std:: collections:: { HashMap , HashSet } ;
48
50
use std:: env;
51
+ use std:: future:: Future ;
49
52
use std:: path:: PathBuf ;
53
+ use std:: pin:: Pin ;
50
54
use std:: sync:: { Arc , RwLock } ;
51
55
use std:: time:: Duration ;
52
56
@@ -1207,6 +1211,76 @@ impl TestSyncStore {
1207
1211
}
1208
1212
}
1209
1213
1214
+ impl KVStore for TestSyncStore {
1215
+ fn read (
1216
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
1217
+ ) -> Pin < Box < dyn Future < Output = Result < Vec < u8 > , io:: Error > > + Send > > {
1218
+ let primary_namespace = primary_namespace. to_string ( ) ;
1219
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1220
+ let key = key. to_string ( ) ;
1221
+ let inner = Arc :: clone ( & self . inner ) ;
1222
+ let fut = tokio:: task:: spawn_blocking ( move || {
1223
+ inner. read_internal ( & primary_namespace, & secondary_namespace, & key)
1224
+ } ) ;
1225
+ Box :: pin ( async move {
1226
+ fut. await . unwrap_or_else ( |e| {
1227
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1228
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1229
+ } )
1230
+ } )
1231
+ }
1232
+ fn write (
1233
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : Vec < u8 > ,
1234
+ ) -> Pin < Box < dyn Future < Output = Result < ( ) , io:: Error > > + Send > > {
1235
+ let primary_namespace = primary_namespace. to_string ( ) ;
1236
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1237
+ let key = key. to_string ( ) ;
1238
+ let inner = Arc :: clone ( & self . inner ) ;
1239
+ let fut = tokio:: task:: spawn_blocking ( move || {
1240
+ inner. write_internal ( & primary_namespace, & secondary_namespace, & key, buf)
1241
+ } ) ;
1242
+ Box :: pin ( async move {
1243
+ fut. await . unwrap_or_else ( |e| {
1244
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1245
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1246
+ } )
1247
+ } )
1248
+ }
1249
+ fn remove (
1250
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
1251
+ ) -> Pin < Box < dyn Future < Output = Result < ( ) , io:: Error > > + Send > > {
1252
+ let primary_namespace = primary_namespace. to_string ( ) ;
1253
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1254
+ let key = key. to_string ( ) ;
1255
+ let inner = Arc :: clone ( & self . inner ) ;
1256
+ let fut = tokio:: task:: spawn_blocking ( move || {
1257
+ inner. remove_internal ( & primary_namespace, & secondary_namespace, & key, lazy)
1258
+ } ) ;
1259
+ Box :: pin ( async move {
1260
+ fut. await . unwrap_or_else ( |e| {
1261
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1262
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1263
+ } )
1264
+ } )
1265
+ }
1266
+ fn list (
1267
+ & self , primary_namespace : & str , secondary_namespace : & str ,
1268
+ ) -> Pin < Box < dyn Future < Output = Result < Vec < String > , io:: Error > > + Send > > {
1269
+ let primary_namespace = primary_namespace. to_string ( ) ;
1270
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1271
+ let inner = Arc :: clone ( & self . inner ) ;
1272
+ let fut = tokio:: task:: spawn_blocking ( move || {
1273
+ inner. list_internal ( & primary_namespace, & secondary_namespace)
1274
+ } ) ;
1275
+ Box :: pin ( async move {
1276
+ fut. await . unwrap_or_else ( |e| {
1277
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1278
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1279
+ } )
1280
+ } )
1281
+ }
1282
+ }
1283
+
1210
1284
impl KVStoreSync for TestSyncStore {
1211
1285
fn read (
1212
1286
& self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
@@ -1261,9 +1335,10 @@ impl TestSyncStoreInner {
1261
1335
fn do_list (
1262
1336
& self , primary_namespace : & str , secondary_namespace : & str ,
1263
1337
) -> lightning:: io:: Result < Vec < String > > {
1264
- let fs_res = self . fs_store . list ( primary_namespace, secondary_namespace) ;
1265
- let sqlite_res = self . sqlite_store . list ( primary_namespace, secondary_namespace) ;
1266
- let test_res = self . test_store . list ( primary_namespace, secondary_namespace) ;
1338
+ let fs_res = KVStoreSync :: list ( & self . fs_store , primary_namespace, secondary_namespace) ;
1339
+ let sqlite_res =
1340
+ KVStoreSync :: list ( & self . sqlite_store , primary_namespace, secondary_namespace) ;
1341
+ let test_res = KVStoreSync :: list ( & self . test_store , primary_namespace, secondary_namespace) ;
1267
1342
1268
1343
match fs_res {
1269
1344
Ok ( mut list) => {
@@ -1292,9 +1367,11 @@ impl TestSyncStoreInner {
1292
1367
) -> lightning:: io:: Result < Vec < u8 > > {
1293
1368
let _guard = self . serializer . read ( ) . unwrap ( ) ;
1294
1369
1295
- let fs_res = self . fs_store . read ( primary_namespace, secondary_namespace, key) ;
1296
- let sqlite_res = self . sqlite_store . read ( primary_namespace, secondary_namespace, key) ;
1297
- let test_res = self . test_store . read ( primary_namespace, secondary_namespace, key) ;
1370
+ let fs_res = KVStoreSync :: read ( & self . fs_store , primary_namespace, secondary_namespace, key) ;
1371
+ let sqlite_res =
1372
+ KVStoreSync :: read ( & self . sqlite_store , primary_namespace, secondary_namespace, key) ;
1373
+ let test_res =
1374
+ KVStoreSync :: read ( & self . test_store , primary_namespace, secondary_namespace, key) ;
1298
1375
1299
1376
match fs_res {
1300
1377
Ok ( read) => {
@@ -1316,11 +1393,27 @@ impl TestSyncStoreInner {
1316
1393
& self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : Vec < u8 > ,
1317
1394
) -> lightning:: io:: Result < ( ) > {
1318
1395
let _guard = self . serializer . write ( ) . unwrap ( ) ;
1319
- let fs_res = self . fs_store . write ( primary_namespace, secondary_namespace, key, buf. clone ( ) ) ;
1320
- let sqlite_res =
1321
- self . sqlite_store . write ( primary_namespace, secondary_namespace, key, buf. clone ( ) ) ;
1322
- let test_res =
1323
- self . test_store . write ( primary_namespace, secondary_namespace, key, buf. clone ( ) ) ;
1396
+ let fs_res = KVStoreSync :: write (
1397
+ & self . fs_store ,
1398
+ primary_namespace,
1399
+ secondary_namespace,
1400
+ key,
1401
+ buf. clone ( ) ,
1402
+ ) ;
1403
+ let sqlite_res = KVStoreSync :: write (
1404
+ & self . sqlite_store ,
1405
+ primary_namespace,
1406
+ secondary_namespace,
1407
+ key,
1408
+ buf. clone ( ) ,
1409
+ ) ;
1410
+ let test_res = KVStoreSync :: write (
1411
+ & self . test_store ,
1412
+ primary_namespace,
1413
+ secondary_namespace,
1414
+ key,
1415
+ buf. clone ( ) ,
1416
+ ) ;
1324
1417
1325
1418
assert ! ( self
1326
1419
. do_list( primary_namespace, secondary_namespace)
@@ -1345,10 +1438,22 @@ impl TestSyncStoreInner {
1345
1438
& self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
1346
1439
) -> lightning:: io:: Result < ( ) > {
1347
1440
let _guard = self . serializer . write ( ) . unwrap ( ) ;
1348
- let fs_res = self . fs_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
1349
- let sqlite_res =
1350
- self . sqlite_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
1351
- let test_res = self . test_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
1441
+ let fs_res =
1442
+ KVStoreSync :: remove ( & self . fs_store , primary_namespace, secondary_namespace, key, lazy) ;
1443
+ let sqlite_res = KVStoreSync :: remove (
1444
+ & self . sqlite_store ,
1445
+ primary_namespace,
1446
+ secondary_namespace,
1447
+ key,
1448
+ lazy,
1449
+ ) ;
1450
+ let test_res = KVStoreSync :: remove (
1451
+ & self . test_store ,
1452
+ primary_namespace,
1453
+ secondary_namespace,
1454
+ key,
1455
+ lazy,
1456
+ ) ;
1352
1457
1353
1458
assert ! ( !self
1354
1459
. do_list( primary_namespace, secondary_namespace)
0 commit comments