mirror of
https://github.com/sigp/lighthouse.git
synced 2026-07-01 03:44:30 +00:00
Use the `LruCache` implementation provided by `hashlink` instead of the current `lru` one. This is mostly a 1-to-1 swap with only slight API incompatibilities. I have decided to leave some config files which previously used `NonZeroUsize` but they may not be required anymore and could potentially switch to `usize`. Co-Authored-By: Mac L <mjladson@pm.me>
43 lines
1.4 KiB
Rust
43 lines
1.4 KiB
Rust
use hashlink::lru_cache::LruCache;
|
|
use parking_lot::Mutex;
|
|
use std::sync::Arc;
|
|
use types::{AttestationShufflingId, CommitteeCache, Epoch};
|
|
|
|
/// See `shuffling_cache::DEFAULT_CACHE_SIZE` for rationale
|
|
pub const DEFAULT_HISTORICAL_COMMITTEE_CACHE_SIZE: usize = 16;
|
|
|
|
/// Indexes the `HistoricalCommitteeCache`. We can compute committees for very old epochs, and we
|
|
/// can't retrieve the decision root cheaply from a state. For those cases we allow the cache to
|
|
/// key those committees by finalized epoch.
|
|
#[derive(Eq, Hash, PartialEq)]
|
|
pub enum HistoricalShufflingId {
|
|
FinalizedEpoch(Epoch),
|
|
ShufflingId(AttestationShufflingId),
|
|
}
|
|
|
|
/// Dedicated cache for attestation committees, used exclusively by the HTTP API.
|
|
///
|
|
/// This may contain committees for finalized and unfinalized epochs. The name is slightly
|
|
/// missleading :)
|
|
pub struct HistoricalCommitteeCache {
|
|
committees: Mutex<LruCache<HistoricalShufflingId, Arc<CommitteeCache>>>,
|
|
}
|
|
|
|
impl HistoricalCommitteeCache {
|
|
pub fn new(size: usize) -> Self {
|
|
Self {
|
|
committees: Mutex::new(LruCache::new(size)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl HistoricalCommitteeCache {
|
|
pub fn get(&self, id: &HistoricalShufflingId) -> Option<Arc<CommitteeCache>> {
|
|
self.committees.lock().get(id).cloned()
|
|
}
|
|
|
|
pub fn insert(&self, id: HistoricalShufflingId, cache: Arc<CommitteeCache>) {
|
|
self.committees.lock().insert(id, cache);
|
|
}
|
|
}
|