Skip to content

Commit 108f11b

Browse files
committed
f no cache trait
1 parent 9eab8f1 commit 108f11b

File tree

2 files changed

+14
-35
lines changed

2 files changed

+14
-35
lines changed

lightning-block-sync/src/init.rs

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use crate::async_poll::{MultiResultFuturePoller, ResultFuture};
55
use crate::poll::{ChainPoller, Poll, Validate, ValidatedBlockHeader};
6-
use crate::{BlockData, BlockSource, BlockSourceResult, Cache, ChainNotifier, HeaderCache};
6+
use crate::{BlockData, BlockSource, BlockSourceResult, ChainNotifier, HeaderCache};
77

88
use bitcoin::block::Header;
99
use 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)]
279252
mod 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() {

lightning-block-sync/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,15 @@ pub const HEADER_CACHE_LIMIT: u32 = 6 * 24 * 7;
193193
/// Retains only the latest [`HEADER_CACHE_LIMIT`] block headers based on height.
194194
pub struct HeaderCache {
195195
headers: std::collections::HashMap<BlockHash, ValidatedBlockHeader>,
196+
/// When set, [`Self::blocks_disconnected`] will not evict headers above the fork point.
197+
/// This is used during initial sync to retain headers across multiple listeners.
198+
retain_on_disconnect: bool,
196199
}
197200

198201
impl HeaderCache {
199202
/// Creates a new empty header cache.
200203
pub fn new() -> Self {
201-
Self { headers: std::collections::HashMap::new() }
204+
Self { headers: std::collections::HashMap::new(), retain_on_disconnect: false }
202205
}
203206

204207
/// Retrieves the block header keyed by the given block hash.
@@ -234,9 +237,12 @@ impl HeaderCache {
234237
/// Called when blocks have been disconnected from the best chain. Only the fork point
235238
/// (best common ancestor) is provided.
236239
///
237-
/// Once disconnected, a block's header is no longer needed and thus can be removed.
240+
/// Once disconnected, unless [`Self::retain_on_disconnect`] is set, a block's header is no
241+
/// longer needed and thus can be removed.
238242
pub(crate) fn blocks_disconnected(&mut self, fork_point: &ValidatedBlockHeader) {
239-
self.headers.retain(|_, block_info| block_info.height <= fork_point.height);
243+
if !self.retain_on_disconnect {
244+
self.headers.retain(|_, block_info| block_info.height <= fork_point.height);
245+
}
240246
}
241247
}
242248

0 commit comments

Comments
 (0)