2019-05-21 08:49:24 +00:00
|
|
|
use super::{Error, Store};
|
2019-12-06 07:52:11 +00:00
|
|
|
use crate::forwards_iter::SimpleForwardsBlockRootsIterator;
|
2019-11-26 23:54:46 +00:00
|
|
|
use crate::impls::beacon_state::{get_full_state, store_full_state};
|
2019-05-20 08:01:51 +00:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use std::collections::HashMap;
|
2019-12-06 07:52:11 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::sync::Arc;
|
2019-11-26 23:54:46 +00:00
|
|
|
use types::*;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
type DBHashMap = HashMap<Vec<u8>, Vec<u8>>;
|
|
|
|
|
2019-05-21 08:49:24 +00:00
|
|
|
/// A thread-safe `HashMap` wrapper.
|
2019-12-06 07:52:11 +00:00
|
|
|
pub struct MemoryStore<E: EthSpec> {
|
2019-11-26 23:54:46 +00:00
|
|
|
db: RwLock<DBHashMap>,
|
2019-12-06 07:52:11 +00:00
|
|
|
_phantom: PhantomData<E>,
|
2019-11-26 23:54:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:52:11 +00:00
|
|
|
impl<E: EthSpec> Clone for MemoryStore<E> {
|
2019-11-26 23:54:46 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
db: RwLock::new(self.db.read().clone()),
|
2019-12-06 07:52:11 +00:00
|
|
|
_phantom: PhantomData,
|
2019-11-26 23:54:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 07:52:11 +00:00
|
|
|
impl<E: EthSpec> MemoryStore<E> {
|
2019-05-21 08:49:24 +00:00
|
|
|
/// Create a new, empty database.
|
2019-02-14 01:09:18 +00:00
|
|
|
pub fn open() -> Self {
|
|
|
|
Self {
|
2019-11-26 23:54:46 +00:00
|
|
|
db: RwLock::new(HashMap::new()),
|
2019-12-06 07:52:11 +00:00
|
|
|
_phantom: PhantomData,
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_key_for_col(col: &str, key: &[u8]) -> Vec<u8> {
|
2019-05-20 08:01:51 +00:00
|
|
|
let mut col = col.as_bytes().to_vec();
|
|
|
|
col.append(&mut key.to_vec());
|
|
|
|
col
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 07:52:11 +00:00
|
|
|
impl<E: EthSpec> Store<E> for MemoryStore<E> {
|
|
|
|
type ForwardsBlockRootsIterator = SimpleForwardsBlockRootsIterator;
|
|
|
|
|
2019-02-14 01:09:18 +00:00
|
|
|
/// Get the value of some key from the database. Returns `None` if the key does not exist.
|
2019-05-21 08:49:24 +00:00
|
|
|
fn get_bytes(&self, col: &str, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
|
2019-12-06 07:52:11 +00:00
|
|
|
let column_key = Self::get_key_for_col(col, key);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2020-01-21 07:38:56 +00:00
|
|
|
Ok(self.db.read().get(&column_key).cloned())
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Puts a key in the database.
|
2019-05-20 08:01:51 +00:00
|
|
|
fn put_bytes(&self, col: &str, key: &[u8], val: &[u8]) -> Result<(), Error> {
|
2019-12-06 07:52:11 +00:00
|
|
|
let column_key = Self::get_key_for_col(col, key);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-05-20 08:01:51 +00:00
|
|
|
self.db.write().insert(column_key, val.to_vec());
|
|
|
|
|
|
|
|
Ok(())
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if some key exists in some column.
|
2019-05-20 08:01:51 +00:00
|
|
|
fn key_exists(&self, col: &str, key: &[u8]) -> Result<bool, Error> {
|
2019-12-06 07:52:11 +00:00
|
|
|
let column_key = Self::get_key_for_col(col, key);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-05-20 08:01:51 +00:00
|
|
|
Ok(self.db.read().contains_key(&column_key))
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Delete some key from the database.
|
2019-05-20 08:01:51 +00:00
|
|
|
fn key_delete(&self, col: &str, key: &[u8]) -> Result<(), Error> {
|
2019-12-06 07:52:11 +00:00
|
|
|
let column_key = Self::get_key_for_col(col, key);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-05-20 08:01:51 +00:00
|
|
|
self.db.write().remove(&column_key);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-05-20 08:01:51 +00:00
|
|
|
Ok(())
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
2019-11-26 23:54:46 +00:00
|
|
|
|
|
|
|
/// Store a state in the store.
|
2020-02-10 00:30:21 +00:00
|
|
|
fn put_state(&self, state_root: &Hash256, state: BeaconState<E>) -> Result<(), Error> {
|
|
|
|
store_full_state(self, state_root, &state)
|
2019-11-26 23:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch a state from the store.
|
2019-12-06 07:52:11 +00:00
|
|
|
fn get_state(
|
2019-11-26 23:54:46 +00:00
|
|
|
&self,
|
|
|
|
state_root: &Hash256,
|
|
|
|
_: Option<Slot>,
|
|
|
|
) -> Result<Option<BeaconState<E>>, Error> {
|
|
|
|
get_full_state(self, state_root)
|
|
|
|
}
|
2019-12-06 07:52:11 +00:00
|
|
|
|
|
|
|
fn forwards_block_roots_iterator(
|
|
|
|
store: Arc<Self>,
|
|
|
|
start_slot: Slot,
|
|
|
|
end_state: BeaconState<E>,
|
|
|
|
end_block_root: Hash256,
|
|
|
|
_: &ChainSpec,
|
|
|
|
) -> Self::ForwardsBlockRootsIterator {
|
|
|
|
SimpleForwardsBlockRootsIterator::new(store, start_slot, end_state, end_block_root)
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|