@@ -36,10 +36,11 @@ mod test_utils;
3636#[ cfg( any( feature = "rest-client" , feature = "rpc-client" ) ) ]
3737mod utils;
3838
39- use crate :: poll:: { ChainTip , Poll , ValidatedBlockHeader } ;
39+ use crate :: poll:: { ChainTip , Poll , Validate , ValidatedBlockHeader } ;
4040
4141use bitcoin:: blockdata:: block:: { Block , BlockHeader } ;
4242use bitcoin:: hash_types:: BlockHash ;
43+ use bitcoin:: network:: constants:: Network ;
4344use bitcoin:: util:: uint:: Uint256 ;
4445
4546use std:: future:: Future ;
@@ -168,6 +169,32 @@ pub trait ChainListener {
168169 fn block_disconnected ( & mut self , header : & BlockHeader , height : u32 ) ;
169170}
170171
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+
171198/// The `Cache` trait defines behavior for managing a block header cache, where block headers are
172199/// keyed by block hash.
173200///
0 commit comments