Add spec fn to BeaconStateTypes

This commit is contained in:
Paul Hauner 2019-05-08 16:29:27 +10:00
parent 8cefd20e9d
commit 81c1dcceec
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
6 changed files with 43 additions and 23 deletions

View File

@ -13,7 +13,7 @@ use test_random_derive::TestRandom;
use tree_hash::TreeHash; use tree_hash::TreeHash;
use tree_hash_derive::{CachedTreeHash, TreeHash}; use tree_hash_derive::{CachedTreeHash, TreeHash};
pub use beacon_state_types::{BeaconStateTypes, FewValidatorsBeaconState, FoundationBeaconState}; pub use beacon_state_types::*;
pub mod beacon_state_types; pub mod beacon_state_types;
mod epoch_cache; mod epoch_cache;

View File

@ -7,30 +7,40 @@ pub trait BeaconStateTypes {
type LatestRandaoMixesLength: Unsigned + Clone + Sync + Send; type LatestRandaoMixesLength: Unsigned + Clone + Sync + Send;
type LatestActiveIndexRootsLength: Unsigned + Clone + Sync + Send; type LatestActiveIndexRootsLength: Unsigned + Clone + Sync + Send;
type LatestSlashedExitLength: Unsigned + Clone + Sync + Send; type LatestSlashedExitLength: Unsigned + Clone + Sync + Send;
fn spec() -> ChainSpec;
} }
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
pub struct FoundationStateParams; pub struct FoundationStateTypes;
impl BeaconStateTypes for FoundationStateParams { impl BeaconStateTypes for FoundationStateTypes {
type ShardCount = U1024; type ShardCount = U1024;
type SlotsPerHistoricalRoot = U8192; type SlotsPerHistoricalRoot = U8192;
type LatestRandaoMixesLength = U8192; type LatestRandaoMixesLength = U8192;
type LatestActiveIndexRootsLength = U8192; type LatestActiveIndexRootsLength = U8192;
type LatestSlashedExitLength = U8192; type LatestSlashedExitLength = U8192;
fn spec() -> ChainSpec {
ChainSpec::foundation()
}
} }
pub type FoundationBeaconState = BeaconState<FoundationStateParams>; pub type FoundationBeaconState = BeaconState<FoundationStateTypes>;
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
pub struct FewValidatorsStateParams; pub struct FewValidatorsStateTypes;
impl BeaconStateTypes for FewValidatorsStateParams { impl BeaconStateTypes for FewValidatorsStateTypes {
type ShardCount = U8; type ShardCount = U8;
type SlotsPerHistoricalRoot = U8192; type SlotsPerHistoricalRoot = U8192;
type LatestRandaoMixesLength = U8192; type LatestRandaoMixesLength = U8192;
type LatestActiveIndexRootsLength = U8192; type LatestActiveIndexRootsLength = U8192;
type LatestSlashedExitLength = U8192; type LatestSlashedExitLength = U8192;
fn spec() -> ChainSpec {
ChainSpec::few_validators()
}
} }
pub type FewValidatorsBeaconState = BeaconState<FewValidatorsStateParams>; pub type FewValidatorsBeaconState = BeaconState<FewValidatorsStateTypes>;

View File

@ -1,7 +1,7 @@
#![cfg(test)] #![cfg(test)]
use super::*; use super::*;
use crate::beacon_state::FewValidatorsBeaconState; use crate::beacon_state::FewValidatorsStateTypes;
use crate::test_utils::*; use crate::test_utils::*;
use swap_or_not_shuffle::shuffle_list; use swap_or_not_shuffle::shuffle_list;
@ -102,10 +102,13 @@ fn setup_sane_cache_test<T: BeaconStateTypes>(
#[test] #[test]
fn builds_sane_current_epoch_cache() { fn builds_sane_current_epoch_cache() {
let mut spec = ChainSpec::few_validators(); let mut spec = FewValidatorsStateTypes::spec();
spec.shard_count = 4; spec.shard_count = 4;
let validator_count = (spec.shard_count * spec.target_committee_size) + 1; let validator_count = (spec.shard_count * spec.target_committee_size) + 1;
let state: FewValidatorsBeaconState = setup_sane_cache_test(validator_count as usize, &spec);
let state: BeaconState<FewValidatorsStateTypes> =
setup_sane_cache_test(validator_count as usize, &spec);
do_sane_cache_test( do_sane_cache_test(
state.clone(), state.clone(),
state.current_epoch(&spec), state.current_epoch(&spec),
@ -119,10 +122,13 @@ fn builds_sane_current_epoch_cache() {
#[test] #[test]
fn builds_sane_previous_epoch_cache() { fn builds_sane_previous_epoch_cache() {
let mut spec = ChainSpec::few_validators(); let mut spec = FewValidatorsStateTypes::spec();
spec.shard_count = 2; spec.shard_count = 2;
let validator_count = (spec.shard_count * spec.target_committee_size) + 1; let validator_count = (spec.shard_count * spec.target_committee_size) + 1;
let state: FewValidatorsBeaconState = setup_sane_cache_test(validator_count as usize, &spec);
let state: BeaconState<FewValidatorsStateTypes> =
setup_sane_cache_test(validator_count as usize, &spec);
do_sane_cache_test( do_sane_cache_test(
state.clone(), state.clone(),
state.previous_epoch(&spec), state.previous_epoch(&spec),
@ -136,11 +142,13 @@ fn builds_sane_previous_epoch_cache() {
#[test] #[test]
fn builds_sane_next_without_update_epoch_cache() { fn builds_sane_next_without_update_epoch_cache() {
let mut spec = ChainSpec::few_validators(); let mut spec = FewValidatorsStateTypes::spec();
spec.shard_count = 2; spec.shard_count = 2;
let validator_count = (spec.shard_count * spec.target_committee_size) + 1; let validator_count = (spec.shard_count * spec.target_committee_size) + 1;
let mut state: FewValidatorsBeaconState =
let mut state: BeaconState<FewValidatorsStateTypes> =
setup_sane_cache_test(validator_count as usize, &spec); setup_sane_cache_test(validator_count as usize, &spec);
state.validator_registry_update_epoch = state.slot.epoch(spec.slots_per_epoch); state.validator_registry_update_epoch = state.slot.epoch(spec.slots_per_epoch);
do_sane_cache_test( do_sane_cache_test(
state.clone(), state.clone(),

View File

@ -1,6 +1,6 @@
#![cfg(test)] #![cfg(test)]
use super::*; use super::*;
use crate::beacon_state::{FewValidatorsBeaconState, FoundationBeaconState}; use crate::beacon_state::FewValidatorsStateTypes;
use crate::test_utils::*; use crate::test_utils::*;
ssz_tests!(FoundationBeaconState); ssz_tests!(FoundationBeaconState);
@ -46,9 +46,11 @@ fn test_cache_initialization<'a, T: BeaconStateTypes>(
#[test] #[test]
fn cache_initialization() { fn cache_initialization() {
let spec = ChainSpec::few_validators(); let spec = FewValidatorsStateTypes::spec();
let (mut state, _keypairs): (FewValidatorsBeaconState, Vec<Keypair>) =
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(16, &spec).build(); let builder: TestingBeaconStateBuilder<FewValidatorsStateTypes> =
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(16, &spec);
let (mut state, _keypairs) = builder.build();
state.slot = (spec.genesis_epoch + 1).start_slot(spec.slots_per_epoch); state.slot = (spec.genesis_epoch + 1).start_slot(spec.slots_per_epoch);

View File

@ -154,7 +154,7 @@ impl ChainSpec {
/// Returns a `ChainSpec` compatible with the Ethereum Foundation specification. /// Returns a `ChainSpec` compatible with the Ethereum Foundation specification.
/// ///
/// Spec v0.5.1 /// Spec v0.5.1
pub fn foundation() -> Self { pub(crate) fn foundation() -> Self {
let genesis_slot = Slot::new(2_u64.pow(32)); let genesis_slot = Slot::new(2_u64.pow(32));
let slots_per_epoch = 64; let slots_per_epoch = 64;
let genesis_epoch = genesis_slot.epoch(slots_per_epoch); let genesis_epoch = genesis_slot.epoch(slots_per_epoch);
@ -248,7 +248,7 @@ impl ChainSpec {
/// Returns a `ChainSpec` compatible with the Lighthouse testnet specification. /// Returns a `ChainSpec` compatible with the Lighthouse testnet specification.
/// ///
/// Spec v0.4.0 /// Spec v0.4.0
pub fn lighthouse_testnet() -> Self { pub(crate) fn lighthouse_testnet() -> Self {
/* /*
* Lighthouse testnet bootnodes * Lighthouse testnet bootnodes
*/ */
@ -264,7 +264,7 @@ impl ChainSpec {
} }
/// Returns a `ChainSpec` compatible with the specification suitable for 8 validators. /// Returns a `ChainSpec` compatible with the specification suitable for 8 validators.
pub fn few_validators() -> Self { pub(crate) fn few_validators() -> Self {
let genesis_slot = Slot::new(2_u64.pow(32)); let genesis_slot = Slot::new(2_u64.pow(32));
let slots_per_epoch = 8; let slots_per_epoch = 8;
let genesis_epoch = genesis_slot.epoch(slots_per_epoch); let genesis_epoch = genesis_slot.epoch(slots_per_epoch);

View File

@ -31,9 +31,9 @@ pub struct HistoricalBatch<T: BeaconStateTypes> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::beacon_state::beacon_state_types::FoundationStateParams; use crate::beacon_state::beacon_state_types::FoundationStateTypes;
pub type FoundationHistoricalBatch = HistoricalBatch<FoundationStateParams>; pub type FoundationHistoricalBatch = HistoricalBatch<FoundationStateTypes>;
ssz_tests!(FoundationHistoricalBatch); ssz_tests!(FoundationHistoricalBatch);
cached_tree_hash_tests!(FoundationHistoricalBatch); cached_tree_hash_tests!(FoundationHistoricalBatch);