More project-wide fixes for new DB

This commit is contained in:
Paul Hauner
2019-05-21 17:45:35 +10:00
parent 058829b64d
commit b62f4477e1
8 changed files with 48 additions and 36 deletions

View File

@@ -1,5 +1,4 @@
use clap::ArgMatches; use clap::ArgMatches;
use db::DBType;
use fork_choice::ForkChoiceAlgorithm; use fork_choice::ForkChoiceAlgorithm;
use network::NetworkConfig; use network::NetworkConfig;
use slog::error; use slog::error;
@@ -12,6 +11,12 @@ use types::multiaddr::ToMultiaddr;
use types::Multiaddr; use types::Multiaddr;
use types::{ChainSpec, EthSpec, LighthouseTestnetEthSpec}; use types::{ChainSpec, EthSpec, LighthouseTestnetEthSpec};
#[derive(Debug, Clone)]
pub enum DBType {
Memory,
Disk,
}
/// Stores the client configuration for this Lighthouse instance. /// Stores the client configuration for this Lighthouse instance.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ClientConfig { pub struct ClientConfig {
@@ -132,7 +137,7 @@ impl ClientConfig {
} }
match args.value_of("db") { match args.value_of("db") {
Some("rocks") => config.db_type = DBType::RocksDB, Some("disk") => config.db_type = DBType::Disk,
Some("memory") => config.db_type = DBType::Memory, Some("memory") => config.db_type = DBType::Memory,
_ => unreachable!(), // clap prevents this. _ => unreachable!(), // clap prevents this.
}; };

View File

@@ -1,6 +1,6 @@
use crate::{ArcBeaconChain, ClientConfig}; use crate::{ArcBeaconChain, ClientConfig};
use beacon_chain::{ use beacon_chain::{
db::{ClientDB, DiskDB, MemoryDB}, db::{DiskDB, MemoryDB, Store},
fork_choice::BitwiseLMDGhost, fork_choice::BitwiseLMDGhost,
initialise, initialise,
slot_clock::{SlotClock, SystemTimeSlotClock}, slot_clock::{SlotClock, SystemTimeSlotClock},
@@ -9,7 +9,7 @@ use fork_choice::ForkChoice;
use types::{EthSpec, FewValidatorsEthSpec, FoundationEthSpec}; use types::{EthSpec, FewValidatorsEthSpec, FoundationEthSpec};
pub trait ClientTypes { pub trait ClientTypes {
type DB: ClientDB + 'static; type DB: Store + 'static;
type SlotClock: SlotClock + 'static; type SlotClock: SlotClock + 'static;
type ForkChoice: ForkChoice + 'static; type ForkChoice: ForkChoice + 'static;
type EthSpec: EthSpec + 'static; type EthSpec: EthSpec + 'static;
@@ -24,7 +24,7 @@ pub struct StandardClientType;
impl ClientTypes for StandardClientType { impl ClientTypes for StandardClientType {
type DB = DiskDB; type DB = DiskDB;
type SlotClock = SystemTimeSlotClock; type SlotClock = SystemTimeSlotClock;
type ForkChoice = BitwiseLMDGhost<DiskDB, Self::EthSpec>; type ForkChoice = BitwiseLMDGhost<Self::DB, Self::EthSpec>;
type EthSpec = FoundationEthSpec; type EthSpec = FoundationEthSpec;
fn initialise_beacon_chain( fn initialise_beacon_chain(
@@ -39,12 +39,27 @@ pub struct MemoryDBTestingClientType;
impl ClientTypes for MemoryDBTestingClientType { impl ClientTypes for MemoryDBTestingClientType {
type DB = MemoryDB; type DB = MemoryDB;
type SlotClock = SystemTimeSlotClock; type SlotClock = SystemTimeSlotClock;
type ForkChoice = BitwiseLMDGhost<MemoryDB, Self::EthSpec>; type ForkChoice = BitwiseLMDGhost<Self::DB, Self::EthSpec>;
type EthSpec = FewValidatorsEthSpec; type EthSpec = FewValidatorsEthSpec;
fn initialise_beacon_chain( fn initialise_beacon_chain(
config: &ClientConfig, config: &ClientConfig,
) -> ArcBeaconChain<Self::DB, Self::SlotClock, Self::ForkChoice, Self::EthSpec> { ) -> ArcBeaconChain<Self::DB, Self::SlotClock, Self::ForkChoice, Self::EthSpec> {
initialise::initialise_test_beacon_chain(&config.spec, None) initialise::initialise_test_beacon_chain_with_memory_db(&config.spec, None)
}
}
pub struct DiskDBTestingClientType;
impl ClientTypes for DiskDBTestingClientType {
type DB = DiskDB;
type SlotClock = SystemTimeSlotClock;
type ForkChoice = BitwiseLMDGhost<Self::DB, Self::EthSpec>;
type EthSpec = FewValidatorsEthSpec;
fn initialise_beacon_chain(
config: &ClientConfig,
) -> ArcBeaconChain<Self::DB, Self::SlotClock, Self::ForkChoice, Self::EthSpec> {
initialise::initialise_test_beacon_chain_with_disk_db(&config.spec, Some(&config.db_name))
} }
} }

View File

@@ -6,9 +6,9 @@ pub mod error;
pub mod notifier; pub mod notifier;
use beacon_chain::BeaconChain; use beacon_chain::BeaconChain;
pub use client_config::ClientConfig; pub use client_config::{ClientConfig, DBType};
pub use client_types::ClientTypes; pub use client_types::ClientTypes;
use db::ClientDB; use db::Store;
use exit_future::Signal; use exit_future::Signal;
use fork_choice::ForkChoice; use fork_choice::ForkChoice;
use futures::{future::Future, Stream}; use futures::{future::Future, Stream};
@@ -146,7 +146,7 @@ impl<TClientType: ClientTypes> Client<TClientType> {
fn do_state_catchup<T, U, F, E>(chain: &Arc<BeaconChain<T, U, F, E>>, log: &slog::Logger) fn do_state_catchup<T, U, F, E>(chain: &Arc<BeaconChain<T, U, F, E>>, log: &slog::Logger)
where where
T: ClientDB, T: Store,
U: SlotClock, U: SlotClock,
F: ForkChoice, F: ForkChoice,
E: EthSpec, E: EthSpec,

View File

@@ -1,6 +1,6 @@
use beacon_chain::BeaconChain as RawBeaconChain; use beacon_chain::BeaconChain as RawBeaconChain;
use beacon_chain::{ use beacon_chain::{
db::ClientDB, db::Store,
fork_choice::ForkChoice, fork_choice::ForkChoice,
parking_lot::RwLockReadGuard, parking_lot::RwLockReadGuard,
slot_clock::SlotClock, slot_clock::SlotClock,
@@ -66,7 +66,7 @@ pub trait BeaconChain<E: EthSpec>: Send + Sync {
impl<T, U, F, E> BeaconChain<E> for RawBeaconChain<T, U, F, E> impl<T, U, F, E> BeaconChain<E> for RawBeaconChain<T, U, F, E>
where where
T: ClientDB + Sized, T: Store,
U: SlotClock, U: SlotClock,
F: ForkChoice, F: ForkChoice,
E: EthSpec, E: EthSpec,

View File

@@ -1,6 +1,6 @@
use beacon_chain::BeaconChain as RawBeaconChain; use beacon_chain::BeaconChain as RawBeaconChain;
use beacon_chain::{ use beacon_chain::{
db::ClientDB, db::Store,
fork_choice::ForkChoice, fork_choice::ForkChoice,
parking_lot::{RwLockReadGuard, RwLockWriteGuard}, parking_lot::{RwLockReadGuard, RwLockWriteGuard},
slot_clock::SlotClock, slot_clock::SlotClock,
@@ -36,7 +36,7 @@ pub trait BeaconChain<E: EthSpec>: Send + Sync {
impl<T, U, F, E> BeaconChain<E> for RawBeaconChain<T, U, F, E> impl<T, U, F, E> BeaconChain<E> for RawBeaconChain<T, U, F, E>
where where
T: ClientDB + Sized, T: Store,
U: SlotClock, U: SlotClock,
F: ForkChoice, F: ForkChoice,
E: EthSpec, E: EthSpec,

View File

@@ -74,7 +74,7 @@ fn main() {
.value_name("DB") .value_name("DB")
.help("Type of database to use.") .help("Type of database to use.")
.takes_value(true) .takes_value(true)
.possible_values(&["rocks", "memory"]) .possible_values(&["disk", "memory"])
.default_value("memory"), .default_value("memory"),
) )
.get_matches(); .get_matches();

View File

@@ -1,7 +1,6 @@
use client::client_types::{DiskDBTestingClientType, MemoryDBTestingClientType}; use client::client_types::{DiskDBTestingClientType, MemoryDBTestingClientType};
use client::error; use client::{error, DBType};
use client::{notifier, Client, ClientConfig, ClientTypes}; use client::{notifier, Client, ClientConfig, ClientTypes};
use db::DBType;
use futures::sync::oneshot; use futures::sync::oneshot;
use futures::Future; use futures::Future;
use slog::info; use slog::info;
@@ -26,7 +25,7 @@ pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Resul
let executor = runtime.executor(); let executor = runtime.executor();
match config.db_type { match config.db_type {
DBType::RocksDB => { DBType::Disk => {
info!( info!(
log, log,
"BeaconNode starting"; "BeaconNode starting";

View File

@@ -14,8 +14,8 @@ extern crate yaml_rust;
pub use beacon_chain::BeaconChain; pub use beacon_chain::BeaconChain;
use bls::Signature; use bls::Signature;
use db::stores::{BeaconBlockStore, BeaconStateStore};
use db::MemoryDB; use db::MemoryDB;
use db::Store;
// use env_logger::{Builder, Env}; // use env_logger::{Builder, Env};
use fork_choice::{ use fork_choice::{
BitwiseLMDGhost, ForkChoice, ForkChoiceAlgorithm, LongestChain, OptimizedLMDGhost, SlowLMDGhost, BitwiseLMDGhost, ForkChoice, ForkChoiceAlgorithm, LongestChain, OptimizedLMDGhost, SlowLMDGhost,
@@ -106,7 +106,7 @@ fn test_yaml_vectors(
// process the tests // process the tests
for test_case in test_cases { for test_case in test_cases {
// setup a fresh test // setup a fresh test
let (mut fork_choice, block_store, state_root) = let (mut fork_choice, store, state_root) =
setup_inital_state(&fork_choice_algo, emulated_validators); setup_inital_state(&fork_choice_algo, emulated_validators);
// keep a hashmap of block_id's to block_hashes (random hashes to abstract block_id) // keep a hashmap of block_id's to block_hashes (random hashes to abstract block_id)
@@ -149,9 +149,7 @@ fn test_yaml_vectors(
}; };
// Store the block. // Store the block.
block_store store.put(&block_hash, &beacon_block).unwrap();
.put(&block_hash, &ssz_encode(&beacon_block)[..])
.unwrap();
// run add block for fork choice if not genesis // run add block for fork choice if not genesis
if parent_id != block_id { if parent_id != block_id {
@@ -222,29 +220,26 @@ fn load_test_cases_from_yaml(file_path: &str) -> Vec<yaml_rust::Yaml> {
fn setup_inital_state( fn setup_inital_state(
fork_choice_algo: &ForkChoiceAlgorithm, fork_choice_algo: &ForkChoiceAlgorithm,
num_validators: usize, num_validators: usize,
) -> (Box<ForkChoice>, Arc<BeaconBlockStore<MemoryDB>>, Hash256) { ) -> (Box<ForkChoice>, Arc<MemoryDB>, Hash256) {
let db = Arc::new(MemoryDB::open()); let store = Arc::new(MemoryDB::open());
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
let state_store = Arc::new(BeaconStateStore::new(db.clone()));
// the fork choice instantiation // the fork choice instantiation
let fork_choice: Box<ForkChoice> = match fork_choice_algo { let fork_choice: Box<ForkChoice> = match fork_choice_algo {
ForkChoiceAlgorithm::OptimizedLMDGhost => { ForkChoiceAlgorithm::OptimizedLMDGhost => {
let f: OptimizedLMDGhost<MemoryDB, FoundationEthSpec> = let f: OptimizedLMDGhost<MemoryDB, FoundationEthSpec> =
OptimizedLMDGhost::new(block_store.clone(), state_store.clone()); OptimizedLMDGhost::new(store.clone());
Box::new(f) Box::new(f)
} }
ForkChoiceAlgorithm::BitwiseLMDGhost => { ForkChoiceAlgorithm::BitwiseLMDGhost => {
let f: BitwiseLMDGhost<MemoryDB, FoundationEthSpec> = let f: BitwiseLMDGhost<MemoryDB, FoundationEthSpec> =
BitwiseLMDGhost::new(block_store.clone(), state_store.clone()); BitwiseLMDGhost::new(store.clone());
Box::new(f) Box::new(f)
} }
ForkChoiceAlgorithm::SlowLMDGhost => { ForkChoiceAlgorithm::SlowLMDGhost => {
let f: SlowLMDGhost<MemoryDB, FoundationEthSpec> = let f: SlowLMDGhost<MemoryDB, FoundationEthSpec> = SlowLMDGhost::new(store.clone());
SlowLMDGhost::new(block_store.clone(), state_store.clone());
Box::new(f) Box::new(f)
} }
ForkChoiceAlgorithm::LongestChain => Box::new(LongestChain::new(block_store.clone())), ForkChoiceAlgorithm::LongestChain => Box::new(LongestChain::new(store.clone())),
}; };
let spec = FoundationEthSpec::spec(); let spec = FoundationEthSpec::spec();
@@ -255,12 +250,10 @@ fn setup_inital_state(
let (state, _keypairs) = state_builder.build(); let (state, _keypairs) = state_builder.build();
let state_root = state.canonical_root(); let state_root = state.canonical_root();
state_store store.put(&state_root, &state).unwrap();
.put(&state_root, &ssz_encode(&state)[..])
.unwrap();
// return initialised vars // return initialised vars
(fork_choice, block_store, state_root) (fork_choice, store, state_root)
} }
// convert a block_id into a Hash256 -- assume input is hex encoded; // convert a block_id into a Hash256 -- assume input is hex encoded;