Finish genesis for BeaconChain

This commit is contained in:
Paul Hauner 2018-10-22 06:48:44 +11:00
parent 41bfb7a0e2
commit 42e774cb48
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
9 changed files with 65 additions and 41 deletions

View File

@ -1,12 +0,0 @@
use super::{
BeaconChain,
BeaconChainError,
};
impl BeaconChain {
pub fn validate_serialized_block(&self, ssz: &[u8])
-> Result<(), BeaconChainError>
{
Ok(())
}
}

View File

@ -1,6 +1,7 @@
use types::{ use types::{
CrosslinkRecord, CrosslinkRecord,
Hash256, Hash256,
ValidatorRegistration,
}; };
use super::{ use super::{
ActiveState, ActiveState,
@ -9,10 +10,7 @@ use super::{
BeaconChainError, BeaconChainError,
ChainConfig, ChainConfig,
}; };
use validator_induction::{ use validator_induction::ValidatorInductor;
ValidatorInductor,
ValidatorRegistration,
};
use validator_shuffling::{ use validator_shuffling::{
shard_and_committees_for_cycle, shard_and_committees_for_cycle,
ValidatorAssignmentError, ValidatorAssignmentError,
@ -30,9 +28,7 @@ impl BeaconChain {
/// Initialize a new ChainHead with genesis parameters. /// Initialize a new ChainHead with genesis parameters.
/// ///
/// Used when syncing a chain from scratch. /// Used when syncing a chain from scratch.
pub fn genesis_states( pub fn genesis_states(config: &ChainConfig)
initial_validator_entries: &[ValidatorRegistration],
config: &ChainConfig)
-> Result<(ActiveState, CrystallizedState), ValidatorAssignmentError> -> Result<(ActiveState, CrystallizedState), ValidatorAssignmentError>
{ {
/* /*
@ -42,7 +38,7 @@ impl BeaconChain {
*/ */
let validators = { let validators = {
let mut inductor = ValidatorInductor::new(0, config.shard_count, vec![]); let mut inductor = ValidatorInductor::new(0, config.shard_count, vec![]);
for registration in initial_validator_entries { for registration in &config.initial_validators {
let _ = inductor.induct(&registration); let _ = inductor.induct(&registration);
}; };
inductor.to_vec() inductor.to_vec()
@ -120,17 +116,19 @@ mod tests {
extern crate bls; extern crate bls;
use super::*; use super::*;
use self::bls::Keypair; use self::bls::{
create_proof_of_possession,
Keypair,
};
use types::{ use types::{
Hash256, Hash256,
Address, Address,
}; };
use validator_induction::create_proof_of_possession;
#[test] #[test]
fn test_genesis_no_validators() { fn test_genesis_no_validators() {
let config = ChainConfig::standard(); let config = ChainConfig::standard();
let (act, cry) = BeaconChain::genesis_states(&vec![], &config).unwrap(); let (act, cry) = BeaconChain::genesis_states(&config).unwrap();
assert_eq!(cry.validator_set_change_slot, 0); assert_eq!(cry.validator_set_change_slot, 0);
assert_eq!(cry.validators.len(), 0); assert_eq!(cry.validators.len(), 0);
@ -170,41 +168,39 @@ mod tests {
#[test] #[test]
fn test_genesis_valid_validators() { fn test_genesis_valid_validators() {
let config = ChainConfig::standard(); let mut config = ChainConfig::standard();
let validator_count = 5; let validator_count = 5;
let mut validators = vec![];
for _ in 0..validator_count { for _ in 0..validator_count {
validators.push(random_registration()); config.initial_validators.push(random_registration());
} }
let (_, cry) = BeaconChain::genesis_states(&validators, &config).unwrap(); let (_, cry) = BeaconChain::genesis_states(&config).unwrap();
assert_eq!(cry.validators.len(), validator_count); assert_eq!(cry.validators.len(), validator_count);
} }
#[test] #[test]
fn test_genesis_invalid_validators() { fn test_genesis_invalid_validators() {
let config = ChainConfig::standard(); let mut config = ChainConfig::standard();
let good_validator_count = 5; let good_validator_count = 5;
let mut all_validators = vec![];
for _ in 0..good_validator_count { for _ in 0..good_validator_count {
all_validators.push(random_registration()); config.initial_validators.push(random_registration());
} }
let mut bad_v = random_registration(); let mut bad_v = random_registration();
let bad_kp = Keypair::random(); let bad_kp = Keypair::random();
bad_v.proof_of_possession = create_proof_of_possession(&bad_kp); bad_v.proof_of_possession = create_proof_of_possession(&bad_kp);
all_validators.push(bad_v); config.initial_validators.push(bad_v);
let mut bad_v = random_registration(); let mut bad_v = random_registration();
bad_v.withdrawal_shard = config.shard_count + 1; bad_v.withdrawal_shard = config.shard_count + 1;
all_validators.push(bad_v); config.initial_validators.push(bad_v);
let (_, cry) = BeaconChain::genesis_states(&all_validators, &config).unwrap(); let (_, cry) = BeaconChain::genesis_states(&config).unwrap();
assert!(all_validators.len() != good_validator_count, "test is invalid"); assert!(config.initial_validators.len() != good_validator_count, "test is invalid");
assert_eq!(cry.validators.len(), good_validator_count); assert_eq!(cry.validators.len(), good_validator_count);
} }
} }

View File

@ -2,7 +2,6 @@ extern crate types;
extern crate validator_induction; extern crate validator_induction;
extern crate validator_shuffling; extern crate validator_shuffling;
mod blocks;
mod genesis; mod genesis;
use std::collections::HashMap; use std::collections::HashMap;
@ -13,6 +12,7 @@ use types::{
Hash256, Hash256,
}; };
#[derive(Debug, PartialEq)]
pub enum BeaconChainError { pub enum BeaconChainError {
InvalidGenesis, InvalidGenesis,
DBError(String), DBError(String),
@ -24,15 +24,14 @@ pub struct BeaconChain {
pub fork_latest_block_hashes: Vec<Hash256>, pub fork_latest_block_hashes: Vec<Hash256>,
pub active_states: HashMap<Hash256, ActiveState>, pub active_states: HashMap<Hash256, ActiveState>,
pub crystallized_states: HashMap<Hash256, CrystallizedState>, pub crystallized_states: HashMap<Hash256, CrystallizedState>,
pub config: ChainConfig,
} }
impl BeaconChain { impl BeaconChain {
pub fn new(config: ChainConfig) pub fn new(config: ChainConfig)
-> Result<Self, BeaconChainError> -> Result<Self, BeaconChainError>
{ {
let initial_validators = vec![]; let (active_state, crystallized_state) = BeaconChain::genesis_states(&config)?;
let (active_state, crystallized_state) = BeaconChain::genesis_states(
&initial_validators, &config)?;
let canonical_latest_block_hash = Hash256::zero(); let canonical_latest_block_hash = Hash256::zero();
let fork_latest_block_hashes = vec![]; let fork_latest_block_hashes = vec![];
@ -48,6 +47,7 @@ impl BeaconChain {
fork_latest_block_hashes, fork_latest_block_hashes,
active_states, active_states,
crystallized_states, crystallized_states,
config,
}) })
} }
} }
@ -56,9 +56,26 @@ impl BeaconChain {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use types::ValidatorRegistration;
#[test] #[test]
fn test_new_chain() { fn test_new_chain() {
assert_eq!(2 + 2, 4); let mut config = ChainConfig::standard();
for _ in 0..4 {
config.initial_validators.push(ValidatorRegistration::random())
}
let chain = BeaconChain::new(config.clone()).unwrap();
let (act, cry) = BeaconChain::genesis_states(&config).unwrap();
assert_eq!(chain.last_finalized_slot, None);
assert_eq!(chain.canonical_latest_block_hash, Hash256::zero());
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);
} }
} }

View File

@ -4,6 +4,7 @@ use super::{
SpecialRecord, SpecialRecord,
}; };
#[derive(Debug, PartialEq)]
pub struct ActiveState { pub struct ActiveState {
pub pending_attestations: Vec<AttestationRecord>, pub pending_attestations: Vec<AttestationRecord>,
pub pending_specials: Vec<SpecialRecord>, pub pending_specials: Vec<SpecialRecord>,

View File

@ -1,8 +1,12 @@
use super::ValidatorRegistration;
#[derive(Debug, Clone, PartialEq)]
pub struct ChainConfig { pub struct ChainConfig {
pub cycle_length: u8, pub cycle_length: u8,
pub shard_count: u16, pub shard_count: u16,
pub min_committee_size: u64, pub min_committee_size: u64,
pub genesis_time: u64, pub genesis_time: u64,
pub initial_validators: Vec<ValidatorRegistration>,
} }
/* /*
@ -17,6 +21,7 @@ impl ChainConfig {
shard_count: 1024, shard_count: 1024,
min_committee_size: 128, min_committee_size: 128,
genesis_time: GENESIS_TIME, // arbitrary genesis_time: GENESIS_TIME, // arbitrary
initial_validators: vec![],
} }
} }
@ -41,6 +46,7 @@ impl ChainConfig {
shard_count: 2, shard_count: 2,
min_committee_size: 2, min_committee_size: 2,
genesis_time: GENESIS_TIME, // arbitrary genesis_time: GENESIS_TIME, // arbitrary
initial_validators: vec![],
} }
} }
} }

View File

@ -1,6 +1,6 @@
use super::Hash256; use super::Hash256;
#[derive(Clone)] #[derive(Clone, Debug, PartialEq)]
pub struct CrosslinkRecord { pub struct CrosslinkRecord {
pub recently_changed: bool, pub recently_changed: bool,
pub slot: u64, pub slot: u64,

View File

@ -4,6 +4,7 @@ use super::shard_and_committee::ShardAndCommittee;
use super::Hash256; use super::Hash256;
#[derive(Debug, PartialEq)]
pub struct CrystallizedState { pub struct CrystallizedState {
pub validator_set_change_slot: u64, pub validator_set_change_slot: u64,
pub validators: Vec<ValidatorRecord>, pub validators: Vec<ValidatorRecord>,

View File

@ -1,4 +1,4 @@
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
pub struct ShardAndCommittee { pub struct ShardAndCommittee {
pub shard_id: u16, pub shard_id: u16,
pub committee: Vec<usize> pub committee: Vec<usize>

View File

@ -1,4 +1,5 @@
use bls::{ use bls::{
create_proof_of_possession,
Keypair, Keypair,
PublicKey, PublicKey,
Signature, Signature,
@ -18,3 +19,17 @@ pub struct ValidatorRegistration {
pub randao_commitment: Hash256, pub randao_commitment: Hash256,
pub proof_of_possession: Signature, pub proof_of_possession: Signature,
} }
impl ValidatorRegistration {
pub fn random() -> Self {
let keypair = Keypair::random();
Self {
pubkey: keypair.pk.clone(),
withdrawal_shard: 0,
withdrawal_address: Address::random(),
randao_commitment: Hash256::random(),
proof_of_possession: create_proof_of_possession(&keypair),
}
}
}