@@ -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 ) -> PathBuf {
80
+ let mut path = PathBuf :: from ( self . path_to_channel_data . clone ( ) ) ;
81
+ path. push ( "monitors" ) ;
82
+ path
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 > (
@@ -88,54 +95,55 @@ impl FilesystemPersister {
88
95
F : FeeEstimator ,
89
96
L : Logger
90
97
{
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)
92
100
}
93
101
94
102
#[ cfg( test) ]
95
103
fn load_channel_data < Keys : KeysInterface > ( & self , keys : & Keys ) ->
96
104
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 ( ) ) {
106
106
return Err ( ChannelMonitorUpdateErr :: PermanentFailure ) ;
107
107
}
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
+ }
108
116
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 ) ; }
111
119
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 ) ; }
114
122
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 ) ; }
117
125
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
+ }
123
132
}
133
+ Ok ( res)
124
134
}
125
- Ok ( res)
126
- }
127
135
}
128
136
129
137
impl < ChannelSigner : Sign + Send + Sync > channelmonitor:: Persist < ChannelSigner > for FilesystemPersister {
130
138
fn persist_new_channel ( & self , funding_txo : OutPoint , monitor : & ChannelMonitor < ChannelSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > {
131
139
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)
133
141
. map_err ( |_| ChannelMonitorUpdateErr :: PermanentFailure )
134
142
}
135
143
136
144
fn update_persisted_channel ( & self , funding_txo : OutPoint , _update : & ChannelMonitorUpdate , monitor : & ChannelMonitor < ChannelSigner > ) -> Result < ( ) , ChannelMonitorUpdateErr > {
137
145
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)
139
147
. map_err ( |_| ChannelMonitorUpdateErr :: PermanentFailure )
140
148
}
141
149
}
0 commit comments