2019-03-17 12:11:07 +00:00
|
|
|
use super::per_block_processing::{errors::BlockProcessingError, process_deposits};
|
2019-04-16 04:14:38 +00:00
|
|
|
use tree_hash::TreeHash;
|
2019-03-17 12:11:07 +00:00
|
|
|
use types::*;
|
|
|
|
|
|
|
|
pub enum GenesisError {
|
|
|
|
BlockProcessingError(BlockProcessingError),
|
|
|
|
BeaconStateError(BeaconStateError),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the genesis `BeaconState`
|
|
|
|
///
|
2019-06-17 06:25:57 +00:00
|
|
|
/// Spec v0.6.3
|
2019-05-20 05:03:28 +00:00
|
|
|
pub fn get_genesis_beacon_state<T: EthSpec>(
|
2019-03-17 12:11:07 +00:00
|
|
|
genesis_validator_deposits: &[Deposit],
|
|
|
|
genesis_time: u64,
|
|
|
|
genesis_eth1_data: Eth1Data,
|
|
|
|
spec: &ChainSpec,
|
2019-05-08 05:36:02 +00:00
|
|
|
) -> Result<BeaconState<T>, BlockProcessingError> {
|
2019-03-17 12:11:07 +00:00
|
|
|
// Get the genesis `BeaconState`
|
|
|
|
let mut state = BeaconState::genesis(genesis_time, genesis_eth1_data, spec);
|
|
|
|
|
|
|
|
// Process genesis deposits.
|
|
|
|
process_deposits(&mut state, genesis_validator_deposits, spec)?;
|
|
|
|
|
|
|
|
// Process genesis activations.
|
2019-05-29 06:36:50 +00:00
|
|
|
for validator in &mut state.validator_registry {
|
2019-05-20 05:03:28 +00:00
|
|
|
if validator.effective_balance >= spec.max_effective_balance {
|
2019-06-08 12:17:42 +00:00
|
|
|
validator.activation_eligibility_epoch = T::genesis_epoch();
|
|
|
|
validator.activation_epoch = T::genesis_epoch();
|
2019-03-17 12:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the current epoch cache is built.
|
2019-05-20 04:36:54 +00:00
|
|
|
state.build_committee_cache(RelativeEpoch::Current, spec)?;
|
2019-03-17 12:11:07 +00:00
|
|
|
|
|
|
|
// Set all the active index roots to be the genesis active index root.
|
|
|
|
let active_validator_indices = state
|
2019-05-21 06:40:48 +00:00
|
|
|
.get_cached_active_validator_indices(RelativeEpoch::Current)?
|
2019-03-17 12:11:07 +00:00
|
|
|
.to_vec();
|
2019-04-16 04:14:38 +00:00
|
|
|
let genesis_active_index_root = Hash256::from_slice(&active_validator_indices.tree_hash_root());
|
2019-05-08 05:36:02 +00:00
|
|
|
state.fill_active_index_roots_with(genesis_active_index_root);
|
2019-03-17 12:11:07 +00:00
|
|
|
|
2019-04-17 00:33:31 +00:00
|
|
|
Ok(state)
|
2019-03-17 12:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BlockProcessingError> for GenesisError {
|
|
|
|
fn from(e: BlockProcessingError) -> GenesisError {
|
|
|
|
GenesisError::BlockProcessingError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BeaconStateError> for GenesisError {
|
|
|
|
fn from(e: BeaconStateError) -> GenesisError {
|
|
|
|
GenesisError::BeaconStateError(e)
|
|
|
|
}
|
|
|
|
}
|