Tidy beacon node runtime code

This commit is contained in:
Paul Hauner 2019-06-08 09:46:04 -04:00
parent 749f2fcb5f
commit fd6766c268
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
7 changed files with 59 additions and 40 deletions

View File

@ -10,7 +10,6 @@ types = { path = "../eth2/types" }
toml = "^0.5" toml = "^0.5"
store = { path = "./store" } store = { path = "./store" }
client = { path = "client" } client = { path = "client" }
fork_choice = { path = "../eth2/fork_choice" }
version = { path = "version" } version = { path = "version" }
clap = "2.32.0" clap = "2.32.0"
slog = { version = "^2.2.3" , features = ["max_level_trace", "release_max_level_debug"] } slog = { version = "^2.2.3" , features = ["max_level_trace", "release_max_level_debug"] }

View File

@ -1,18 +1,14 @@
use beacon_chain::{ use beacon_chain::{
fork_choice::OptimizedLMDGhost, fork_choice::OptimizedLMDGhost, slot_clock::SystemTimeSlotClock, store::Store, BeaconChain,
slot_clock::SystemTimeSlotClock, BeaconChainTypes,
store::{DiskStore, MemoryStore, Store},
BeaconChain, BeaconChainTypes,
}; };
use fork_choice::ForkChoice; use fork_choice::ForkChoice;
use slog::{info, Logger}; use slog::{info, Logger};
use slot_clock::SlotClock; use slot_clock::SlotClock;
use std::marker::PhantomData;
use std::sync::Arc; use std::sync::Arc;
use tree_hash::TreeHash; use tree_hash::TreeHash;
use types::{ use types::{test_utils::TestingBeaconStateBuilder, BeaconBlock, ChainSpec, EthSpec, Hash256};
test_utils::TestingBeaconStateBuilder, BeaconBlock, ChainSpec, EthSpec, Hash256,
MinimalEthSpec,
};
/// The number initial validators when starting the `Minimal`. /// The number initial validators when starting the `Minimal`.
const TESTNET_VALIDATOR_COUNT: usize = 16; const TESTNET_VALIDATOR_COUNT: usize = 16;
@ -28,27 +24,19 @@ pub trait InitialiseBeaconChain<T: BeaconChainTypes> {
} }
} }
/// A testnet-suitable BeaconChainType, using `MemoryStore`.
#[derive(Clone)] #[derive(Clone)]
pub struct TestnetMemoryBeaconChainTypes; pub struct ClientType<S: Store, E: EthSpec> {
impl BeaconChainTypes for TestnetMemoryBeaconChainTypes { _phantom_t: PhantomData<S>,
type Store = MemoryStore; _phantom_u: PhantomData<E>,
type SlotClock = SystemTimeSlotClock;
type ForkChoice = OptimizedLMDGhost<Self::Store, Self::EthSpec>;
type EthSpec = MinimalEthSpec;
} }
impl<T: BeaconChainTypes> InitialiseBeaconChain<T> for TestnetMemoryBeaconChainTypes {}
/// A testnet-suitable BeaconChainType, using `DiskStore`. impl<S: Store, E: EthSpec + Clone> BeaconChainTypes for ClientType<S, E> {
#[derive(Clone)] type Store = S;
pub struct TestnetDiskBeaconChainTypes;
impl BeaconChainTypes for TestnetDiskBeaconChainTypes {
type Store = DiskStore;
type SlotClock = SystemTimeSlotClock; type SlotClock = SystemTimeSlotClock;
type ForkChoice = OptimizedLMDGhost<Self::Store, Self::EthSpec>; type ForkChoice = OptimizedLMDGhost<S, E>;
type EthSpec = MinimalEthSpec; type EthSpec = E;
} }
impl<T: BeaconChainTypes> InitialiseBeaconChain<T> for TestnetDiskBeaconChainTypes {} impl<T: Store, E: EthSpec, X: BeaconChainTypes> InitialiseBeaconChain<X> for ClientType<T, E> {}
/// Loads a `BeaconChain` from `store`, if it exists. Otherwise, create a new chain from genesis. /// Loads a `BeaconChain` from `store`, if it exists. Otherwise, create a new chain from genesis.
fn maybe_load_from_store_for_testnet<T, U: Store, V: EthSpec>( fn maybe_load_from_store_for_testnet<T, U: Store, V: EthSpec>(

View File

@ -19,8 +19,8 @@ use tokio::runtime::TaskExecutor;
use tokio::timer::Interval; use tokio::timer::Interval;
pub use beacon_chain::BeaconChainTypes; pub use beacon_chain::BeaconChainTypes;
pub use beacon_chain_types::ClientType;
pub use beacon_chain_types::InitialiseBeaconChain; pub use beacon_chain_types::InitialiseBeaconChain;
pub use beacon_chain_types::{TestnetDiskBeaconChainTypes, TestnetMemoryBeaconChainTypes};
pub use client_config::ClientConfig; pub use client_config::ClientConfig;
/// Main beacon node client service. This provides the connection and initialisation of the clients /// Main beacon node client service. This provides the connection and initialisation of the clients

View File

@ -27,7 +27,8 @@ pub fn start_server<T: BeaconChainTypes + Clone + 'static>(
network_chan: crossbeam_channel::Sender<NetworkMessage>, network_chan: crossbeam_channel::Sender<NetworkMessage>,
beacon_chain: Arc<BeaconChain<T>>, beacon_chain: Arc<BeaconChain<T>>,
log: &slog::Logger, log: &slog::Logger,
) -> exit_future::Signal { ) -> exit_future::Signal
{
let log = log.new(o!("Service"=>"RPC")); let log = log.new(o!("Service"=>"RPC"));
let env = Arc::new(Environment::new(1)); let env = Arc::new(Environment::new(1));

View File

@ -1,10 +1,9 @@
use client::{ use client::{
error, notifier, BeaconChainTypes, Client, ClientConfig, InitialiseBeaconChain, error, notifier, BeaconChainTypes, Client, ClientConfig, ClientType, InitialiseBeaconChain,
TestnetDiskBeaconChainTypes, TestnetMemoryBeaconChainTypes,
}; };
use futures::sync::oneshot; use futures::sync::oneshot;
use futures::Future; use futures::Future;
use slog::{error, info}; use slog::{warn, error, info};
use std::cell::RefCell; use std::cell::RefCell;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
@ -13,6 +12,7 @@ use tokio::runtime::Builder;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use tokio::runtime::TaskExecutor; use tokio::runtime::TaskExecutor;
use tokio_timer::clock::Clock; use tokio_timer::clock::Clock;
use types::{MainnetEthSpec, MinimalEthSpec};
pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Result<()> { pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Result<()> {
let runtime = Builder::new() let runtime = Builder::new()
@ -27,16 +27,22 @@ pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Resul
.db_path() .db_path()
.ok_or_else::<error::Error, _>(|| "Unable to access database path".into())?; .ok_or_else::<error::Error, _>(|| "Unable to access database path".into())?;
let db_type = &config.db_type; let db_type = &config.db_type;
let spec_constants = &config.spec_constants; let spec_constants = config.spec_constants.clone();
let other_config = config.clone(); let other_config = config.clone();
let result = match (db_type.as_str(), spec_constants.as_str()) { let result = match (db_type.as_str(), spec_constants.as_str()) {
("disk", "testnet") => { ("disk", "minimal") => {
run::<TestnetDiskBeaconChainTypes>(&db_path, config, executor, runtime, log) run::<ClientType<DiskStore, MinimalEthSpec>>(&db_path, config, executor, runtime, log)
} }
("memory", "testnet") => { ("memory", "minimal") => {
run::<TestnetMemoryBeaconChainTypes>(&db_path, config, executor, runtime, log) run::<ClientType<MemoryStore, MinimalEthSpec>>(&db_path, config, executor, runtime, log)
}
("disk", "mainnet") => {
run::<ClientType<DiskStore, MainnetEthSpec>>(&db_path, config, executor, runtime, log)
}
("memory", "mainnet") => {
run::<ClientType<MemoryStore, MainnetEthSpec>>(&db_path, config, executor, runtime, log)
} }
(db_type, spec) => { (db_type, spec) => {
error!(log, "Unknown runtime configuration"; "spec" => spec, "db_type" => db_type); error!(log, "Unknown runtime configuration"; "spec" => spec, "db_type" => db_type);
@ -53,6 +59,23 @@ pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Resul
"spec_constants" => &other_config.spec_constants, "spec_constants" => &other_config.spec_constants,
"db_type" => &other_config.db_type, "db_type" => &other_config.db_type,
); );
// `SHUFFLE_ROUND_COUNT == 10` in minimal, this is not considered safe.
if spec_constants.as_str() == "minimal" {
warn!(
log,
"The minimal specification does not use cryptographically secure committee selection."
)
}
// Mainnet is not really complete, it still generates determinitic, unsafe initial
// validators.
if spec_constants.as_str() == "mainnet" {
warn!(
log,
"The mainnet specification uses unsafe validator keypairs."
)
}
} }
result result
@ -66,7 +89,7 @@ pub fn run<T>(
log: &slog::Logger, log: &slog::Logger,
) -> error::Result<()> ) -> error::Result<()>
where where
T: BeaconChainTypes + InitialiseBeaconChain<T> + Send + Sync + 'static + Clone, T: BeaconChainTypes + InitialiseBeaconChain<T> + Clone + Send + Sync + 'static,
T::Store: OpenDatabase, T::Store: OpenDatabase,
{ {
let store = T::Store::open_database(&db_path)?; let store = T::Store::open_database(&db_path)?;

View File

@ -5,10 +5,14 @@ use leveldb::database::Database;
use leveldb::error::Error as LevelDBError; use leveldb::error::Error as LevelDBError;
use leveldb::options::{Options, ReadOptions, WriteOptions}; use leveldb::options::{Options, ReadOptions, WriteOptions};
use std::path::Path; use std::path::Path;
use std::sync::Arc;
/// A wrapped leveldb database. /// A wrapped leveldb database.
#[derive(Clone)]
pub struct LevelDB { pub struct LevelDB {
db: Database<BytesKey>, // Note: this `Arc` is only included because of an artificial constraint by gRPC. Hopefully we
// can remove this one day.
db: Arc<Database<BytesKey>>,
} }
impl LevelDB { impl LevelDB {
@ -18,7 +22,7 @@ impl LevelDB {
options.create_if_missing = true; options.create_if_missing = true;
let db = Database::open(path, options)?; let db = Arc::new(Database::open(path, options)?);
Ok(Self { db }) Ok(Self { db })
} }

View File

@ -1,19 +1,23 @@
use super::{Error, Store}; use super::{Error, Store};
use parking_lot::RwLock; use parking_lot::RwLock;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc;
type DBHashMap = HashMap<Vec<u8>, Vec<u8>>; type DBHashMap = HashMap<Vec<u8>, Vec<u8>>;
/// A thread-safe `HashMap` wrapper. /// A thread-safe `HashMap` wrapper.
#[derive(Clone)]
pub struct MemoryStore { pub struct MemoryStore {
db: RwLock<DBHashMap>, // Note: this `Arc` is only included because of an artificial constraint by gRPC. Hopefully we
// can remove this one day.
db: Arc<RwLock<DBHashMap>>,
} }
impl MemoryStore { impl MemoryStore {
/// Create a new, empty database. /// Create a new, empty database.
pub fn open() -> Self { pub fn open() -> Self {
Self { Self {
db: RwLock::new(HashMap::new()), db: Arc::new(RwLock::new(HashMap::new())),
} }
} }