Add extra params to ChainSpec

This commit is contained in:
Paul Hauner 2018-12-13 16:01:04 +11:00
parent 4f0a223579
commit 06e1ee0e5f
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
6 changed files with 87 additions and 57 deletions

View File

@ -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<BeaconState, Error> {
pub fn genesis_beacon_state(spec: &ChainSpec) -> Result<BeaconState, Error> {
/*
* 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(&registration, 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<ValidatorRegistration> {
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
);
}
}

View File

View File

@ -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;

View File

@ -4,4 +4,5 @@ version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
[dependencies]
bls = { path = "../utils/bls" }
types = { path = "../types" }

View File

@ -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<ValidatorRegistration> {
// 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();
}
}

View File

@ -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<ValidatorRegistration>,
pub genesis_time: u64,
pub processed_pow_receipt_root: Hash256,
}