@@ -67,7 +67,9 @@ impl FilesystemStore {
6767 }
6868 }
6969
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 > {
7173 let mut dest_dir_path = {
7274 #[ cfg( target_os = "windows" ) ]
7375 {
@@ -91,7 +93,9 @@ impl FilesystemStore {
9193}
9294
9395impl KVStore for FilesystemStore {
94- fn read ( & self , primary_namespace : & str , secondary_namespace : & str , key : & str ) -> std:: io:: Result < Vec < u8 > > {
96+ fn read (
97+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
98+ ) -> std:: io:: Result < Vec < u8 > > {
9599 check_namespace_key_validity ( primary_namespace, secondary_namespace, Some ( key) , "read" ) ?;
96100
97101 let mut dest_file_path = self . get_dest_dir_path ( primary_namespace, secondary_namespace) ?;
@@ -114,19 +118,19 @@ impl KVStore for FilesystemStore {
114118 Ok ( buf)
115119 }
116120
117- fn write ( & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : & [ u8 ] ) -> std:: io:: Result < ( ) > {
121+ fn write (
122+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : & [ u8 ] ,
123+ ) -> std:: io:: Result < ( ) > {
118124 check_namespace_key_validity ( primary_namespace, secondary_namespace, Some ( key) , "write" ) ?;
119125
120126 let mut dest_file_path = self . get_dest_dir_path ( primary_namespace, secondary_namespace) ?;
121127 dest_file_path. push ( key) ;
122128
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+ } ) ?;
130134 fs:: create_dir_all ( & parent_directory) ?;
131135
132136 // Do a crazy dance with lots of fsync()s to be overly cautious here...
@@ -186,11 +190,11 @@ impl KVStore for FilesystemStore {
186190 match res {
187191 Ok ( ( ) ) => {
188192 // 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) ?;
191195 dest_file. sync_all ( ) ?;
192196 Ok ( ( ) )
193- }
197+ } ,
194198 Err ( e) => Err ( e) ,
195199 }
196200 }
@@ -201,7 +205,9 @@ impl KVStore for FilesystemStore {
201205 res
202206 }
203207
204- fn remove ( & self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ) -> std:: io:: Result < ( ) > {
208+ fn remove (
209+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
210+ ) -> std:: io:: Result < ( ) > {
205211 check_namespace_key_validity ( primary_namespace, secondary_namespace, Some ( key) , "remove" ) ?;
206212
207213 let mut dest_file_path = self . get_dest_dir_path ( primary_namespace, secondary_namespace) ?;
@@ -229,8 +235,10 @@ impl KVStore for FilesystemStore {
229235 fs:: remove_file ( & dest_file_path) ?;
230236
231237 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+ ) ;
234242 std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidInput , msg)
235243 } ) ?;
236244 let dir_file = fs:: OpenOptions :: new ( ) . read ( true ) . open ( parent_directory) ?;
@@ -257,8 +265,8 @@ impl KVStore for FilesystemStore {
257265 // However, all this is partially based on assumptions and local experiments, as
258266 // Windows API is horribly underdocumented.
259267 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 ) ) ;
262270 trash_file_path. set_extension ( trash_file_ext) ;
263271
264272 call ! ( unsafe {
@@ -273,7 +281,9 @@ impl KVStore for FilesystemStore {
273281 {
274282 // We fsync the trash file in hopes this will also flush the original's file
275283 // 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 )
277287 . open ( & trash_file_path. clone ( ) ) ?;
278288 trash_file. sync_all ( ) ?;
279289 }
@@ -290,7 +300,9 @@ impl KVStore for FilesystemStore {
290300 Ok ( ( ) )
291301 }
292302
293- fn list ( & self , primary_namespace : & str , secondary_namespace : & str ) -> std:: io:: Result < Vec < String > > {
303+ fn list (
304+ & self , primary_namespace : & str , secondary_namespace : & str ,
305+ ) -> std:: io:: Result < Vec < String > > {
294306 check_namespace_key_validity ( primary_namespace, secondary_namespace, None , "list" ) ?;
295307
296308 let prefixed_dest = self . get_dest_dir_path ( primary_namespace, secondary_namespace) ?;
@@ -327,10 +339,17 @@ impl KVStore for FilesystemStore {
327339
328340 // If we otherwise don't find a file at the given path something went wrong.
329341 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+ ) ;
334353 return Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg) ) ;
335354 }
336355
@@ -341,20 +360,36 @@ impl KVStore for FilesystemStore {
341360 keys. push ( relative_path. to_string ( ) )
342361 }
343362 } 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) ) ;
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+ ) ;
348374 return Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg) ) ;
349375 }
350- }
376+ } ,
351377 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) ;
378+ debug_assert ! (
379+ false ,
380+ "Failed to list keys of {}/{}: {}" ,
381+ PrintableString ( primary_namespace) ,
382+ PrintableString ( secondary_namespace) ,
383+ e
384+ ) ;
385+ let msg = format ! (
386+ "Failed to list keys of {}/{}: {}" ,
387+ PrintableString ( primary_namespace) ,
388+ PrintableString ( secondary_namespace) ,
389+ e
390+ ) ;
356391 return Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , msg) ) ;
357- }
392+ } ,
358393 }
359394 }
360395
@@ -371,14 +406,14 @@ mod tests {
371406
372407 use bitcoin:: Txid ;
373408
374- use lightning:: chain:: ChannelMonitorUpdateStatus ;
375409 use lightning:: chain:: chainmonitor:: Persist ;
376410 use lightning:: chain:: transaction:: OutPoint ;
411+ use lightning:: chain:: ChannelMonitorUpdateStatus ;
377412 use lightning:: check_closed_event;
378413 use lightning:: events:: { ClosureReason , MessageSendEventsProvider } ;
379414 use lightning:: ln:: functional_test_utils:: * ;
380- use lightning:: util:: test_utils;
381415 use lightning:: util:: persist:: read_channel_monitors;
416+ use lightning:: util:: test_utils;
382417 use std:: str:: FromStr ;
383418
384419 impl Drop for FilesystemStore {
@@ -387,7 +422,7 @@ mod tests {
387422 // fails.
388423 match fs:: remove_dir_all ( & self . data_dir ) {
389424 Err ( e) => println ! ( "Failed to remove test persister directory: {}" , e) ,
390- _ => { }
425+ _ => { } ,
391426 }
392427 }
393428 }
@@ -411,14 +446,23 @@ mod tests {
411446
412447 let chanmon_cfgs = create_chanmon_cfgs ( 1 ) ;
413448 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 ) ;
449+ let chain_mon_0 = test_utils:: TestChainMonitor :: new (
450+ Some ( & chanmon_cfgs[ 0 ] . chain_source ) ,
451+ & chanmon_cfgs[ 0 ] . tx_broadcaster ,
452+ & chanmon_cfgs[ 0 ] . logger ,
453+ & chanmon_cfgs[ 0 ] . fee_estimator ,
454+ & store,
455+ node_cfgs[ 0 ] . keys_manager ,
456+ ) ;
415457 node_cfgs[ 0 ] . chain_monitor = chain_mon_0;
416458 let node_chanmgrs = create_node_chanmgrs ( 1 , & node_cfgs, & [ None ] ) ;
417459 let nodes = create_network ( 1 , & node_cfgs, & node_chanmgrs) ;
418460
419461 // Check that read_channel_monitors() returns error if monitors/ is not a
420462 // directory.
421- assert ! ( read_channel_monitors( & store, nodes[ 0 ] . keys_manager, nodes[ 0 ] . keys_manager) . is_err( ) ) ;
463+ assert ! (
464+ read_channel_monitors( & store, nodes[ 0 ] . keys_manager, nodes[ 0 ] . keys_manager) . is_err( )
465+ ) ;
422466 }
423467
424468 #[ test]
@@ -446,8 +490,21 @@ mod tests {
446490 let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
447491 let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
448492 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 ) ;
493+ nodes[ 1 ]
494+ . node
495+ . force_close_broadcasting_latest_txn (
496+ & chan. 2 ,
497+ & nodes[ 0 ] . node . get_our_node_id ( ) ,
498+ error_message. to_string ( ) ,
499+ )
500+ . unwrap ( ) ;
501+ check_closed_event ! (
502+ nodes[ 1 ] ,
503+ 1 ,
504+ ClosureReason :: HolderForceClosed { broadcasted_latest_txn: Some ( true ) } ,
505+ [ nodes[ 0 ] . node. get_our_node_id( ) ] ,
506+ 100000
507+ ) ;
451508 let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
452509
453510 // Set the store's directory to read-only, which should result in
@@ -459,12 +516,15 @@ mod tests {
459516 fs:: set_permissions ( path, perms) . unwrap ( ) ;
460517
461518 let test_txo = OutPoint {
462- txid : Txid :: from_str ( "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ) . unwrap ( ) ,
463- index : 0
519+ txid : Txid :: from_str (
520+ "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ,
521+ )
522+ . unwrap ( ) ,
523+ index : 0 ,
464524 } ;
465525 match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 ) {
466526 ChannelMonitorUpdateStatus :: UnrecoverableError => { } ,
467- _ => panic ! ( "unexpected result from persisting new channel" )
527+ _ => panic ! ( "unexpected result from persisting new channel" ) ,
468528 }
469529
470530 nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
@@ -484,8 +544,21 @@ mod tests {
484544 let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
485545 let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
486546 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 ) ;
547+ nodes[ 1 ]
548+ . node
549+ . force_close_broadcasting_latest_txn (
550+ & chan. 2 ,
551+ & nodes[ 0 ] . node . get_our_node_id ( ) ,
552+ error_message. to_string ( ) ,
553+ )
554+ . unwrap ( ) ;
555+ check_closed_event ! (
556+ nodes[ 1 ] ,
557+ 1 ,
558+ ClosureReason :: HolderForceClosed { broadcasted_latest_txn: Some ( true ) } ,
559+ [ nodes[ 0 ] . node. get_our_node_id( ) ] ,
560+ 100000
561+ ) ;
489562 let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
490563 let update_map = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) ;
491564 let update_id = update_map. get ( & added_monitors[ 0 ] . 1 . channel_id ( ) ) . unwrap ( ) ;
@@ -497,12 +570,15 @@ mod tests {
497570 let store = FilesystemStore :: new ( ":<>/" . into ( ) ) ;
498571
499572 let test_txo = OutPoint {
500- txid : Txid :: from_str ( "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ) . unwrap ( ) ,
501- index : 0
573+ txid : Txid :: from_str (
574+ "8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be" ,
575+ )
576+ . unwrap ( ) ,
577+ index : 0 ,
502578 } ;
503579 match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 ) {
504580 ChannelMonitorUpdateStatus :: UnrecoverableError => { } ,
505- _ => panic ! ( "unexpected result from persisting new channel" )
581+ _ => panic ! ( "unexpected result from persisting new channel" ) ,
506582 }
507583
508584 nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
@@ -520,6 +596,10 @@ pub mod bench {
520596 let store_a = super :: FilesystemStore :: new ( "bench_filesystem_store_a" . into ( ) ) ;
521597 let store_b = super :: FilesystemStore :: new ( "bench_filesystem_store_b" . into ( ) ) ;
522598 lightning:: ln:: channelmanager:: bench:: bench_two_sends (
523- bench, "bench_filesystem_persisted_sends" , store_a, store_b) ;
599+ bench,
600+ "bench_filesystem_persisted_sends" ,
601+ store_a,
602+ store_b,
603+ ) ;
524604 }
525605}
0 commit comments