diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index f2c4b3dbea..9f08b6f644 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -79,32 +79,33 @@ impl BlockProcessingOutcome { } } -pub struct BeaconChain { - pub store: Arc, - pub slot_clock: U, - pub op_pool: OperationPool, - canonical_head: RwLock>, - finalized_head: RwLock>, - pub state: RwLock>, - pub spec: ChainSpec, - pub fork_choice: RwLock, +pub trait BeaconChainTypes { + type Store: store::Store; + type SlotClock: slot_clock::SlotClock; + type ForkChoice: fork_choice::ForkChoice; + type EthSpec: types::EthSpec; } -impl BeaconChain -where - T: Store, - U: SlotClock, - F: ForkChoice, - E: EthSpec, -{ +pub struct BeaconChain { + pub store: Arc, + pub slot_clock: T::SlotClock, + pub op_pool: OperationPool, + canonical_head: RwLock>, + finalized_head: RwLock>, + pub state: RwLock>, + pub spec: ChainSpec, + pub fork_choice: RwLock, +} + +impl BeaconChain { /// Instantiate a new Beacon Chain, from genesis. pub fn from_genesis( - store: Arc, - slot_clock: U, - mut genesis_state: BeaconState, + store: Arc, + slot_clock: T::SlotClock, + mut genesis_state: BeaconState, genesis_block: BeaconBlock, spec: ChainSpec, - fork_choice: F, + fork_choice: T::ForkChoice, ) -> Result { let state_root = genesis_state.canonical_root(); store.put(&state_root, &genesis_state)?; @@ -223,7 +224,7 @@ where Err(BeaconStateError::SlotOutOfBounds) => { // Read the earliest historic state in the current slot. let earliest_historic_slot = - state.slot - Slot::from(E::SlotsPerHistoricalRoot::to_usize()); + state.slot - Slot::from(T::EthSpec::slots_per_historical_root()); // Load the earlier state from disk. let new_state_root = state.get_state_root(earliest_historic_slot)?; @@ -263,7 +264,7 @@ where &self, new_beacon_block: BeaconBlock, new_beacon_block_root: Hash256, - new_beacon_state: BeaconState, + new_beacon_state: BeaconState, new_beacon_state_root: Hash256, ) { debug!( @@ -285,7 +286,7 @@ where /// It is important to note that the `beacon_state` returned may not match the present slot. It /// is the state as it was when the head block was received, which could be some slots prior to /// now. - pub fn head(&self) -> RwLockReadGuard> { + pub fn head(&self) -> RwLockReadGuard> { self.canonical_head.read() } @@ -295,7 +296,7 @@ where /// state and calling `catchup_state` as it will not result in an old state being installed and /// then having it iteratively updated -- in such a case it's possible for another thread to /// find the state at an old slot. - pub fn update_state(&self, mut state: BeaconState) -> Result<(), Error> { + pub fn update_state(&self, mut state: BeaconState) -> Result<(), Error> { let present_slot = match self.slot_clock.present_slot() { Ok(Some(slot)) => slot, _ => return Err(Error::UnableToReadSlot), @@ -350,7 +351,7 @@ where &self, new_beacon_block: BeaconBlock, new_beacon_block_root: Hash256, - new_beacon_state: BeaconState, + new_beacon_state: BeaconState, new_beacon_state_root: Hash256, ) { let mut finalized_head = self.finalized_head.write(); @@ -364,7 +365,7 @@ where /// Returns a read-lock guarded `CheckPoint` struct for reading the justified head (as chosen, /// indirectly, by the fork-choice rule). - pub fn finalized_head(&self) -> RwLockReadGuard> { + pub fn finalized_head(&self) -> RwLockReadGuard> { self.finalized_head.read() } @@ -602,7 +603,7 @@ where // significantly lower exposure surface to DoS attacks. // Transition the parent state to the block slot. - let mut state: BeaconState = parent_state; + let mut state: BeaconState = parent_state; for _ in state.slot.as_u64()..block.slot.as_u64() { if let Err(e) = per_slot_processing(&mut state, &self.spec) { return Ok(BlockProcessingOutcome::InvalidBlock( @@ -657,7 +658,7 @@ where pub fn produce_block( &self, randao_reveal: Signature, - ) -> Result<(BeaconBlock, BeaconState), BlockProductionError> { + ) -> Result<(BeaconBlock, BeaconState), BlockProductionError> { debug!("Producing block at slot {}...", self.state.read().slot); let mut state = self.state.read().clone(); @@ -728,7 +729,7 @@ where .ok_or_else(|| Error::MissingBeaconBlock(new_head))?; let block_root = block.canonical_root(); - let state: BeaconState = self + let state: BeaconState = self .store .get(&block.state_root)? .ok_or_else(|| Error::MissingBeaconState(block.state_root))?; @@ -752,7 +753,7 @@ where /// /// This could be a very expensive operation and should only be done in testing/analysis /// activities. - pub fn chain_dump(&self) -> Result>, Error> { + pub fn chain_dump(&self) -> Result>, Error> { let mut dump = vec![]; let mut last_slot = CheckPoint { diff --git a/beacon_node/beacon_chain/src/initialise.rs b/beacon_node/beacon_chain/src/initialise.rs deleted file mode 100644 index b9d950ed54..0000000000 --- a/beacon_node/beacon_chain/src/initialise.rs +++ /dev/null @@ -1,153 +0,0 @@ -// Initialisation functions to generate a new BeaconChain. -// Note: A new version of ClientTypes may need to be implemented for the lighthouse -// testnet. These are examples. Also. there is code duplication which can/should be cleaned up. - -use crate::BeaconChain; -use fork_choice::BitwiseLMDGhost; -use slot_clock::SystemTimeSlotClock; -use std::path::PathBuf; -use std::sync::Arc; -use store::{DiskStore, MemoryStore}; -use tree_hash::TreeHash; -use types::test_utils::TestingBeaconStateBuilder; -use types::{BeaconBlock, ChainSpec, FewValidatorsEthSpec, FoundationEthSpec, Hash256}; - -//TODO: Correct this for prod -//TODO: Account for historical db -pub fn initialise_beacon_chain( - spec: &ChainSpec, - db_name: Option<&PathBuf>, -) -> Arc< - BeaconChain< - DiskStore, - SystemTimeSlotClock, - BitwiseLMDGhost, - FoundationEthSpec, - >, -> { - let path = db_name.expect("db_name cannot be None."); - let store = DiskStore::open(path).expect("Unable to open DB."); - let store = Arc::new(store); - - let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, &spec); - let (genesis_state, _keypairs) = state_builder.build(); - - let mut genesis_block = BeaconBlock::empty(&spec); - genesis_block.state_root = Hash256::from_slice(&genesis_state.tree_hash_root()); - - // Slot clock - let slot_clock = SystemTimeSlotClock::new( - spec.genesis_slot, - genesis_state.genesis_time, - spec.seconds_per_slot, - ) - .expect("Unable to load SystemTimeSlotClock"); - // Choose the fork choice - let fork_choice = BitwiseLMDGhost::new(store.clone()); - - // Genesis chain - //TODO: Handle error correctly - Arc::new( - BeaconChain::from_genesis( - store, - slot_clock, - genesis_state, - genesis_block, - spec.clone(), - fork_choice, - ) - .expect("Terminate if beacon chain generation fails"), - ) -} - -/// Initialisation of a test beacon chain, uses an in memory db with fixed genesis time. -pub fn initialise_test_beacon_chain_with_memory_db( - spec: &ChainSpec, - _db_name: Option<&PathBuf>, -) -> Arc< - BeaconChain< - MemoryStore, - SystemTimeSlotClock, - BitwiseLMDGhost, - FewValidatorsEthSpec, - >, -> { - let store = Arc::new(MemoryStore::open()); - - let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, spec); - let (genesis_state, _keypairs) = state_builder.build(); - - let mut genesis_block = BeaconBlock::empty(spec); - genesis_block.state_root = Hash256::from_slice(&genesis_state.tree_hash_root()); - - // Slot clock - let slot_clock = SystemTimeSlotClock::new( - spec.genesis_slot, - genesis_state.genesis_time, - spec.seconds_per_slot, - ) - .expect("Unable to load SystemTimeSlotClock"); - // Choose the fork choice - let fork_choice = BitwiseLMDGhost::new(store.clone()); - - // Genesis chain - //TODO: Handle error correctly - Arc::new( - BeaconChain::from_genesis( - store, - slot_clock, - genesis_state, - genesis_block, - spec.clone(), - fork_choice, - ) - .expect("Terminate if beacon chain generation fails"), - ) -} - -/// Initialisation of a test beacon chain, uses an in memory db with fixed genesis time. -pub fn initialise_test_beacon_chain_with_disk_db( - spec: &ChainSpec, - db_name: Option<&PathBuf>, -) -> Arc< - BeaconChain< - DiskStore, - SystemTimeSlotClock, - BitwiseLMDGhost, - FewValidatorsEthSpec, - >, -> { - let path = db_name.expect("db_name cannot be None."); - let store = DiskStore::open(path).expect("Unable to open DB."); - let store = Arc::new(store); - - let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, spec); - let (genesis_state, _keypairs) = state_builder.build(); - - let mut genesis_block = BeaconBlock::empty(spec); - genesis_block.state_root = Hash256::from_slice(&genesis_state.tree_hash_root()); - - // Slot clock - let slot_clock = SystemTimeSlotClock::new( - spec.genesis_slot, - genesis_state.genesis_time, - spec.seconds_per_slot, - ) - .expect("Unable to load SystemTimeSlotClock"); - // Choose the fork choice - let fork_choice = BitwiseLMDGhost::new(store.clone()); - - // Genesis chain - //TODO: Handle error correctly - Arc::new( - BeaconChain::from_genesis( - store, - slot_clock, - genesis_state, - genesis_block, - spec.clone(), - fork_choice, - ) - .expect("Terminate if beacon chain generation fails"), - ) -} diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 6ac01a5d54..9f3058d0bc 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -1,10 +1,10 @@ mod beacon_chain; mod checkpoint; mod errors; -pub mod initialise; -pub mod test_utils; -pub use self::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock}; +pub use self::beacon_chain::{ + BeaconChain, BeaconChainTypes, BlockProcessingOutcome, InvalidBlock, ValidBlock, +}; pub use self::checkpoint::CheckPoint; pub use self::errors::{BeaconChainError, BlockProductionError}; pub use fork_choice; diff --git a/beacon_node/beacon_chain/src/test_utils/mod.rs b/beacon_node/beacon_chain/src/test_utils/mod.rs deleted file mode 100644 index ad251a3c9e..0000000000 --- a/beacon_node/beacon_chain/src/test_utils/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod testing_beacon_chain_builder; - -pub use testing_beacon_chain_builder::TestingBeaconChainBuilder; diff --git a/beacon_node/beacon_chain/src/test_utils/testing_beacon_chain_builder.rs b/beacon_node/beacon_chain/src/test_utils/testing_beacon_chain_builder.rs deleted file mode 100644 index b6b1defcc2..0000000000 --- a/beacon_node/beacon_chain/src/test_utils/testing_beacon_chain_builder.rs +++ /dev/null @@ -1,49 +0,0 @@ -pub use crate::{BeaconChain, BeaconChainError, CheckPoint}; -use fork_choice::BitwiseLMDGhost; -use slot_clock::TestingSlotClock; -use std::sync::Arc; -use store::MemoryStore; -use tree_hash::TreeHash; -use types::*; -use types::{test_utils::TestingBeaconStateBuilder, EthSpec, FewValidatorsEthSpec}; - -type TestingBeaconChain = BeaconChain< - MemoryStore, - TestingSlotClock, - BitwiseLMDGhost, - E, ->; - -pub struct TestingBeaconChainBuilder { - state_builder: TestingBeaconStateBuilder, -} - -impl TestingBeaconChainBuilder { - pub fn build(self, spec: &ChainSpec) -> TestingBeaconChain { - let store = Arc::new(MemoryStore::open()); - let slot_clock = TestingSlotClock::new(spec.genesis_slot.as_u64()); - let fork_choice = BitwiseLMDGhost::new(store.clone()); - - let (genesis_state, _keypairs) = self.state_builder.build(); - - let mut genesis_block = BeaconBlock::empty(&spec); - genesis_block.state_root = Hash256::from_slice(&genesis_state.tree_hash_root()); - - // Create the Beacon Chain - BeaconChain::from_genesis( - store, - slot_clock, - genesis_state, - genesis_block, - spec.clone(), - fork_choice, - ) - .unwrap() - } -} - -impl From> for TestingBeaconChainBuilder { - fn from(state_builder: TestingBeaconStateBuilder) -> TestingBeaconChainBuilder { - TestingBeaconChainBuilder { state_builder } - } -} diff --git a/beacon_node/client/Cargo.toml b/beacon_node/client/Cargo.toml index 6634e260d5..387bf16757 100644 --- a/beacon_node/client/Cargo.toml +++ b/beacon_node/client/Cargo.toml @@ -12,6 +12,7 @@ http_server = { path = "../http_server" } rpc = { path = "../rpc" } fork_choice = { path = "../../eth2/fork_choice" } types = { path = "../../eth2/types" } +tree_hash = { path = "../../eth2/utils/tree_hash" } slot_clock = { path = "../../eth2/utils/slot_clock" } error-chain = "0.12.0" slog = "^2.2.3" diff --git a/beacon_node/client/src/beacon_chain_types.rs b/beacon_node/client/src/beacon_chain_types.rs new file mode 100644 index 0000000000..b8236c679f --- /dev/null +++ b/beacon_node/client/src/beacon_chain_types.rs @@ -0,0 +1,109 @@ +use crate::ClientConfig; +use beacon_chain::{ + fork_choice::BitwiseLMDGhost, + slot_clock::SystemTimeSlotClock, + store::{DiskStore, MemoryStore, Store}, + BeaconChain, BeaconChainTypes, +}; +use std::sync::Arc; +use tree_hash::TreeHash; +use types::{ + test_utils::TestingBeaconStateBuilder, BeaconBlock, EthSpec, FewValidatorsEthSpec, Hash256, +}; + +/// Provides a new, initialized `BeaconChain` +pub trait InitialiseBeaconChain { + fn initialise_beacon_chain(config: &ClientConfig) -> BeaconChain; +} + +/// A testnet-suitable BeaconChainType, using `MemoryStore`. +#[derive(Clone)] +pub struct TestnetMemoryBeaconChainTypes; + +impl BeaconChainTypes for TestnetMemoryBeaconChainTypes { + type Store = MemoryStore; + type SlotClock = SystemTimeSlotClock; + type ForkChoice = BitwiseLMDGhost; + type EthSpec = FewValidatorsEthSpec; +} + +impl InitialiseBeaconChain for TestnetMemoryBeaconChainTypes +where + T: BeaconChainTypes< + Store = MemoryStore, + SlotClock = SystemTimeSlotClock, + ForkChoice = BitwiseLMDGhost, + >, +{ + fn initialise_beacon_chain(_config: &ClientConfig) -> BeaconChain { + initialize_chain(MemoryStore::open()) + } +} + +/// A testnet-suitable BeaconChainType, using `DiskStore`. +#[derive(Clone)] +pub struct TestnetDiskBeaconChainTypes; + +impl BeaconChainTypes for TestnetDiskBeaconChainTypes { + type Store = DiskStore; + type SlotClock = SystemTimeSlotClock; + type ForkChoice = BitwiseLMDGhost; + type EthSpec = FewValidatorsEthSpec; +} + +impl InitialiseBeaconChain for TestnetDiskBeaconChainTypes +where + T: BeaconChainTypes< + Store = DiskStore, + SlotClock = SystemTimeSlotClock, + ForkChoice = BitwiseLMDGhost, + >, +{ + fn initialise_beacon_chain(config: &ClientConfig) -> BeaconChain { + let store = DiskStore::open(&config.db_name).expect("Unable to open DB."); + + initialize_chain(store) + } +} + +/// Produces a `BeaconChain` given some pre-initialized `Store`. +fn initialize_chain(store: U) -> BeaconChain +where + T: BeaconChainTypes< + Store = U, + SlotClock = SystemTimeSlotClock, + ForkChoice = BitwiseLMDGhost, + >, +{ + let spec = T::EthSpec::spec(); + + let store = Arc::new(store); + + let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, &spec); + let (genesis_state, _keypairs) = state_builder.build(); + + let mut genesis_block = BeaconBlock::empty(&spec); + genesis_block.state_root = Hash256::from_slice(&genesis_state.tree_hash_root()); + + // Slot clock + let slot_clock = SystemTimeSlotClock::new( + spec.genesis_slot, + genesis_state.genesis_time, + spec.seconds_per_slot, + ) + .expect("Unable to load SystemTimeSlotClock"); + // Choose the fork choice + let fork_choice = BitwiseLMDGhost::new(store.clone()); + + // Genesis chain + //TODO: Handle error correctly + BeaconChain::from_genesis( + store, + slot_clock, + genesis_state, + genesis_block, + spec.clone(), + fork_choice, + ) + .expect("Terminate if beacon chain generation fails") +} diff --git a/beacon_node/client/src/client_types.rs b/beacon_node/client/src/client_types.rs deleted file mode 100644 index 4cce42a064..0000000000 --- a/beacon_node/client/src/client_types.rs +++ /dev/null @@ -1,65 +0,0 @@ -use crate::{ArcBeaconChain, ClientConfig}; -use beacon_chain::{ - fork_choice::BitwiseLMDGhost, - initialise, - slot_clock::{SlotClock, SystemTimeSlotClock}, - store::{DiskStore, MemoryStore, Store}, -}; -use fork_choice::ForkChoice; -use types::{EthSpec, FewValidatorsEthSpec, FoundationEthSpec}; - -pub trait ClientTypes { - type DB: Store + 'static; - type SlotClock: SlotClock + 'static; - type ForkChoice: ForkChoice + 'static; - type EthSpec: EthSpec + 'static; - - fn initialise_beacon_chain( - config: &ClientConfig, - ) -> ArcBeaconChain; -} - -pub struct StandardClientType; - -impl ClientTypes for StandardClientType { - type DB = DiskStore; - type SlotClock = SystemTimeSlotClock; - type ForkChoice = BitwiseLMDGhost; - type EthSpec = FoundationEthSpec; - - fn initialise_beacon_chain( - config: &ClientConfig, - ) -> ArcBeaconChain { - initialise::initialise_beacon_chain(&config.spec, Some(&config.db_name)) - } -} - -pub struct MemoryStoreTestingClientType; - -impl ClientTypes for MemoryStoreTestingClientType { - type DB = MemoryStore; - type SlotClock = SystemTimeSlotClock; - type ForkChoice = BitwiseLMDGhost; - type EthSpec = FewValidatorsEthSpec; - - fn initialise_beacon_chain( - config: &ClientConfig, - ) -> ArcBeaconChain { - initialise::initialise_test_beacon_chain_with_memory_db(&config.spec, None) - } -} - -pub struct DiskStoreTestingClientType; - -impl ClientTypes for DiskStoreTestingClientType { - type DB = DiskStore; - type SlotClock = SystemTimeSlotClock; - type ForkChoice = BitwiseLMDGhost; - type EthSpec = FewValidatorsEthSpec; - - fn initialise_beacon_chain( - config: &ClientConfig, - ) -> ArcBeaconChain { - initialise::initialise_test_beacon_chain_with_disk_db(&config.spec, Some(&config.db_name)) - } -} diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index 9445799d57..40be9b7b8e 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -1,15 +1,13 @@ extern crate slog; +mod beacon_chain_types; mod client_config; -pub mod client_types; pub mod error; pub mod notifier; use beacon_chain::BeaconChain; -pub use client_config::{ClientConfig, DBType}; -pub use client_types::ClientTypes; +use beacon_chain_types::InitialiseBeaconChain; use exit_future::Signal; -use fork_choice::ForkChoice; use futures::{future::Future, Stream}; use network::Service as NetworkService; use slog::{error, info, o}; @@ -17,22 +15,22 @@ use slot_clock::SlotClock; use std::marker::PhantomData; use std::sync::Arc; use std::time::{Duration, Instant}; -use store::Store; use tokio::runtime::TaskExecutor; use tokio::timer::Interval; -use types::EthSpec; -type ArcBeaconChain = Arc>; +pub use beacon_chain::BeaconChainTypes; +pub use beacon_chain_types::{TestnetDiskBeaconChainTypes, TestnetMemoryBeaconChainTypes}; +pub use client_config::{ClientConfig, DBType}; /// Main beacon node client service. This provides the connection and initialisation of the clients /// sub-services in multiple threads. -pub struct Client { +pub struct Client { /// Configuration for the lighthouse client. _config: ClientConfig, /// The beacon chain for the running client. - _beacon_chain: ArcBeaconChain, + _beacon_chain: Arc>, /// Reference to the network service. - pub network: Arc>, + pub network: Arc>, /// Signal to terminate the RPC server. pub rpc_exit_signal: Option, /// Signal to terminate the HTTP server. @@ -45,7 +43,10 @@ pub struct Client { phantom: PhantomData, } -impl Client { +impl Client +where + T: BeaconChainTypes + InitialiseBeaconChain + Clone + 'static, +{ /// Generate an instance of the client. Spawn and link all internal sub-processes. pub fn new( config: ClientConfig, @@ -53,7 +54,7 @@ impl Client { executor: &TaskExecutor, ) -> error::Result { // generate a beacon chain - let beacon_chain = TClientType::initialise_beacon_chain(&config); + let beacon_chain = Arc::new(T::initialise_beacon_chain(&config)); if beacon_chain.read_slot_clock().is_none() { panic!("Cannot start client before genesis!") @@ -158,13 +159,7 @@ impl Client { } } -fn do_state_catchup(chain: &Arc>, log: &slog::Logger) -where - T: Store, - U: SlotClock, - F: ForkChoice, - E: EthSpec, -{ +fn do_state_catchup(chain: &Arc>, log: &slog::Logger) { if let Some(genesis_height) = chain.slots_since_genesis() { let result = chain.catchup_state(); diff --git a/beacon_node/client/src/notifier.rs b/beacon_node/client/src/notifier.rs index aa1e43c3cd..977342b1a1 100644 --- a/beacon_node/client/src/notifier.rs +++ b/beacon_node/client/src/notifier.rs @@ -1,5 +1,5 @@ use crate::Client; -use crate::ClientTypes; +use beacon_chain::BeaconChainTypes; use exit_future::Exit; use futures::{Future, Stream}; use slog::{debug, o}; @@ -10,7 +10,11 @@ use tokio::timer::Interval; /// Thread that monitors the client and reports useful statistics to the user. -pub fn run(client: &Client, executor: TaskExecutor, exit: Exit) { +pub fn run( + client: &Client, + executor: TaskExecutor, + exit: Exit, +) { // notification heartbeat let interval = Interval::new(Instant::now(), Duration::from_secs(5)); diff --git a/beacon_node/http_server/src/lib.rs b/beacon_node/http_server/src/lib.rs index b2c3a86fc3..13754980d0 100644 --- a/beacon_node/http_server/src/lib.rs +++ b/beacon_node/http_server/src/lib.rs @@ -1,6 +1,6 @@ mod prometheus_handler; -use beacon_chain::BeaconChain; +use beacon_chain::{BeaconChain, BeaconChainTypes}; use futures::Future; use iron::prelude::*; use iron::{status::Status, Handler, IronResult, Request, Response}; @@ -10,7 +10,6 @@ use router::Router; use slog::{info, o, warn}; use std::sync::Arc; use tokio::runtime::TaskExecutor; -use types::EthSpec; #[derive(PartialEq, Clone, Debug)] pub struct HttpServerConfig { @@ -45,15 +44,9 @@ impl Handler for IndexHandler { } } -pub fn create_iron_http_server( - beacon_chain: Arc>, -) -> Iron -where - T: store::Store + 'static, - U: slot_clock::SlotClock + 'static, - F: fork_choice::ForkChoice + 'static, - E: EthSpec + 'static, -{ +pub fn create_iron_http_server( + beacon_chain: Arc>, +) -> Iron { let index_handler = IndexHandler { message: "Hello world".to_string(), }; @@ -67,19 +60,13 @@ where Iron::new(router) } -pub fn start_service( +pub fn start_service( config: &HttpServerConfig, executor: &TaskExecutor, _network_chan: crossbeam_channel::Sender, - beacon_chain: Arc>, + beacon_chain: Arc>, log: &slog::Logger, -) -> exit_future::Signal -where - T: store::Store + 'static, - U: slot_clock::SlotClock + 'static, - F: fork_choice::ForkChoice + 'static, - E: EthSpec + 'static, -{ +) -> exit_future::Signal { let log = log.new(o!("Service"=>"HTTP")); // Create: diff --git a/beacon_node/http_server/src/prometheus_handler.rs b/beacon_node/http_server/src/prometheus_handler.rs index ae84d48713..60f56084cd 100644 --- a/beacon_node/http_server/src/prometheus_handler.rs +++ b/beacon_node/http_server/src/prometheus_handler.rs @@ -1,22 +1,17 @@ -use beacon_chain::BeaconChain; +use beacon_chain::{BeaconChain, BeaconChainTypes}; use iron::{status::Status, Handler, IronResult, Request, Response}; use prometheus::{Encoder, IntCounter, Opts, Registry, TextEncoder}; +use slot_clock::SlotClock; use std::sync::Arc; -use types::{EthSpec, Slot}; +use types::Slot; -pub struct PrometheusHandler { - pub beacon_chain: Arc>, +pub struct PrometheusHandler { + pub beacon_chain: Arc>, } -impl PrometheusHandler where E: EthSpec {} +impl PrometheusHandler {} -impl Handler for PrometheusHandler -where - E: EthSpec + 'static, - U: slot_clock::SlotClock + Send + Sync + 'static, - T: Send + Sync + 'static, - F: Send + Sync + 'static, -{ +impl Handler for PrometheusHandler { fn handle(&self, _: &mut Request) -> IronResult { let r = Registry::new(); diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index 2a42376f7c..6324e3a940 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -1,9 +1,6 @@ use beacon_chain::BeaconChain as RawBeaconChain; use beacon_chain::{ - fork_choice::ForkChoice, parking_lot::RwLockReadGuard, - slot_clock::SlotClock, - store::Store, types::{BeaconState, ChainSpec}, AttestationValidationError, CheckPoint, }; @@ -12,17 +9,17 @@ use types::{ Attestation, BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Epoch, EthSpec, Hash256, Slot, }; -pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome, InvalidBlock}; +pub use beacon_chain::{BeaconChainError, BeaconChainTypes, BlockProcessingOutcome, InvalidBlock}; /// The network's API to the beacon chain. -pub trait BeaconChain: Send + Sync { +pub trait BeaconChain: Send + Sync { fn get_spec(&self) -> &ChainSpec; - fn get_state(&self) -> RwLockReadGuard>; + fn get_state(&self) -> RwLockReadGuard>; fn slot(&self) -> Slot; - fn head(&self) -> RwLockReadGuard>; + fn head(&self) -> RwLockReadGuard>; fn get_block(&self, block_root: &Hash256) -> Result, BeaconChainError>; @@ -30,7 +27,7 @@ pub trait BeaconChain: Send + Sync { fn best_block_root(&self) -> Hash256; - fn finalized_head(&self) -> RwLockReadGuard>; + fn finalized_head(&self) -> RwLockReadGuard>; fn finalized_epoch(&self) -> Epoch; @@ -64,18 +61,12 @@ pub trait BeaconChain: Send + Sync { fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result; } -impl BeaconChain for RawBeaconChain -where - T: Store, - U: SlotClock, - F: ForkChoice, - E: EthSpec, -{ +impl BeaconChain for RawBeaconChain { fn get_spec(&self) -> &ChainSpec { &self.spec } - fn get_state(&self) -> RwLockReadGuard> { + fn get_state(&self) -> RwLockReadGuard> { self.state.read() } @@ -83,7 +74,7 @@ where self.get_state().slot } - fn head(&self) -> RwLockReadGuard> { + fn head(&self) -> RwLockReadGuard> { self.head() } @@ -95,7 +86,7 @@ where self.get_state().finalized_epoch } - fn finalized_head(&self) -> RwLockReadGuard> { + fn finalized_head(&self) -> RwLockReadGuard> { self.finalized_head() } diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index a7d0ff2a13..f6a27ad600 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -1,4 +1,4 @@ -use crate::beacon_chain::BeaconChain; +use crate::beacon_chain::{BeaconChain, BeaconChainTypes}; use crate::error; use crate::service::{NetworkMessage, OutgoingMessage}; use crate::sync::SimpleSync; @@ -13,7 +13,6 @@ use slog::{debug, warn}; use std::collections::HashMap; use std::sync::Arc; use std::time::Instant; -use types::EthSpec; /// Timeout for RPC requests. // const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); @@ -21,11 +20,11 @@ use types::EthSpec; // const HELLO_TIMEOUT: Duration = Duration::from_secs(30); /// Handles messages received from the network and client and organises syncing. -pub struct MessageHandler { +pub struct MessageHandler { /// Currently loaded and initialised beacon chain. - _chain: Arc>, + _chain: Arc>, /// The syncing framework. - sync: SimpleSync, + sync: SimpleSync, /// The context required to send messages to, and process messages from peers. network_context: NetworkContext, /// The `MessageHandler` logger. @@ -45,10 +44,10 @@ pub enum HandlerMessage { PubsubMessage(PeerId, Box), } -impl MessageHandler { +impl MessageHandler { /// Initializes and runs the MessageHandler. pub fn spawn( - beacon_chain: Arc>, + beacon_chain: Arc>, network_send: crossbeam_channel::Sender, executor: &tokio::runtime::TaskExecutor, log: slog::Logger, diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index 50454a8759..d87b9e5a9d 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -1,4 +1,4 @@ -use crate::beacon_chain::BeaconChain; +use crate::beacon_chain::{BeaconChain, BeaconChainTypes}; use crate::error; use crate::message_handler::{HandlerMessage, MessageHandler}; use crate::NetworkConfig; @@ -13,20 +13,20 @@ use slog::{debug, info, o, trace}; use std::marker::PhantomData; use std::sync::Arc; use tokio::runtime::TaskExecutor; -use types::{EthSpec, Topic}; +use types::Topic; /// Service that handles communication between internal services and the eth2_libp2p network service. -pub struct Service { +pub struct Service { //libp2p_service: Arc>, _libp2p_exit: oneshot::Sender<()>, network_send: crossbeam_channel::Sender, - _phantom: PhantomData, //message_handler: MessageHandler, + _phantom: PhantomData, //message_handler: MessageHandler, //message_handler_send: Sender } -impl Service { +impl Service { pub fn new( - beacon_chain: Arc>, + beacon_chain: Arc>, config: &NetworkConfig, executor: &TaskExecutor, log: slog::Logger, diff --git a/beacon_node/network/src/sync/import_queue.rs b/beacon_node/network/src/sync/import_queue.rs index 6c2fc33eeb..793f4c395e 100644 --- a/beacon_node/network/src/sync/import_queue.rs +++ b/beacon_node/network/src/sync/import_queue.rs @@ -1,11 +1,11 @@ -use crate::beacon_chain::BeaconChain; +use crate::beacon_chain::{BeaconChain, BeaconChainTypes}; use eth2_libp2p::rpc::methods::*; use eth2_libp2p::PeerId; use slog::{debug, error}; use std::sync::Arc; use std::time::{Duration, Instant}; use tree_hash::TreeHash; -use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, EthSpec, Hash256, Slot}; +use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Hash256, Slot}; /// Provides a queue for fully and partially built `BeaconBlock`s. /// @@ -19,8 +19,8 @@ use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, EthSpec, Hash256, S /// `BeaconBlockBody` as the key. /// - It is possible for multiple distinct blocks to have identical `BeaconBlockBodies`. Therefore /// we cannot use a `HashMap` keyed by the root of `BeaconBlockBody`. -pub struct ImportQueue { - pub chain: Arc>, +pub struct ImportQueue { + pub chain: Arc>, /// Partially imported blocks, keyed by the root of `BeaconBlockBody`. pub partials: Vec, /// Time before a queue entry is considered state. @@ -29,9 +29,9 @@ pub struct ImportQueue { log: slog::Logger, } -impl ImportQueue { +impl ImportQueue { /// Return a new, empty queue. - pub fn new(chain: Arc>, stale_time: Duration, log: slog::Logger) -> Self { + pub fn new(chain: Arc>, stale_time: Duration, log: slog::Logger) -> Self { Self { chain, partials: vec![], diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index d44ffd4b75..6ab8ea7d9a 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -1,5 +1,5 @@ use super::import_queue::ImportQueue; -use crate::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock}; +use crate::beacon_chain::{BeaconChain, BeaconChainTypes, BlockProcessingOutcome, InvalidBlock}; use crate::message_handler::NetworkContext; use eth2_libp2p::rpc::methods::*; use eth2_libp2p::rpc::{RPCRequest, RPCResponse, RequestId}; @@ -9,7 +9,7 @@ use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; use tree_hash::TreeHash; -use types::{Attestation, BeaconBlock, Epoch, EthSpec, Hash256, Slot}; +use types::{Attestation, BeaconBlock, Epoch, Hash256, Slot}; /// The number of slots that we can import blocks ahead of us, before going into full Sync mode. const SLOT_IMPORT_TOLERANCE: u64 = 100; @@ -88,8 +88,8 @@ impl From for PeerSyncInfo { } } -impl From<&Arc>> for PeerSyncInfo { - fn from(chain: &Arc>) -> PeerSyncInfo { +impl From<&Arc>> for PeerSyncInfo { + fn from(chain: &Arc>) -> PeerSyncInfo { Self::from(chain.hello_message()) } } @@ -103,22 +103,22 @@ pub enum SyncState { } /// Simple Syncing protocol. -pub struct SimpleSync { +pub struct SimpleSync { /// A reference to the underlying beacon chain. - chain: Arc>, + chain: Arc>, /// A mapping of Peers to their respective PeerSyncInfo. known_peers: HashMap, /// A queue to allow importing of blocks - import_queue: ImportQueue, + import_queue: ImportQueue, /// The current state of the syncing protocol. state: SyncState, /// Sync logger. log: slog::Logger, } -impl SimpleSync { +impl SimpleSync { /// Instantiate a `SimpleSync` instance, with no peers and an empty queue. - pub fn new(beacon_chain: Arc>, log: &slog::Logger) -> Self { + pub fn new(beacon_chain: Arc>, log: &slog::Logger) -> Self { let sync_logger = log.new(o!("Service"=> "Sync")); let queue_item_stale_time = Duration::from_secs(QUEUE_STALE_SECS); diff --git a/beacon_node/rpc/src/attestation.rs b/beacon_node/rpc/src/attestation.rs index e22715b555..6048e42b1b 100644 --- a/beacon_node/rpc/src/attestation.rs +++ b/beacon_node/rpc/src/attestation.rs @@ -1,4 +1,4 @@ -use crate::beacon_chain::BeaconChain; +use crate::beacon_chain::{BeaconChain, BeaconChainTypes}; use futures::Future; use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; use protos::services::{ @@ -9,15 +9,15 @@ use protos::services_grpc::AttestationService; use slog::{error, info, trace, warn}; use ssz::{ssz_encode, Decode}; use std::sync::Arc; -use types::{Attestation, EthSpec}; +use types::Attestation; #[derive(Clone)] -pub struct AttestationServiceInstance { - pub chain: Arc>, +pub struct AttestationServiceInstance { + pub chain: Arc>, pub log: slog::Logger, } -impl AttestationService for AttestationServiceInstance { +impl AttestationService for AttestationServiceInstance { /// Produce the `AttestationData` for signing by a validator. fn produce_attestation_data( &mut self, diff --git a/beacon_node/rpc/src/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs index bbe6a8ee28..e553b79e7f 100644 --- a/beacon_node/rpc/src/beacon_block.rs +++ b/beacon_node/rpc/src/beacon_block.rs @@ -1,4 +1,4 @@ -use crate::beacon_chain::BeaconChain; +use crate::beacon_chain::{BeaconChain, BeaconChainTypes}; use crossbeam_channel; use eth2_libp2p::PubsubMessage; use futures::Future; @@ -13,16 +13,16 @@ use slog::Logger; use slog::{error, info, trace, warn}; use ssz::{ssz_encode, Decode}; use std::sync::Arc; -use types::{BeaconBlock, EthSpec, Signature, Slot}; +use types::{BeaconBlock, Signature, Slot}; #[derive(Clone)] -pub struct BeaconBlockServiceInstance { - pub chain: Arc>, +pub struct BeaconBlockServiceInstance { + pub chain: Arc>, pub network_chan: crossbeam_channel::Sender, pub log: Logger, } -impl BeaconBlockService for BeaconBlockServiceInstance { +impl BeaconBlockService for BeaconBlockServiceInstance { /// Produce a `BeaconBlock` for signing by a validator. fn produce_beacon_block( &mut self, diff --git a/beacon_node/rpc/src/beacon_chain.rs b/beacon_node/rpc/src/beacon_chain.rs index d12baf1d13..b0a490137c 100644 --- a/beacon_node/rpc/src/beacon_chain.rs +++ b/beacon_node/rpc/src/beacon_chain.rs @@ -1,22 +1,19 @@ use beacon_chain::BeaconChain as RawBeaconChain; use beacon_chain::{ - fork_choice::ForkChoice, parking_lot::{RwLockReadGuard, RwLockWriteGuard}, - slot_clock::SlotClock, - store::Store, types::{BeaconState, ChainSpec, Signature}, AttestationValidationError, BlockProductionError, }; -pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome}; +pub use beacon_chain::{BeaconChainError, BeaconChainTypes, BlockProcessingOutcome}; use types::{Attestation, AttestationData, BeaconBlock, EthSpec}; /// The RPC's API to the beacon chain. -pub trait BeaconChain: Send + Sync { +pub trait BeaconChain: Send + Sync { fn get_spec(&self) -> &ChainSpec; - fn get_state(&self) -> RwLockReadGuard>; + fn get_state(&self) -> RwLockReadGuard>; - fn get_mut_state(&self) -> RwLockWriteGuard>; + fn get_mut_state(&self) -> RwLockWriteGuard>; fn process_block(&self, block: BeaconBlock) -> Result; @@ -24,7 +21,7 @@ pub trait BeaconChain: Send + Sync { fn produce_block( &self, randao_reveal: Signature, - ) -> Result<(BeaconBlock, BeaconState), BlockProductionError>; + ) -> Result<(BeaconBlock, BeaconState), BlockProductionError>; fn produce_attestation_data(&self, shard: u64) -> Result; @@ -34,22 +31,16 @@ pub trait BeaconChain: Send + Sync { ) -> Result<(), AttestationValidationError>; } -impl BeaconChain for RawBeaconChain -where - T: Store, - U: SlotClock, - F: ForkChoice, - E: EthSpec, -{ +impl BeaconChain for RawBeaconChain { fn get_spec(&self) -> &ChainSpec { &self.spec } - fn get_state(&self) -> RwLockReadGuard> { + fn get_state(&self) -> RwLockReadGuard> { self.state.read() } - fn get_mut_state(&self) -> RwLockWriteGuard> { + fn get_mut_state(&self) -> RwLockWriteGuard> { self.state.write() } @@ -63,7 +54,7 @@ where fn produce_block( &self, randao_reveal: Signature, - ) -> Result<(BeaconBlock, BeaconState), BlockProductionError> { + ) -> Result<(BeaconBlock, BeaconState), BlockProductionError> { self.produce_block(randao_reveal) } diff --git a/beacon_node/rpc/src/beacon_node.rs b/beacon_node/rpc/src/beacon_node.rs index 2ca39ae512..a923bbb356 100644 --- a/beacon_node/rpc/src/beacon_node.rs +++ b/beacon_node/rpc/src/beacon_node.rs @@ -1,19 +1,18 @@ -use crate::beacon_chain::BeaconChain; +use crate::beacon_chain::{BeaconChain, BeaconChainTypes}; use futures::Future; use grpcio::{RpcContext, UnarySink}; use protos::services::{Empty, Fork, NodeInfoResponse}; use protos::services_grpc::BeaconNodeService; use slog::{trace, warn}; use std::sync::Arc; -use types::EthSpec; #[derive(Clone)] -pub struct BeaconNodeServiceInstance { - pub chain: Arc>, +pub struct BeaconNodeServiceInstance { + pub chain: Arc>, pub log: slog::Logger, } -impl BeaconNodeService for BeaconNodeServiceInstance { +impl BeaconNodeService for BeaconNodeServiceInstance { /// Provides basic node information. fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink) { trace!(self.log, "Node info requested via RPC"); diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index f1d5e9c886..9646135b68 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -7,7 +7,7 @@ mod validator; use self::attestation::AttestationServiceInstance; use self::beacon_block::BeaconBlockServiceInstance; -use self::beacon_chain::BeaconChain; +use self::beacon_chain::{BeaconChain, BeaconChainTypes}; use self::beacon_node::BeaconNodeServiceInstance; use self::validator::ValidatorServiceInstance; pub use config::Config as RPCConfig; @@ -21,13 +21,12 @@ use protos::services_grpc::{ use slog::{info, o, warn}; use std::sync::Arc; use tokio::runtime::TaskExecutor; -use types::EthSpec; -pub fn start_server( +pub fn start_server( config: &RPCConfig, executor: &TaskExecutor, network_chan: crossbeam_channel::Sender, - beacon_chain: Arc>, + beacon_chain: Arc>, log: &slog::Logger, ) -> exit_future::Signal { let log = log.new(o!("Service"=>"RPC")); diff --git a/beacon_node/rpc/src/validator.rs b/beacon_node/rpc/src/validator.rs index 34fbba5c49..e58c202d66 100644 --- a/beacon_node/rpc/src/validator.rs +++ b/beacon_node/rpc/src/validator.rs @@ -1,4 +1,4 @@ -use crate::beacon_chain::BeaconChain; +use crate::beacon_chain::{BeaconChain, BeaconChainTypes}; use bls::PublicKey; use futures::Future; use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; @@ -7,16 +7,16 @@ use protos::services_grpc::ValidatorService; use slog::{trace, warn}; use ssz::Decode; use std::sync::Arc; -use types::{Epoch, EthSpec, RelativeEpoch}; +use types::{Epoch, RelativeEpoch}; #[derive(Clone)] -pub struct ValidatorServiceInstance { - pub chain: Arc>, +pub struct ValidatorServiceInstance { + pub chain: Arc>, pub log: slog::Logger, } //TODO: Refactor Errors -impl ValidatorService for ValidatorServiceInstance { +impl ValidatorService for ValidatorServiceInstance { /// For a list of validator public keys, this function returns the slot at which each /// validator must propose a block, attest to a shard, their shard committee and the shard they /// need to attest to. diff --git a/beacon_node/src/run.rs b/beacon_node/src/run.rs index 4cf930060f..6ec65a92d4 100644 --- a/beacon_node/src/run.rs +++ b/beacon_node/src/run.rs @@ -1,6 +1,7 @@ -use client::client_types::{DiskStoreTestingClientType, MemoryStoreTestingClientType}; -use client::{error, DBType}; -use client::{notifier, Client, ClientConfig, ClientTypes}; +use client::{ + error, notifier, BeaconChainTypes, Client, ClientConfig, DBType, TestnetDiskBeaconChainTypes, + TestnetMemoryBeaconChainTypes, +}; use futures::sync::oneshot; use futures::Future; use slog::info; @@ -29,9 +30,9 @@ pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Resul info!( log, "BeaconNode starting"; - "type" => "DiskStoreTestingClientType" + "type" => "TestnetDiskBeaconChainTypes" ); - let client: Client = + let client: Client = Client::new(config, log.clone(), &executor)?; run(client, executor, runtime, log) @@ -40,9 +41,9 @@ pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Resul info!( log, "BeaconNode starting"; - "type" => "MemoryStoreTestingClientType" + "type" => "TestnetMemoryBeaconChainTypes" ); - let client: Client = + let client: Client = Client::new(config, log.clone(), &executor)?; run(client, executor, runtime, log) @@ -50,7 +51,7 @@ pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Resul } } -pub fn run( +pub fn run( client: Client, executor: TaskExecutor, mut runtime: Runtime,