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 namespace under which the [`ChannelManager`] will be persisted.
30
32
pub const CHANNEL_MANAGER_PERSISTENCE_NAMESPACE : & str = "" ;
@@ -156,4 +158,48 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner, K: KVStore> Persist<ChannelSign
156
158
}
157
159
}
158
160
159
-
161
+ /// Read previously persisted [`ChannelMonitor`]s from the store.
162
+ pub fn read_channel_monitors < K : Deref , ES : Deref , SP : Deref > (
163
+ kv_store : K , entropy_source : ES , signer_provider : SP ,
164
+ ) -> crate :: io:: Result < Vec < ( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: Signer > ) > >
165
+ where
166
+ K :: Target : KVStore ,
167
+ ES :: Target : EntropySource + Sized ,
168
+ SP :: Target : SignerProvider + Sized ,
169
+ {
170
+ let mut res = Vec :: new ( ) ;
171
+
172
+ for stored_key in kv_store. list ( CHANNEL_MONITOR_PERSISTENCE_NAMESPACE ) ? {
173
+ let txid = Txid :: from_hex ( stored_key. split_at ( 64 ) . 0 ) . map_err ( |_| {
174
+ crate :: io:: Error :: new ( crate :: io:: ErrorKind :: InvalidData , "Invalid tx ID in stored key" )
175
+ } ) ?;
176
+
177
+ let index: u16 = stored_key. split_at ( 65 ) . 1 . parse ( ) . map_err ( |_| {
178
+ crate :: io:: Error :: new ( crate :: io:: ErrorKind :: InvalidData , "Invalid tx index in stored key" )
179
+ } ) ?;
180
+
181
+ match <( BlockHash , ChannelMonitor < <SP :: Target as SignerProvider >:: Signer > ) >:: read (
182
+ & mut kv_store. read ( CHANNEL_MONITOR_PERSISTENCE_NAMESPACE , & stored_key) ?,
183
+ ( & * entropy_source, & * signer_provider) ,
184
+ ) {
185
+ Ok ( ( block_hash, channel_monitor) ) => {
186
+ if channel_monitor. get_funding_txo ( ) . 0 . txid != txid
187
+ || channel_monitor. get_funding_txo ( ) . 0 . index != index
188
+ {
189
+ return Err ( crate :: io:: Error :: new (
190
+ crate :: io:: ErrorKind :: InvalidData ,
191
+ "ChannelMonitor was stored under the wrong key" ,
192
+ ) ) ;
193
+ }
194
+ res. push ( ( block_hash, channel_monitor) ) ;
195
+ }
196
+ Err ( _) => {
197
+ return Err ( crate :: io:: Error :: new (
198
+ crate :: io:: ErrorKind :: InvalidData ,
199
+ "Failed to deserialize ChannelMonitor"
200
+ ) )
201
+ }
202
+ }
203
+ }
204
+ Ok ( res)
205
+ }
0 commit comments