@@ -67,7 +67,9 @@ impl FilesystemStore {
67
67
}
68
68
}
69
69
70
- fn get_dest_dir_path ( & self , primary_namespace : & str , secondary_namespace : & str ) -> std:: io:: Result < PathBuf > {
70
+ fn get_dest_dir_path (
71
+ & self , primary_namespace : & str , secondary_namespace : & str ,
72
+ ) -> std:: io:: Result < PathBuf > {
71
73
let mut dest_dir_path = {
72
74
#[ cfg( target_os = "windows" ) ]
73
75
{
@@ -91,7 +93,9 @@ impl FilesystemStore {
91
93
}
92
94
93
95
impl KVStore for FilesystemStore {
94
- fn read ( & self , primary_namespace : & str , secondary_namespace : & str , key : & str ) -> lightning:: io:: Result < Vec < u8 > > {
96
+ fn read (
97
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
98
+ ) -> lightning:: io:: Result < Vec < u8 > > {
95
99
check_namespace_key_validity ( primary_namespace, secondary_namespace, Some ( key) , "read" ) ?;
96
100
97
101
let mut dest_file_path = self . get_dest_dir_path ( primary_namespace, secondary_namespace) ?;
@@ -114,19 +118,19 @@ impl KVStore for FilesystemStore {
114
118
Ok ( buf)
115
119
}
116
120
117
- fn write ( & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : & [ u8 ] ) -> lightning:: io:: Result < ( ) > {
121
+ fn write (
122
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : & [ u8 ] ,
123
+ ) -> lightning:: io:: Result < ( ) > {
118
124
check_namespace_key_validity ( primary_namespace, secondary_namespace, Some ( key) , "write" ) ?;
119
125
120
126
let mut dest_file_path = self . get_dest_dir_path ( primary_namespace, secondary_namespace) ?;
121
127
dest_file_path. push ( key) ;
122
128
123
- let parent_directory = dest_file_path
124
- . parent ( )
125
- . ok_or_else ( || {
126
- let msg =
127
- format ! ( "Could not retrieve parent directory of {}." , dest_file_path. display( ) ) ;
128
- std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidInput , msg)
129
- } ) ?;
129
+ let parent_directory = dest_file_path. parent ( ) . ok_or_else ( || {
130
+ let msg =
131
+ format ! ( "Could not retrieve parent directory of {}." , dest_file_path. display( ) ) ;
132
+ std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidInput , msg)
133
+ } ) ?;
130
134
fs:: create_dir_all ( & parent_directory) ?;
131
135
132
136
// Do a crazy dance with lots of fsync()s to be overly cautious here...
@@ -186,11 +190,11 @@ impl KVStore for FilesystemStore {
186
190
match res {
187
191
Ok ( ( ) ) => {
188
192
// We fsync the dest file in hopes this will also flush the metadata to disk.
189
- let dest_file = fs :: OpenOptions :: new ( ) . read ( true ) . write ( true )
190
- . open ( & dest_file_path) ?;
193
+ let dest_file =
194
+ fs :: OpenOptions :: new ( ) . read ( true ) . write ( true ) . open ( & dest_file_path) ?;
191
195
dest_file. sync_all ( ) ?;
192
196
Ok ( ( ) )
193
- }
197
+ } ,
194
198
Err ( e) => Err ( e. into ( ) ) ,
195
199
}
196
200
}
@@ -201,7 +205,9 @@ impl KVStore for FilesystemStore {
201
205
res
202
206
}
203
207
204
- fn remove ( & self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ) -> lightning:: io:: Result < ( ) > {
208
+ fn remove (
209
+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
210
+ ) -> lightning:: io:: Result < ( ) > {
205
211
check_namespace_key_validity ( primary_namespace, secondary_namespace, Some ( key) , "remove" ) ?;
206
212
207
213
let mut dest_file_path = self . get_dest_dir_path ( primary_namespace, secondary_namespace) ?;
@@ -229,8 +235,10 @@ impl KVStore for FilesystemStore {
229
235
fs:: remove_file ( & dest_file_path) ?;
230
236
231
237
let parent_directory = dest_file_path. parent ( ) . ok_or_else ( || {
232
- let msg =
233
- format ! ( "Could not retrieve parent directory of {}." , dest_file_path. display( ) ) ;
238
+ let msg = format ! (
239
+ "Could not retrieve parent directory of {}." ,
240
+ dest_file_path. display( )
241
+ ) ;
234
242
std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidInput , msg)
235
243
} ) ?;
236
244
let dir_file = fs:: OpenOptions :: new ( ) . read ( true ) . open ( parent_directory) ?;
@@ -257,8 +265,8 @@ impl KVStore for FilesystemStore {
257
265
// However, all this is partially based on assumptions and local experiments, as
258
266
// Windows API is horribly underdocumented.
259
267
let mut trash_file_path = dest_file_path. clone ( ) ;
260
- let trash_file_ext = format ! ( "{}.trash" ,
261
- self . tmp_file_counter. fetch_add( 1 , Ordering :: AcqRel ) ) ;
268
+ let trash_file_ext =
269
+ format ! ( "{}.trash" , self . tmp_file_counter. fetch_add( 1 , Ordering :: AcqRel ) ) ;
262
270
trash_file_path. set_extension ( trash_file_ext) ;
263
271
264
272
call ! ( unsafe {
@@ -273,7 +281,9 @@ impl KVStore for FilesystemStore {
273
281
{
274
282
// We fsync the trash file in hopes this will also flush the original's file
275
283
// metadata to disk.
276
- let trash_file = fs:: OpenOptions :: new ( ) . read ( true ) . write ( true )
284
+ let trash_file = fs:: OpenOptions :: new ( )
285
+ . read ( true )
286
+ . write ( true )
277
287
. open ( & trash_file_path. clone ( ) ) ?;
278
288
trash_file. sync_all ( ) ?;
279
289
}
@@ -290,7 +300,9 @@ impl KVStore for FilesystemStore {
290
300
Ok ( ( ) )
291
301
}
292
302
293
- fn list ( & self , primary_namespace : & str , secondary_namespace : & str ) -> lightning:: io:: Result < Vec < String > > {
303
+ fn list (
304
+ & self , primary_namespace : & str , secondary_namespace : & str ,
305
+ ) -> lightning:: io:: Result < Vec < String > > {
294
306
check_namespace_key_validity ( primary_namespace, secondary_namespace, None , "list" ) ?;
295
307
296
308
let prefixed_dest = self . get_dest_dir_path ( primary_namespace, secondary_namespace) ?;
@@ -327,10 +339,17 @@ impl KVStore for FilesystemStore {
327
339
328
340
// If we otherwise don't find a file at the given path something went wrong.
329
341
if !metadata. is_file ( ) {
330
- debug_assert ! ( false , "Failed to list keys of {}/{}: file couldn't be accessed." ,
331
- PrintableString ( primary_namespace) , PrintableString ( secondary_namespace) ) ;
332
- let msg = format ! ( "Failed to list keys of {}/{}: file couldn't be accessed." ,
333
- PrintableString ( primary_namespace) , PrintableString ( secondary_namespace) ) ;
342
+ debug_assert ! (
343
+ false ,
344
+ "Failed to list keys of {}/{}: file couldn't be accessed." ,
345
+ PrintableString ( primary_namespace) ,
346
+ PrintableString ( secondary_namespace)
347
+ ) ;
348
+ let msg = format ! (
349
+ "Failed to list keys of {}/{}: file couldn't be accessed." ,
350
+ PrintableString ( primary_namespace) ,
351
+ PrintableString ( secondary_namespace)
352
+ ) ;
334
353
return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
335
354
}
336
355
@@ -341,20 +360,39 @@ impl KVStore for FilesystemStore {
341
360
keys. push ( relative_path. to_string ( ) )
342
361
}
343
362
} else {
344
- debug_assert ! ( false , "Failed to list keys of {}/{}: file path is not valid UTF-8" ,
345
- PrintableString ( primary_namespace) , PrintableString ( secondary_namespace) ) ;
346
- let msg = format ! ( "Failed to list keys of {}/{}: file path is not valid UTF-8" ,
347
- PrintableString ( primary_namespace) , PrintableString ( secondary_namespace) ) ;
348
- return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
363
+ debug_assert ! (
364
+ false ,
365
+ "Failed to list keys of {}/{}: file path is not valid UTF-8" ,
366
+ PrintableString ( primary_namespace) ,
367
+ PrintableString ( secondary_namespace)
368
+ ) ;
369
+ let msg = format ! (
370
+ "Failed to list keys of {}/{}: file path is not valid UTF-8" ,
371
+ PrintableString ( primary_namespace) ,
372
+ PrintableString ( secondary_namespace)
373
+ ) ;
374
+ return Err ( lightning:: io:: Error :: new (
375
+ lightning:: io:: ErrorKind :: Other ,
376
+ msg,
377
+ ) ) ;
349
378
}
350
- }
379
+ } ,
351
380
Err ( e) => {
352
- debug_assert ! ( false , "Failed to list keys of {}/{}: {}" ,
353
- PrintableString ( primary_namespace) , PrintableString ( secondary_namespace) , e) ;
354
- let msg = format ! ( "Failed to list keys of {}/{}: {}" ,
355
- PrintableString ( primary_namespace) , PrintableString ( secondary_namespace) , e) ;
381
+ debug_assert ! (
382
+ false ,
383
+ "Failed to list keys of {}/{}: {}" ,
384
+ PrintableString ( primary_namespace) ,
385
+ PrintableString ( secondary_namespace) ,
386
+ e
387
+ ) ;
388
+ let msg = format ! (
389
+ "Failed to list keys of {}/{}: {}" ,
390
+ PrintableString ( primary_namespace) ,
391
+ PrintableString ( secondary_namespace) ,
392
+ e
393
+ ) ;
356
394
return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
357
- }
395
+ } ,
358
396
}
359
397
}
360
398
@@ -371,14 +409,14 @@ mod tests {
371
409
372
410
use bitcoin:: Txid ;
373
411
374
- use lightning:: chain:: ChannelMonitorUpdateStatus ;
375
412
use lightning:: chain:: chainmonitor:: Persist ;
376
413
use lightning:: chain:: transaction:: OutPoint ;
414
+ use lightning:: chain:: ChannelMonitorUpdateStatus ;
377
415
use lightning:: check_closed_event;
378
416
use lightning:: events:: { ClosureReason , MessageSendEventsProvider } ;
379
417
use lightning:: ln:: functional_test_utils:: * ;
380
- use lightning:: util:: test_utils;
381
418
use lightning:: util:: persist:: read_channel_monitors;
419
+ use lightning:: util:: test_utils;
382
420
use std:: str:: FromStr ;
383
421
384
422
impl Drop for FilesystemStore {
@@ -387,7 +425,7 @@ mod tests {
387
425
// fails.
388
426
match fs:: remove_dir_all ( & self . data_dir ) {
389
427
Err ( e) => println ! ( "Failed to remove test persister directory: {}" , e) ,
390
- _ => { }
428
+ _ => { } ,
391
429
}
392
430
}
393
431
}
@@ -411,14 +449,23 @@ mod tests {
411
449
412
450
let chanmon_cfgs = create_chanmon_cfgs ( 1 ) ;
413
451
let mut node_cfgs = create_node_cfgs ( 1 , & chanmon_cfgs) ;
414
- let chain_mon_0 = test_utils:: TestChainMonitor :: new ( Some ( & chanmon_cfgs[ 0 ] . chain_source ) , & chanmon_cfgs[ 0 ] . tx_broadcaster , & chanmon_cfgs[ 0 ] . logger , & chanmon_cfgs[ 0 ] . fee_estimator , & store, node_cfgs[ 0 ] . keys_manager ) ;
452
+ let chain_mon_0 = test_utils:: TestChainMonitor :: new (
453
+ Some ( & chanmon_cfgs[ 0 ] . chain_source ) ,
454
+ & chanmon_cfgs[ 0 ] . tx_broadcaster ,
455
+ & chanmon_cfgs[ 0 ] . logger ,
456
+ & chanmon_cfgs[ 0 ] . fee_estimator ,
457
+ & store,
458
+ node_cfgs[ 0 ] . keys_manager ,
459
+ ) ;
415
460
node_cfgs[ 0 ] . chain_monitor = chain_mon_0;
416
461
let node_chanmgrs = create_node_chanmgrs ( 1 , & node_cfgs, & [ None ] ) ;
417
462
let nodes = create_network ( 1 , & node_cfgs, & node_chanmgrs) ;
418
463
419
464
// Check that read_channel_monitors() returns error if monitors/ is not a
420
465
// directory.
421
- assert ! ( read_channel_monitors( & store, nodes[ 0 ] . keys_manager, nodes[ 0 ] . keys_manager) . is_err( ) ) ;
466
+ assert ! (
467
+ read_channel_monitors( & store, nodes[ 0 ] . keys_manager, nodes[ 0 ] . keys_manager) . is_err( )
468
+ ) ;
422
469
}
423
470
424
471
#[ test]
@@ -446,8 +493,21 @@ mod tests {
446
493
let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
447
494
let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
448
495
let error_message = "Channel force-closed" ;
449
- nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan. 2 , & nodes[ 0 ] . node . get_our_node_id ( ) , error_message. to_string ( ) ) . unwrap ( ) ;
450
- check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed { broadcasted_latest_txn: Some ( true ) } , [ nodes[ 0 ] . node. get_our_node_id( ) ] , 100000 ) ;
496
+ nodes[ 1 ]
497
+ . node
498
+ . force_close_broadcasting_latest_txn (
499
+ & chan. 2 ,
500
+ & nodes[ 0 ] . node . get_our_node_id ( ) ,
501
+ error_message. to_string ( ) ,
502
+ )
503
+ . unwrap ( ) ;
504
+ check_closed_event ! (
505
+ nodes[ 1 ] ,
506
+ 1 ,
507
+ ClosureReason :: HolderForceClosed { broadcasted_latest_txn: Some ( true ) } ,
508
+ [ nodes[ 0 ] . node. get_our_node_id( ) ] ,
509
+ 100000
510
+ ) ;
451
511
let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
452
512
453
513
// Set the store's directory to read-only, which should result in
@@ -459,12 +519,15 @@ mod tests {
459
519
fs:: set_permissions ( path, perms) . unwrap ( ) ;
460
520
461
521
let test_txo = OutPoint {
462
- txid : Txid :: from_str ( "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ) . unwrap ( ) ,
463
- index : 0
522
+ txid : Txid :: from_str (
523
+ "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ,
524
+ )
525
+ . unwrap ( ) ,
526
+ index : 0 ,
464
527
} ;
465
528
match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 ) {
466
529
ChannelMonitorUpdateStatus :: UnrecoverableError => { } ,
467
- _ => panic ! ( "unexpected result from persisting new channel" )
530
+ _ => panic ! ( "unexpected result from persisting new channel" ) ,
468
531
}
469
532
470
533
nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
@@ -484,8 +547,21 @@ mod tests {
484
547
let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
485
548
let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
486
549
let error_message = "Channel force-closed" ;
487
- nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan. 2 , & nodes[ 0 ] . node . get_our_node_id ( ) , error_message. to_string ( ) ) . unwrap ( ) ;
488
- check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed { broadcasted_latest_txn: Some ( true ) } , [ nodes[ 0 ] . node. get_our_node_id( ) ] , 100000 ) ;
550
+ nodes[ 1 ]
551
+ . node
552
+ . force_close_broadcasting_latest_txn (
553
+ & chan. 2 ,
554
+ & nodes[ 0 ] . node . get_our_node_id ( ) ,
555
+ error_message. to_string ( ) ,
556
+ )
557
+ . unwrap ( ) ;
558
+ check_closed_event ! (
559
+ nodes[ 1 ] ,
560
+ 1 ,
561
+ ClosureReason :: HolderForceClosed { broadcasted_latest_txn: Some ( true ) } ,
562
+ [ nodes[ 0 ] . node. get_our_node_id( ) ] ,
563
+ 100000
564
+ ) ;
489
565
let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
490
566
let update_map = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) ;
491
567
let update_id = update_map. get ( & added_monitors[ 0 ] . 1 . channel_id ( ) ) . unwrap ( ) ;
@@ -497,12 +573,15 @@ mod tests {
497
573
let store = FilesystemStore :: new ( ":<>/" . into ( ) ) ;
498
574
499
575
let test_txo = OutPoint {
500
- txid : Txid :: from_str ( "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ) . unwrap ( ) ,
501
- index : 0
576
+ txid : Txid :: from_str (
577
+ "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ,
578
+ )
579
+ . unwrap ( ) ,
580
+ index : 0 ,
502
581
} ;
503
582
match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 ) {
504
583
ChannelMonitorUpdateStatus :: UnrecoverableError => { } ,
505
- _ => panic ! ( "unexpected result from persisting new channel" )
584
+ _ => panic ! ( "unexpected result from persisting new channel" ) ,
506
585
}
507
586
508
587
nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
@@ -520,6 +599,10 @@ pub mod bench {
520
599
let store_a = super :: FilesystemStore :: new ( "bench_filesystem_store_a" . into ( ) ) ;
521
600
let store_b = super :: FilesystemStore :: new ( "bench_filesystem_store_b" . into ( ) ) ;
522
601
lightning:: ln:: channelmanager:: bench:: bench_two_sends (
523
- bench, "bench_filesystem_persisted_sends" , store_a, store_b) ;
602
+ bench,
603
+ "bench_filesystem_persisted_sends" ,
604
+ store_a,
605
+ store_b,
606
+ ) ;
524
607
}
525
608
}
0 commit comments