@@ -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
@@ -1199,6 +1203,76 @@ impl TestSyncStore {
1199
1203
}
1200
1204
}
1201
1205
1206
+ impl KVStore for TestSyncStore {
1207
+ fn read (
1208
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
1209
+ ) -> Pin < Box < dyn Future < Output = Result < Vec < u8 > , io:: Error > > + Send > > {
1210
+ let primary_namespace = primary_namespace. to_string ( ) ;
1211
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1212
+ let key = key. to_string ( ) ;
1213
+ let inner = Arc :: clone ( & self . inner ) ;
1214
+ let fut = tokio:: task:: spawn_blocking ( move || {
1215
+ inner. read_internal ( & primary_namespace, & secondary_namespace, & key)
1216
+ } ) ;
1217
+ Box :: pin ( async move {
1218
+ fut. await . unwrap_or_else ( |e| {
1219
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1220
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1221
+ } )
1222
+ } )
1223
+ }
1224
+ fn write (
1225
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : Vec < u8 > ,
1226
+ ) -> Pin < Box < dyn Future < Output = Result < ( ) , io:: Error > > + Send > > {
1227
+ let primary_namespace = primary_namespace. to_string ( ) ;
1228
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1229
+ let key = key. to_string ( ) ;
1230
+ let inner = Arc :: clone ( & self . inner ) ;
1231
+ let fut = tokio:: task:: spawn_blocking ( move || {
1232
+ inner. write_internal ( & primary_namespace, & secondary_namespace, & key, buf)
1233
+ } ) ;
1234
+ Box :: pin ( async move {
1235
+ fut. await . unwrap_or_else ( |e| {
1236
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1237
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1238
+ } )
1239
+ } )
1240
+ }
1241
+ fn remove (
1242
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
1243
+ ) -> Pin < Box < dyn Future < Output = Result < ( ) , io:: Error > > + Send > > {
1244
+ let primary_namespace = primary_namespace. to_string ( ) ;
1245
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1246
+ let key = key. to_string ( ) ;
1247
+ let inner = Arc :: clone ( & self . inner ) ;
1248
+ let fut = tokio:: task:: spawn_blocking ( move || {
1249
+ inner. remove_internal ( & primary_namespace, & secondary_namespace, & key, lazy)
1250
+ } ) ;
1251
+ Box :: pin ( async move {
1252
+ fut. await . unwrap_or_else ( |e| {
1253
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1254
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1255
+ } )
1256
+ } )
1257
+ }
1258
+ fn list (
1259
+ & self , primary_namespace : & str , secondary_namespace : & str ,
1260
+ ) -> Pin < Box < dyn Future < Output = Result < Vec < String > , io:: Error > > + Send > > {
1261
+ let primary_namespace = primary_namespace. to_string ( ) ;
1262
+ let secondary_namespace = secondary_namespace. to_string ( ) ;
1263
+ let inner = Arc :: clone ( & self . inner ) ;
1264
+ let fut = tokio:: task:: spawn_blocking ( move || {
1265
+ inner. list_internal ( & primary_namespace, & secondary_namespace)
1266
+ } ) ;
1267
+ Box :: pin ( async move {
1268
+ fut. await . unwrap_or_else ( |e| {
1269
+ let msg = format ! ( "Failed to IO operation due join error: {}" , e) ;
1270
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , msg) )
1271
+ } )
1272
+ } )
1273
+ }
1274
+ }
1275
+
1202
1276
impl KVStoreSync for TestSyncStore {
1203
1277
fn read (
1204
1278
& self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
@@ -1253,9 +1327,10 @@ impl TestSyncStoreInner {
1253
1327
fn do_list (
1254
1328
& self , primary_namespace : & str , secondary_namespace : & str ,
1255
1329
) -> lightning:: io:: Result < Vec < String > > {
1256
- let fs_res = self . fs_store . list ( primary_namespace, secondary_namespace) ;
1257
- let sqlite_res = self . sqlite_store . list ( primary_namespace, secondary_namespace) ;
1258
- let test_res = self . test_store . list ( primary_namespace, secondary_namespace) ;
1330
+ let fs_res = KVStoreSync :: list ( & self . fs_store , primary_namespace, secondary_namespace) ;
1331
+ let sqlite_res =
1332
+ KVStoreSync :: list ( & self . sqlite_store , primary_namespace, secondary_namespace) ;
1333
+ let test_res = KVStoreSync :: list ( & self . test_store , primary_namespace, secondary_namespace) ;
1259
1334
1260
1335
match fs_res {
1261
1336
Ok ( mut list) => {
@@ -1284,9 +1359,11 @@ impl TestSyncStoreInner {
1284
1359
) -> lightning:: io:: Result < Vec < u8 > > {
1285
1360
let _guard = self . serializer . read ( ) . unwrap ( ) ;
1286
1361
1287
- let fs_res = self . fs_store . read ( primary_namespace, secondary_namespace, key) ;
1288
- let sqlite_res = self . sqlite_store . read ( primary_namespace, secondary_namespace, key) ;
1289
- let test_res = self . test_store . read ( primary_namespace, secondary_namespace, key) ;
1362
+ let fs_res = KVStoreSync :: read ( & self . fs_store , primary_namespace, secondary_namespace, key) ;
1363
+ let sqlite_res =
1364
+ KVStoreSync :: read ( & self . sqlite_store , primary_namespace, secondary_namespace, key) ;
1365
+ let test_res =
1366
+ KVStoreSync :: read ( & self . test_store , primary_namespace, secondary_namespace, key) ;
1290
1367
1291
1368
match fs_res {
1292
1369
Ok ( read) => {
@@ -1308,11 +1385,27 @@ impl TestSyncStoreInner {
1308
1385
& self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : Vec < u8 > ,
1309
1386
) -> lightning:: io:: Result < ( ) > {
1310
1387
let _guard = self . serializer . write ( ) . unwrap ( ) ;
1311
- let fs_res = self . fs_store . write ( primary_namespace, secondary_namespace, key, buf. clone ( ) ) ;
1312
- let sqlite_res =
1313
- self . sqlite_store . write ( primary_namespace, secondary_namespace, key, buf. clone ( ) ) ;
1314
- let test_res =
1315
- self . test_store . write ( primary_namespace, secondary_namespace, key, buf. clone ( ) ) ;
1388
+ let fs_res = KVStoreSync :: write (
1389
+ & self . fs_store ,
1390
+ primary_namespace,
1391
+ secondary_namespace,
1392
+ key,
1393
+ buf. clone ( ) ,
1394
+ ) ;
1395
+ let sqlite_res = KVStoreSync :: write (
1396
+ & self . sqlite_store ,
1397
+ primary_namespace,
1398
+ secondary_namespace,
1399
+ key,
1400
+ buf. clone ( ) ,
1401
+ ) ;
1402
+ let test_res = KVStoreSync :: write (
1403
+ & self . test_store ,
1404
+ primary_namespace,
1405
+ secondary_namespace,
1406
+ key,
1407
+ buf. clone ( ) ,
1408
+ ) ;
1316
1409
1317
1410
assert ! ( self
1318
1411
. do_list( primary_namespace, secondary_namespace)
@@ -1337,10 +1430,22 @@ impl TestSyncStoreInner {
1337
1430
& self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
1338
1431
) -> lightning:: io:: Result < ( ) > {
1339
1432
let _guard = self . serializer . write ( ) . unwrap ( ) ;
1340
- let fs_res = self . fs_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
1341
- let sqlite_res =
1342
- self . sqlite_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
1343
- let test_res = self . test_store . remove ( primary_namespace, secondary_namespace, key, lazy) ;
1433
+ let fs_res =
1434
+ KVStoreSync :: remove ( & self . fs_store , primary_namespace, secondary_namespace, key, lazy) ;
1435
+ let sqlite_res = KVStoreSync :: remove (
1436
+ & self . sqlite_store ,
1437
+ primary_namespace,
1438
+ secondary_namespace,
1439
+ key,
1440
+ lazy,
1441
+ ) ;
1442
+ let test_res = KVStoreSync :: remove (
1443
+ & self . test_store ,
1444
+ primary_namespace,
1445
+ secondary_namespace,
1446
+ key,
1447
+ lazy,
1448
+ ) ;
1344
1449
1345
1450
assert ! ( !self
1346
1451
. do_list( primary_namespace, secondary_namespace)
0 commit comments