Update BeaconChain w/ new genesis code

This commit is contained in:
Paul Hauner 2018-12-13 17:33:53 +11:00
parent 493a16ac18
commit 01f3b2f0c1
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
5 changed files with 32 additions and 25 deletions

View File

@ -6,6 +6,7 @@ authors = ["Paul Hauner <paul@paulhauner.com>"]
[dependencies] [dependencies]
bls = { path = "../utils/bls" } bls = { path = "../utils/bls" }
db = { path = "../../lighthouse/db" } db = { path = "../../lighthouse/db" }
genesis = { path = "../genesis" }
naive_fork_choice = { path = "../naive_fork_choice" } naive_fork_choice = { path = "../naive_fork_choice" }
spec = { path = "../spec" } spec = { path = "../spec" }
ssz = { path = "../utils/ssz" } ssz = { path = "../utils/ssz" }

View File

@ -1,5 +1,6 @@
extern crate db; extern crate db;
extern crate naive_fork_choice; extern crate naive_fork_choice;
extern crate genesis;
extern crate spec; extern crate spec;
extern crate ssz; extern crate ssz;
extern crate ssz_helpers; extern crate ssz_helpers;
@ -14,12 +15,13 @@ mod stores;
mod transition; mod transition;
use db::ClientDB; 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 maps::{generate_attester_and_proposer_maps, AttesterAndProposerMapError};
use spec::ChainSpec;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use stores::BeaconChainStore; use stores::BeaconChainStore;
use types::{ActiveState, AttesterMap, ChainConfig, CrystallizedState, Hash256, ProposerMap}; use types::{AttesterMap, BeaconState, Hash256, ProposerMap};
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum BeaconChainError { pub enum BeaconChainError {
@ -38,40 +40,45 @@ pub struct BeaconChain<T: ClientDB + Sized> {
/// The index of the canonical block in `head_block_hashes`. /// The index of the canonical block in `head_block_hashes`.
pub canonical_head_block_hash: usize, pub canonical_head_block_hash: usize,
/// An in-memory map of root hash to beacon state. /// An in-memory map of root hash to beacon state.
pub beacon_states: HashMap<Hash256, ActiveState>, pub beacon_states: HashMap<Hash256, BeaconState>,
/// A map of crystallized state to a proposer and attester map. /// A map of crystallized state to a proposer and attester map.
pub attester_proposer_maps: HashMap<Hash256, (Arc<AttesterMap>, Arc<ProposerMap>)>, pub attester_proposer_maps: HashMap<Hash256, (Arc<AttesterMap>, Arc<ProposerMap>)>,
/// A collection of database stores used by the chain. /// A collection of database stores used by the chain.
pub store: BeaconChainStore<T>, pub store: BeaconChainStore<T>,
/// The chain configuration. /// The chain configuration.
pub config: ChainConfig, pub spec: ChainSpec,
} }
impl<T> BeaconChain<T> impl<T> BeaconChain<T>
where where
T: ClientDB + Sized, T: ClientDB + Sized,
{ {
pub fn new(store: BeaconChainStore<T>, config: ChainConfig) -> Result<Self, BeaconChainError> { pub fn new(store: BeaconChainStore<T>, spec: ChainSpec) -> Result<Self, BeaconChainError> {
if config.initial_validators.is_empty() { if spec.initial_validators.is_empty() {
return Err(BeaconChainError::InsufficientValidators); 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 canonical_latest_block_hash = Hash256::zero();
let head_block_hashes = vec![canonical_latest_block_hash]; let head_block_hashes = vec![canonical_latest_block_hash];
let canonical_head_block_hash = 0; 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 mut attester_proposer_maps = HashMap::new();
let (attester_map, proposer_map) = generate_attester_and_proposer_maps( let (attester_map, proposer_map) = generate_attester_and_proposer_maps(
&crystallized_state.shard_and_committee_for_slots, &genesis_state.shard_committees_at_slots,
0, 0,
)?; )?;
active_states.insert(canonical_latest_block_hash, active_state);
crystallized_states.insert(canonical_latest_block_hash, crystallized_state);
attester_proposer_maps.insert( attester_proposer_maps.insert(
canonical_latest_block_hash, canonical_latest_block_hash,
(Arc::new(attester_map), Arc::new(proposer_map)), (Arc::new(attester_map), Arc::new(proposer_map)),
@ -81,11 +88,10 @@ where
last_finalized_slot: 0, last_finalized_slot: 0,
head_block_hashes, head_block_hashes,
canonical_head_block_hash, canonical_head_block_hash,
active_states, beacon_states,
crystallized_states,
attester_proposer_maps, attester_proposer_maps,
store, store,
config, spec,
}) })
} }

View File

@ -1,8 +1,8 @@
use types::{AttesterMap, ProposerMap, ShardAndCommittee}; use types::{AttesterMap, ProposerMap, ShardCommittee};
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum AttesterAndProposerMapError { pub enum AttesterAndProposerMapError {
NoShardAndCommitteeForSlot, NoShardCommitteeForSlot,
NoAvailableProposer, NoAvailableProposer,
} }
@ -10,7 +10,7 @@ pub enum AttesterAndProposerMapError {
/// ///
/// The attester map is used to optimise the lookup of a committee. /// The attester map is used to optimise the lookup of a committee.
pub fn generate_attester_and_proposer_maps( pub fn generate_attester_and_proposer_maps(
shard_and_committee_for_slots: &Vec<Vec<ShardAndCommittee>>, shard_and_committee_for_slots: &Vec<Vec<ShardCommittee>>,
start_slot: u64, start_slot: u64,
) -> Result<(AttesterMap, ProposerMap), AttesterAndProposerMapError> { ) -> Result<(AttesterMap, ProposerMap), AttesterAndProposerMapError> {
let mut attester_map = AttesterMap::new(); 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 slot_number = (i as u64).saturating_add(start_slot);
let first_committee = &slot let first_committee = &slot
.get(0) .get(0)
.ok_or(AttesterAndProposerMapError::NoShardAndCommitteeForSlot)? .ok_or(AttesterAndProposerMapError::NoShardCommitteeForSlot)?
.committee; .committee;
let proposer_index = (slot_number as usize) let proposer_index = (slot_number as usize)
.checked_rem(first_committee.len()) .checked_rem(first_committee.len())
@ -49,15 +49,15 @@ mod tests {
slot_count: usize, slot_count: usize,
sac_per_slot: usize, sac_per_slot: usize,
committee_size: usize, committee_size: usize,
) -> Vec<Vec<ShardAndCommittee>> { ) -> Vec<Vec<ShardCommittee>> {
let mut shard = 0; let mut shard = 0;
let mut validator = 0; let mut validator = 0;
let mut cycle = vec![]; let mut cycle = vec![];
for _ in 0..slot_count { for _ in 0..slot_count {
let mut slot: Vec<ShardAndCommittee> = vec![]; let mut slot: Vec<ShardCommittee> = vec![];
for _ in 0..sac_per_slot { for _ in 0..sac_per_slot {
let mut sac = ShardAndCommittee { let mut sac = ShardCommittee {
shard: shard % shard_count, shard: shard % shard_count,
committee: vec![], committee: vec![],
}; };
@ -79,7 +79,7 @@ mod tests {
let result = generate_attester_and_proposer_maps(&sac, 0); let result = generate_attester_and_proposer_maps(&sac, 0);
assert_eq!( assert_eq!(
result, result,
Err(AttesterAndProposerMapError::NoShardAndCommitteeForSlot) Err(AttesterAndProposerMapError::NoShardCommitteeForSlot)
); );
} }

View File

@ -19,7 +19,7 @@ where
.checked_sub(cry_state.last_state_recalculation_slot) .checked_sub(cry_state.last_state_recalculation_slot)
.ok_or(StateTransitionError::BlockSlotBeforeRecalcSlot)?; .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!") panic!("Not implemented!")
} else { } else {
let new_act_state = extend_active_state(act_state, block, block_hash)?; let new_act_state = extend_active_state(act_state, block, block_hash)?;

View File

@ -5,5 +5,5 @@ extern crate validator_shuffling;
mod beacon_state; mod beacon_state;
pub use beacon_state::genesis_beacon_state; pub use beacon_state::{genesis_beacon_state, Error as GenesisError};