Skip to content

Commit 7913593

Browse files
committed
Drop the Cache trait entirely
Now that `Cache` is crate-private, there's not actually any reason to have it at all. In a later commit we'll have to reach into its internals a bit, but all within the `lightning-block-sync` crate, so having a trait indirection is somewhat useless.
1 parent f709bdd commit 7913593

2 files changed

Lines changed: 35 additions & 68 deletions

File tree

lightning-block-sync/src/lib.rs

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use bitcoin::hash_types::BlockHash;
4949
use bitcoin::pow::Work;
5050

5151
use lightning::chain;
52-
use lightning::chain::{BestBlock, Listen};
52+
use lightning::chain::BestBlock;
5353

5454
use std::future::Future;
5555
use std::ops::Deref;
@@ -176,32 +176,8 @@ where
176176
{
177177
chain_tip: ValidatedBlockHeader,
178178
chain_poller: P,
179-
chain_notifier: ChainNotifier<HeaderCache, L>,
180-
}
181-
182-
/// The `Cache` trait defines behavior for managing a block header cache, where block headers are
183-
/// keyed by block hash.
184-
///
185-
/// Used by [`ChainNotifier`] to store headers along the best chain, which is important for ensuring
186-
/// that blocks can be disconnected if they are no longer accessible from a block source (e.g., if
187-
/// the block source does not store stale forks indefinitely).
188-
///
189-
/// Implementations may define how long to retain headers such that it's unlikely they will ever be
190-
/// needed to disconnect a block. In cases where block sources provide access to headers on stale
191-
/// forks reliably, caches may be entirely unnecessary.
192-
pub(crate) trait Cache {
193-
/// Retrieves the block header keyed by the given block hash.
194-
fn look_up(&self, block_hash: &BlockHash) -> Option<&ValidatedBlockHeader>;
195-
196-
/// Called when a block has been connected to the best chain to ensure it is available to be
197-
/// disconnected later if needed.
198-
fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader);
199-
200-
/// Called when blocks have been disconnected from the best chain. Only the fork point
201-
/// (best common ancestor) is provided.
202-
///
203-
/// Once disconnected, a block's header is no longer needed and thus can be removed.
204-
fn blocks_disconnected(&mut self, fork_point: &ValidatedBlockHeader);
179+
header_cache: HeaderCache,
180+
chain_listener: L,
205181
}
206182

207183
/// The maximum number of [`ValidatedBlockHeader`]s stored in a [`HeaderCache`].
@@ -210,44 +186,40 @@ pub const HEADER_CACHE_LIMIT: u32 = 6 * 24 * 7;
210186
/// Bounded cache of block headers keyed by block hash.
211187
///
212188
/// Retains only the latest [`HEADER_CACHE_LIMIT`] block headers based on height.
213-
pub struct HeaderCache(std::collections::HashMap<BlockHash, ValidatedBlockHeader>);
189+
pub struct HeaderCache {
190+
headers: std::collections::HashMap<BlockHash, ValidatedBlockHeader>,
191+
}
214192

215193
impl HeaderCache {
216194
/// Creates a new empty header cache.
217195
pub fn new() -> Self {
218-
Self(std::collections::HashMap::new())
196+
Self { headers: std::collections::HashMap::new() }
219197
}
220-
}
221198

222-
impl Cache for HeaderCache {
223-
fn look_up(&self, block_hash: &BlockHash) -> Option<&ValidatedBlockHeader> {
224-
self.0.get(block_hash)
199+
/// Retrieves the block header keyed by the given block hash.
200+
pub fn look_up(&self, block_hash: &BlockHash) -> Option<&ValidatedBlockHeader> {
201+
self.headers.get(block_hash)
225202
}
226203

227-
fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
228-
self.0.insert(block_hash, block_header);
204+
205+
/// Called when a block has been connected to the best chain to ensure it is available to be
206+
/// disconnected later if needed.
207+
pub(crate) fn block_connected(
208+
&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader,
209+
) {
210+
self.headers.insert(block_hash, block_header);
229211

230212
// Remove headers older than a week.
231213
let cutoff_height = block_header.height.saturating_sub(HEADER_CACHE_LIMIT);
232-
self.0.retain(|_, header| header.height >= cutoff_height);
214+
self.headers.retain(|_, header| header.height >= cutoff_height);
233215
}
234216

235-
fn blocks_disconnected(&mut self, fork_point: &ValidatedBlockHeader) {
236-
self.0.retain(|_, block_info| block_info.height <= fork_point.height);
237-
}
238-
}
239-
240-
impl Cache for &mut HeaderCache {
241-
fn look_up(&self, block_hash: &BlockHash) -> Option<&ValidatedBlockHeader> {
242-
self.0.get(block_hash)
243-
}
244-
245-
fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
246-
(*self).block_connected(block_hash, block_header);
247-
}
248-
249-
fn blocks_disconnected(&mut self, fork_point: &ValidatedBlockHeader) {
250-
self.0.retain(|_, block_info| block_info.height <= fork_point.height);
217+
/// Called when blocks have been disconnected from the best chain. Only the fork point
218+
/// (best common ancestor) is provided.
219+
///
220+
/// Once disconnected, a block's header is no longer needed and thus can be removed.
221+
pub(crate) fn blocks_disconnected(&mut self, fork_point: &ValidatedBlockHeader) {
222+
self.headers.retain(|_, block_info| block_info.height <= fork_point.height);
251223
}
252224
}
253225

@@ -269,8 +241,7 @@ where
269241
chain_tip: ValidatedBlockHeader, chain_poller: P, header_cache: HeaderCache,
270242
chain_listener: L,
271243
) -> Self {
272-
let chain_notifier = ChainNotifier { header_cache, chain_listener };
273-
Self { chain_tip, chain_poller, chain_notifier }
244+
Self { chain_tip, chain_poller, header_cache, chain_listener }
274245
}
275246

276247
/// Polls for the best tip and updates the chain listener with any connected or disconnected
@@ -299,8 +270,11 @@ where
299270
/// Updates the chain tip, syncing the chain listener with any connected or disconnected
300271
/// blocks. Returns whether there were any such blocks.
301272
async fn update_chain_tip(&mut self, best_chain_tip: ValidatedBlockHeader) -> bool {
302-
match self
303-
.chain_notifier
273+
let mut chain_notifier = ChainNotifier {
274+
header_cache: &mut self.header_cache,
275+
chain_listener: &*self.chain_listener,
276+
};
277+
match chain_notifier
304278
.synchronize_listener(best_chain_tip, &self.chain_tip, &mut self.chain_poller)
305279
.await
306280
{
@@ -320,15 +294,12 @@ where
320294
/// Notifies [listeners] of blocks that have been connected or disconnected from the chain.
321295
///
322296
/// [listeners]: lightning::chain::Listen
323-
pub(crate) struct ChainNotifier<C: Cache, L: Deref>
324-
where
325-
L::Target: chain::Listen,
326-
{
297+
pub(crate) struct ChainNotifier<'a, L: chain::Listen + ?Sized> {
327298
/// Cache for looking up headers before fetching from a block source.
328-
pub(crate) header_cache: C,
299+
pub(crate) header_cache: &'a mut HeaderCache,
329300

330301
/// Listener that will be notified of connected or disconnected blocks.
331-
pub(crate) chain_listener: L,
302+
pub(crate) chain_listener: &'a L,
332303
}
333304

334305
/// Changes made to the chain between subsequent polls that transformed it from having one chain tip
@@ -346,10 +317,7 @@ struct ChainDifference {
346317
connected_blocks: Vec<ValidatedBlockHeader>,
347318
}
348319

349-
impl<C: Cache, L: Deref> ChainNotifier<C, L>
350-
where
351-
L::Target: chain::Listen,
352-
{
320+
impl<'a, L: chain::Listen + ?Sized> ChainNotifier<'a, L> {
353321
/// Finds the first common ancestor between `new_header` and `old_header`, disconnecting blocks
354322
/// from `old_header` to get to that point and then connecting blocks until `new_header`.
355323
///

lightning-block-sync/src/test_utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::poll::{Validate, ValidatedBlockHeader};
22
use crate::{
3-
BlockData, BlockHeaderData, BlockSource, BlockSourceError, BlockSourceResult, Cache,
4-
HeaderCache,
3+
BlockData, BlockHeaderData, BlockSource, BlockSourceError, BlockSourceResult, HeaderCache,
54
};
65

76
use bitcoin::block::{Block, Header, Version};

0 commit comments

Comments
 (0)