diff --git a/beacon_node/beacon_chain/src/block_processing.rs b/beacon_node/beacon_chain/src/block_processing.rs index 6dd48abda..249bd9df7 100644 --- a/beacon_node/beacon_chain/src/block_processing.rs +++ b/beacon_node/beacon_chain/src/block_processing.rs @@ -1,6 +1,5 @@ use super::{BeaconChain, ClientDB, DBError, SlotClock}; use log::debug; -use slot_clock::{SystemTimeSlotClockError, TestingSlotClockError}; use ssz::{ssz_encode, Encodable}; use types::{ beacon_state::{BlockProcessingError, SlotProcessingError}, @@ -10,32 +9,46 @@ use types::{ #[derive(Debug, PartialEq)] pub enum ValidBlock { + /// The block was sucessfully processed. Processed, } #[derive(Debug, PartialEq)] pub enum InvalidBlock { + /// The block slot is greater than the present slot. FutureSlot, + /// The block state_root does not match the generated state. StateRootMismatch, } #[derive(Debug, PartialEq)] pub enum Outcome { + /// The block was sucessfully validated. ValidBlock(ValidBlock), + /// The block was not sucessfully validated. InvalidBlock(InvalidBlock), } #[derive(Debug, PartialEq)] pub enum Error { + /// There was in internal database error. DBError(String), + /// The block SSZ encoding is unreadable. UnableToDecodeBlock, - SlotClockError(SystemTimeSlotClockError), + /// The blocks parent state is not in the database. This is an internal error. MissingParentState(Hash256), + /// The blocks parent state is in the database, but invalid. This is an internal error. InvalidParentState(Hash256), + /// The blocks parent state is in the database, but invalid. This is an internal error. MissingBeaconBlock(Hash256), + /// The parent block is not in the database. The block should not be processed. InvalidBeaconBlock(Hash256), + /// The parent block is not in the database, but invalid. This is an internal error. MissingParentBlock(Hash256), + /// There was an error whilst advancing the parent state to the present slot. This is an + /// internal error. SlotProcessingError(SlotProcessingError), + /// There was an error whilst processing the block against it's state. The block is invalid. PerBlockProcessingError(BlockProcessingError), } @@ -43,8 +56,10 @@ impl BeaconChain where T: ClientDB, U: SlotClock, - Error: From<::Error>, { + /// Accept some block and attempt to add it to block DAG. + /// + /// Will accept blocks from prior slots, however it will reject any block from a future slot. pub fn process_block(&self, block: V) -> Result where V: BeaconBlockReader + Encodable + Sized, @@ -94,12 +109,14 @@ where self.block_store.put(&block_root, &ssz_encode(&block)[..])?; self.state_store.put(&state_root, &ssz_encode(&state)[..])?; + // Update the block DAG. self.block_graph .add_leaf(&parent_block_root, block_root.clone()); // If the parent block was the parent_block, automatically update the canonical head. // - // TODO: this is a first-in-best-dressed scenario that is not ideal -- find a solution. + // TODO: this is a first-in-best-dressed scenario that is not ideal; fork_choice should be + // run instead. if self.head().beacon_block_root == parent_block_root { self.update_canonical_head( block.clone(), @@ -107,10 +124,10 @@ where state.clone(), state_root.clone(), ); + // Update the local state variable. *self.state.write() = state.clone(); } - // The block was sucessfully processed. Ok(Outcome::ValidBlock(ValidBlock::Processed)) } } @@ -132,15 +149,3 @@ impl From for Error { Error::PerBlockProcessingError(e) } } - -impl From for Error { - fn from(_: TestingSlotClockError) -> Error { - unreachable!(); // Testing clock never throws an error. - } -} - -impl From for Error { - fn from(e: SystemTimeSlotClockError) -> Error { - Error::SlotClockError(e) - } -} diff --git a/beacon_node/beacon_chain/test_harness/src/validator/beacon_node/attester.rs b/beacon_node/beacon_chain/test_harness/src/validator/beacon_node/attester.rs index c3e3191f7..bcfb929ea 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator/beacon_node/attester.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator/beacon_node/attester.rs @@ -1,6 +1,5 @@ use super::BenchingBeaconNode; use attester::{BeaconNode as AttesterBeaconNode, BeaconNodeError as NodeError, PublishOutcome}; -use beacon_chain::block_processing::Error as ProcessingError; use beacon_chain::block_production::Error as BlockProductionError; use db::ClientDB; use slot_clock::SlotClock; @@ -9,7 +8,6 @@ use types::{AttestationData, FreeAttestation}; impl AttesterBeaconNode for BenchingBeaconNode where BlockProductionError: From<::Error>, - ProcessingError: From<::Error>, { fn produce_attestation_data( &self, diff --git a/beacon_node/beacon_chain/test_harness/src/validator/beacon_node/producer.rs b/beacon_node/beacon_chain/test_harness/src/validator/beacon_node/producer.rs index 70ca4ff0c..9d0b6b91f 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator/beacon_node/producer.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator/beacon_node/producer.rs @@ -1,5 +1,4 @@ use super::BenchingBeaconNode; -use beacon_chain::block_processing::Error as ProcessingError; use beacon_chain::block_production::Error as BlockProductionError; use block_producer::{ BeaconNode as BeaconBlockNode, BeaconNodeError as BeaconBlockNodeError, PublishOutcome, @@ -11,7 +10,6 @@ use types::{BeaconBlock, PublicKey, Signature}; impl BeaconBlockNode for BenchingBeaconNode where BlockProductionError: From<::Error>, - ProcessingError: From<::Error>, { /// Requests the `proposer_nonce` from the `BeaconChain`. fn proposer_nonce(&self, pubkey: &PublicKey) -> Result { diff --git a/beacon_node/beacon_chain/test_harness/src/validator/mod.rs b/beacon_node/beacon_chain/test_harness/src/validator/mod.rs index adb2cdc45..018ffbc86 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator/mod.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator/mod.rs @@ -2,7 +2,6 @@ use attester::{Attester, Error as AttestationPollError}; use beacon_chain::BeaconChain; use block_producer::{BlockProducer, Error as BlockPollError}; use db::MemoryDB; -use log::trace; use signer::TestSigner; use slot_clock::TestingSlotClock; use std::sync::Arc;