Update beacon_chain as per test bugs

This commit is contained in:
Paul Hauner
2019-01-25 11:30:06 +11:00
parent 643fc20063
commit f4f5b3a13c
9 changed files with 188 additions and 86 deletions

View File

@@ -8,16 +8,23 @@ use types::{
};
#[derive(Debug, PartialEq)]
pub enum Outcome {
FutureSlot,
pub enum ValidBlock {
Processed,
NewCanonicalBlock,
NewReorgBlock,
NewForkBlock,
}
#[derive(Debug, PartialEq)]
pub enum InvalidBlock {
FutureSlot,
StateTransitionFailed(TransitionError),
StateRootMismatch,
}
#[derive(Debug, PartialEq)]
pub enum Outcome {
ValidBlock(ValidBlock),
InvalidBlock(InvalidBlock),
}
#[derive(Debug, PartialEq)]
pub enum Error {
DBError(String),
@@ -70,7 +77,7 @@ where
// Block from future slots (i.e., greater than the present slot) should not be processed.
if block.slot() > present_slot {
return Ok(Outcome::FutureSlot);
return Ok(Outcome::InvalidBlock(InvalidBlock::FutureSlot));
}
let parent_block_root = block.parent_root();
@@ -80,7 +87,8 @@ where
.get_reader(&parent_block_root)?
.ok_or(Error::MissingParentBlock(parent_block_root))?;
let parent_state_root = parent_block.parent_root();
let parent_state_root = parent_block.state_root();
let parent_state = self
.state_store
.get_reader(&parent_state_root)?
@@ -90,13 +98,17 @@ where
let state = match self.state_transition(parent_state, &block) {
Ok(state) => state,
Err(error) => return Ok(Outcome::StateTransitionFailed(error)),
Err(error) => {
return Ok(Outcome::InvalidBlock(InvalidBlock::StateTransitionFailed(
error,
)))
}
};
let state_root = state.canonical_root();
if block.state_root != state_root {
return Ok(Outcome::StateRootMismatch);
return Ok(Outcome::InvalidBlock(InvalidBlock::StateRootMismatch));
}
// Store the block and state.
@@ -119,7 +131,7 @@ where
}
// The block was sucessfully processed.
Ok(Outcome::Processed)
Ok(Outcome::ValidBlock(ValidBlock::Processed))
}
}

View File

@@ -0,0 +1,34 @@
use crate::{BeaconChain, CheckPoint, ClientDB, SlotClock};
use std::sync::RwLockReadGuard;
use types::{BeaconBlock, BeaconState, Hash256};
impl<T, U> BeaconChain<T, U>
where
T: ClientDB,
U: SlotClock,
{
pub fn update_finalized_head(
&self,
new_beacon_block: BeaconBlock,
new_beacon_block_root: Hash256,
new_beacon_state: BeaconState,
new_beacon_state_root: Hash256,
) {
let mut finalized_head = self
.finalized_head
.write()
.expect("CRITICAL: finalized_head poisioned.");
finalized_head.update(
new_beacon_block,
new_beacon_block_root,
new_beacon_state,
new_beacon_state_root,
);
}
pub fn finalized_head(&self) -> RwLockReadGuard<CheckPoint> {
self.finalized_head
.read()
.expect("CRITICAL: finalized_head poisioned.")
}
}

View File

@@ -1,8 +1,9 @@
mod attestation_targets;
mod block_graph;
mod block_processing;
pub mod block_processing;
pub mod block_production;
mod canonical_head;
mod finalized_head;
mod info;
mod lmd_ghost;
mod state_transition;
@@ -17,9 +18,8 @@ use genesis::{genesis_beacon_block, genesis_beacon_state, GenesisError};
use slot_clock::SlotClock;
use spec::ChainSpec;
use ssz::ssz_encode;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};
use types::{BeaconBlock, BeaconState, Hash256, PublicKey};
use types::{BeaconBlock, BeaconState, Hash256};
pub use self::block_processing::Outcome as BlockProcessingOutcome;

View File

@@ -32,7 +32,31 @@ where
U: SlotClock,
Error: From<<U as SlotClock>::Error>,
{
pub fn slow_lmd_ghost(&mut self, start_hash: &Hash256) -> Result<Hash256, Error> {
pub fn fork_choice(&self) -> Result<(), Error> {
let present_head = &self.finalized_head().beacon_block_root;
let new_head = self.slow_lmd_ghost(&self.finalized_head().beacon_block_root)?;
if new_head != *present_head {
let block = self
.block_store
.get_deserialized(&new_head)?
.ok_or_else(|| Error::MissingBeaconBlock(new_head))?;
let block_root = block.canonical_root();
let state = self
.state_store
.get_deserialized(&block.state_root)?
.ok_or_else(|| Error::MissingBeaconState(block.state_root))?;
let state_root = state.canonical_root();
self.update_canonical_head(block, block_root, state, state_root);
}
Ok(())
}
pub fn slow_lmd_ghost(&self, start_hash: &Hash256) -> Result<Hash256, Error> {
let start = self
.block_store
.get_reader(&start_hash)?