From 01f3b2f0c179e8b76b2c83141fac5bb26dd303c1 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 13 Dec 2018 17:33:53 +1100 Subject: [PATCH] Update `BeaconChain` w/ new genesis code --- beacon_chain/chain/Cargo.toml | 1 + beacon_chain/chain/src/lib.rs | 36 ++++++++++++++++------------ beacon_chain/chain/src/maps.rs | 16 ++++++------- beacon_chain/chain/src/transition.rs | 2 +- beacon_chain/genesis/src/lib.rs | 2 +- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/beacon_chain/chain/Cargo.toml b/beacon_chain/chain/Cargo.toml index 973e1560a0..f62a71b272 100644 --- a/beacon_chain/chain/Cargo.toml +++ b/beacon_chain/chain/Cargo.toml @@ -6,6 +6,7 @@ authors = ["Paul Hauner "] [dependencies] bls = { path = "../utils/bls" } db = { path = "../../lighthouse/db" } +genesis = { path = "../genesis" } naive_fork_choice = { path = "../naive_fork_choice" } spec = { path = "../spec" } ssz = { path = "../utils/ssz" } diff --git a/beacon_chain/chain/src/lib.rs b/beacon_chain/chain/src/lib.rs index adfa682c79..1093347b9d 100644 --- a/beacon_chain/chain/src/lib.rs +++ b/beacon_chain/chain/src/lib.rs @@ -1,5 +1,6 @@ extern crate db; extern crate naive_fork_choice; +extern crate genesis; extern crate spec; extern crate ssz; extern crate ssz_helpers; @@ -14,12 +15,13 @@ mod stores; mod transition; use db::ClientDB; -use genesis::{genesis_states, Error as GenesisError}; +use genesis::{genesis_beacon_state, GenesisError}; use maps::{generate_attester_and_proposer_maps, AttesterAndProposerMapError}; +use spec::ChainSpec; use std::collections::HashMap; use std::sync::Arc; use stores::BeaconChainStore; -use types::{ActiveState, AttesterMap, ChainConfig, CrystallizedState, Hash256, ProposerMap}; +use types::{AttesterMap, BeaconState, Hash256, ProposerMap}; #[derive(Debug, PartialEq)] pub enum BeaconChainError { @@ -38,40 +40,45 @@ pub struct BeaconChain { /// The index of the canonical block in `head_block_hashes`. pub canonical_head_block_hash: usize, /// An in-memory map of root hash to beacon state. - pub beacon_states: HashMap, + pub beacon_states: HashMap, /// A map of crystallized state to a proposer and attester map. pub attester_proposer_maps: HashMap, Arc)>, /// A collection of database stores used by the chain. pub store: BeaconChainStore, /// The chain configuration. - pub config: ChainConfig, + pub spec: ChainSpec, } impl BeaconChain where T: ClientDB + Sized, { - pub fn new(store: BeaconChainStore, config: ChainConfig) -> Result { - if config.initial_validators.is_empty() { + pub fn new(store: BeaconChainStore, spec: ChainSpec) -> Result { + if spec.initial_validators.is_empty() { return Err(BeaconChainError::InsufficientValidators); } - let (active_state, crystallized_state) = genesis_states(&config)?; + /* + * Generate and process the genesis state. + */ + let genesis_state = genesis_beacon_state(&spec)?; + let mut beacon_states = HashMap::new(); + beacon_states.insert(genesis_state.canonical_root(), genesis_state.clone()); + // TODO: implement genesis block + // https://github.com/sigp/lighthouse/issues/105 let canonical_latest_block_hash = Hash256::zero(); + let head_block_hashes = vec![canonical_latest_block_hash]; let canonical_head_block_hash = 0; - let mut active_states = HashMap::new(); - let mut crystallized_states = HashMap::new(); + let mut attester_proposer_maps = HashMap::new(); let (attester_map, proposer_map) = generate_attester_and_proposer_maps( - &crystallized_state.shard_and_committee_for_slots, + &genesis_state.shard_committees_at_slots, 0, )?; - active_states.insert(canonical_latest_block_hash, active_state); - crystallized_states.insert(canonical_latest_block_hash, crystallized_state); attester_proposer_maps.insert( canonical_latest_block_hash, (Arc::new(attester_map), Arc::new(proposer_map)), @@ -81,11 +88,10 @@ where last_finalized_slot: 0, head_block_hashes, canonical_head_block_hash, - active_states, - crystallized_states, + beacon_states, attester_proposer_maps, store, - config, + spec, }) } diff --git a/beacon_chain/chain/src/maps.rs b/beacon_chain/chain/src/maps.rs index 9188531f28..cecd87c8d8 100644 --- a/beacon_chain/chain/src/maps.rs +++ b/beacon_chain/chain/src/maps.rs @@ -1,8 +1,8 @@ -use types::{AttesterMap, ProposerMap, ShardAndCommittee}; +use types::{AttesterMap, ProposerMap, ShardCommittee}; #[derive(Debug, PartialEq)] pub enum AttesterAndProposerMapError { - NoShardAndCommitteeForSlot, + NoShardCommitteeForSlot, NoAvailableProposer, } @@ -10,7 +10,7 @@ pub enum AttesterAndProposerMapError { /// /// The attester map is used to optimise the lookup of a committee. pub fn generate_attester_and_proposer_maps( - shard_and_committee_for_slots: &Vec>, + shard_and_committee_for_slots: &Vec>, start_slot: u64, ) -> Result<(AttesterMap, ProposerMap), AttesterAndProposerMapError> { let mut attester_map = AttesterMap::new(); @@ -22,7 +22,7 @@ pub fn generate_attester_and_proposer_maps( let slot_number = (i as u64).saturating_add(start_slot); let first_committee = &slot .get(0) - .ok_or(AttesterAndProposerMapError::NoShardAndCommitteeForSlot)? + .ok_or(AttesterAndProposerMapError::NoShardCommitteeForSlot)? .committee; let proposer_index = (slot_number as usize) .checked_rem(first_committee.len()) @@ -49,15 +49,15 @@ mod tests { slot_count: usize, sac_per_slot: usize, committee_size: usize, - ) -> Vec> { + ) -> Vec> { let mut shard = 0; let mut validator = 0; let mut cycle = vec![]; for _ in 0..slot_count { - let mut slot: Vec = vec![]; + let mut slot: Vec = vec![]; for _ in 0..sac_per_slot { - let mut sac = ShardAndCommittee { + let mut sac = ShardCommittee { shard: shard % shard_count, committee: vec![], }; @@ -79,7 +79,7 @@ mod tests { let result = generate_attester_and_proposer_maps(&sac, 0); assert_eq!( result, - Err(AttesterAndProposerMapError::NoShardAndCommitteeForSlot) + Err(AttesterAndProposerMapError::NoShardCommitteeForSlot) ); } diff --git a/beacon_chain/chain/src/transition.rs b/beacon_chain/chain/src/transition.rs index 7598f25175..df8803b27f 100644 --- a/beacon_chain/chain/src/transition.rs +++ b/beacon_chain/chain/src/transition.rs @@ -19,7 +19,7 @@ where .checked_sub(cry_state.last_state_recalculation_slot) .ok_or(StateTransitionError::BlockSlotBeforeRecalcSlot)?; - if state_recalc_distance >= u64::from(self.config.cycle_length) { + if state_recalc_distance >= u64::from(self.spec.epoch_length) { panic!("Not implemented!") } else { let new_act_state = extend_active_state(act_state, block, block_hash)?; diff --git a/beacon_chain/genesis/src/lib.rs b/beacon_chain/genesis/src/lib.rs index 3256ff5587..1f901e7a71 100644 --- a/beacon_chain/genesis/src/lib.rs +++ b/beacon_chain/genesis/src/lib.rs @@ -5,5 +5,5 @@ extern crate validator_shuffling; mod beacon_state; -pub use beacon_state::genesis_beacon_state; +pub use beacon_state::{genesis_beacon_state, Error as GenesisError};