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 db::DBType;
use fork_choice::ForkChoiceAlgorithm;
use network::NetworkConfig;
use slog::error;
@@ -12,6 +11,12 @@ use types::multiaddr::ToMultiaddr;
use types::Multiaddr;
use types::{ChainSpec, EthSpec, LighthouseTestnetEthSpec};
#[derive(Debug, Clone)]
pub enum DBType {
Memory,
Disk,
}
/// Stores the client configuration for this Lighthouse instance.
#[derive(Debug, Clone)]
pub struct ClientConfig {
@@ -132,7 +137,7 @@ impl ClientConfig {
}
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,
_ => unreachable!(), // clap prevents this.
};

View File

@@ -1,6 +1,6 @@
use crate::{ArcBeaconChain, ClientConfig};
use beacon_chain::{
db::{ClientDB, DiskDB, MemoryDB},
db::{DiskDB, MemoryDB, Store},
fork_choice::BitwiseLMDGhost,
initialise,
slot_clock::{SlotClock, SystemTimeSlotClock},
@@ -9,7 +9,7 @@ use fork_choice::ForkChoice;
use types::{EthSpec, FewValidatorsEthSpec, FoundationEthSpec};
pub trait ClientTypes {
type DB: ClientDB + 'static;
type DB: Store + 'static;
type SlotClock: SlotClock + 'static;
type ForkChoice: ForkChoice + 'static;
type EthSpec: EthSpec + 'static;
@@ -24,7 +24,7 @@ pub struct StandardClientType;
impl ClientTypes for StandardClientType {
type DB = DiskDB;
type SlotClock = SystemTimeSlotClock;
type ForkChoice = BitwiseLMDGhost<DiskDB, Self::EthSpec>;
type ForkChoice = BitwiseLMDGhost<Self::DB, Self::EthSpec>;
type EthSpec = FoundationEthSpec;
fn initialise_beacon_chain(
@@ -39,12 +39,27 @@ pub struct MemoryDBTestingClientType;
impl ClientTypes for MemoryDBTestingClientType {
type DB = MemoryDB;
type SlotClock = SystemTimeSlotClock;
type ForkChoice = BitwiseLMDGhost<MemoryDB, Self::EthSpec>;
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(&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;
use beacon_chain::BeaconChain;
pub use client_config::ClientConfig;
pub use client_config::{ClientConfig, DBType};
pub use client_types::ClientTypes;
use db::ClientDB;
use db::Store;
use exit_future::Signal;
use fork_choice::ForkChoice;
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)
where
T: ClientDB,
T: Store,
U: SlotClock,
F: ForkChoice,
E: EthSpec,

View File

@@ -1,6 +1,6 @@
use beacon_chain::BeaconChain as RawBeaconChain;
use beacon_chain::{
db::ClientDB,
db::Store,
fork_choice::ForkChoice,
parking_lot::RwLockReadGuard,
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>
where
T: ClientDB + Sized,
T: Store,
U: SlotClock,
F: ForkChoice,
E: EthSpec,

View File

@@ -1,6 +1,6 @@
use beacon_chain::BeaconChain as RawBeaconChain;
use beacon_chain::{
db::ClientDB,
db::Store,
fork_choice::ForkChoice,
parking_lot::{RwLockReadGuard, RwLockWriteGuard},
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>
where
T: ClientDB + Sized,
T: Store,
U: SlotClock,
F: ForkChoice,
E: EthSpec,

View File

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

View File

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

View File

@@ -14,8 +14,8 @@ extern crate yaml_rust;
pub use beacon_chain::BeaconChain;
use bls::Signature;
use db::stores::{BeaconBlockStore, BeaconStateStore};
use db::MemoryDB;
use db::Store;
// use env_logger::{Builder, Env};
use fork_choice::{
BitwiseLMDGhost, ForkChoice, ForkChoiceAlgorithm, LongestChain, OptimizedLMDGhost, SlowLMDGhost,
@@ -106,7 +106,7 @@ fn test_yaml_vectors(
// process the tests
for test_case in test_cases {
// 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);
// 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.
block_store
.put(&block_hash, &ssz_encode(&beacon_block)[..])
.unwrap();
store.put(&block_hash, &beacon_block).unwrap();
// run add block for fork choice if not genesis
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(
fork_choice_algo: &ForkChoiceAlgorithm,
num_validators: usize,
) -> (Box<ForkChoice>, Arc<BeaconBlockStore<MemoryDB>>, Hash256) {
let db = Arc::new(MemoryDB::open());
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
let state_store = Arc::new(BeaconStateStore::new(db.clone()));
) -> (Box<ForkChoice>, Arc<MemoryDB>, Hash256) {
let store = Arc::new(MemoryDB::open());
// the fork choice instantiation
let fork_choice: Box<ForkChoice> = match fork_choice_algo {
ForkChoiceAlgorithm::OptimizedLMDGhost => {
let f: OptimizedLMDGhost<MemoryDB, FoundationEthSpec> =
OptimizedLMDGhost::new(block_store.clone(), state_store.clone());
OptimizedLMDGhost::new(store.clone());
Box::new(f)
}
ForkChoiceAlgorithm::BitwiseLMDGhost => {
let f: BitwiseLMDGhost<MemoryDB, FoundationEthSpec> =
BitwiseLMDGhost::new(block_store.clone(), state_store.clone());
BitwiseLMDGhost::new(store.clone());
Box::new(f)
}
ForkChoiceAlgorithm::SlowLMDGhost => {
let f: SlowLMDGhost<MemoryDB, FoundationEthSpec> =
SlowLMDGhost::new(block_store.clone(), state_store.clone());
let f: SlowLMDGhost<MemoryDB, FoundationEthSpec> = SlowLMDGhost::new(store.clone());
Box::new(f)
}
ForkChoiceAlgorithm::LongestChain => Box::new(LongestChain::new(block_store.clone())),
ForkChoiceAlgorithm::LongestChain => Box::new(LongestChain::new(store.clone())),
};
let spec = FoundationEthSpec::spec();
@@ -255,12 +250,10 @@ fn setup_inital_state(
let (state, _keypairs) = state_builder.build();
let state_root = state.canonical_root();
state_store
.put(&state_root, &ssz_encode(&state)[..])
.unwrap();
store.put(&state_root, &state).unwrap();
// 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;