Allow fork_choice and beacon_chain to compile

This commit is contained in:
Paul Hauner 2019-05-08 18:18:17 +10:00
parent 9fd8af8428
commit 51dc97ee42
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
8 changed files with 87 additions and 64 deletions

View File

@ -83,30 +83,31 @@ impl BlockProcessingOutcome {
}
}
pub struct BeaconChain<T: ClientDB + Sized, U: SlotClock, F: ForkChoice> {
pub struct BeaconChain<T: ClientDB + Sized, U: SlotClock, F: ForkChoice, B: BeaconStateTypes> {
pub block_store: Arc<BeaconBlockStore<T>>,
pub state_store: Arc<BeaconStateStore<T>>,
pub slot_clock: U,
pub op_pool: OperationPool,
canonical_head: RwLock<CheckPoint>,
finalized_head: RwLock<CheckPoint>,
pub state: RwLock<BeaconState>,
pub op_pool: OperationPool<B>,
canonical_head: RwLock<CheckPoint<B>>,
finalized_head: RwLock<CheckPoint<B>>,
pub state: RwLock<BeaconState<B>>,
pub spec: ChainSpec,
pub fork_choice: RwLock<F>,
}
impl<T, U, F> BeaconChain<T, U, F>
impl<T, U, F, B> BeaconChain<T, U, F, B>
where
T: ClientDB,
U: SlotClock,
F: ForkChoice,
B: BeaconStateTypes,
{
/// Instantiate a new Beacon Chain, from genesis.
pub fn from_genesis(
state_store: Arc<BeaconStateStore<T>>,
block_store: Arc<BeaconBlockStore<T>>,
slot_clock: U,
mut genesis_state: BeaconState,
mut genesis_state: BeaconState<B>,
genesis_block: BeaconBlock,
spec: ChainSpec,
fork_choice: F,
@ -218,7 +219,7 @@ where
//
// If we get `SlotOutOfBounds` error, load the oldest available historic
// state from the DB.
match state.get_block_root(slot, spec) {
match state.get_block_root(slot) {
Ok(root) => {
if slot < earliest_slot {
break;
@ -230,9 +231,9 @@ where
Err(BeaconStateError::SlotOutOfBounds) => {
// Read the earliest historic state in the current slot.
let earliest_historic_slot =
state.slot - Slot::from(spec.slots_per_historical_root);
state.slot - Slot::from(B::SlotsPerHistoricalRoot::to_usize());
// Load the earlier state from disk.
let new_state_root = state.get_state_root(earliest_historic_slot, spec)?;
let new_state_root = state.get_state_root(earliest_historic_slot)?;
// Break if the DB is unable to load the state.
state = match self.state_store.get_deserialized(&new_state_root) {
@ -270,7 +271,7 @@ where
&self,
new_beacon_block: BeaconBlock,
new_beacon_block_root: Hash256,
new_beacon_state: BeaconState,
new_beacon_state: BeaconState<B>,
new_beacon_state_root: Hash256,
) {
debug!(
@ -292,7 +293,7 @@ where
/// It is important to note that the `beacon_state` returned may not match the present slot. It
/// is the state as it was when the head block was received, which could be some slots prior to
/// now.
pub fn head(&self) -> RwLockReadGuard<CheckPoint> {
pub fn head(&self) -> RwLockReadGuard<CheckPoint<B>> {
self.canonical_head.read()
}
@ -302,7 +303,7 @@ where
/// state and calling `catchup_state` as it will not result in an old state being installed and
/// then having it iteratively updated -- in such a case it's possible for another thread to
/// find the state at an old slot.
pub fn update_state(&self, mut state: BeaconState) -> Result<(), Error> {
pub fn update_state(&self, mut state: BeaconState<B>) -> Result<(), Error> {
let present_slot = match self.slot_clock.present_slot() {
Ok(Some(slot)) => slot,
_ => return Err(Error::UnableToReadSlot),
@ -357,7 +358,7 @@ where
&self,
new_beacon_block: BeaconBlock,
new_beacon_block_root: Hash256,
new_beacon_state: BeaconState,
new_beacon_state: BeaconState<B>,
new_beacon_state_root: Hash256,
) {
let mut finalized_head = self.finalized_head.write();
@ -371,7 +372,7 @@ where
/// Returns a read-lock guarded `CheckPoint` struct for reading the justified head (as chosen,
/// indirectly, by the fork-choice rule).
pub fn finalized_head(&self) -> RwLockReadGuard<CheckPoint> {
pub fn finalized_head(&self) -> RwLockReadGuard<CheckPoint<B>> {
self.finalized_head.read()
}
@ -493,17 +494,14 @@ where
} else {
// If the current head block is not from this slot, use the slot from the previous
// epoch.
*self.state.read().get_block_root(
current_epoch_start_slot - self.spec.slots_per_epoch,
&self.spec,
)?
*self
.state
.read()
.get_block_root(current_epoch_start_slot - self.spec.slots_per_epoch)?
}
} else {
// If we're not on the first slot of the epoch.
*self
.state
.read()
.get_block_root(current_epoch_start_slot, &self.spec)?
*self.state.read().get_block_root(current_epoch_start_slot)?
};
Ok(AttestationData {
@ -667,7 +665,7 @@ where
pub fn produce_block(
&self,
randao_reveal: Signature,
) -> Result<(BeaconBlock, BeaconState), BlockProductionError> {
) -> Result<(BeaconBlock, BeaconState<B>), BlockProductionError> {
debug!("Producing block at slot {}...", self.state.read().slot);
let mut state = self.state.read().clone();
@ -677,7 +675,7 @@ where
trace!("Finding attestations for new block...");
let previous_block_root = *state
.get_block_root(state.slot - 1, &self.spec)
.get_block_root(state.slot - 1)
.map_err(|_| BlockProductionError::UnableToGetBlockRootFromState)?;
let (proposer_slashings, attester_slashings) =
@ -762,7 +760,7 @@ where
///
/// This could be a very expensive operation and should only be done in testing/analysis
/// activities.
pub fn chain_dump(&self) -> Result<Vec<CheckPoint>, Error> {
pub fn chain_dump(&self) -> Result<Vec<CheckPoint<B>>, Error> {
let mut dump = vec![];
let mut last_slot = CheckPoint {

View File

@ -1,22 +1,22 @@
use serde_derive::Serialize;
use types::{BeaconBlock, BeaconState, Hash256};
use types::{BeaconBlock, BeaconState, BeaconStateTypes, Hash256};
/// Represents some block and it's associated state. Generally, this will be used for tracking the
/// head, justified head and finalized head.
#[derive(Clone, Serialize, PartialEq, Debug)]
pub struct CheckPoint {
pub struct CheckPoint<B: BeaconStateTypes> {
pub beacon_block: BeaconBlock,
pub beacon_block_root: Hash256,
pub beacon_state: BeaconState,
pub beacon_state: BeaconState<B>,
pub beacon_state_root: Hash256,
}
impl CheckPoint {
impl<B: BeaconStateTypes> CheckPoint<B> {
/// Create a new checkpoint.
pub fn new(
beacon_block: BeaconBlock,
beacon_block_root: Hash256,
beacon_state: BeaconState,
beacon_state: BeaconState<B>,
beacon_state_root: Hash256,
) -> Self {
Self {
@ -32,7 +32,7 @@ impl CheckPoint {
&mut self,
beacon_block: BeaconBlock,
beacon_block_root: Hash256,
beacon_state: BeaconState,
beacon_state: BeaconState<B>,
beacon_state_root: Hash256,
) {
self.beacon_block = beacon_block;

View File

@ -11,14 +11,21 @@ use std::path::PathBuf;
use std::sync::Arc;
use tree_hash::TreeHash;
use types::test_utils::TestingBeaconStateBuilder;
use types::{BeaconBlock, ChainSpec, Hash256};
use types::{BeaconBlock, ChainSpec, FewValidatorsStateTypes, FoundationStateTypes, Hash256};
//TODO: Correct this for prod
//TODO: Account for historical db
pub fn initialise_beacon_chain(
spec: &ChainSpec,
db_name: Option<&PathBuf>,
) -> Arc<BeaconChain<DiskDB, SystemTimeSlotClock, BitwiseLMDGhost<DiskDB>>> {
) -> Arc<
BeaconChain<
DiskDB,
SystemTimeSlotClock,
BitwiseLMDGhost<DiskDB, FoundationStateTypes>,
FoundationStateTypes,
>,
> {
// set up the db
let db = Arc::new(DiskDB::open(
db_name.expect("Database directory must be included"),
@ -64,7 +71,14 @@ pub fn initialise_beacon_chain(
pub fn initialise_test_beacon_chain(
spec: &ChainSpec,
_db_name: Option<&PathBuf>,
) -> Arc<BeaconChain<MemoryDB, SystemTimeSlotClock, BitwiseLMDGhost<MemoryDB>>> {
) -> Arc<
BeaconChain<
MemoryDB,
SystemTimeSlotClock,
BitwiseLMDGhost<MemoryDB, FewValidatorsStateTypes>,
FewValidatorsStateTypes,
>,
> {
let db = Arc::new(MemoryDB::open());
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
let state_store = Arc::new(BeaconStateStore::new(db.clone()));

View File

@ -7,17 +7,18 @@ use fork_choice::BitwiseLMDGhost;
use slot_clock::TestingSlotClock;
use std::sync::Arc;
use tree_hash::TreeHash;
use types::test_utils::TestingBeaconStateBuilder;
use types::*;
use types::{test_utils::TestingBeaconStateBuilder, BeaconStateTypes, FewValidatorsStateTypes};
type TestingBeaconChain = BeaconChain<MemoryDB, TestingSlotClock, BitwiseLMDGhost<MemoryDB>>;
type TestingBeaconChain<B> =
BeaconChain<MemoryDB, TestingSlotClock, BitwiseLMDGhost<MemoryDB, FewValidatorsStateTypes>, B>;
pub struct TestingBeaconChainBuilder {
state_builder: TestingBeaconStateBuilder,
pub struct TestingBeaconChainBuilder<B: BeaconStateTypes> {
state_builder: TestingBeaconStateBuilder<B>,
}
impl TestingBeaconChainBuilder {
pub fn build(self, spec: &ChainSpec) -> TestingBeaconChain {
impl<B: BeaconStateTypes> TestingBeaconChainBuilder<B> {
pub fn build(self, spec: &ChainSpec) -> TestingBeaconChain<B> {
let db = Arc::new(MemoryDB::open());
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
let state_store = Arc::new(BeaconStateStore::new(db.clone()));
@ -43,8 +44,8 @@ impl TestingBeaconChainBuilder {
}
}
impl From<TestingBeaconStateBuilder> for TestingBeaconChainBuilder {
fn from(state_builder: TestingBeaconStateBuilder) -> TestingBeaconChainBuilder {
impl<B: BeaconStateTypes> From<TestingBeaconStateBuilder<B>> for TestingBeaconChainBuilder<B> {
fn from(state_builder: TestingBeaconStateBuilder<B>) -> TestingBeaconChainBuilder<B> {
TestingBeaconChainBuilder { state_builder }
}
}

View File

@ -9,8 +9,9 @@ use db::{
};
use log::{debug, trace};
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;
use types::{BeaconBlock, ChainSpec, Hash256, Slot, SlotHeight};
use types::{BeaconBlock, BeaconState, BeaconStateTypes, ChainSpec, Hash256, Slot, SlotHeight};
//TODO: Pruning - Children
//TODO: Handle Syncing
@ -33,7 +34,7 @@ fn power_of_2_below(x: u64) -> u64 {
}
/// Stores the necessary data structures to run the optimised bitwise lmd ghost algorithm.
pub struct BitwiseLMDGhost<T: ClientDB + Sized> {
pub struct BitwiseLMDGhost<T: ClientDB + Sized, B: BeaconStateTypes> {
/// A cache of known ancestors at given heights for a specific block.
//TODO: Consider FnvHashMap
cache: HashMap<CacheKey<u64>, Hash256>,
@ -50,9 +51,10 @@ pub struct BitwiseLMDGhost<T: ClientDB + Sized> {
/// State storage access.
state_store: Arc<BeaconStateStore<T>>,
max_known_height: SlotHeight,
_phantom: PhantomData<B>,
}
impl<T> BitwiseLMDGhost<T>
impl<T, B: BeaconStateTypes> BitwiseLMDGhost<T, B>
where
T: ClientDB + Sized,
{
@ -68,6 +70,7 @@ where
max_known_height: SlotHeight::new(0),
block_store,
state_store,
_phantom: PhantomData,
}
}
@ -85,7 +88,7 @@ where
// build a hashmap of block_hash to weighted votes
let mut latest_votes: HashMap<Hash256, u64> = HashMap::new();
// gets the current weighted votes
let current_state = self
let current_state: BeaconState<B> = self
.state_store
.get_deserialized(&state_root)?
.ok_or_else(|| ForkChoiceError::MissingBeaconState(*state_root))?;
@ -240,7 +243,7 @@ where
}
}
impl<T: ClientDB + Sized> ForkChoice for BitwiseLMDGhost<T> {
impl<T: ClientDB + Sized, B: BeaconStateTypes> ForkChoice for BitwiseLMDGhost<T, B> {
fn add_block(
&mut self,
block: &BeaconBlock,

View File

@ -9,8 +9,9 @@ use db::{
use log::{debug, trace};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;
use types::{BeaconBlock, ChainSpec, Hash256, Slot, SlotHeight};
use types::{BeaconBlock, BeaconState, BeaconStateTypes, ChainSpec, Hash256, Slot, SlotHeight};
//TODO: Pruning - Children
//TODO: Handle Syncing
@ -33,7 +34,7 @@ fn power_of_2_below(x: u64) -> u64 {
}
/// Stores the necessary data structures to run the optimised lmd ghost algorithm.
pub struct OptimizedLMDGhost<T: ClientDB + Sized> {
pub struct OptimizedLMDGhost<T: ClientDB + Sized, B: BeaconStateTypes> {
/// A cache of known ancestors at given heights for a specific block.
//TODO: Consider FnvHashMap
cache: HashMap<CacheKey<u64>, Hash256>,
@ -50,9 +51,10 @@ pub struct OptimizedLMDGhost<T: ClientDB + Sized> {
/// State storage access.
state_store: Arc<BeaconStateStore<T>>,
max_known_height: SlotHeight,
_phantom: PhantomData<B>,
}
impl<T> OptimizedLMDGhost<T>
impl<T, B: BeaconStateTypes> OptimizedLMDGhost<T, B>
where
T: ClientDB + Sized,
{
@ -68,6 +70,7 @@ where
max_known_height: SlotHeight::new(0),
block_store,
state_store,
_phantom: PhantomData,
}
}
@ -85,7 +88,7 @@ where
// build a hashmap of block_hash to weighted votes
let mut latest_votes: HashMap<Hash256, u64> = HashMap::new();
// gets the current weighted votes
let current_state = self
let current_state: BeaconState<B> = self
.state_store
.get_deserialized(&state_root)?
.ok_or_else(|| ForkChoiceError::MissingBeaconState(*state_root))?;
@ -211,7 +214,7 @@ where
}
}
impl<T: ClientDB + Sized> ForkChoice for OptimizedLMDGhost<T> {
impl<T: ClientDB + Sized, B: BeaconStateTypes> ForkChoice for OptimizedLMDGhost<T, B> {
fn add_block(
&mut self,
block: &BeaconBlock,

View File

@ -7,12 +7,13 @@ use db::{
};
use log::{debug, trace};
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;
use types::{BeaconBlock, ChainSpec, Hash256, Slot};
use types::{BeaconBlock, BeaconState, BeaconStateTypes, ChainSpec, Hash256, Slot};
//TODO: Pruning and syncing
pub struct SlowLMDGhost<T: ClientDB + Sized> {
pub struct SlowLMDGhost<T: ClientDB + Sized, B: BeaconStateTypes> {
/// The latest attestation targets as a map of validator index to block hash.
//TODO: Could this be a fixed size vec
latest_attestation_targets: HashMap<u64, Hash256>,
@ -22,9 +23,10 @@ pub struct SlowLMDGhost<T: ClientDB + Sized> {
block_store: Arc<BeaconBlockStore<T>>,
/// State storage access.
state_store: Arc<BeaconStateStore<T>>,
_phantom: PhantomData<B>,
}
impl<T> SlowLMDGhost<T>
impl<T, B: BeaconStateTypes> SlowLMDGhost<T, B>
where
T: ClientDB + Sized,
{
@ -37,6 +39,7 @@ where
children: HashMap::new(),
block_store,
state_store,
_phantom: PhantomData,
}
}
@ -54,7 +57,7 @@ where
// build a hashmap of block_hash to weighted votes
let mut latest_votes: HashMap<Hash256, u64> = HashMap::new();
// gets the current weighted votes
let current_state = self
let current_state: BeaconState<B> = self
.state_store
.get_deserialized(&state_root)?
.ok_or_else(|| ForkChoiceError::MissingBeaconState(*state_root))?;
@ -105,7 +108,7 @@ where
}
}
impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
impl<T: ClientDB + Sized, B: BeaconStateTypes> ForkChoice for SlowLMDGhost<T, B> {
/// Process when a block is added
fn add_block(
&mut self,

View File

@ -1,12 +1,13 @@
use crate::*;
use fixed_len_vec::typenum::{Unsigned, U1024, U8, U8192};
use std::fmt::Debug;
pub trait BeaconStateTypes: Default {
type ShardCount: Unsigned + Clone + Sync + Send;
type SlotsPerHistoricalRoot: Unsigned + Clone + Sync + Send;
type LatestRandaoMixesLength: Unsigned + Clone + Sync + Send;
type LatestActiveIndexRootsLength: Unsigned + Clone + Sync + Send;
type LatestSlashedExitLength: Unsigned + Clone + Sync + Send;
pub trait BeaconStateTypes: Default + Sync + Send + Clone + Debug + PartialEq {
type ShardCount: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type SlotsPerHistoricalRoot: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type LatestRandaoMixesLength: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type LatestActiveIndexRootsLength: Unsigned + Clone + Sync + Send + Debug + PartialEq;
type LatestSlashedExitLength: Unsigned + Clone + Sync + Send + Debug + PartialEq;
fn spec() -> ChainSpec;
}