From 69c97745d2d2446d828b22d7b57c2a28a7335c28 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 24 Sep 2018 14:15:59 +1000 Subject: [PATCH] Set MemoryDB to use constant DB columns This is instead of needing to be passed them each time open() is called. --- lighthouse/db/memory_db.rs | 34 +++++++++++++------------ lighthouse/db/mod.rs | 2 +- lighthouse/db/stores/block_store.rs | 4 +-- lighthouse/db/stores/mod.rs | 13 +++++++--- lighthouse/db/stores/validator_store.rs | 14 +++------- 5 files changed, 32 insertions(+), 35 deletions(-) diff --git a/lighthouse/db/memory_db.rs b/lighthouse/db/memory_db.rs index 41d1f7e6de..94bd5f2906 100644 --- a/lighthouse/db/memory_db.rs +++ b/lighthouse/db/memory_db.rs @@ -1,6 +1,7 @@ use std::collections::{ HashSet, HashMap }; use std::sync::RwLock; use super::blake2::blake2b::blake2b; +use super::COLUMNS; use super::{ ClientDB, DBValue, @@ -24,13 +25,11 @@ impl MemoryDB { /// /// All columns must be supplied initially, you will get an error if you try to access a column /// that was not declared here. This condition is enforced artificially to simulate RocksDB. - pub fn open(columns: Option<&[&str]>) -> Self { + pub fn open() -> Self { let db: DBHashMap = HashMap::new(); let mut known_columns: ColumnHashSet = HashSet::new(); - if let Some(columns) = columns { - for col in columns { - known_columns.insert(col.to_string()); - } + for col in COLUMNS.iter() { + known_columns.insert(col.to_string()); } Self { db: RwLock::new(db), @@ -103,18 +102,22 @@ mod tests { use super::super::ClientDB; use std::thread; use std::sync::Arc; + use super::super::stores::{ + BLOCKS_DB_COLUMN, + VALIDATOR_DB_COLUMN, + }; #[test] fn test_memorydb_column_access() { - let col_a: &str = "ColumnA"; - let col_b: &str = "ColumnB"; + let col_a: &str = BLOCKS_DB_COLUMN; + let col_b: &str = VALIDATOR_DB_COLUMN; let column_families = vec![ col_a, col_b, ]; - let db = MemoryDB::open(Some(&column_families)); + let db = MemoryDB::open(); /* * Testing that if we write to the same key in different columns that @@ -131,7 +134,7 @@ mod tests { #[test] fn test_memorydb_unknown_column_access() { - let col_a: &str = "ColumnA"; + let col_a: &str = BLOCKS_DB_COLUMN; let col_x: &str = "ColumnX"; let column_families = vec![ @@ -139,7 +142,7 @@ mod tests { // col_x is excluded on purpose ]; - let db = MemoryDB::open(Some(&column_families)); + let db = MemoryDB::open(); /* * Test that we get errors when using undeclared columns @@ -153,15 +156,15 @@ mod tests { #[test] fn test_memorydb_exists() { - let col_a: &str = "ColumnA"; - let col_b: &str = "ColumnB"; + let col_a: &str = BLOCKS_DB_COLUMN; + let col_b: &str = VALIDATOR_DB_COLUMN; let column_families = vec![ col_a, col_b, ]; - let db = MemoryDB::open(Some(&column_families)); + let db = MemoryDB::open(); /* * Testing that if we write to the same key in different columns that @@ -178,10 +181,9 @@ mod tests { #[test] fn test_memorydb_threading() { - let col_name: &str = "TestColumn"; - let column_families = vec![col_name]; + let col_name: &str = BLOCKS_DB_COLUMN; - let db = Arc::new(MemoryDB::open(Some(&column_families))); + let db = Arc::new(MemoryDB::open()); let thread_count = 10; let write_count = 10; diff --git a/lighthouse/db/mod.rs b/lighthouse/db/mod.rs index 5f8898861f..40cab486d3 100644 --- a/lighthouse/db/mod.rs +++ b/lighthouse/db/mod.rs @@ -7,7 +7,7 @@ mod traits; pub mod stores; use super::bls; - +use self::stores::COLUMNS; pub use self::disk_db::DiskDB; pub use self::memory_db::MemoryDB; diff --git a/lighthouse/db/stores/block_store.rs b/lighthouse/db/stores/block_store.rs index fde09bec87..1836923c95 100644 --- a/lighthouse/db/stores/block_store.rs +++ b/lighthouse/db/stores/block_store.rs @@ -46,9 +46,7 @@ mod tests { #[test] fn test_block_store_on_disk_db() { - let column_families = vec![DB_COLUMN]; - - let db = Arc::new(MemoryDB::open(Some(&column_families))); + let db = Arc::new(MemoryDB::open()); let bs = Arc::new(BlockStore::new(db.clone())); let thread_count = 10; diff --git a/lighthouse/db/stores/mod.rs b/lighthouse/db/stores/mod.rs index 58c0a52dbb..93250e7dbc 100644 --- a/lighthouse/db/stores/mod.rs +++ b/lighthouse/db/stores/mod.rs @@ -13,15 +13,20 @@ pub use self::validator_store::ValidatorStore; use super::bls; -const BLOCKS_DB_COLUMN: &str = "blocks"; -const POW_CHAIN_DB_COLUMN: &str = "powchain"; -const VALIDATOR_DB_COLUMN: &str = "validator"; +pub const BLOCKS_DB_COLUMN: &str = "blocks"; +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, +]; #[derive(Debug, PartialEq)] pub enum StoreError { DBError(String), DecodeError, - EncodeError, } impl From for StoreError { diff --git a/lighthouse/db/stores/validator_store.rs b/lighthouse/db/stores/validator_store.rs index a20e7503a8..25e5893088 100644 --- a/lighthouse/db/stores/validator_store.rs +++ b/lighthouse/db/stores/validator_store.rs @@ -76,20 +76,12 @@ impl ValidatorStore { #[cfg(test)] mod tests { use super::*; - use super::super::super::{ - MemoryDB, - ClientDB, - }; + use super::super::super::MemoryDB; use super::super::bls::Keypair; - fn open_client_db() -> MemoryDB { - let columns = vec![DB_COLUMN]; - MemoryDB::open(Some(&columns)) - } - #[test] fn test_validator_store_put_get() { - let db = Arc::new(open_client_db()); + let db = Arc::new(MemoryDB::open()); let store = ValidatorStore::new(db); let keys = vec![ @@ -122,7 +114,7 @@ mod tests { #[test] fn test_validator_store_bad_key() { - let db = Arc::new(open_client_db()); + let db = Arc::new(MemoryDB::open()); let store = ValidatorStore::new(db.clone()); let key = store.get_db_key_for_index(KeyPrefixes::PublicKey, 42);