2018-10-23 11:16:26 +00:00
|
|
|
extern crate db;
|
2018-10-29 19:29:15 +00:00
|
|
|
extern crate naive_fork_choice;
|
|
|
|
extern crate ssz;
|
2018-10-25 08:14:43 +00:00
|
|
|
extern crate ssz_helpers;
|
2018-11-04 14:35:00 +00:00
|
|
|
extern crate state_transition;
|
2018-10-29 19:29:15 +00:00
|
|
|
extern crate types;
|
2018-10-25 08:14:43 +00:00
|
|
|
extern crate validation;
|
2018-10-20 05:34:08 +00:00
|
|
|
extern crate validator_induction;
|
|
|
|
extern crate validator_shuffling;
|
|
|
|
|
2018-10-25 08:14:43 +00:00
|
|
|
mod block_context;
|
|
|
|
mod block_processing;
|
2018-10-21 15:18:35 +00:00
|
|
|
mod genesis;
|
2018-10-29 19:29:15 +00:00
|
|
|
mod maps;
|
|
|
|
mod stores;
|
2018-11-04 14:35:00 +00:00
|
|
|
mod transition;
|
2018-10-21 15:18:35 +00:00
|
|
|
|
2018-10-23 11:16:26 +00:00
|
|
|
use db::ClientDB;
|
|
|
|
use genesis::genesis_states;
|
2018-10-29 19:29:15 +00:00
|
|
|
use maps::{generate_attester_and_proposer_maps, AttesterAndProposerMapError};
|
2018-10-21 15:18:35 +00:00
|
|
|
use std::collections::HashMap;
|
2018-10-23 11:16:26 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use stores::BeaconChainStore;
|
2018-10-29 19:29:15 +00:00
|
|
|
use types::{ActiveState, AttesterMap, ChainConfig, CrystallizedState, Hash256, ProposerMap};
|
2018-10-20 08:02:15 +00:00
|
|
|
|
2018-10-21 19:48:44 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2018-10-21 15:18:35 +00:00
|
|
|
pub enum BeaconChainError {
|
|
|
|
InvalidGenesis,
|
2018-10-23 11:16:26 +00:00
|
|
|
InsufficientValidators,
|
|
|
|
UnableToGenerateMaps(AttesterAndProposerMapError),
|
2018-10-21 15:18:35 +00:00
|
|
|
DBError(String),
|
|
|
|
}
|
2018-10-20 05:34:08 +00:00
|
|
|
|
2018-10-23 11:16:26 +00:00
|
|
|
pub struct BeaconChain<T: ClientDB + Sized> {
|
|
|
|
/// The last slot which has been finalized, this is common to all forks.
|
|
|
|
pub last_finalized_slot: u64,
|
2018-10-25 08:14:43 +00:00
|
|
|
/// A vec of all block heads (tips of chains).
|
|
|
|
pub head_block_hashes: Vec<Hash256>,
|
|
|
|
/// The index of the canonical block in `head_block_hashes`.
|
|
|
|
pub canonical_head_block_hash: usize,
|
2018-10-23 11:16:26 +00:00
|
|
|
/// A map where the value is an active state the the key is its hash.
|
2018-10-21 15:18:35 +00:00
|
|
|
pub active_states: HashMap<Hash256, ActiveState>,
|
2018-10-23 11:16:26 +00:00
|
|
|
/// A map where the value is crystallized state the the key is its hash.
|
2018-10-21 15:18:35 +00:00
|
|
|
pub crystallized_states: HashMap<Hash256, CrystallizedState>,
|
2018-10-23 11:16:26 +00:00
|
|
|
/// 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.
|
2018-10-21 19:48:44 +00:00
|
|
|
pub config: ChainConfig,
|
2018-10-20 05:34:08 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 11:16:26 +00:00
|
|
|
impl<T> BeaconChain<T>
|
2018-10-29 19:29:15 +00:00
|
|
|
where
|
|
|
|
T: ClientDB + Sized,
|
2018-10-23 11:16:26 +00:00
|
|
|
{
|
2018-10-29 19:29:15 +00:00
|
|
|
pub fn new(store: BeaconChainStore<T>, config: ChainConfig) -> Result<Self, BeaconChainError> {
|
2018-10-23 11:16:26 +00:00
|
|
|
if config.initial_validators.is_empty() {
|
|
|
|
return Err(BeaconChainError::InsufficientValidators);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (active_state, crystallized_state) = genesis_states(&config)?;
|
2018-10-20 08:02:15 +00:00
|
|
|
|
2018-10-21 15:18:35 +00:00
|
|
|
let canonical_latest_block_hash = Hash256::zero();
|
2018-10-25 08:14:43 +00:00
|
|
|
let head_block_hashes = vec![canonical_latest_block_hash];
|
|
|
|
let canonical_head_block_hash = 0;
|
2018-10-21 15:18:35 +00:00
|
|
|
let mut active_states = HashMap::new();
|
|
|
|
let mut crystallized_states = HashMap::new();
|
2018-10-23 11:16:26 +00:00
|
|
|
let mut attester_proposer_maps = HashMap::new();
|
|
|
|
|
|
|
|
let (attester_map, proposer_map) = generate_attester_and_proposer_maps(
|
2018-10-29 19:29:15 +00:00
|
|
|
&crystallized_state.shard_and_committee_for_slots,
|
|
|
|
0,
|
|
|
|
)?;
|
2018-10-20 08:02:15 +00:00
|
|
|
|
2018-10-21 15:18:35 +00:00
|
|
|
active_states.insert(canonical_latest_block_hash, active_state);
|
|
|
|
crystallized_states.insert(canonical_latest_block_hash, crystallized_state);
|
2018-10-23 11:16:26 +00:00
|
|
|
attester_proposer_maps.insert(
|
|
|
|
canonical_latest_block_hash,
|
2018-10-29 19:29:15 +00:00
|
|
|
(Arc::new(attester_map), Arc::new(proposer_map)),
|
|
|
|
);
|
2018-10-20 08:02:15 +00:00
|
|
|
|
2018-10-29 19:29:15 +00:00
|
|
|
Ok(Self {
|
2018-10-23 11:16:26 +00:00
|
|
|
last_finalized_slot: 0,
|
2018-10-25 08:14:43 +00:00
|
|
|
head_block_hashes,
|
|
|
|
canonical_head_block_hash,
|
2018-10-21 15:18:35 +00:00
|
|
|
active_states,
|
|
|
|
crystallized_states,
|
2018-10-23 11:16:26 +00:00
|
|
|
attester_proposer_maps,
|
|
|
|
store,
|
2018-10-21 19:48:44 +00:00
|
|
|
config,
|
2018-10-20 08:02:15 +00:00
|
|
|
})
|
2018-10-20 05:34:08 +00:00
|
|
|
}
|
2018-10-25 08:14:43 +00:00
|
|
|
|
2018-10-29 19:29:15 +00:00
|
|
|
pub fn canonical_block_hash(&self) -> Hash256 {
|
2018-10-25 08:14:43 +00:00
|
|
|
self.head_block_hashes[self.canonical_head_block_hash]
|
|
|
|
}
|
2018-10-20 05:34:08 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 19:29:15 +00:00
|
|
|
impl From<AttesterAndProposerMapError> for BeaconChainError {
|
|
|
|
fn from(e: AttesterAndProposerMapError) -> BeaconChainError {
|
|
|
|
BeaconChainError::UnableToGenerateMaps(e)
|
|
|
|
}
|
|
|
|
}
|
2018-10-21 15:18:35 +00:00
|
|
|
|
2018-10-20 05:34:08 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-10-21 15:18:35 +00:00
|
|
|
use super::*;
|
2018-10-23 11:16:26 +00:00
|
|
|
use db::stores::*;
|
2018-10-29 19:29:15 +00:00
|
|
|
use db::MemoryDB;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use types::ValidatorRegistration;
|
2018-10-21 15:18:35 +00:00
|
|
|
|
2018-10-20 05:34:08 +00:00
|
|
|
#[test]
|
2018-10-21 15:18:35 +00:00
|
|
|
fn test_new_chain() {
|
2018-10-21 19:48:44 +00:00
|
|
|
let mut config = ChainConfig::standard();
|
2018-10-23 11:16:26 +00:00
|
|
|
config.cycle_length = 4;
|
|
|
|
config.shard_count = 4;
|
|
|
|
let db = Arc::new(MemoryDB::open());
|
|
|
|
let store = BeaconChainStore {
|
|
|
|
block: Arc::new(BeaconBlockStore::new(db.clone())),
|
|
|
|
pow_chain: Arc::new(PoWChainStore::new(db.clone())),
|
|
|
|
validator: Arc::new(ValidatorStore::new(db.clone())),
|
|
|
|
};
|
|
|
|
|
|
|
|
for _ in 0..config.cycle_length * 2 {
|
2018-10-29 19:29:15 +00:00
|
|
|
config
|
|
|
|
.initial_validators
|
|
|
|
.push(ValidatorRegistration::random())
|
2018-10-21 19:48:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 11:16:26 +00:00
|
|
|
let chain = BeaconChain::new(store, config.clone()).unwrap();
|
|
|
|
let (act, cry) = genesis_states(&config).unwrap();
|
2018-10-21 19:48:44 +00:00
|
|
|
|
2018-10-23 11:16:26 +00:00
|
|
|
assert_eq!(chain.last_finalized_slot, 0);
|
2018-10-25 08:14:43 +00:00
|
|
|
assert_eq!(chain.canonical_block_hash(), Hash256::zero());
|
2018-10-21 19:48:44 +00:00
|
|
|
|
|
|
|
let stored_act = chain.active_states.get(&Hash256::zero()).unwrap();
|
|
|
|
assert_eq!(act, *stored_act);
|
|
|
|
|
|
|
|
let stored_cry = chain.crystallized_states.get(&Hash256::zero()).unwrap();
|
|
|
|
assert_eq!(cry, *stored_cry);
|
2018-10-20 05:34:08 +00:00
|
|
|
}
|
|
|
|
}
|