use std::num::NonZeroUsize;
use nonzero_ext::nonzero;
use crate::prelude::*;
use crate::utils::cache::SizeTrackingCache;
const DEFAULT_CID_CACHE_CAPACITY: NonZeroUsize = nonzero!(1usize << 15);
#[derive(Debug, derive_more::Deref)]
pub struct BadBlockCache {
cache: SizeTrackingCache<CidWrapper, ()>,
}
impl Default for BadBlockCache {
fn default() -> Self {
Self::new(DEFAULT_CID_CACHE_CAPACITY)
}
}
impl ShallowClone for BadBlockCache {
fn shallow_clone(&self) -> Self {
Self {
cache: self.cache.shallow_clone(),
}
}
}
impl BadBlockCache {
pub fn new(cap: NonZeroUsize) -> Self {
Self {
cache: SizeTrackingCache::new_with_metrics("bad_block", cap),
}
}
pub fn push(&self, c: Cid) {
self.cache.insert(c.into(), ());
tracing::warn!("Marked bad block: {c}");
}
}
#[derive(Debug, derive_more::Deref)]
pub struct SeenBlockCache {
cache: SizeTrackingCache<CidWrapper, ()>,
}
impl ShallowClone for SeenBlockCache {
fn shallow_clone(&self) -> Self {
Self {
cache: self.cache.shallow_clone(),
}
}
}
impl Default for SeenBlockCache {
fn default() -> Self {
Self::new(DEFAULT_CID_CACHE_CAPACITY)
}
}
impl SeenBlockCache {
pub fn new(cap: NonZeroUsize) -> Self {
Self {
cache: SizeTrackingCache::new_with_metrics("seen_gossip_block", cap),
}
}
pub fn test_and_insert(&self, c: &Cid) -> bool {
self.cache.push_and_get_prev((*c).into(), ()).is_some()
}
}