mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 12:11:59 +00:00
* Use `E` for `EthSpec` globally * Fix tests * Merge branch 'unstable' into e-ethspec * Merge branch 'unstable' into e-ethspec # Conflicts: # beacon_node/execution_layer/src/engine_api.rs # beacon_node/execution_layer/src/engine_api/http.rs # beacon_node/execution_layer/src/engine_api/json_structures.rs # beacon_node/execution_layer/src/test_utils/handle_rpc.rs # beacon_node/store/src/partial_beacon_state.rs # consensus/types/src/beacon_block.rs # consensus/types/src/beacon_block_body.rs # consensus/types/src/beacon_state.rs # consensus/types/src/config_and_preset.rs # consensus/types/src/execution_payload.rs # consensus/types/src/execution_payload_header.rs # consensus/types/src/light_client_optimistic_update.rs # consensus/types/src/payload.rs # lcli/src/parse_ssz.rs
41 lines
1.2 KiB
Rust
41 lines
1.2 KiB
Rust
use eth2::types::FullPayloadContents;
|
|
use lru::LruCache;
|
|
use parking_lot::Mutex;
|
|
use std::num::NonZeroUsize;
|
|
use tree_hash::TreeHash;
|
|
use types::non_zero_usize::new_non_zero_usize;
|
|
use types::{EthSpec, Hash256};
|
|
|
|
pub const DEFAULT_PAYLOAD_CACHE_SIZE: NonZeroUsize = new_non_zero_usize(10);
|
|
|
|
/// A cache mapping execution payloads by tree hash roots.
|
|
pub struct PayloadCache<E: EthSpec> {
|
|
payloads: Mutex<LruCache<PayloadCacheId, FullPayloadContents<E>>>,
|
|
}
|
|
|
|
#[derive(Hash, PartialEq, Eq)]
|
|
struct PayloadCacheId(Hash256);
|
|
|
|
impl<E: EthSpec> Default for PayloadCache<E> {
|
|
fn default() -> Self {
|
|
PayloadCache {
|
|
payloads: Mutex::new(LruCache::new(DEFAULT_PAYLOAD_CACHE_SIZE)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<E: EthSpec> PayloadCache<E> {
|
|
pub fn put(&self, payload: FullPayloadContents<E>) -> Option<FullPayloadContents<E>> {
|
|
let root = payload.payload_ref().tree_hash_root();
|
|
self.payloads.lock().put(PayloadCacheId(root), payload)
|
|
}
|
|
|
|
pub fn pop(&self, root: &Hash256) -> Option<FullPayloadContents<E>> {
|
|
self.payloads.lock().pop(&PayloadCacheId(*root))
|
|
}
|
|
|
|
pub fn get(&self, hash: &Hash256) -> Option<FullPayloadContents<E>> {
|
|
self.payloads.lock().get(&PayloadCacheId(*hash)).cloned()
|
|
}
|
|
}
|