@@ -17,6 +17,7 @@ use lightning::util::logger::Logger;
17
17
use lightning:: util:: ser:: Writeable ;
18
18
use std:: fs;
19
19
use std:: io:: Error ;
20
+ use std:: path:: PathBuf ;
20
21
use std:: sync:: Arc ;
21
22
22
23
#[ cfg( test) ]
@@ -75,6 +76,12 @@ impl FilesystemPersister {
75
76
self . path_to_channel_data . clone ( )
76
77
}
77
78
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
+
78
85
/// Writes the provided `ChannelManager` to the path provided at `FilesystemPersister`
79
86
/// initialization, within a file called "manager".
80
87
pub fn persist_manager < Signer , M , T , K , F , L > (
@@ -94,48 +101,48 @@ impl FilesystemPersister {
94
101
#[ cfg( test) ]
95
102
fn load_channel_data < Keys : KeysInterface > ( & self , keys : & Keys ) ->
96
103
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 ( ) ) {
106
105
return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ;
107
106
}
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
+ }
108
115
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 ) ; }
111
118
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 ) ; }
114
121
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 ) ; }
117
124
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
+ }
123
131
}
132
+ Ok ( res)
124
133
}
125
- Ok ( res)
126
- }
127
134
}
128
135
129
136
impl < ChannelSigner : Sign + Send + Sync > channelmonitor:: Persist < ChannelSigner > for FilesystemPersister {
130
137
fn persist_new_channel ( & self , funding_txo : OutPoint , monitor : & ChannelMonitor < ChannelSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > {
131
138
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)
133
140
. map_err ( |_| ChannelMonitorUpdateErr :: PermanentFailure )
134
141
}
135
142
136
143
fn update_persisted_channel ( & self , funding_txo : OutPoint , _update : & ChannelMonitorUpdate , monitor : & ChannelMonitor < ChannelSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > {
137
144
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)
139
146
. map_err ( |_| ChannelMonitorUpdateErr :: PermanentFailure )
140
147
}
141
148
}
0 commit comments