//! Storage functionality for Lighthouse. //! //! Provides the following stores: //! //! - `DiskStore`: an on-disk store backed by leveldb. Used in production. //! - `MemoryStore`: an in-memory store backed by a hash-map. Used for testing. //! //! Provides a simple API for storing/retrieving all types that sometimes needs type-hints. See //! tests for implementation examples. #[macro_use] extern crate lazy_static; mod block_at_slot; pub mod chunked_iter; pub mod chunked_vector; pub mod config; mod errors; mod forwards_iter; mod hot_cold_store; mod impls; mod leveldb_store; mod memory_store; mod metrics; mod partial_beacon_state; mod state_batch; pub mod iter; pub mod migrate; use std::sync::Arc; pub use self::config::StoreConfig; pub use self::hot_cold_store::{HotColdDB as DiskStore, HotStateSummary}; pub use self::leveldb_store::LevelDB as SimpleDiskStore; pub use self::memory_store::MemoryStore; pub use self::migrate::Migrate; pub use self::partial_beacon_state::PartialBeaconState; pub use errors::Error; pub use impls::beacon_state::StorageContainer as BeaconStateStorageContainer; pub use metrics::scrape_for_metrics; pub use state_batch::StateBatch; pub use types::beacon_state::CloneConfig; pub use types::*; /// An object capable of storing and retrieving objects implementing `StoreItem`. /// /// A `Store` is fundamentally backed by a key-value database, however it provides support for /// columns. A simple column implementation might involve prefixing a key with some bytes unique to /// each column. pub trait Store: Sync + Send + Sized + 'static { type ForwardsBlockRootsIterator: Iterator; /// Retrieve some bytes in `column` with `key`. fn get_bytes(&self, column: &str, key: &[u8]) -> Result>, Error>; /// Store some `value` in `column`, indexed with `key`. fn put_bytes(&self, column: &str, key: &[u8], value: &[u8]) -> Result<(), Error>; /// Return `true` if `key` exists in `column`. fn key_exists(&self, column: &str, key: &[u8]) -> Result; /// Removes `key` from `column`. fn key_delete(&self, column: &str, key: &[u8]) -> Result<(), Error>; /// Store an item in `Self`. fn put(&self, key: &Hash256, item: &I) -> Result<(), Error> { item.db_put(self, key) } /// Retrieve an item from `Self`. fn get(&self, key: &Hash256) -> Result, Error> { I::db_get(self, key) } /// Returns `true` if the given key represents an item in `Self`. fn exists(&self, key: &Hash256) -> Result { I::db_exists(self, key) } /// Remove an item from `Self`. fn delete(&self, key: &Hash256) -> Result<(), Error> { I::db_delete(self, key) } /// Store a block in the store. fn put_block(&self, block_root: &Hash256, block: BeaconBlock) -> Result<(), Error> { self.put(block_root, &block) } /// Fetch a block from the store. fn get_block(&self, block_root: &Hash256) -> Result>, Error> { self.get(block_root) } /// Store a state in the store. fn put_state(&self, state_root: &Hash256, state: BeaconState) -> Result<(), Error>; /// Store a state summary in the store. // NOTE: this is a hack for the HotColdDb, we could consider splitting this // trait and removing the generic `S: Store` types everywhere? fn put_state_summary( &self, state_root: &Hash256, summary: HotStateSummary, ) -> Result<(), Error> { summary.db_put(self, state_root).map_err(Into::into) } /// Fetch a state from the store. fn get_state( &self, state_root: &Hash256, slot: Option, ) -> Result>, Error>; /// Fetch a state from the store, controlling which cache fields are cloned. fn get_state_with( &self, state_root: &Hash256, slot: Option, _clone_config: CloneConfig, ) -> Result>, Error> { // Default impl ignores config. Overriden in `HotColdDb`. self.get_state(state_root, slot) } /// Given the root of an existing block in the store (`start_block_root`), return a parent /// block with the specified `slot`. /// /// Returns `None` if no parent block exists at that slot, or if `slot` is greater than the /// slot of `start_block_root`. fn get_block_at_preceeding_slot( &self, start_block_root: Hash256, slot: Slot, ) -> Result)>, Error> { block_at_slot::get_block_at_preceeding_slot::<_, E>(self, slot, start_block_root) } /// (Optionally) Move all data before the frozen slot to the freezer database. fn freeze_to_state( _store: Arc, _frozen_head_root: Hash256, _frozen_head: &BeaconState, ) -> Result<(), Error> { Ok(()) } /// Get a forwards (slot-ascending) iterator over the beacon block roots since `start_slot`. /// /// Will be efficient for frozen portions of the database if using `DiskStore`. /// /// The `end_state` and `end_block_root` are required for backtracking in the post-finalization /// part of the chain, and should be usually be set to the current head. Importantly, the /// `end_state` must be a state that has had a block applied to it, and the hash of that /// block must be `end_block_root`. // NOTE: could maybe optimise by getting the `BeaconState` and end block root from a closure, as // it's not always required. fn forwards_block_roots_iterator( store: Arc, start_slot: Slot, end_state: BeaconState, end_block_root: Hash256, spec: &ChainSpec, ) -> Self::ForwardsBlockRootsIterator; /// Load the most recent ancestor state of `state_root` which lies on an epoch boundary. /// /// If `state_root` corresponds to an epoch boundary state, then that state itself should be /// returned. fn load_epoch_boundary_state( &self, state_root: &Hash256, ) -> Result>, Error> { // The default implementation is not very efficient, but isn't used in prod. // See `HotColdDB` for the optimized implementation. if let Some(state) = self.get_state(state_root, None)? { let epoch_boundary_slot = state.slot / E::slots_per_epoch() * E::slots_per_epoch(); if state.slot == epoch_boundary_slot { Ok(Some(state)) } else { let epoch_boundary_state_root = state.get_state_root(epoch_boundary_slot)?; self.get_state(epoch_boundary_state_root, Some(epoch_boundary_slot)) } } else { Ok(None) } } } /// A unique column identifier. #[derive(Debug, Clone, Copy, PartialEq)] pub enum DBColumn { /// For data related to the database itself. BeaconMeta, BeaconBlock, BeaconState, BeaconChain, /// For the table mapping restore point numbers to state roots. BeaconRestorePoint, /// For the mapping from state roots to their slots or summaries. BeaconStateSummary, BeaconBlockRoots, BeaconStateRoots, BeaconHistoricalRoots, BeaconRandaoMixes, DhtEnrs, } impl Into<&'static str> for DBColumn { /// Returns a `&str` that can be used for keying a key-value data base. fn into(self) -> &'static str { match self { DBColumn::BeaconMeta => "bma", DBColumn::BeaconBlock => "blk", DBColumn::BeaconState => "ste", DBColumn::BeaconChain => "bch", DBColumn::BeaconRestorePoint => "brp", DBColumn::BeaconStateSummary => "bss", DBColumn::BeaconBlockRoots => "bbr", DBColumn::BeaconStateRoots => "bsr", DBColumn::BeaconHistoricalRoots => "bhr", DBColumn::BeaconRandaoMixes => "brm", DBColumn::DhtEnrs => "dht", } } } /// An item that may stored in a `Store` by serializing and deserializing from bytes. pub trait SimpleStoreItem: Sized { /// Identifies which column this item should be placed in. fn db_column() -> DBColumn; /// Serialize `self` as bytes. fn as_store_bytes(&self) -> Vec; /// De-serialize `self` from bytes. /// /// Return an instance of the type and the number of bytes that were read. fn from_store_bytes(bytes: &[u8]) -> Result; } /// An item that may be stored in a `Store`. pub trait StoreItem: Sized { /// Store `self`. fn db_put, E: EthSpec>(&self, store: &S, key: &Hash256) -> Result<(), Error>; /// Retrieve an instance of `Self` from `store`. fn db_get, E: EthSpec>(store: &S, key: &Hash256) -> Result, Error>; /// Return `true` if an instance of `Self` exists in `store`. fn db_exists, E: EthSpec>(store: &S, key: &Hash256) -> Result; /// Delete an instance of `Self` from `store`. fn db_delete, E: EthSpec>(store: &S, key: &Hash256) -> Result<(), Error>; } impl StoreItem for T where T: SimpleStoreItem, { /// Store `self`. fn db_put, E: EthSpec>(&self, store: &S, key: &Hash256) -> Result<(), Error> { let column = Self::db_column().into(); let key = key.as_bytes(); store .put_bytes(column, key, &self.as_store_bytes()) .map_err(Into::into) } /// Retrieve an instance of `Self`. fn db_get, E: EthSpec>(store: &S, key: &Hash256) -> Result, Error> { let column = Self::db_column().into(); let key = key.as_bytes(); match store.get_bytes(column, key)? { Some(bytes) => Ok(Some(Self::from_store_bytes(&bytes[..])?)), None => Ok(None), } } /// Return `true` if an instance of `Self` exists in `Store`. fn db_exists, E: EthSpec>(store: &S, key: &Hash256) -> Result { let column = Self::db_column().into(); let key = key.as_bytes(); store.key_exists(column, key) } /// Delete `self` from the `Store`. fn db_delete, E: EthSpec>(store: &S, key: &Hash256) -> Result<(), Error> { let column = Self::db_column().into(); let key = key.as_bytes(); store.key_delete(column, key) } } #[cfg(test)] mod tests { use super::*; use ssz::{Decode, Encode}; use ssz_derive::{Decode, Encode}; use tempfile::tempdir; #[derive(PartialEq, Debug, Encode, Decode)] struct StorableThing { a: u64, b: u64, } impl SimpleStoreItem for StorableThing { fn db_column() -> DBColumn { DBColumn::BeaconBlock } fn as_store_bytes(&self) -> Vec { self.as_ssz_bytes() } fn from_store_bytes(bytes: &[u8]) -> Result { Self::from_ssz_bytes(bytes).map_err(Into::into) } } fn test_impl(store: impl Store) { let key = Hash256::random(); let item = StorableThing { a: 1, b: 42 }; assert_eq!(store.exists::(&key), Ok(false)); store.put(&key, &item).unwrap(); assert_eq!(store.exists::(&key), Ok(true)); let retrieved = store.get(&key).unwrap().unwrap(); assert_eq!(item, retrieved); store.delete::(&key).unwrap(); assert_eq!(store.exists::(&key), Ok(false)); assert_eq!(store.get::(&key), Ok(None)); } #[test] fn diskdb() { use sloggers::{null::NullLoggerBuilder, Build}; let hot_dir = tempdir().unwrap(); let cold_dir = tempdir().unwrap(); let spec = MinimalEthSpec::default_spec(); let log = NullLoggerBuilder.build().unwrap(); let store = DiskStore::open( &hot_dir.path(), &cold_dir.path(), StoreConfig::default(), spec, log, ) .unwrap(); test_impl(store); } #[test] fn simplediskdb() { let dir = tempdir().unwrap(); let path = dir.path(); let store = SimpleDiskStore::open(&path).unwrap(); test_impl(store); } #[test] fn memorydb() { let store = MemoryStore::open(); test_impl(store); } #[test] fn exists() { let store = MemoryStore::::open(); let key = Hash256::random(); let item = StorableThing { a: 1, b: 42 }; assert_eq!(store.exists::(&key).unwrap(), false); store.put(&key, &item).unwrap(); assert_eq!(store.exists::(&key).unwrap(), true); store.delete::(&key).unwrap(); assert_eq!(store.exists::(&key).unwrap(), false); } }