9
9
//! and [`ChannelMonitor`] all in one place.
10
10
11
11
use core:: ops:: Deref ;
12
- use bitcoin:: hashes:: hex:: ToHex ;
12
+ use bitcoin:: hashes:: hex:: { FromHex , ToHex } ;
13
+ use bitcoin:: { BlockHash , Txid } ;
14
+
13
15
use crate :: io;
14
16
use crate :: prelude:: { Vec , String } ;
15
17
use crate :: routing:: scoring:: WriteableScore ;
@@ -24,7 +26,7 @@ use crate::ln::channelmanager::ChannelManager;
24
26
use crate :: routing:: router:: Router ;
25
27
use crate :: routing:: gossip:: NetworkGraph ;
26
28
use crate :: util:: logger:: Logger ;
27
- use crate :: util:: ser:: Writeable ;
29
+ use crate :: util:: ser:: { ReadableArgs , Writeable } ;
28
30
29
31
/// The alphabet of characters allowed for namespaces and keys.
30
32
pub const KVSTORE_NAMESPACE_KEY_ALPHABET : & str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" ;
@@ -187,3 +189,52 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStorePersister> Persist<Ch
187
189
}
188
190
}
189
191
}
192
+
193
+ /// Read previously persisted [`ChannelMonitor`]s from the store.
194
+ pub fn read_channel_monitors < K : Deref , ES : Deref , SP : Deref > (
195
+ kv_store : K , entropy_source : ES , signer_provider : SP ,
196
+ ) -> io:: Result < Vec < ( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: Signer > ) > >
197
+ where
198
+ K :: Target : KVStore ,
199
+ ES :: Target : EntropySource + Sized ,
200
+ SP :: Target : SignerProvider + Sized ,
201
+ {
202
+ let mut res = Vec :: new ( ) ;
203
+
204
+ for stored_key in kv_store. list (
205
+ CHANNEL_MONITOR_PERSISTENCE_NAMESPACE , CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE ) ?
206
+ {
207
+ let txid = Txid :: from_hex ( stored_key. split_at ( 64 ) . 0 ) . map_err ( |_| {
208
+ io:: Error :: new ( io:: ErrorKind :: InvalidData , "Invalid tx ID in stored key" )
209
+ } ) ?;
210
+
211
+ let index: u16 = stored_key. split_at ( 65 ) . 1 . parse ( ) . map_err ( |_| {
212
+ io:: Error :: new ( io:: ErrorKind :: InvalidData , "Invalid tx index in stored key" )
213
+ } ) ?;
214
+
215
+ match <( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: Signer > ) >:: read (
216
+ & mut io:: Cursor :: new (
217
+ kv_store. read ( CHANNEL_MONITOR_PERSISTENCE_NAMESPACE , CHANNEL_MONITOR_PERSISTENCE_SUB_NAMESPACE , & stored_key) ?) ,
218
+ ( & * entropy_source, & * signer_provider) ,
219
+ ) {
220
+ Ok ( ( block_hash, channel_monitor) ) => {
221
+ if channel_monitor. get_funding_txo ( ) . 0 . txid != txid
222
+ || channel_monitor. get_funding_txo ( ) . 0 . index != index
223
+ {
224
+ return Err ( io:: Error :: new (
225
+ io:: ErrorKind :: InvalidData ,
226
+ "ChannelMonitor was stored under the wrong key" ,
227
+ ) ) ;
228
+ }
229
+ res. push ( ( block_hash, channel_monitor) ) ;
230
+ }
231
+ Err ( _) => {
232
+ return Err ( io:: Error :: new (
233
+ io:: ErrorKind :: InvalidData ,
234
+ "Failed to deserialize ChannelMonitor"
235
+ ) )
236
+ }
237
+ }
238
+ }
239
+ Ok ( res)
240
+ }
0 commit comments