Update BeaconChain
w/ new genesis code
This commit is contained in:
parent
493a16ac18
commit
01f3b2f0c1
@ -6,6 +6,7 @@ authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
[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" }
|
||||
|
@ -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<T: ClientDB + Sized> {
|
||||
/// 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<Hash256, ActiveState>,
|
||||
pub beacon_states: HashMap<Hash256, BeaconState>,
|
||||
/// A map of crystallized state to a proposer and attester map.
|
||||
pub attester_proposer_maps: HashMap<Hash256, (Arc<AttesterMap>, Arc<ProposerMap>)>,
|
||||
/// A collection of database stores used by the chain.
|
||||
pub store: BeaconChainStore<T>,
|
||||
/// The chain configuration.
|
||||
pub config: ChainConfig,
|
||||
pub spec: ChainSpec,
|
||||
}
|
||||
|
||||
impl<T> BeaconChain<T>
|
||||
where
|
||||
T: ClientDB + Sized,
|
||||
{
|
||||
pub fn new(store: BeaconChainStore<T>, config: ChainConfig) -> Result<Self, BeaconChainError> {
|
||||
if config.initial_validators.is_empty() {
|
||||
pub fn new(store: BeaconChainStore<T>, spec: ChainSpec) -> Result<Self, BeaconChainError> {
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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<Vec<ShardAndCommittee>>,
|
||||
shard_and_committee_for_slots: &Vec<Vec<ShardCommittee>>,
|
||||
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<ShardAndCommittee>> {
|
||||
) -> Vec<Vec<ShardCommittee>> {
|
||||
let mut shard = 0;
|
||||
let mut validator = 0;
|
||||
let mut cycle = vec![];
|
||||
|
||||
for _ in 0..slot_count {
|
||||
let mut slot: Vec<ShardAndCommittee> = vec![];
|
||||
let mut slot: Vec<ShardCommittee> = 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)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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)?;
|
||||
|
@ -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};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user