From dc8fbf813fd44726c0fe287b9f0602bccb4a9197 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 30 Dec 2018 12:56:07 +1100 Subject: [PATCH] Update db function signatures --- .../db/src/stores/beacon_block_store.rs | 53 ++++++++++++------- .../db/src/stores/beacon_state_store.rs | 16 +++++- lighthouse/db/src/stores/mod.rs | 5 +- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/lighthouse/db/src/stores/beacon_block_store.rs b/lighthouse/db/src/stores/beacon_block_store.rs index 5f777ea0ae..5faa208dff 100644 --- a/lighthouse/db/src/stores/beacon_block_store.rs +++ b/lighthouse/db/src/stores/beacon_block_store.rs @@ -2,7 +2,7 @@ use super::BLOCKS_DB_COLUMN as DB_COLUMN; use super::{ClientDB, DBError}; use ssz::{Decodable, DecodeError}; use std::sync::Arc; -use types::Hash256; +use types::{readers::BeaconBlockReader, BeaconBlock, Hash256}; type BeaconBlockHash = Vec; type BeaconBlockSsz = Vec; @@ -26,22 +26,35 @@ impl BeaconBlockStore { 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) } - pub fn get_serialized_block(&self, hash: &[u8]) -> Result>, DBError> { + pub fn get(&self, hash: &[u8]) -> Result>, DBError> { self.db.get(DB_COLUMN, hash) } - pub fn block_exists(&self, hash: &[u8]) -> Result { + pub fn exists(&self, hash: &[u8]) -> Result { 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) } + /// Retuns a fully de-serialized `BeaconBlock` (or `None` if hash not known). + pub fn get_deserialized(&self, hash: &[u8]) -> Result, 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. /// /// A "head_hash" must be a block hash with a slot number greater than or equal to the desired @@ -56,7 +69,7 @@ impl BeaconBlockStore { head_hash: &[u8], slot: u64, ) -> Result, BeaconBlockAtSlotError> { - match self.get_serialized_block(head_hash)? { + match self.get(head_hash)? { None => Err(BeaconBlockAtSlotError::UnknownBeaconBlock), Some(ssz) => { let (retrieved_slot, parent_hash) = slot_and_parent_from_block_ssz(&ssz, 0) @@ -102,19 +115,19 @@ mod tests { use types::Hash256; #[test] - fn test_put_serialized_block() { + fn test_put() { let db = Arc::new(MemoryDB::open()); let store = BeaconBlockStore::new(db.clone()); let ssz = "some bytes".as_bytes(); 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); } #[test] - fn test_get_serialized_block() { + fn test_get() { let db = Arc::new(MemoryDB::open()); let store = BeaconBlockStore::new(db.clone()); @@ -122,11 +135,11 @@ mod tests { let hash = &Hash256::from("some hash".as_bytes()).to_vec(); 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] - fn test_get_unknown_serialized_block() { + fn test_get_unknown() { let db = Arc::new(MemoryDB::open()); let store = BeaconBlockStore::new(db.clone()); @@ -135,11 +148,11 @@ mod tests { let other_hash = &Hash256::from("another hash".as_bytes()).to_vec(); 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] - fn test_block_exists() { + fn test_exists() { let db = Arc::new(MemoryDB::open()); let store = BeaconBlockStore::new(db.clone()); @@ -147,7 +160,7 @@ mod tests { let hash = &Hash256::from("some hash".as_bytes()).to_vec(); db.put(DB_COLUMN, hash, ssz).unwrap(); - assert!(store.block_exists(hash).unwrap()); + assert!(store.exists(hash).unwrap()); } #[test] @@ -160,11 +173,11 @@ mod tests { let other_hash = &Hash256::from("another hash".as_bytes()).to_vec(); db.put(DB_COLUMN, hash, ssz).unwrap(); - assert!(!store.block_exists(other_hash).unwrap()); + assert!(!store.exists(other_hash).unwrap()); } #[test] - fn test_delete_block() { + fn test_delete() { let db = Arc::new(MemoryDB::open()); let store = BeaconBlockStore::new(db.clone()); @@ -174,7 +187,7 @@ mod tests { db.put(DB_COLUMN, hash, ssz).unwrap(); assert!(db.exists(DB_COLUMN, hash).unwrap()); - store.delete_block(hash).unwrap(); + store.delete(hash).unwrap(); assert!(!db.exists(DB_COLUMN, hash).unwrap()); } @@ -228,7 +241,7 @@ mod tests { for w in 0..wc { let key = (t * w) as u8; let val = 42; - bs.put_serialized_block(&vec![key], &vec![val]).unwrap(); + bs.put(&vec![key], &vec![val]).unwrap(); } }); handles.push(handle); @@ -241,8 +254,8 @@ mod tests { for t in 0..thread_count { for w in 0..write_count { let key = (t * w) as u8; - assert!(bs.block_exists(&vec![key]).unwrap()); - let val = bs.get_serialized_block(&vec![key]).unwrap().unwrap(); + assert!(bs.exists(&vec![key]).unwrap()); + let val = bs.get(&vec![key]).unwrap().unwrap(); assert_eq!(vec![42], val); } } diff --git a/lighthouse/db/src/stores/beacon_state_store.rs b/lighthouse/db/src/stores/beacon_state_store.rs index 4d3bbb36c9..687fc0f222 100644 --- a/lighthouse/db/src/stores/beacon_state_store.rs +++ b/lighthouse/db/src/stores/beacon_state_store.rs @@ -1,7 +1,8 @@ use super::STATES_DB_COLUMN as DB_COLUMN; use super::{ClientDB, DBError}; +use ssz::Decodable; use std::sync::Arc; -use types::Hash256; +use types::{readers::BeaconStateReader, BeaconState, Hash256}; pub struct BeaconStateStore where @@ -30,4 +31,17 @@ impl BeaconStateStore { pub fn delete(&self, hash: &[u8]) -> Result<(), DBError> { 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, 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)) + } + } + } } diff --git a/lighthouse/db/src/stores/mod.rs b/lighthouse/db/src/stores/mod.rs index f3adec2576..7fc51abe70 100644 --- a/lighthouse/db/src/stores/mod.rs +++ b/lighthouse/db/src/stores/mod.rs @@ -1,17 +1,20 @@ use super::{ClientDB, DBError}; mod beacon_block_store; +mod beacon_state_store; mod pow_chain_store; mod validator_store; 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::validator_store::{ValidatorStore, ValidatorStoreError}; use super::bls; pub const BLOCKS_DB_COLUMN: &str = "blocks"; +pub const STATES_DB_COLUMN: &str = "states"; pub const POW_CHAIN_DB_COLUMN: &str = "powchain"; 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];