Add new fns to ForkChoice and SlotClock

This commit is contained in:
Paul Hauner
2019-05-27 15:12:51 +10:00
parent 9922ed2239
commit 76602a65fc
11 changed files with 88 additions and 108 deletions

View File

@@ -48,18 +48,6 @@ pub struct BitwiseLMDGhost<T, E> {
}
impl<T: Store, E: EthSpec> BitwiseLMDGhost<T, E> {
pub fn new(store: Arc<T>) -> Self {
BitwiseLMDGhost {
cache: HashMap::new(),
ancestors: vec![HashMap::new(); 16],
latest_attestation_targets: HashMap::new(),
children: HashMap::new(),
max_known_height: SlotHeight::new(0),
store,
_phantom: PhantomData,
}
}
/// Finds the latest votes weighted by validator balance. Returns a hashmap of block_hash to
/// weighted votes.
pub fn get_latest_votes(
@@ -229,7 +217,19 @@ impl<T: Store, E: EthSpec> BitwiseLMDGhost<T, E> {
}
}
impl<T: Store, E: EthSpec> ForkChoice for BitwiseLMDGhost<T, E> {
impl<T: Store, E: EthSpec> ForkChoice<T> for BitwiseLMDGhost<T, E> {
fn new(store: Arc<T>) -> Self {
BitwiseLMDGhost {
cache: HashMap::new(),
ancestors: vec![HashMap::new(); 16],
latest_attestation_targets: HashMap::new(),
children: HashMap::new(),
max_known_height: SlotHeight::new(0),
store,
_phantom: PhantomData,
}
}
fn add_block(
&mut self,
block: &BeaconBlock,

View File

@@ -21,8 +21,7 @@ pub mod longest_chain;
pub mod optimized_lmd_ghost;
pub mod slow_lmd_ghost;
// use store::stores::BeaconBlockAtSlotError;
// use store::DBError;
use std::sync::Arc;
use store::Error as DBError;
use types::{BeaconBlock, ChainSpec, Hash256};
@@ -34,7 +33,10 @@ pub use slow_lmd_ghost::SlowLMDGhost;
/// Defines the interface for Fork Choices. Each Fork choice will define their own data structures
/// which can be built in block processing through the `add_block` and `add_attestation` functions.
/// The main fork choice algorithm is specified in `find_head
pub trait ForkChoice: Send + Sync {
pub trait ForkChoice<T>: Send + Sync {
/// Create a new `ForkChoice` which reads from `store`.
fn new(store: Arc<T>) -> Self;
/// Called when a block has been added. Allows generic block-level data structures to be
/// built for a given fork-choice.
fn add_block(
@@ -78,22 +80,6 @@ impl From<DBError> for ForkChoiceError {
}
}
/*
impl From<BeaconBlockAtSlotError> for ForkChoiceError {
fn from(e: BeaconBlockAtSlotError) -> ForkChoiceError {
match e {
BeaconBlockAtSlotError::UnknownBeaconBlock(hash) => {
ForkChoiceError::MissingBeaconBlock(hash)
}
BeaconBlockAtSlotError::InvalidBeaconBlock(hash) => {
ForkChoiceError::MissingBeaconBlock(hash)
}
BeaconBlockAtSlotError::DBError(string) => ForkChoiceError::StorageError(string),
}
}
}
*/
/// Fork choice options that are currently implemented.
#[derive(Debug, Clone)]
pub enum ForkChoiceAlgorithm {

View File

@@ -10,16 +10,14 @@ pub struct LongestChain<T> {
store: Arc<T>,
}
impl<T: Store> LongestChain<T> {
pub fn new(store: Arc<T>) -> Self {
impl<T: Store> ForkChoice<T> for LongestChain<T> {
fn new(store: Arc<T>) -> Self {
LongestChain {
head_block_hashes: Vec::new(),
store,
}
}
}
impl<T: Store> ForkChoice for LongestChain<T> {
fn add_block(
&mut self,
block: &BeaconBlock,

View File

@@ -48,18 +48,6 @@ pub struct OptimizedLMDGhost<T, E> {
}
impl<T: Store, E: EthSpec> OptimizedLMDGhost<T, E> {
pub fn new(store: Arc<T>) -> Self {
OptimizedLMDGhost {
cache: HashMap::new(),
ancestors: vec![HashMap::new(); 16],
latest_attestation_targets: HashMap::new(),
children: HashMap::new(),
max_known_height: SlotHeight::new(0),
store,
_phantom: PhantomData,
}
}
/// Finds the latest votes weighted by validator balance. Returns a hashmap of block_hash to
/// weighted votes.
pub fn get_latest_votes(
@@ -200,7 +188,19 @@ impl<T: Store, E: EthSpec> OptimizedLMDGhost<T, E> {
}
}
impl<T: Store, E: EthSpec> ForkChoice for OptimizedLMDGhost<T, E> {
impl<T: Store, E: EthSpec> ForkChoice<T> for OptimizedLMDGhost<T, E> {
fn new(store: Arc<T>) -> Self {
OptimizedLMDGhost {
cache: HashMap::new(),
ancestors: vec![HashMap::new(); 16],
latest_attestation_targets: HashMap::new(),
children: HashMap::new(),
max_known_height: SlotHeight::new(0),
store,
_phantom: PhantomData,
}
}
fn add_block(
&mut self,
block: &BeaconBlock,

View File

@@ -20,15 +20,6 @@ pub struct SlowLMDGhost<T, E> {
}
impl<T: Store, E: EthSpec> SlowLMDGhost<T, E> {
pub fn new(store: Arc<T>) -> Self {
SlowLMDGhost {
latest_attestation_targets: HashMap::new(),
children: HashMap::new(),
store,
_phantom: PhantomData,
}
}
/// Finds the latest votes weighted by validator balance. Returns a hashmap of block_hash to
/// weighted votes.
pub fn get_latest_votes(
@@ -94,7 +85,16 @@ impl<T: Store, E: EthSpec> SlowLMDGhost<T, E> {
}
}
impl<T: Store, E: EthSpec> ForkChoice for SlowLMDGhost<T, E> {
impl<T: Store, E: EthSpec> ForkChoice<T> for SlowLMDGhost<T, E> {
fn new(store: Arc<T>) -> Self {
SlowLMDGhost {
latest_attestation_targets: HashMap::new(),
children: HashMap::new(),
store,
_phantom: PhantomData,
}
}
/// Process when a block is added
fn add_block(
&mut self,