Move genesis code into its own crate

This commit is contained in:
Paul Hauner 2018-12-13 12:27:45 +11:00
parent bf49c881d5
commit 6847e68c5e
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
6 changed files with 75 additions and 92 deletions

View File

@ -34,6 +34,7 @@ name = "lighthouse"
members = [ members = [
"beacon_chain/attestation_validation", "beacon_chain/attestation_validation",
"beacon_chain/chain", "beacon_chain/chain",
"beacon_chain/genesis",
"beacon_chain/naive_fork_choice", "beacon_chain/naive_fork_choice",
"beacon_chain/spec", "beacon_chain/spec",
"beacon_chain/state-transition", "beacon_chain/state-transition",

View File

@ -9,7 +9,6 @@ extern crate validator_induction;
extern crate validator_shuffling; extern crate validator_shuffling;
mod block_processing; mod block_processing;
mod genesis;
mod maps; mod maps;
mod stores; mod stores;
mod transition; mod transition;

View File

@ -0,0 +1,11 @@
[package]
name = "genesis"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
[dependencies]
bls = { path = "../utils/bls" }
spec = { path = "../spec" }
types = { path = "../types" }
validator_induction = { path = "../validator_induction" }
validator_shuffling = { path = "../validator_shuffling" }

View File

@ -7,24 +7,16 @@ use validator_shuffling::{shard_and_committees_for_cycle, ValidatorAssignmentErr
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
NoValidators,
ValidationAssignmentError(ValidatorAssignmentError), ValidationAssignmentError(ValidatorAssignmentError),
NotImplemented, NotImplemented,
} }
impl From<ValidatorAssignmentError> for Error { pub fn genesis_beacon_state(
fn from(e: ValidatorAssignmentError) -> Error {
Error::ValidationAssignmentError(e)
}
}
/// Initialize a new ChainHead with genesis parameters.
///
/// Used when syncing a chain from scratch.
pub fn genesis_states(
spec: &ChainSpec, spec: &ChainSpec,
initial_validators: Vec<ValidatorRegistration>, initial_validators: &[ValidatorRegistration],
genesis_time: u64, genesis_time: u64,
processed_pow_receipt_root: Hash256, processed_pow_receipt_root: &Hash256,
) -> Result<BeaconState, Error> { ) -> Result<BeaconState, Error> {
/* /*
* Parse the ValidatorRegistrations into ValidatorRecords and induct them. * Parse the ValidatorRegistrations into ValidatorRecords and induct them.
@ -33,7 +25,7 @@ pub fn genesis_states(
*/ */
let validators = { let validators = {
let mut inductor = ValidatorInductor::new(0, spec.shard_count, vec![]); let mut inductor = ValidatorInductor::new(0, spec.shard_count, vec![]);
for registration in &initial_validators { for registration in initial_validators {
let _ = inductor.induct(&registration, ValidatorStatus::Active); let _ = inductor.induct(&registration, ValidatorStatus::Active);
} }
inductor.to_vec() inductor.to_vec()
@ -45,7 +37,7 @@ pub fn genesis_states(
* Crystallizedstate stores two cycles, so we simply repeat the same assignment twice. * Crystallizedstate stores two cycles, so we simply repeat the same assignment twice.
*/ */
let _shard_and_committee_for_slots = { let _shard_and_committee_for_slots = {
let mut a = shard_and_committees_for_cycle(&vec![0; 32], &validators, 0, &spec)?; let mut a = shard_and_committees_for_cycle(&[0; 32], &validators, 0, &spec)?;
let mut b = a.clone(); let mut b = a.clone();
a.append(&mut b); a.append(&mut b);
a a
@ -79,15 +71,15 @@ pub fn genesis_states(
*/ */
randao_mix: spec.zero_hash, randao_mix: spec.zero_hash,
next_seed: spec.zero_hash, next_seed: spec.zero_hash,
shard_committees_at_slot: vec![], shard_committees_at_slots: vec![],
persistent_committees: vec![], persistent_committees: vec![],
persisten_committee_reassignments: vec![], persistent_committee_reassignments: vec![],
/* /*
* Finality * Finality
*/ */
previous_justified_slot: spec.initial_slot_number, previous_justified_slot: spec.initial_slot_number,
justified_slot: spec.initial_slot_number, justified_slot: spec.initial_slot_number,
justified_bitfield: 0, justification_bitfield: 0,
finalized_slot: spec.initial_slot_number, finalized_slot: spec.initial_slot_number,
/* /*
* Recent state * Recent state
@ -99,59 +91,28 @@ pub fn genesis_states(
/* /*
* PoW receipt root * PoW receipt root
*/ */
processed_pow_receipt_root, processed_pow_receipt_root: *processed_pow_receipt_root,
candidate_pow_receipt_roots: vec![], candidate_pow_receipt_roots: vec![],
}) })
} }
impl From<ValidatorAssignmentError> for Error {
fn from(e: ValidatorAssignmentError) -> Error {
Error::ValidationAssignmentError(e)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
extern crate bls; extern crate bls;
extern crate validator_induction; extern crate validator_induction;
// TODO: implement genesis for `BeaconState`
// https://github.com/sigp/lighthouse/issues/99
//
/*
use self::bls::{create_proof_of_possession, Keypair}; use self::bls::{create_proof_of_possession, Keypair};
use super::*; use super::*;
use types::{Address, Hash256, ValidatorRegistration}; use types::{Address, Hash256, ValidatorRegistration};
#[test] // TODO: enhance these tests.
fn test_genesis_no_validators() { // https://github.com/sigp/lighthouse/issues/117
let config = ChainConfig::standard();
let (act, cry) = genesis_states(&config).unwrap();
assert_eq!(cry.validator_set_change_slot, 0);
assert_eq!(cry.validators.len(), 0);
assert_eq!(cry.crosslinks.len(), config.shard_count as usize);
for cl in cry.crosslinks {
assert_eq!(cl.recently_changed, false);
assert_eq!(cl.slot, 0);
assert_eq!(cl.hash, Hash256::zero());
}
assert_eq!(cry.last_state_recalculation_slot, 0);
assert_eq!(cry.last_finalized_slot, 0);
assert_eq!(cry.last_justified_slot, 0);
assert_eq!(cry.justified_streak, 0);
assert_eq!(
cry.shard_and_committee_for_slots.len(),
(config.cycle_length as usize) * 2
);
assert_eq!(cry.deposits_penalized_in_period.len(), 0);
assert_eq!(cry.validator_set_delta_hash_chain, Hash256::zero());
assert_eq!(cry.pre_fork_version, INITIAL_FORK_VERSION);
assert_eq!(cry.post_fork_version, INITIAL_FORK_VERSION);
assert_eq!(cry.fork_slot_number, 0);
assert_eq!(act.pending_attestations.len(), 0);
assert_eq!(act.pending_specials.len(), 0);
assert_eq!(
act.recent_block_hashes,
vec![Hash256::zero(); config.cycle_length as usize]
);
assert_eq!(act.randao_mix, Hash256::zero());
}
fn random_registration() -> ValidatorRegistration { fn random_registration() -> ValidatorRegistration {
let keypair = Keypair::random(); let keypair = Keypair::random();
@ -164,45 +125,48 @@ mod tests {
} }
} }
#[test] fn random_registrations(n: usize) -> Vec<ValidatorRegistration> {
fn test_genesis_valid_validators() { let mut output = Vec::with_capacity(n);
let mut config = ChainConfig::standard(); for _ in 0..n {
let validator_count = 5; output.push(random_registration())
for _ in 0..validator_count {
config.initial_validators.push(random_registration());
} }
output
let (_, cry) = genesis_states(&config).unwrap();
assert_eq!(cry.validators.len(), validator_count);
} }
#[test] #[test]
fn test_genesis_invalid_validators() { fn test_genesis() {
let mut config = ChainConfig::standard(); let spec = ChainSpec::foundation();
let good_validator_count = 5; let genesis_time = 42;
let initial_validators = random_registrations(4);
let processed_pow_receipt_root = Hash256::from("pow_root".as_bytes());
for _ in 0..good_validator_count { let state = genesis_beacon_state(
config.initial_validators.push(random_registration()); &spec,
&initial_validators,
genesis_time,
&processed_pow_receipt_root,
).unwrap();
assert_eq!(state.validator_registry.len(), 4);
} }
let mut bad_v = random_registration(); #[test]
let bad_kp = Keypair::random(); fn test_genesis_bad_validator() {
bad_v.proof_of_possession = create_proof_of_possession(&bad_kp); let spec = ChainSpec::foundation();
config.initial_validators.push(bad_v); let genesis_time = 42;
let mut initial_validators = random_registrations(5);
let processed_pow_receipt_root = Hash256::from("pow_root".as_bytes());
let mut bad_v = random_registration(); let random_kp = Keypair::random();
bad_v.withdrawal_shard = config.shard_count + 1; initial_validators[4].proof_of_possession = create_proof_of_possession(&random_kp);
config.initial_validators.push(bad_v);
let (_, cry) = genesis_states(&config).unwrap(); let state = genesis_beacon_state(
&spec,
&initial_validators,
genesis_time,
&processed_pow_receipt_root,
).unwrap();
assert!( assert_eq!(state.validator_registry.len(), 4);
config.initial_validators.len() != good_validator_count,
"test is invalid"
);
assert_eq!(cry.validators.len(), good_validator_count);
} }
*/
} }

View File

@ -0,0 +1,7 @@
extern crate spec;
extern crate types;
extern crate validator_induction;
extern crate validator_shuffling;
pub mod beacon_state;

View File

@ -4,6 +4,7 @@ mod foundation;
use types::{Address, Hash256}; use types::{Address, Hash256};
#[derive(PartialEq, Debug)]
pub struct ChainSpec { pub struct ChainSpec {
/* /*
* Misc * Misc