From 06e1ee0e5f9a7d74fe0cca199ac034d38e0c3b1d Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 13 Dec 2018 16:01:04 +1100 Subject: [PATCH] Add extra params to `ChainSpec` --- beacon_chain/genesis/src/beacon_state.rs | 71 ++++++------------------ beacon_chain/genesis/src/block.rs | 0 beacon_chain/genesis/src/lib.rs | 4 +- beacon_chain/spec/Cargo.toml | 1 + beacon_chain/spec/src/foundation.rs | 59 +++++++++++++++++++- beacon_chain/spec/src/lib.rs | 9 ++- 6 files changed, 87 insertions(+), 57 deletions(-) create mode 100644 beacon_chain/genesis/src/block.rs diff --git a/beacon_chain/genesis/src/beacon_state.rs b/beacon_chain/genesis/src/beacon_state.rs index d95136c6f..fda94552f 100644 --- a/beacon_chain/genesis/src/beacon_state.rs +++ b/beacon_chain/genesis/src/beacon_state.rs @@ -1,7 +1,5 @@ use spec::ChainSpec; -use types::{ - BeaconState, CrosslinkRecord, ForkData, Hash256, ValidatorRegistration, ValidatorStatus, -}; +use types::{BeaconState, CrosslinkRecord, ForkData, ValidatorStatus}; use validator_induction::ValidatorInductor; use validator_shuffling::{shard_and_committees_for_cycle, ValidatorAssignmentError}; @@ -12,12 +10,7 @@ pub enum Error { NotImplemented, } -pub fn genesis_beacon_state( - spec: &ChainSpec, - initial_validators: &[ValidatorRegistration], - genesis_time: u64, - processed_pow_receipt_root: &Hash256, -) -> Result { +pub fn genesis_beacon_state(spec: &ChainSpec) -> Result { /* * Parse the ValidatorRegistrations into ValidatorRecords and induct them. * @@ -25,7 +18,7 @@ pub fn genesis_beacon_state( */ let validators = { let mut inductor = ValidatorInductor::new(0, spec.shard_count, vec![]); - for registration in initial_validators { + for registration in &spec.initial_validators { let _ = inductor.induct(®istration, ValidatorStatus::Active); } inductor.to_vec() @@ -53,7 +46,7 @@ pub fn genesis_beacon_state( * Misc */ slot: spec.initial_slot_number, - genesis_time, + genesis_time: spec.genesis_time, fork_data: ForkData { pre_fork_version: spec.initial_fork_version, post_fork_version: spec.initial_fork_version, @@ -91,7 +84,7 @@ pub fn genesis_beacon_state( /* * PoW receipt root */ - processed_pow_receipt_root: *processed_pow_receipt_root, + processed_pow_receipt_root: spec.processed_pow_receipt_root, candidate_pow_receipt_roots: vec![], }) } @@ -109,64 +102,34 @@ mod tests { use self::bls::{create_proof_of_possession, Keypair}; use super::*; - use types::{Address, Hash256, ValidatorRegistration}; // TODO: enhance these tests. // https://github.com/sigp/lighthouse/issues/117 - fn random_registration() -> ValidatorRegistration { - let keypair = Keypair::random(); - ValidatorRegistration { - pubkey: keypair.pk.clone(), - withdrawal_shard: 0, - withdrawal_address: Address::random(), - randao_commitment: Hash256::random(), - proof_of_possession: create_proof_of_possession(&keypair), - } - } - - fn random_registrations(n: usize) -> Vec { - let mut output = Vec::with_capacity(n); - for _ in 0..n { - output.push(random_registration()) - } - output - } - #[test] fn test_genesis() { let spec = ChainSpec::foundation(); - let genesis_time = 42; - let initial_validators = random_registrations(4); - let processed_pow_receipt_root = Hash256::from("pow_root".as_bytes()); - let state = genesis_beacon_state( - &spec, - &initial_validators, - genesis_time, - &processed_pow_receipt_root, - ).unwrap(); + let state = genesis_beacon_state(&spec).unwrap(); - assert_eq!(state.validator_registry.len(), 4); + assert_eq!( + state.validator_registry.len(), + spec.initial_validators.len() + ); } #[test] fn test_genesis_bad_validator() { - let spec = ChainSpec::foundation(); - let genesis_time = 42; - let mut initial_validators = random_registrations(5); - let processed_pow_receipt_root = Hash256::from("pow_root".as_bytes()); + let mut spec = ChainSpec::foundation(); let random_kp = Keypair::random(); - initial_validators[4].proof_of_possession = create_proof_of_possession(&random_kp); + spec.initial_validators[4].proof_of_possession = create_proof_of_possession(&random_kp); - let state = genesis_beacon_state( - &spec, - &initial_validators, - genesis_time, - &processed_pow_receipt_root, - ).unwrap(); + let state = genesis_beacon_state(&spec).unwrap(); - assert_eq!(state.validator_registry.len(), 4); + assert_eq!( + state.validator_registry.len(), + spec.initial_validators.len() - 1 + ); } } diff --git a/beacon_chain/genesis/src/block.rs b/beacon_chain/genesis/src/block.rs new file mode 100644 index 000000000..e69de29bb diff --git a/beacon_chain/genesis/src/lib.rs b/beacon_chain/genesis/src/lib.rs index 54d45dae8..3256ff558 100644 --- a/beacon_chain/genesis/src/lib.rs +++ b/beacon_chain/genesis/src/lib.rs @@ -3,5 +3,7 @@ extern crate types; extern crate validator_induction; extern crate validator_shuffling; -pub mod beacon_state; +mod beacon_state; + +pub use beacon_state::genesis_beacon_state; diff --git a/beacon_chain/spec/Cargo.toml b/beacon_chain/spec/Cargo.toml index a2eac0692..62ae74cc7 100644 --- a/beacon_chain/spec/Cargo.toml +++ b/beacon_chain/spec/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" authors = ["Paul Hauner "] [dependencies] +bls = { path = "../utils/bls" } types = { path = "../types" } diff --git a/beacon_chain/spec/src/foundation.rs b/beacon_chain/spec/src/foundation.rs index 5bf25e1e7..b80187281 100644 --- a/beacon_chain/spec/src/foundation.rs +++ b/beacon_chain/spec/src/foundation.rs @@ -1,9 +1,13 @@ use super::ChainSpec; +use bls::{create_proof_of_possession, Keypair, PublicKey, SecretKey}; -use types::{Address, Hash256}; +use types::{Address, Hash256, ValidatorRegistration}; impl ChainSpec { /// Returns a `ChainSpec` compatible with the specification from Ethereum Foundation. + /// + /// Of course, the actual foundation specs are unknown at this point so these are just a rough + /// estimate. pub fn foundation() -> Self { Self { /* @@ -56,6 +60,59 @@ impl ChainSpec { max_attestations: 128, max_deposits: 16, max_exits: 16, + /* + * Intialization parameters + */ + initial_validators: initial_validators_for_testing(), + genesis_time: 1544672897, + processed_pow_receipt_root: Hash256::from("pow_root".as_bytes()), } } } + +/// Generate a set of validator registrations to use with testing until the real chain starts. +fn initial_validators_for_testing() -> Vec { + // Some dummy private keys to start with. + let key_strings = vec![ + "jzjxxgjajfjrmgodszzsgqccmhnyvetcuxobhtynojtpdtbj", + "gpeehcjudxdijzhjgirfuhahmnjutlchjmoffxmimbdejakd", + "ntrrdwwebodokuwaclhoqreqyodngoyhurvesghjfxeswoaj", + "cibmzkqrzdgdlrvqaxinwpvyhcgjkeysrsjkqtkcxvznsvth", + "erqrfuahdwprsstkawggounxmihzhrvbhchcyiwtaypqcedr", + ]; + + let mut initial_validators = Vec::with_capacity(key_strings.len()); + for key_string in key_strings { + let keypair = { + let secret_key = match SecretKey::from_bytes(&key_string.as_bytes()) { + Ok(key) => key, + Err(_) => unreachable!(), // Keys are static and should not fail. + }; + let public_key = PublicKey::from_secret_key(&secret_key); + Keypair { + sk: secret_key, + pk: public_key, + } + }; + let validator_registration = ValidatorRegistration { + pubkey: keypair.pk.clone(), + withdrawal_shard: 0, + withdrawal_address: Address::random(), + randao_commitment: Hash256::random(), + proof_of_possession: create_proof_of_possession(&keypair), + }; + initial_validators.push(validator_registration); + } + + initial_validators +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_foundation_spec_can_be_constructed() { + let _ = ChainSpec::foundation(); + } +} diff --git a/beacon_chain/spec/src/lib.rs b/beacon_chain/spec/src/lib.rs index e8f3cfdc3..3a05f2696 100644 --- a/beacon_chain/spec/src/lib.rs +++ b/beacon_chain/spec/src/lib.rs @@ -1,8 +1,9 @@ +extern crate bls; extern crate types; mod foundation; -use types::{Address, Hash256}; +use types::{Address, Hash256, ValidatorRegistration}; #[derive(PartialEq, Debug)] pub struct ChainSpec { @@ -56,4 +57,10 @@ pub struct ChainSpec { pub max_attestations: u64, pub max_deposits: u64, pub max_exits: u64, + /* + * Intialization parameters + */ + pub initial_validators: Vec, + pub genesis_time: u64, + pub processed_pow_receipt_root: Hash256, }