Delete SlotClock errs from block_processing, tidy.

This commit is contained in:
Paul Hauner 2019-02-01 16:07:59 +11:00
parent 1e6f85a5eb
commit 9d1f98ba8f
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
4 changed files with 22 additions and 22 deletions

View File

@ -1,6 +1,5 @@
use super::{BeaconChain, ClientDB, DBError, SlotClock}; use super::{BeaconChain, ClientDB, DBError, SlotClock};
use log::debug; use log::debug;
use slot_clock::{SystemTimeSlotClockError, TestingSlotClockError};
use ssz::{ssz_encode, Encodable}; use ssz::{ssz_encode, Encodable};
use types::{ use types::{
beacon_state::{BlockProcessingError, SlotProcessingError}, beacon_state::{BlockProcessingError, SlotProcessingError},
@ -10,32 +9,46 @@ use types::{
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum ValidBlock { pub enum ValidBlock {
/// The block was sucessfully processed.
Processed, Processed,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum InvalidBlock { pub enum InvalidBlock {
/// The block slot is greater than the present slot.
FutureSlot, FutureSlot,
/// The block state_root does not match the generated state.
StateRootMismatch, StateRootMismatch,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Outcome { pub enum Outcome {
/// The block was sucessfully validated.
ValidBlock(ValidBlock), ValidBlock(ValidBlock),
/// The block was not sucessfully validated.
InvalidBlock(InvalidBlock), InvalidBlock(InvalidBlock),
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
/// There was in internal database error.
DBError(String), DBError(String),
/// The block SSZ encoding is unreadable.
UnableToDecodeBlock, UnableToDecodeBlock,
SlotClockError(SystemTimeSlotClockError), /// The blocks parent state is not in the database. This is an internal error.
MissingParentState(Hash256), MissingParentState(Hash256),
/// The blocks parent state is in the database, but invalid. This is an internal error.
InvalidParentState(Hash256), InvalidParentState(Hash256),
/// The blocks parent state is in the database, but invalid. This is an internal error.
MissingBeaconBlock(Hash256), MissingBeaconBlock(Hash256),
/// The parent block is not in the database. The block should not be processed.
InvalidBeaconBlock(Hash256), InvalidBeaconBlock(Hash256),
/// The parent block is not in the database, but invalid. This is an internal error.
MissingParentBlock(Hash256), MissingParentBlock(Hash256),
/// There was an error whilst advancing the parent state to the present slot. This is an
/// internal error.
SlotProcessingError(SlotProcessingError), SlotProcessingError(SlotProcessingError),
/// There was an error whilst processing the block against it's state. The block is invalid.
PerBlockProcessingError(BlockProcessingError), PerBlockProcessingError(BlockProcessingError),
} }
@ -43,8 +56,10 @@ impl<T, U> BeaconChain<T, U>
where where
T: ClientDB, T: ClientDB,
U: SlotClock, U: SlotClock,
Error: From<<U as SlotClock>::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<V>(&self, block: V) -> Result<Outcome, Error> pub fn process_block<V>(&self, block: V) -> Result<Outcome, Error>
where where
V: BeaconBlockReader + Encodable + Sized, V: BeaconBlockReader + Encodable + Sized,
@ -94,12 +109,14 @@ where
self.block_store.put(&block_root, &ssz_encode(&block)[..])?; self.block_store.put(&block_root, &ssz_encode(&block)[..])?;
self.state_store.put(&state_root, &ssz_encode(&state)[..])?; self.state_store.put(&state_root, &ssz_encode(&state)[..])?;
// Update the block DAG.
self.block_graph self.block_graph
.add_leaf(&parent_block_root, block_root.clone()); .add_leaf(&parent_block_root, block_root.clone());
// If the parent block was the parent_block, automatically update the canonical head. // 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 { if self.head().beacon_block_root == parent_block_root {
self.update_canonical_head( self.update_canonical_head(
block.clone(), block.clone(),
@ -107,10 +124,10 @@ where
state.clone(), state.clone(),
state_root.clone(), state_root.clone(),
); );
// Update the local state variable.
*self.state.write() = state.clone(); *self.state.write() = state.clone();
} }
// The block was sucessfully processed.
Ok(Outcome::ValidBlock(ValidBlock::Processed)) Ok(Outcome::ValidBlock(ValidBlock::Processed))
} }
} }
@ -132,15 +149,3 @@ impl From<BlockProcessingError> for Error {
Error::PerBlockProcessingError(e) Error::PerBlockProcessingError(e)
} }
} }
impl From<TestingSlotClockError> for Error {
fn from(_: TestingSlotClockError) -> Error {
unreachable!(); // Testing clock never throws an error.
}
}
impl From<SystemTimeSlotClockError> for Error {
fn from(e: SystemTimeSlotClockError) -> Error {
Error::SlotClockError(e)
}
}

View File

@ -1,6 +1,5 @@
use super::BenchingBeaconNode; use super::BenchingBeaconNode;
use attester::{BeaconNode as AttesterBeaconNode, BeaconNodeError as NodeError, PublishOutcome}; 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 beacon_chain::block_production::Error as BlockProductionError;
use db::ClientDB; use db::ClientDB;
use slot_clock::SlotClock; use slot_clock::SlotClock;
@ -9,7 +8,6 @@ use types::{AttestationData, FreeAttestation};
impl<T: ClientDB, U: SlotClock> AttesterBeaconNode for BenchingBeaconNode<T, U> impl<T: ClientDB, U: SlotClock> AttesterBeaconNode for BenchingBeaconNode<T, U>
where where
BlockProductionError: From<<U>::Error>, BlockProductionError: From<<U>::Error>,
ProcessingError: From<<U as SlotClock>::Error>,
{ {
fn produce_attestation_data( fn produce_attestation_data(
&self, &self,

View File

@ -1,5 +1,4 @@
use super::BenchingBeaconNode; use super::BenchingBeaconNode;
use beacon_chain::block_processing::Error as ProcessingError;
use beacon_chain::block_production::Error as BlockProductionError; use beacon_chain::block_production::Error as BlockProductionError;
use block_producer::{ use block_producer::{
BeaconNode as BeaconBlockNode, BeaconNodeError as BeaconBlockNodeError, PublishOutcome, BeaconNode as BeaconBlockNode, BeaconNodeError as BeaconBlockNodeError, PublishOutcome,
@ -11,7 +10,6 @@ use types::{BeaconBlock, PublicKey, Signature};
impl<T: ClientDB, U: SlotClock> BeaconBlockNode for BenchingBeaconNode<T, U> impl<T: ClientDB, U: SlotClock> BeaconBlockNode for BenchingBeaconNode<T, U>
where where
BlockProductionError: From<<U>::Error>, BlockProductionError: From<<U>::Error>,
ProcessingError: From<<U as SlotClock>::Error>,
{ {
/// Requests the `proposer_nonce` from the `BeaconChain`. /// Requests the `proposer_nonce` from the `BeaconChain`.
fn proposer_nonce(&self, pubkey: &PublicKey) -> Result<u64, BeaconBlockNodeError> { fn proposer_nonce(&self, pubkey: &PublicKey) -> Result<u64, BeaconBlockNodeError> {

View File

@ -2,7 +2,6 @@ use attester::{Attester, Error as AttestationPollError};
use beacon_chain::BeaconChain; use beacon_chain::BeaconChain;
use block_producer::{BlockProducer, Error as BlockPollError}; use block_producer::{BlockProducer, Error as BlockPollError};
use db::MemoryDB; use db::MemoryDB;
use log::trace;
use signer::TestSigner; use signer::TestSigner;
use slot_clock::TestingSlotClock; use slot_clock::TestingSlotClock;
use std::sync::Arc; use std::sync::Arc;