forest/chain_sync/
bad_block_cache.rs1use std::num::NonZeroUsize;
5
6use cid::Cid;
7use nonzero_ext::nonzero;
8
9use crate::utils::{ShallowClone, cache::SizeTrackingLruCache, get_size};
10
11const DEFAULT_CID_CACHE_CAPACITY: NonZeroUsize = nonzero!(1usize << 15);
14
15#[derive(Debug)]
19pub struct BadBlockCache {
20 cache: SizeTrackingLruCache<get_size::CidWrapper, ()>,
21}
22
23impl Default for BadBlockCache {
24 fn default() -> Self {
25 Self::new(DEFAULT_CID_CACHE_CAPACITY)
26 }
27}
28
29impl BadBlockCache {
30 pub fn new(cap: NonZeroUsize) -> Self {
31 Self {
32 cache: SizeTrackingLruCache::new_with_metrics("bad_block".into(), cap),
33 }
34 }
35
36 pub fn push(&self, c: Cid) {
37 self.cache.push(c.into(), ());
38 tracing::warn!("Marked bad block: {c}");
39 }
40
41 pub fn peek(&self, c: &Cid) -> Option<()> {
44 self.cache.peek_cloned(&(*c).into())
45 }
46
47 pub fn clear(&self) {
48 self.cache.clear()
49 }
50}
51
52#[derive(Debug)]
55pub struct SeenBlockCache {
56 cache: SizeTrackingLruCache<get_size::CidWrapper, ()>,
57}
58
59impl ShallowClone for SeenBlockCache {
60 fn shallow_clone(&self) -> Self {
61 Self {
62 cache: self.cache.shallow_clone(),
63 }
64 }
65}
66
67impl Default for SeenBlockCache {
68 fn default() -> Self {
69 Self::new(DEFAULT_CID_CACHE_CAPACITY)
70 }
71}
72
73impl SeenBlockCache {
74 pub fn new(cap: NonZeroUsize) -> Self {
75 Self {
76 cache: SizeTrackingLruCache::new_with_metrics("seen_gossip_block".into(), cap),
77 }
78 }
79
80 pub fn test_and_insert(&self, c: &Cid) -> bool {
83 self.cache.push((*c).into(), ()).is_some()
84 }
85}