Add new fork choice struct to beacon chain

This commit is contained in:
Paul Hauner
2019-06-15 14:03:29 -04:00
parent c43bbfe183
commit 2ee71aa808
7 changed files with 127 additions and 129 deletions

View File

@@ -1,20 +1,15 @@
pub mod reduced_tree;
mod reduced_tree;
use state_processing::common::get_attesting_indices_unsorted;
use std::marker::PhantomData;
use std::sync::Arc;
use types::{Attestation, BeaconBlock, BeaconState, BeaconStateError, EthSpec, Hash256, Slot};
use store::Store;
use types::{EthSpec, Hash256, Slot};
type Result<T> = std::result::Result<T, Error>;
pub use reduced_tree::ThreadSafeReducedTree;
#[derive(Debug, PartialEq)]
pub enum Error {
BackendError(String),
BeaconStateError(BeaconStateError),
}
pub type Result<T> = std::result::Result<T, String>;
pub trait LmdGhostBackend<T, E: EthSpec>: Send + Sync {
fn new(store: Arc<T>) -> Self;
pub trait LmdGhost<S: Store, E: EthSpec>: Send + Sync {
fn new(store: Arc<S>) -> Self;
fn process_message(
&self,
@@ -25,75 +20,3 @@ pub trait LmdGhostBackend<T, E: EthSpec>: Send + Sync {
fn find_head(&self) -> Result<Hash256>;
}
pub struct ForkChoice<T, E> {
backend: T,
_phantom: PhantomData<E>,
}
impl<T, E> ForkChoice<T, E>
where
T: LmdGhostBackend<T, E>,
E: EthSpec,
{
pub fn new(store: Arc<T>) -> Self {
Self {
backend: T::new(store),
_phantom: PhantomData,
}
}
pub fn find_head(&self) -> Result<Hash256> {
self.backend.find_head()
}
pub fn process_attestation(
&self,
state: &BeaconState<E>,
attestation: &Attestation,
) -> Result<()> {
let validator_indices = get_attesting_indices_unsorted(
state,
&attestation.data,
&attestation.aggregation_bitfield,
)?;
let block_hash = attestation.data.target_root;
// TODO: what happens when the target root is not the same slot as the block?
let block_slot = attestation
.data
.target_epoch
.start_slot(E::slots_per_epoch());
for validator_index in validator_indices {
self.backend
.process_message(validator_index, block_hash, block_slot)?;
}
Ok(())
}
pub fn process_block(
&self,
state: &BeaconState<E>,
block: &BeaconBlock,
block_hash: Hash256,
block_proposer: usize,
) -> Result<()> {
self.backend
.process_message(block_proposer, block_hash, block.slot)?;
for attestation in &block.body.attestations {
self.process_attestation(state, attestation)?;
}
Ok(())
}
}
impl From<BeaconStateError> for Error {
fn from(e: BeaconStateError) -> Error {
Error::BeaconStateError(e)
}
}

View File

@@ -1,4 +1,4 @@
use super::{Error as SuperError, LmdGhostBackend};
use super::{LmdGhost, Result as SuperResult};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::marker::PhantomData;
@@ -8,8 +8,6 @@ use types::{BeaconBlock, BeaconState, EthSpec, Hash256, Slot};
type Result<T> = std::result::Result<T, Error>;
pub const SKIP_LIST_LEN: usize = 16;
#[derive(Debug, PartialEq)]
pub enum Error {
MissingNode(Hash256),
@@ -51,10 +49,6 @@ impl Node {
pub fn has_votes(&self) -> bool {
!self.voters.is_empty()
}
pub fn is_genesis(&self) -> bool {
self.parent_hash.is_some()
}
}
impl Node {
@@ -69,7 +63,7 @@ pub struct Vote {
slot: Slot,
}
impl<T, E> LmdGhostBackend<T, E> for ThreadSafeReducedTree<T, E>
impl<T, E> LmdGhost<T, E> for ThreadSafeReducedTree<T, E>
where
T: Store,
E: EthSpec,
@@ -85,21 +79,21 @@ where
validator_index: usize,
block_hash: Hash256,
block_slot: Slot,
) -> std::result::Result<(), SuperError> {
) -> SuperResult<()> {
self.core
.write()
.process_message(validator_index, block_hash, block_slot)
.map_err(Into::into)
}
fn find_head(&self) -> std::result::Result<Hash256, SuperError> {
fn find_head(&self) -> SuperResult<Hash256> {
unimplemented!();
}
}
impl From<Error> for SuperError {
fn from(e: Error) -> SuperError {
SuperError::BackendError(format!("{:?}", e))
impl From<Error> for String {
fn from(e: Error) -> String {
format!("{:?}", e)
}
}
@@ -415,11 +409,6 @@ where
&self.0[i]
}
pub fn get_mut(&mut self, i: usize) -> &mut T {
self.ensure(i);
&mut self.0[i]
}
pub fn insert(&mut self, i: usize, element: T) {
self.ensure(i);
self.0[i] = element;