@@ -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 ) -> String {
80+ let mut path = PathBuf :: from ( self . path_to_channel_data . clone ( ) ) ;
81+ path. push ( "monitors" ) ;
82+ path. to_str ( ) . unwrap ( ) . to_string ( )
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 > (
@@ -94,48 +101,48 @@ impl FilesystemPersister {
94101 #[ cfg( test) ]
95102 fn load_channel_data < Keys : KeysInterface > ( & self , keys : & Keys ) ->
96103 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 {
104+ if let Err ( _) = fs:: create_dir_all ( self . path_to_monitor_data ( ) ) {
106105 return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ;
107106 }
107+ let mut res = HashMap :: new ( ) ;
108+ for file_option in fs:: read_dir ( self . path_to_monitor_data ( ) ) . unwrap ( ) {
109+ let file = file_option. unwrap ( ) ;
110+ let owned_file_name = file. file_name ( ) ;
111+ let filename = owned_file_name. to_str ( ) ;
112+ if !filename. is_some ( ) || !filename. unwrap ( ) . is_ascii ( ) || filename. unwrap ( ) . len ( ) < 65 {
113+ return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ;
114+ }
108115
109- let txid = Txid :: from_hex ( filename. unwrap ( ) . split_at ( 64 ) . 0 ) ;
110- if txid. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
116+ let txid = Txid :: from_hex ( filename. unwrap ( ) . split_at ( 64 ) . 0 ) ;
117+ if txid. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
111118
112- let index = filename. unwrap ( ) . split_at ( 65 ) . 1 . split ( '.' ) . next ( ) . unwrap ( ) . parse ( ) ;
113- if index. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
119+ let index = filename. unwrap ( ) . split_at ( 65 ) . 1 . split ( '.' ) . next ( ) . unwrap ( ) . parse ( ) ;
120+ if index. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
114121
115- let contents = fs:: read ( & file. path ( ) ) ;
116- if contents. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
122+ let contents = fs:: read ( & file. path ( ) ) ;
123+ if contents. is_err ( ) { return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ; }
117124
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 ) ;
125+ if let Ok ( ( _, loaded_monitor) ) =
126+ <( BlockHash , ChannelMonitor < Keys :: Signer > ) >:: read ( & mut Cursor :: new ( & contents. unwrap ( ) ) , keys) {
127+ res. insert ( OutPoint { txid : txid. unwrap ( ) , index : index. unwrap ( ) } , loaded_monitor) ;
128+ } else {
129+ return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ;
130+ }
123131 }
132+ Ok ( res)
124133 }
125- Ok ( res)
126- }
127134}
128135
129136impl < ChannelSigner : Sign + Send + Sync > channelmonitor:: Persist < ChannelSigner > for FilesystemPersister {
130137 fn persist_new_channel ( & self , funding_txo : OutPoint , monitor : & ChannelMonitor < ChannelSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > {
131138 let filename = format ! ( "{}_{}" , funding_txo. txid. to_hex( ) , funding_txo. index) ;
132- util:: write_to_file ( self . path_to_channel_data . clone ( ) , filename, monitor)
139+ util:: write_to_file ( self . path_to_monitor_data ( ) , filename, monitor)
133140 . map_err ( |_| ChannelMonitorUpdateErr :: PermanentFailure )
134141 }
135142
136143 fn update_persisted_channel ( & self , funding_txo : OutPoint , _update : & ChannelMonitorUpdate , monitor : & ChannelMonitor < ChannelSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > {
137144 let filename = format ! ( "{}_{}" , funding_txo. txid. to_hex( ) , funding_txo. index) ;
138- util:: write_to_file ( self . path_to_channel_data . clone ( ) , filename, monitor)
145+ util:: write_to_file ( self . path_to_monitor_data ( ) , filename, monitor)
139146 . map_err ( |_| ChannelMonitorUpdateErr :: PermanentFailure )
140147 }
141148}
0 commit comments