Ditch StoreItem trait (#1185)

This commit is contained in:
Adam Szkoda 2020-05-25 02:26:54 +02:00 committed by GitHub
parent a88afb7409
commit 919c81fe7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 78 deletions

View File

@ -7,8 +7,7 @@ use crate::impls::beacon_state::store_full_state;
use crate::iter::{ParentRootBlockIterator, StateRootsIterator}; use crate::iter::{ParentRootBlockIterator, StateRootsIterator};
use crate::metrics; use crate::metrics;
use crate::{ use crate::{
leveldb_store::LevelDB, DBColumn, Error, PartialBeaconState, SimpleStoreItem, Store, StoreItem, leveldb_store::LevelDB, DBColumn, Error, PartialBeaconState, SimpleStoreItem, Store, StoreOp,
StoreOp,
}; };
use lru::LruCache; use lru::LruCache;
use parking_lot::{Mutex, RwLock}; use parking_lot::{Mutex, RwLock};
@ -460,7 +459,7 @@ impl<E: EthSpec> HotColdDB<E> {
// 1. Convert to PartialBeaconState and store that in the DB. // 1. Convert to PartialBeaconState and store that in the DB.
let partial_state = PartialBeaconState::from_state_forgetful(state); let partial_state = PartialBeaconState::from_state_forgetful(state);
partial_state.db_put(&self.cold_db, state_root)?; self.cold_db.put(state_root, &partial_state)?;
// 2. Store updated vector entries. // 2. Store updated vector entries.
let db = &self.cold_db; let db = &self.cold_db;
@ -500,7 +499,9 @@ impl<E: EthSpec> HotColdDB<E> {
/// Load a restore point state by its `state_root`. /// Load a restore point state by its `state_root`.
fn load_restore_point(&self, state_root: &Hash256) -> Result<BeaconState<E>, Error> { fn load_restore_point(&self, state_root: &Hash256) -> Result<BeaconState<E>, Error> {
let mut partial_state = PartialBeaconState::db_get(&self.cold_db, state_root)? let mut partial_state: PartialBeaconState<E> = self
.cold_db
.get(state_root)?
.ok_or_else(|| HotColdDBError::MissingRestorePoint(*state_root))?; .ok_or_else(|| HotColdDBError::MissingRestorePoint(*state_root))?;
// Fill in the fields of the partial state. // Fill in the fields of the partial state.
@ -680,8 +681,9 @@ impl<E: EthSpec> HotColdDB<E> {
/// Load the state root of a restore point. /// Load the state root of a restore point.
fn load_restore_point_hash(&self, restore_point_index: u64) -> Result<Hash256, Error> { fn load_restore_point_hash(&self, restore_point_index: u64) -> Result<Hash256, Error> {
let key = Self::restore_point_key(restore_point_index); let key = Self::restore_point_key(restore_point_index);
RestorePointHash::db_get(&self.cold_db, &key)? self.cold_db
.map(|r| r.state_root) .get(&key)?
.map(|r: RestorePointHash| r.state_root)
.ok_or_else(|| HotColdDBError::MissingRestorePointHash(restore_point_index).into()) .ok_or_else(|| HotColdDBError::MissingRestorePointHash(restore_point_index).into())
} }
@ -692,8 +694,8 @@ impl<E: EthSpec> HotColdDB<E> {
state_root: Hash256, state_root: Hash256,
) -> Result<(), Error> { ) -> Result<(), Error> {
let key = Self::restore_point_key(restore_point_index); let key = Self::restore_point_key(restore_point_index);
RestorePointHash { state_root } self.cold_db
.db_put(&self.cold_db, &key) .put(&key, &RestorePointHash { state_root })
.map_err(Into::into) .map_err(Into::into)
} }
@ -704,13 +706,16 @@ impl<E: EthSpec> HotColdDB<E> {
/// Load a frozen state's slot, given its root. /// Load a frozen state's slot, given its root.
fn load_cold_state_slot(&self, state_root: &Hash256) -> Result<Option<Slot>, Error> { fn load_cold_state_slot(&self, state_root: &Hash256) -> Result<Option<Slot>, Error> {
Ok(ColdStateSummary::db_get(&self.cold_db, state_root)?.map(|s| s.slot)) Ok(self
.cold_db
.get(state_root)?
.map(|s: ColdStateSummary| s.slot))
} }
/// Store the slot of a frozen state. /// Store the slot of a frozen state.
fn store_cold_state_slot(&self, state_root: &Hash256, slot: Slot) -> Result<(), Error> { fn store_cold_state_slot(&self, state_root: &Hash256, slot: Slot) -> Result<(), Error> {
ColdStateSummary { slot } self.cold_db
.db_put(&self.cold_db, state_root) .put(state_root, &ColdStateSummary { slot })
.map_err(Into::into) .map_err(Into::into)
} }
@ -719,7 +724,7 @@ impl<E: EthSpec> HotColdDB<E> {
&self, &self,
state_root: &Hash256, state_root: &Hash256,
) -> Result<Option<HotStateSummary>, Error> { ) -> Result<Option<HotStateSummary>, Error> {
HotStateSummary::db_get(&self.hot_db, state_root) self.hot_db.get(state_root)
} }
/// Check that the restore point frequency is valid. /// Check that the restore point frequency is valid.

View File

@ -59,23 +59,39 @@ pub trait Store<E: EthSpec>: Sync + Send + Sized + 'static {
fn key_delete(&self, column: &str, key: &[u8]) -> Result<(), Error>; fn key_delete(&self, column: &str, key: &[u8]) -> Result<(), Error>;
/// Store an item in `Self`. /// Store an item in `Self`.
fn put<I: StoreItem>(&self, key: &Hash256, item: &I) -> Result<(), Error> { fn put<I: SimpleStoreItem>(&self, key: &Hash256, item: &I) -> Result<(), Error> {
item.db_put(self, key) let column = I::db_column().into();
let key = key.as_bytes();
self.put_bytes(column, key, &item.as_store_bytes())
.map_err(Into::into)
} }
/// Retrieve an item from `Self`. /// Retrieve an item from `Self`.
fn get<I: StoreItem>(&self, key: &Hash256) -> Result<Option<I>, Error> { fn get<I: SimpleStoreItem>(&self, key: &Hash256) -> Result<Option<I>, Error> {
I::db_get(self, key) let column = I::db_column().into();
let key = key.as_bytes();
match self.get_bytes(column, key)? {
Some(bytes) => Ok(Some(I::from_store_bytes(&bytes[..])?)),
None => Ok(None),
}
} }
/// Returns `true` if the given key represents an item in `Self`. /// Returns `true` if the given key represents an item in `Self`.
fn exists<I: StoreItem>(&self, key: &Hash256) -> Result<bool, Error> { fn exists<I: SimpleStoreItem>(&self, key: &Hash256) -> Result<bool, Error> {
I::db_exists(self, key) let column = I::db_column().into();
let key = key.as_bytes();
self.key_exists(column, key)
} }
/// Remove an item from `Self`. /// Remove an item from `Self`.
fn delete<I: StoreItem>(&self, key: &Hash256) -> Result<(), Error> { fn delete<I: SimpleStoreItem>(&self, key: &Hash256) -> Result<(), Error> {
I::db_delete(self, key) let column = I::db_column().into();
let key = key.as_bytes();
self.key_delete(column, key)
} }
/// Store a block in the store. /// Store a block in the store.
@ -107,7 +123,7 @@ pub trait Store<E: EthSpec>: Sync + Send + Sized + 'static {
state_root: &Hash256, state_root: &Hash256,
summary: HotStateSummary, summary: HotStateSummary,
) -> Result<(), Error> { ) -> Result<(), Error> {
summary.db_put(self, state_root).map_err(Into::into) self.put(state_root, &summary).map_err(Into::into)
} }
/// Fetch a state from the store. /// Fetch a state from the store.
@ -249,63 +265,6 @@ pub trait SimpleStoreItem: Sized {
fn from_store_bytes(bytes: &[u8]) -> Result<Self, Error>; fn from_store_bytes(bytes: &[u8]) -> Result<Self, Error>;
} }
/// An item that may be stored in a `Store`.
pub trait StoreItem: Sized {
/// Store `self`.
fn db_put<S: Store<E>, E: EthSpec>(&self, store: &S, key: &Hash256) -> Result<(), Error>;
/// Retrieve an instance of `Self` from `store`.
fn db_get<S: Store<E>, E: EthSpec>(store: &S, key: &Hash256) -> Result<Option<Self>, Error>;
/// Return `true` if an instance of `Self` exists in `store`.
fn db_exists<S: Store<E>, E: EthSpec>(store: &S, key: &Hash256) -> Result<bool, Error>;
/// Delete an instance of `Self` from `store`.
fn db_delete<S: Store<E>, E: EthSpec>(store: &S, key: &Hash256) -> Result<(), Error>;
}
impl<T> StoreItem for T
where
T: SimpleStoreItem,
{
/// Store `self`.
fn db_put<S: Store<E>, 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<S: Store<E>, E: EthSpec>(store: &S, key: &Hash256) -> Result<Option<Self>, 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<S: Store<E>, E: EthSpec>(store: &S, key: &Hash256) -> Result<bool, Error> {
let column = Self::db_column().into();
let key = key.as_bytes();
store.key_exists(column, key)
}
/// Delete `self` from the `Store`.
fn db_delete<S: Store<E>, 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;