Update db function signatures

This commit is contained in:
Paul Hauner 2018-12-30 12:56:07 +11:00
parent 7c63155851
commit dc8fbf813f
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
3 changed files with 52 additions and 22 deletions

View File

@ -2,7 +2,7 @@ use super::BLOCKS_DB_COLUMN as DB_COLUMN;
use super::{ClientDB, DBError}; use super::{ClientDB, DBError};
use ssz::{Decodable, DecodeError}; use ssz::{Decodable, DecodeError};
use std::sync::Arc; use std::sync::Arc;
use types::Hash256; use types::{readers::BeaconBlockReader, BeaconBlock, Hash256};
type BeaconBlockHash = Vec<u8>; type BeaconBlockHash = Vec<u8>;
type BeaconBlockSsz = Vec<u8>; type BeaconBlockSsz = Vec<u8>;
@ -26,22 +26,35 @@ impl<T: ClientDB> BeaconBlockStore<T> {
Self { db } Self { db }
} }
pub fn put_serialized_block(&self, hash: &[u8], ssz: &[u8]) -> Result<(), DBError> { pub fn put(&self, hash: &[u8], ssz: &[u8]) -> Result<(), DBError> {
self.db.put(DB_COLUMN, hash, ssz) self.db.put(DB_COLUMN, hash, ssz)
} }
pub fn get_serialized_block(&self, hash: &[u8]) -> Result<Option<Vec<u8>>, DBError> { pub fn get(&self, hash: &[u8]) -> Result<Option<Vec<u8>>, DBError> {
self.db.get(DB_COLUMN, hash) self.db.get(DB_COLUMN, hash)
} }
pub fn block_exists(&self, hash: &[u8]) -> Result<bool, DBError> { pub fn exists(&self, hash: &[u8]) -> Result<bool, DBError> {
self.db.exists(DB_COLUMN, hash) self.db.exists(DB_COLUMN, hash)
} }
pub fn delete_block(&self, hash: &[u8]) -> Result<(), DBError> { pub fn delete(&self, hash: &[u8]) -> Result<(), DBError> {
self.db.delete(DB_COLUMN, hash) self.db.delete(DB_COLUMN, hash)
} }
/// Retuns a fully de-serialized `BeaconBlock` (or `None` if hash not known).
pub fn get_deserialized(&self, hash: &[u8]) -> Result<Option<impl BeaconBlockReader>, DBError> {
match self.get(&hash)? {
None => Ok(None),
Some(ssz) => {
let (block, _) = BeaconBlock::ssz_decode(&ssz, 0).map_err(|_| DBError {
message: "Bad Block SSZ.".to_string(),
})?;
Ok(Some(block))
}
}
}
/// Retrieve the block at a slot given a "head_hash" and a slot. /// Retrieve the block at a slot given a "head_hash" and a slot.
/// ///
/// A "head_hash" must be a block hash with a slot number greater than or equal to the desired /// A "head_hash" must be a block hash with a slot number greater than or equal to the desired
@ -56,7 +69,7 @@ impl<T: ClientDB> BeaconBlockStore<T> {
head_hash: &[u8], head_hash: &[u8],
slot: u64, slot: u64,
) -> Result<Option<(BeaconBlockHash, BeaconBlockSsz)>, BeaconBlockAtSlotError> { ) -> Result<Option<(BeaconBlockHash, BeaconBlockSsz)>, BeaconBlockAtSlotError> {
match self.get_serialized_block(head_hash)? { match self.get(head_hash)? {
None => Err(BeaconBlockAtSlotError::UnknownBeaconBlock), None => Err(BeaconBlockAtSlotError::UnknownBeaconBlock),
Some(ssz) => { Some(ssz) => {
let (retrieved_slot, parent_hash) = slot_and_parent_from_block_ssz(&ssz, 0) let (retrieved_slot, parent_hash) = slot_and_parent_from_block_ssz(&ssz, 0)
@ -102,19 +115,19 @@ mod tests {
use types::Hash256; use types::Hash256;
#[test] #[test]
fn test_put_serialized_block() { fn test_put() {
let db = Arc::new(MemoryDB::open()); let db = Arc::new(MemoryDB::open());
let store = BeaconBlockStore::new(db.clone()); let store = BeaconBlockStore::new(db.clone());
let ssz = "some bytes".as_bytes(); let ssz = "some bytes".as_bytes();
let hash = &Hash256::from("some hash".as_bytes()).to_vec(); let hash = &Hash256::from("some hash".as_bytes()).to_vec();
store.put_serialized_block(hash, ssz).unwrap(); store.put(hash, ssz).unwrap();
assert_eq!(db.get(DB_COLUMN, hash).unwrap().unwrap(), ssz); assert_eq!(db.get(DB_COLUMN, hash).unwrap().unwrap(), ssz);
} }
#[test] #[test]
fn test_get_serialized_block() { fn test_get() {
let db = Arc::new(MemoryDB::open()); let db = Arc::new(MemoryDB::open());
let store = BeaconBlockStore::new(db.clone()); let store = BeaconBlockStore::new(db.clone());
@ -122,11 +135,11 @@ mod tests {
let hash = &Hash256::from("some hash".as_bytes()).to_vec(); let hash = &Hash256::from("some hash".as_bytes()).to_vec();
db.put(DB_COLUMN, hash, ssz).unwrap(); db.put(DB_COLUMN, hash, ssz).unwrap();
assert_eq!(store.get_serialized_block(hash).unwrap().unwrap(), ssz); assert_eq!(store.get(hash).unwrap().unwrap(), ssz);
} }
#[test] #[test]
fn test_get_unknown_serialized_block() { fn test_get_unknown() {
let db = Arc::new(MemoryDB::open()); let db = Arc::new(MemoryDB::open());
let store = BeaconBlockStore::new(db.clone()); let store = BeaconBlockStore::new(db.clone());
@ -135,11 +148,11 @@ mod tests {
let other_hash = &Hash256::from("another hash".as_bytes()).to_vec(); let other_hash = &Hash256::from("another hash".as_bytes()).to_vec();
db.put(DB_COLUMN, other_hash, ssz).unwrap(); db.put(DB_COLUMN, other_hash, ssz).unwrap();
assert_eq!(store.get_serialized_block(hash).unwrap(), None); assert_eq!(store.get(hash).unwrap(), None);
} }
#[test] #[test]
fn test_block_exists() { fn test_exists() {
let db = Arc::new(MemoryDB::open()); let db = Arc::new(MemoryDB::open());
let store = BeaconBlockStore::new(db.clone()); let store = BeaconBlockStore::new(db.clone());
@ -147,7 +160,7 @@ mod tests {
let hash = &Hash256::from("some hash".as_bytes()).to_vec(); let hash = &Hash256::from("some hash".as_bytes()).to_vec();
db.put(DB_COLUMN, hash, ssz).unwrap(); db.put(DB_COLUMN, hash, ssz).unwrap();
assert!(store.block_exists(hash).unwrap()); assert!(store.exists(hash).unwrap());
} }
#[test] #[test]
@ -160,11 +173,11 @@ mod tests {
let other_hash = &Hash256::from("another hash".as_bytes()).to_vec(); let other_hash = &Hash256::from("another hash".as_bytes()).to_vec();
db.put(DB_COLUMN, hash, ssz).unwrap(); db.put(DB_COLUMN, hash, ssz).unwrap();
assert!(!store.block_exists(other_hash).unwrap()); assert!(!store.exists(other_hash).unwrap());
} }
#[test] #[test]
fn test_delete_block() { fn test_delete() {
let db = Arc::new(MemoryDB::open()); let db = Arc::new(MemoryDB::open());
let store = BeaconBlockStore::new(db.clone()); let store = BeaconBlockStore::new(db.clone());
@ -174,7 +187,7 @@ mod tests {
db.put(DB_COLUMN, hash, ssz).unwrap(); db.put(DB_COLUMN, hash, ssz).unwrap();
assert!(db.exists(DB_COLUMN, hash).unwrap()); assert!(db.exists(DB_COLUMN, hash).unwrap());
store.delete_block(hash).unwrap(); store.delete(hash).unwrap();
assert!(!db.exists(DB_COLUMN, hash).unwrap()); assert!(!db.exists(DB_COLUMN, hash).unwrap());
} }
@ -228,7 +241,7 @@ mod tests {
for w in 0..wc { for w in 0..wc {
let key = (t * w) as u8; let key = (t * w) as u8;
let val = 42; let val = 42;
bs.put_serialized_block(&vec![key], &vec![val]).unwrap(); bs.put(&vec![key], &vec![val]).unwrap();
} }
}); });
handles.push(handle); handles.push(handle);
@ -241,8 +254,8 @@ mod tests {
for t in 0..thread_count { for t in 0..thread_count {
for w in 0..write_count { for w in 0..write_count {
let key = (t * w) as u8; let key = (t * w) as u8;
assert!(bs.block_exists(&vec![key]).unwrap()); assert!(bs.exists(&vec![key]).unwrap());
let val = bs.get_serialized_block(&vec![key]).unwrap().unwrap(); let val = bs.get(&vec![key]).unwrap().unwrap();
assert_eq!(vec![42], val); assert_eq!(vec![42], val);
} }
} }

View File

@ -1,7 +1,8 @@
use super::STATES_DB_COLUMN as DB_COLUMN; use super::STATES_DB_COLUMN as DB_COLUMN;
use super::{ClientDB, DBError}; use super::{ClientDB, DBError};
use ssz::Decodable;
use std::sync::Arc; use std::sync::Arc;
use types::Hash256; use types::{readers::BeaconStateReader, BeaconState, Hash256};
pub struct BeaconStateStore<T> pub struct BeaconStateStore<T>
where where
@ -30,4 +31,17 @@ impl<T: ClientDB> BeaconStateStore<T> {
pub fn delete(&self, hash: &[u8]) -> Result<(), DBError> { pub fn delete(&self, hash: &[u8]) -> Result<(), DBError> {
self.db.delete(DB_COLUMN, hash) self.db.delete(DB_COLUMN, hash)
} }
/// Retuns a fully de-serialized `BeaconState` (or `None` if hash not known).
pub fn get_deserialized(&self, hash: &[u8]) -> Result<Option<impl BeaconStateReader>, DBError> {
match self.get(&hash)? {
None => Ok(None),
Some(ssz) => {
let (state, _) = BeaconState::ssz_decode(&ssz, 0).map_err(|_| DBError {
message: "Bad State SSZ.".to_string(),
})?;
Ok(Some(state))
}
}
}
} }

View File

@ -1,17 +1,20 @@
use super::{ClientDB, DBError}; use super::{ClientDB, DBError};
mod beacon_block_store; mod beacon_block_store;
mod beacon_state_store;
mod pow_chain_store; mod pow_chain_store;
mod validator_store; mod validator_store;
pub use self::beacon_block_store::{BeaconBlockAtSlotError, BeaconBlockStore}; pub use self::beacon_block_store::{BeaconBlockAtSlotError, BeaconBlockStore};
pub use self::beacon_state_store::BeaconStateStore;
pub use self::pow_chain_store::PoWChainStore; pub use self::pow_chain_store::PoWChainStore;
pub use self::validator_store::{ValidatorStore, ValidatorStoreError}; pub use self::validator_store::{ValidatorStore, ValidatorStoreError};
use super::bls; use super::bls;
pub const BLOCKS_DB_COLUMN: &str = "blocks"; pub const BLOCKS_DB_COLUMN: &str = "blocks";
pub const STATES_DB_COLUMN: &str = "states";
pub const POW_CHAIN_DB_COLUMN: &str = "powchain"; pub const POW_CHAIN_DB_COLUMN: &str = "powchain";
pub const VALIDATOR_DB_COLUMN: &str = "validator"; pub const VALIDATOR_DB_COLUMN: &str = "validator";
pub const COLUMNS: [&str; 3] = [BLOCKS_DB_COLUMN, POW_CHAIN_DB_COLUMN, VALIDATOR_DB_COLUMN]; pub const COLUMNS: [&str; 4] = [BLOCKS_DB_COLUMN, STATES_DB_COLUMN, POW_CHAIN_DB_COLUMN, VALIDATOR_DB_COLUMN];