@@ -36,10 +36,11 @@ mod test_utils;
36
36
#[ cfg( any( feature = "rest-client" , feature = "rpc-client" ) ) ]
37
37
mod utils;
38
38
39
- use crate :: poll:: { ChainTip , Poll , ValidatedBlockHeader } ;
39
+ use crate :: poll:: { ChainTip , Poll , Validate , ValidatedBlockHeader } ;
40
40
41
41
use bitcoin:: blockdata:: block:: { Block , BlockHeader } ;
42
42
use bitcoin:: hash_types:: BlockHash ;
43
+ use bitcoin:: network:: constants:: Network ;
43
44
use bitcoin:: util:: uint:: Uint256 ;
44
45
45
46
use std:: future:: Future ;
@@ -168,6 +169,32 @@ pub trait ChainListener {
168
169
fn block_disconnected ( & mut self , header : & BlockHeader , height : u32 ) ;
169
170
}
170
171
172
+ /// Do a one-time sync of a chain listener from a single *trusted* block source bringing its view
173
+ /// of the latest chain tip from old_block to new_block. This is useful on startup when you need
174
+ /// to bring each ChannelMonitor, as well as the overall ChannelManager, into sync with each other.
175
+ ///
176
+ /// Once you have them all at the same block, you should switch to using MicroSPVClient.
177
+ pub async fn init_sync_listener < CL : ChainListener , B : BlockSource > (
178
+ new_block : BlockHash ,
179
+ old_block : BlockHash ,
180
+ block_source : & mut B ,
181
+ network : Network ,
182
+ chain_listener : & mut CL ,
183
+ ) {
184
+ if & old_block[ ..] == & [ 0 ; 32 ] { return ; }
185
+ if old_block == new_block { return ; }
186
+
187
+ let new_header = block_source
188
+ . get_header ( & new_block, None ) . await . unwrap ( )
189
+ . validate ( new_block) . unwrap ( ) ;
190
+ let old_header = block_source
191
+ . get_header ( & old_block, None ) . await . unwrap ( )
192
+ . validate ( old_block) . unwrap ( ) ;
193
+ let mut chain_poller = poll:: ChainPoller :: new ( block_source as & mut dyn BlockSource , network) ;
194
+ let mut chain_notifier = ChainNotifier { header_cache : UnboundedCache :: new ( ) } ;
195
+ chain_notifier. sync_listener ( new_header, & old_header, & mut chain_poller, chain_listener) . await . unwrap ( ) ;
196
+ }
197
+
171
198
/// The `Cache` trait defines behavior for managing a block header cache, where block headers are
172
199
/// keyed by block hash.
173
200
///
0 commit comments