use lru::LruCache; use parking_lot::Mutex; use tree_hash::TreeHash; use types::{EthSpec, ExecutionPayload, Hash256}; pub const DEFAULT_PAYLOAD_CACHE_SIZE: usize = 10; /// A cache mapping execution payloads by tree hash roots. pub struct PayloadCache { payloads: Mutex>>, } #[derive(Hash, PartialEq, Eq)] struct PayloadCacheId(Hash256); impl Default for PayloadCache { fn default() -> Self { PayloadCache { payloads: Mutex::new(LruCache::new(DEFAULT_PAYLOAD_CACHE_SIZE)), } } } impl PayloadCache { pub fn put(&self, payload: ExecutionPayload) -> Option> { let root = payload.tree_hash_root(); self.payloads.lock().put(PayloadCacheId(root), payload) } pub fn pop(&self, root: &Hash256) -> Option> { self.payloads.lock().pop(&PayloadCacheId(*root)) } }