33
44use crate :: async_poll:: { MultiResultFuturePoller , ResultFuture } ;
55use crate :: poll:: { ChainPoller , Poll , Validate , ValidatedBlockHeader } ;
6- use crate :: { BlockData , BlockSource , BlockSourceResult , Cache , ChainNotifier , HeaderCache } ;
6+ use crate :: { BlockData , BlockSource , BlockSourceResult , ChainNotifier , HeaderCache } ;
77
88use bitcoin:: block:: Header ;
99use bitcoin:: network:: Network ;
@@ -152,13 +152,13 @@ where
152152 let mut chain_listeners_at_height = Vec :: new ( ) ;
153153 let mut most_connected_blocks = Vec :: new ( ) ;
154154 let mut header_cache = HeaderCache :: new ( ) ;
155+ header_cache. retain_on_disconnect = true ;
155156 for ( old_best_block, chain_listener) in chain_listeners. drain ( ..) {
156157 // Disconnect any stale blocks, but keep them in the cache for the next iteration.
157158 let ( common_ancestor, connected_blocks) = {
158159 let chain_listener = & DynamicChainListener ( chain_listener) ;
159- let mut cache_wrapper = HeaderCacheNoDisconnect ( & mut header_cache) ;
160160 let mut chain_notifier =
161- ChainNotifier { header_cache : & mut cache_wrapper , chain_listener } ;
161+ ChainNotifier { header_cache : & mut header_cache , chain_listener } ;
162162 let difference = chain_notifier
163163 . find_difference_from_best_block ( best_header, old_best_block, & mut chain_poller)
164164 . await ?;
@@ -228,6 +228,7 @@ where
228228 . truncate ( most_connected_blocks. len ( ) . saturating_sub ( MAX_BLOCKS_AT_ONCE ) ) ;
229229 }
230230
231+ header_cache. retain_on_disconnect = false ;
231232 Ok ( ( header_cache, best_header) )
232233}
233234
@@ -246,40 +247,12 @@ impl<'a, L: chain::Listen + ?Sized> chain::Listen for DynamicChainListener<'a, L
246247 }
247248}
248249
249- /// Wrapper around HeaderCache that ignores `blocks_disconnected` calls, retaining disconnected
250- /// blocks in the cache. This is useful during initial sync to keep headers available across
251- /// multiple listeners.
252- struct HeaderCacheNoDisconnect < ' a > ( & ' a mut HeaderCache ) ;
253-
254- impl < ' a > crate :: Cache for & mut HeaderCacheNoDisconnect < ' a > {
255- fn look_up (
256- & self , block_hash : & bitcoin:: hash_types:: BlockHash ,
257- ) -> Option < & ValidatedBlockHeader > {
258- self . 0 . look_up ( block_hash)
259- }
260-
261- fn insert_during_diff (
262- & mut self , block_hash : bitcoin:: hash_types:: BlockHash , block_header : ValidatedBlockHeader ,
263- ) {
264- self . 0 . insert_during_diff ( block_hash, block_header) ;
265- }
266-
267- fn block_connected (
268- & mut self , block_hash : bitcoin:: hash_types:: BlockHash , block_header : ValidatedBlockHeader ,
269- ) {
270- self . 0 . block_connected ( block_hash, block_header) ;
271- }
272-
273- fn blocks_disconnected ( & mut self , _fork_point : & ValidatedBlockHeader ) {
274- // Intentionally ignore disconnections to retain blocks in cache
275- }
276- }
277250
278251#[ cfg( test) ]
279252mod tests {
280253 use super :: * ;
281254 use crate :: test_utils:: { Blockchain , MockChainListener } ;
282- use crate :: Cache ;
255+
283256
284257 #[ tokio:: test]
285258 async fn sync_from_same_chain ( ) {
0 commit comments