@@ -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 ) -> lightning:: io:: Result < Vec < u8 > > {
96+ fn read (
97+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
98+ ) -> lightning:: 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 ] ) -> lightning:: io:: Result < ( ) > {
121+ fn write (
122+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : & [ u8 ] ,
123+ ) -> lightning:: 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. into ( ) ) ,
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 ) -> lightning:: io:: Result < ( ) > {
208+ fn remove (
209+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
210+ ) -> lightning:: 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 ) -> lightning:: io:: Result < Vec < String > > {
303+ fn list (
304+ & self , primary_namespace : & str , secondary_namespace : & str ,
305+ ) -> lightning:: 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 ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
335354 }
336355
@@ -341,20 +360,39 @@ 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) ) ;
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+ ) ) ;
349378 }
350- }
379+ } ,
351380 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+ ) ;
356394 return Err ( lightning:: io:: Error :: new ( lightning:: io:: ErrorKind :: Other , msg) ) ;
357- }
395+ } ,
358396 }
359397 }
360398
@@ -371,14 +409,14 @@ mod tests {
371409
372410 use bitcoin:: Txid ;
373411
374- use lightning:: chain:: ChannelMonitorUpdateStatus ;
375412 use lightning:: chain:: chainmonitor:: Persist ;
376413 use lightning:: chain:: transaction:: OutPoint ;
414+ use lightning:: chain:: ChannelMonitorUpdateStatus ;
377415 use lightning:: check_closed_event;
378416 use lightning:: events:: { ClosureReason , MessageSendEventsProvider } ;
379417 use lightning:: ln:: functional_test_utils:: * ;
380- use lightning:: util:: test_utils;
381418 use lightning:: util:: persist:: read_channel_monitors;
419+ use lightning:: util:: test_utils;
382420 use std:: str:: FromStr ;
383421
384422 impl Drop for FilesystemStore {
@@ -387,7 +425,7 @@ mod tests {
387425 // fails.
388426 match fs:: remove_dir_all ( & self . data_dir ) {
389427 Err ( e) => println ! ( "Failed to remove test persister directory: {}" , e) ,
390- _ => { }
428+ _ => { } ,
391429 }
392430 }
393431 }
@@ -411,14 +449,23 @@ mod tests {
411449
412450 let chanmon_cfgs = create_chanmon_cfgs ( 1 ) ;
413451 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+ ) ;
415460 node_cfgs[ 0 ] . chain_monitor = chain_mon_0;
416461 let node_chanmgrs = create_node_chanmgrs ( 1 , & node_cfgs, & [ None ] ) ;
417462 let nodes = create_network ( 1 , & node_cfgs, & node_chanmgrs) ;
418463
419464 // Check that read_channel_monitors() returns error if monitors/ is not a
420465 // 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+ ) ;
422469 }
423470
424471 #[ test]
@@ -446,8 +493,21 @@ mod tests {
446493 let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
447494 let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
448495 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+ ) ;
451511 let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
452512
453513 // Set the store's directory to read-only, which should result in
@@ -459,12 +519,15 @@ mod tests {
459519 fs:: set_permissions ( path, perms) . unwrap ( ) ;
460520
461521 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 ,
464527 } ;
465528 match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 ) {
466529 ChannelMonitorUpdateStatus :: UnrecoverableError => { } ,
467- _ => panic ! ( "unexpected result from persisting new channel" )
530+ _ => panic ! ( "unexpected result from persisting new channel" ) ,
468531 }
469532
470533 nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
@@ -484,8 +547,21 @@ mod tests {
484547 let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
485548 let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
486549 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+ ) ;
489565 let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
490566 let update_map = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) ;
491567 let update_id = update_map. get ( & added_monitors[ 0 ] . 1 . channel_id ( ) ) . unwrap ( ) ;
@@ -497,12 +573,15 @@ mod tests {
497573 let store = FilesystemStore :: new ( ":<>/" . into ( ) ) ;
498574
499575 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 ,
502581 } ;
503582 match store. persist_new_channel ( test_txo, & added_monitors[ 0 ] . 1 ) {
504583 ChannelMonitorUpdateStatus :: UnrecoverableError => { } ,
505- _ => panic ! ( "unexpected result from persisting new channel" )
584+ _ => panic ! ( "unexpected result from persisting new channel" ) ,
506585 }
507586
508587 nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
@@ -520,6 +599,10 @@ pub mod bench {
520599 let store_a = super :: FilesystemStore :: new ( "bench_filesystem_store_a" . into ( ) ) ;
521600 let store_b = super :: FilesystemStore :: new ( "bench_filesystem_store_b" . into ( ) ) ;
522601 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+ ) ;
524607 }
525608}
0 commit comments