@@ -17,6 +17,7 @@ use lightning::util::logger::Logger;
1717use lightning:: util:: ser:: Writeable ;
1818use std:: fs;
1919use std:: io:: Error ;
20+ use std:: path:: PathBuf ;
2021use std:: sync:: Arc ;
2122
2223#[ cfg( test) ]
@@ -75,6 +76,12 @@ impl FilesystemPersister {
7576 self . path_to_channel_data . clone ( )
7677 }
7778
79+ pub ( crate ) fn path_to_monitor_data ( & self ) -> PathBuf {
80+ let mut path = PathBuf :: from ( self . path_to_channel_data . clone ( ) ) ;
81+ path. push ( "monitors" ) ;
82+ path
83+ }
84+
7885 /// Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
7986 /// initialization, within a file called "manager".
8087 pub fn persist_manager < Signer , M , T , K , F , L > (
@@ -88,54 +95,55 @@ impl FilesystemPersister {
8895 F : FeeEstimator ,
8996 L : Logger
9097 {
91- util:: write_to_file ( data_dir, "manager" . to_string ( ) , manager)
98+ let path = PathBuf :: from ( data_dir) ;
99+ util:: write_to_file ( path, "manager" . to_string ( ) , manager)
92100 }
93101
94102 #[ cfg( test) ]
95103 fn load_channel_data < Keys : KeysInterface > ( & self , keys : & Keys ) ->
96104 Result < HashMap < OutPoint , ChannelMonitor < Keys :: Signer > > , ChannelMonitorUpdateErr > {
97- if let Err ( _) = fs:: create_dir_all ( & self . path_to_channel_data ) {
98- return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ;
99- }
100- let mut res = HashMap :: new ( ) ;
101- for file_option in fs:: read_dir ( & self . path_to_channel_data ) . unwrap ( ) {
102- let file = file_option. unwrap ( ) ;
103- let owned_file_name = file. file_name ( ) ;
104- let filename = owned_file_name. to_str ( ) ;
105- if !filename. is_some ( ) || !filename. unwrap ( ) . is_ascii ( ) || filename. unwrap ( ) . len ( ) < 65 {
105+ if let Err ( _) = fs:: create_dir_all ( self . path_to_monitor_data ( ) ) {
106106 return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ;
107107 }
108+ let mut res = HashMap :: new ( ) ;
109+ for file_option in fs:: read_dir ( self . path_to_monitor_data ( ) ) . unwrap ( ) {
110+ let file = file_option. unwrap ( ) ;
111+ let owned_file_name = file. file_name ( ) ;
112+ let filename = owned_file_name. to_str ( ) ;
113+ if !filename. is_some ( ) || !filename. unwrap ( ) . is_ascii ( ) || filename. unwrap ( ) . len ( ) < 65 {
114+ return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ;
115+ }
108116
109- let txid = Txid :: from_hex ( filename. unwrap ( ) . split_at ( 64 ) . 0 ) ;
110- if txid. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
117+ let txid = Txid :: from_hex ( filename. unwrap ( ) . split_at ( 64 ) . 0 ) ;
118+ if txid. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
111119
112- let index = filename. unwrap ( ) . split_at ( 65 ) . 1 . split ( '.' ) . next ( ) . unwrap ( ) . parse ( ) ;
113- if index. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
120+ let index = filename. unwrap ( ) . split_at ( 65 ) . 1 . split ( '.' ) . next ( ) . unwrap ( ) . parse ( ) ;
121+ if index. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
114122
115- let contents = fs:: read ( & file. path ( ) ) ;
116- if contents. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
123+ let contents = fs:: read ( & file. path ( ) ) ;
124+ if contents. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
117125
118- if let Ok ( ( _, loaded_monitor) ) =
119- <( BlockHash , ChannelMonitor < Keys :: Signer > ) >:: read ( & mut Cursor :: new ( & contents. unwrap ( ) ) , keys) {
120- res. insert ( OutPoint { txid : txid. unwrap ( ) , index : index. unwrap ( ) } , loaded_monitor) ;
121- } else {
122- return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ;
126+ if let Ok ( ( _, loaded_monitor) ) =
127+ <( BlockHash , ChannelMonitor < Keys :: Signer > ) >:: read ( & mut Cursor :: new ( & contents. unwrap ( ) ) , keys) {
128+ res. insert ( OutPoint { txid : txid. unwrap ( ) , index : index. unwrap ( ) } , loaded_monitor) ;
129+ } else {
130+ return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ;
131+ }
123132 }
133+ Ok ( res)
124134 }
125- Ok ( res)
126- }
127135}
128136
129137impl < ChannelSigner : Sign + Send + Sync > channelmonitor:: Persist < ChannelSigner > for FilesystemPersister {
130138 fn persist_new_channel ( & self , funding_txo : OutPoint , monitor : & ChannelMonitor < ChannelSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > {
131139 let filename = format ! ( "{}_{}" , funding_txo. txid. to_hex( ) , funding_txo. index) ;
132- util:: write_to_file ( self . path_to_channel_data . clone ( ) , filename, monitor)
140+ util:: write_to_file ( self . path_to_monitor_data ( ) , filename, monitor)
133141 . map_err ( |_| ChannelMonitorUpdateErr :: PermanentFailure )
134142 }
135143
136144 fn update_persisted_channel ( & self , funding_txo : OutPoint , _update : & ChannelMonitorUpdate , monitor : & ChannelMonitor < ChannelSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > {
137145 let filename = format ! ( "{}_{}" , funding_txo. txid. to_hex( ) , funding_txo. index) ;
138- util:: write_to_file ( self . path_to_channel_data . clone ( ) , filename, monitor)
146+ util:: write_to_file ( self . path_to_monitor_data ( ) , filename, monitor)
139147 . map_err ( |_| ChannelMonitorUpdateErr :: PermanentFailure )
140148 }
141149}
0 commit comments