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