mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-11 18:04:18 +00:00
Add LRU cache to database (#837)
* Add LRU caches to store * Improvements to LRU caches * Take state by value in `Store::put_state` * Store blocks by value, configurable cache sizes * Use a StateBatch to efficiently store skip states * Fix store tests * Add CloneConfig test, remove unused metrics * Use Mutexes instead of RwLocks for LRU caches
This commit is contained in:
47
beacon_node/store/src/state_batch.rs
Normal file
47
beacon_node/store/src/state_batch.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use crate::{Error, HotStateSummary, Store};
|
||||
use types::{BeaconState, EthSpec, Hash256};
|
||||
|
||||
/// A collection of states to be stored in the database.
|
||||
///
|
||||
/// Consumes minimal space in memory by not storing states between epoch boundaries.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct StateBatch<E: EthSpec> {
|
||||
items: Vec<BatchItem<E>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
enum BatchItem<E: EthSpec> {
|
||||
Full(Hash256, BeaconState<E>),
|
||||
Summary(Hash256, HotStateSummary),
|
||||
}
|
||||
|
||||
impl<E: EthSpec> StateBatch<E> {
|
||||
/// Create a new empty batch.
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Stage a `BeaconState` to be stored.
|
||||
pub fn add_state(&mut self, state_root: Hash256, state: &BeaconState<E>) -> Result<(), Error> {
|
||||
let item = if state.slot % E::slots_per_epoch() == 0 {
|
||||
BatchItem::Full(state_root, state.clone())
|
||||
} else {
|
||||
BatchItem::Summary(state_root, HotStateSummary::new(&state_root, state)?)
|
||||
};
|
||||
self.items.push(item);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write the batch to the database.
|
||||
///
|
||||
/// May fail to write the full batch if any of the items error (i.e. not atomic!)
|
||||
pub fn commit<S: Store<E>>(self, store: &S) -> Result<(), Error> {
|
||||
self.items.into_iter().try_for_each(|item| match item {
|
||||
BatchItem::Full(state_root, state) => store.put_state(&state_root, state),
|
||||
BatchItem::Summary(state_root, summary) => {
|
||||
store.put_state_summary(&state_root, summary)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user