From 5d85c62f6e5fbf519b5a9a61f0b28bcf67b2ee27 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 20 Oct 2018 21:42:07 +1100 Subject: [PATCH] Finish implementing genesis (without tests) --- beacon_chain/chain/src/lib.rs | 44 ++++++++++++------- beacon_chain/types/src/shard_and_committee.rs | 2 +- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/beacon_chain/chain/src/lib.rs b/beacon_chain/chain/src/lib.rs index 27d605403..e4ec8ec8d 100644 --- a/beacon_chain/chain/src/lib.rs +++ b/beacon_chain/chain/src/lib.rs @@ -8,7 +8,6 @@ use types::{ CrosslinkRecord, CrystallizedState, Hash256, - ValidatorRecord, }; use validator_induction::{ ValidatorInductor, @@ -21,13 +20,24 @@ use validator_shuffling::{ pub const INITIAL_FORK_VERSION: u32 = 0; +/// A ChainHead structure represents the "head" or "tip" of a beacon chain blockchain. +/// +/// Initially, a "gensis" chainhead will be created and then new blocks will be built upon it. pub struct ChainHead { - active_state: ActiveState, - crystallized_state: CrystallizedState, - config: ChainConfig, + /// The hash of the block that is the head of the chain. + pub head_hash: Hash256, + /// The active state at this head block. + pub active_state: ActiveState, + /// The crystallized state at this head block. + pub crystallized_state: CrystallizedState, + /// The configuration of the underlying chain. + pub config: ChainConfig, } impl ChainHead { + /// Initialize a new ChainHead with genesis parameters. + /// + /// Used when syncing a chain from scratch. pub fn genesis( initial_validator_entries: &[ValidatorRegistration], config: ChainConfig) @@ -39,17 +49,11 @@ impl ChainHead { * Ignore any records which fail proof-of-possession or are invalid. */ let validators = { - let mut validators = vec![]; - let inductor = ValidatorInductor { - current_slot: 0, - shard_count: config.shard_count, - validators: &mut validators, - empty_validator_start: 0, - }; + let mut inductor = ValidatorInductor::new(0, config.shard_count, vec![]); for registration in initial_validator_entries { let _ = inductor.induct(®istration); }; - validators + inductor.to_vec() }; /* @@ -58,9 +62,10 @@ impl ChainHead { * Crystallizedstate stores two cycles, so we simply repeat the same assignment twice. */ let shard_and_committee_for_slots = { - let x = shard_and_committees_for_cycle(&vec![0; 32], &validators, 0, &config)?; - x.append(&mut x.clone()); - x + let mut a = shard_and_committees_for_cycle(&vec![0; 32], &validators, 0, &config)?; + let mut b = a.clone(); + a.append(&mut b); + a }; /* @@ -83,7 +88,7 @@ impl ChainHead { */ let crystallized_state = CrystallizedState { validator_set_change_slot: 0, - validators, + validators: validators.to_vec(), crosslinks, last_state_recalculation_slot: 0, last_finalized_slot: 0, @@ -97,6 +102,9 @@ impl ChainHead { fork_slot_number: 0, }; + /* + * Set all recent block hashes to zero. + */ let recent_block_hashes = { let mut x = vec![]; for _ in 0..config.cycle_length { @@ -105,6 +113,9 @@ impl ChainHead { x }; + /* + * Create an active state. + */ let active_state = ActiveState { pending_attestations: vec![], pending_specials: vec![], @@ -114,6 +125,7 @@ impl ChainHead { Ok(Self { + head_hash: Hash256::zero(), active_state, crystallized_state, config, diff --git a/beacon_chain/types/src/shard_and_committee.rs b/beacon_chain/types/src/shard_and_committee.rs index 3b2bcd0fd..6cc74429a 100644 --- a/beacon_chain/types/src/shard_and_committee.rs +++ b/beacon_chain/types/src/shard_and_committee.rs @@ -1,4 +1,4 @@ -#[derive(Clone,Debug)] +#[derive(Clone, Debug)] pub struct ShardAndCommittee { pub shard_id: u16, pub committee: Vec