Unify EthSpecs in Mainnet and Minimal

This commit is contained in:
Paul Hauner
2019-06-08 08:49:04 -04:00
parent caddeba81b
commit 749f2fcb5f
27 changed files with 119 additions and 186 deletions

View File

@@ -95,9 +95,9 @@ pub trait EthSpec: 'static + Default + Sync + Send + Clone + Debug + PartialEq {
///
/// Spec v0.6.1
#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
pub struct FoundationEthSpec;
pub struct MainnetEthSpec;
impl EthSpec for FoundationEthSpec {
impl EthSpec for MainnetEthSpec {
type ShardCount = U1024;
type SlotsPerHistoricalRoot = U8192;
type LatestRandaoMixesLength = U8192;
@@ -107,17 +107,17 @@ impl EthSpec for FoundationEthSpec {
type GenesisEpoch = U0;
fn default_spec() -> ChainSpec {
ChainSpec::foundation()
ChainSpec::mainnet()
}
}
pub type FoundationBeaconState = BeaconState<FoundationEthSpec>;
pub type FoundationBeaconState = BeaconState<MainnetEthSpec>;
/// Ethereum Foundation specifications, modified to be suitable for < 1000 validators.
#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
pub struct FewValidatorsEthSpec;
pub struct MinimalEthSpec;
impl EthSpec for FewValidatorsEthSpec {
impl EthSpec for MinimalEthSpec {
type ShardCount = U8;
type SlotsPerHistoricalRoot = U8192;
type LatestRandaoMixesLength = U8192;
@@ -127,28 +127,8 @@ impl EthSpec for FewValidatorsEthSpec {
type GenesisEpoch = U0;
fn default_spec() -> ChainSpec {
ChainSpec::few_validators()
ChainSpec::minimal()
}
}
pub type FewValidatorsBeaconState = BeaconState<FewValidatorsEthSpec>;
/// Specifications suitable for a small-scale (< 1000 validators) lighthouse testnet.
#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
pub struct LighthouseTestnetEthSpec;
impl EthSpec for LighthouseTestnetEthSpec {
type ShardCount = U8;
type SlotsPerHistoricalRoot = U8192;
type LatestRandaoMixesLength = U8192;
type LatestActiveIndexRootsLength = U8192;
type LatestSlashedExitLength = U8192;
type SlotsPerEpoch = U8;
type GenesisEpoch = U0;
fn default_spec() -> ChainSpec {
ChainSpec::lighthouse_testnet()
}
}
pub type LighthouseTestnetBeaconState = BeaconState<LighthouseTestnetEthSpec>;
pub type MinimalBeaconState = BeaconState<MinimalEthSpec>;

View File

@@ -34,8 +34,8 @@ fn new_state<T: EthSpec>(validator_count: usize, slot: Slot) -> BeaconState<T> {
#[test]
fn fails_without_validators() {
let state = new_state::<FewValidatorsEthSpec>(0, Slot::new(0));
let spec = &FewValidatorsEthSpec::default_spec();
let state = new_state::<MinimalEthSpec>(0, Slot::new(0));
let spec = &MinimalEthSpec::default_spec();
assert_eq!(
CommitteeCache::initialized(&state, state.current_epoch(), &spec),
@@ -45,8 +45,8 @@ fn fails_without_validators() {
#[test]
fn initializes_with_the_right_epoch() {
let state = new_state::<FewValidatorsEthSpec>(16, Slot::new(0));
let spec = &FewValidatorsEthSpec::default_spec();
let state = new_state::<MinimalEthSpec>(16, Slot::new(0));
let spec = &MinimalEthSpec::default_spec();
let cache = CommitteeCache::default();
assert_eq!(cache.initialized_epoch, None);
@@ -63,14 +63,14 @@ fn initializes_with_the_right_epoch() {
#[test]
fn shuffles_for_the_right_epoch() {
let num_validators = FewValidatorsEthSpec::minimum_validator_count() * 2;
let num_validators = MinimalEthSpec::minimum_validator_count() * 2;
let epoch = Epoch::new(100_000_000);
let slot = epoch.start_slot(FewValidatorsEthSpec::slots_per_epoch());
let slot = epoch.start_slot(MinimalEthSpec::slots_per_epoch());
let mut state = new_state::<FewValidatorsEthSpec>(num_validators, slot);
let spec = &FewValidatorsEthSpec::default_spec();
let mut state = new_state::<MinimalEthSpec>(num_validators, slot);
let spec = &MinimalEthSpec::default_spec();
let distinct_hashes: Vec<Hash256> = (0..FewValidatorsEthSpec::latest_randao_mixes_length())
let distinct_hashes: Vec<Hash256> = (0..MinimalEthSpec::latest_randao_mixes_length())
.into_iter()
.map(|i| Hash256::from(i as u64))
.collect();
@@ -118,14 +118,14 @@ fn shuffles_for_the_right_epoch() {
#[test]
fn can_start_on_any_shard() {
let num_validators = FewValidatorsEthSpec::minimum_validator_count() * 2;
let num_validators = MinimalEthSpec::minimum_validator_count() * 2;
let epoch = Epoch::new(100_000_000);
let slot = epoch.start_slot(FewValidatorsEthSpec::slots_per_epoch());
let slot = epoch.start_slot(MinimalEthSpec::slots_per_epoch());
let mut state = new_state::<FewValidatorsEthSpec>(num_validators, slot);
let spec = &FewValidatorsEthSpec::default_spec();
let mut state = new_state::<MinimalEthSpec>(num_validators, slot);
let spec = &MinimalEthSpec::default_spec();
for i in 0..FewValidatorsEthSpec::shard_count() as u64 {
for i in 0..MinimalEthSpec::shard_count() as u64 {
state.latest_start_shard = i;
let cache = CommitteeCache::initialized(&state, state.current_epoch(), spec).unwrap();
@@ -154,7 +154,7 @@ impl EthSpec for ExcessShardsEthSpec {
type GenesisEpoch = U0;
fn default_spec() -> ChainSpec {
ChainSpec::few_validators()
ChainSpec::minimal()
}
}

View File

@@ -53,7 +53,7 @@ fn test_beacon_proposer_index<T: EthSpec>() {
#[test]
fn beacon_proposer_index() {
test_beacon_proposer_index::<FewValidatorsEthSpec>();
test_beacon_proposer_index::<MinimalEthSpec>();
}
/// Should produce (note the set notation brackets):
@@ -115,11 +115,11 @@ fn test_active_index<T: EthSpec>(state_slot: Slot) {
#[test]
fn get_active_index_root_index() {
test_active_index::<FoundationEthSpec>(Slot::new(0));
test_active_index::<MainnetEthSpec>(Slot::new(0));
let epoch = Epoch::from(FoundationEthSpec::latest_active_index_roots() * 4);
let slot = epoch.start_slot(FoundationEthSpec::slots_per_epoch());
test_active_index::<FoundationEthSpec>(slot);
let epoch = Epoch::from(MainnetEthSpec::latest_active_index_roots() * 4);
let slot = epoch.start_slot(MainnetEthSpec::slots_per_epoch());
test_active_index::<MainnetEthSpec>(slot);
}
/// Test that
@@ -166,14 +166,14 @@ fn test_cache_initialization<'a, T: EthSpec>(
#[test]
fn cache_initialization() {
let spec = FewValidatorsEthSpec::default_spec();
let spec = MinimalEthSpec::default_spec();
let builder: TestingBeaconStateBuilder<FewValidatorsEthSpec> =
let builder: TestingBeaconStateBuilder<MinimalEthSpec> =
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(16, &spec);
let (mut state, _keypairs) = builder.build();
state.slot = (FewValidatorsEthSpec::genesis_epoch() + 1)
.start_slot(FewValidatorsEthSpec::slots_per_epoch());
state.slot =
(MinimalEthSpec::genesis_epoch() + 1).start_slot(MinimalEthSpec::slots_per_epoch());
test_cache_initialization(&mut state, RelativeEpoch::Previous, &spec);
test_cache_initialization(&mut state, RelativeEpoch::Current, &spec);
@@ -203,7 +203,7 @@ fn tree_hash_cache() {
#[cfg(test)]
mod committees {
use super::*;
use crate::beacon_state::FewValidatorsEthSpec;
use crate::beacon_state::MinimalEthSpec;
use swap_or_not_shuffle::shuffle_list;
fn execute_committee_consistency_test<T: EthSpec>(
@@ -347,16 +347,16 @@ mod committees {
#[test]
fn current_epoch_committee_consistency() {
committee_consistency_test_suite::<FewValidatorsEthSpec>(RelativeEpoch::Current);
committee_consistency_test_suite::<MinimalEthSpec>(RelativeEpoch::Current);
}
#[test]
fn previous_epoch_committee_consistency() {
committee_consistency_test_suite::<FewValidatorsEthSpec>(RelativeEpoch::Previous);
committee_consistency_test_suite::<MinimalEthSpec>(RelativeEpoch::Previous);
}
#[test]
fn next_epoch_committee_consistency() {
committee_consistency_test_suite::<FewValidatorsEthSpec>(RelativeEpoch::Next);
committee_consistency_test_suite::<MinimalEthSpec>(RelativeEpoch::Next);
}
}

View File

@@ -136,7 +136,7 @@ impl ChainSpec {
/// Returns a `ChainSpec` compatible with the Ethereum Foundation specification.
///
/// Spec v0.6.1
pub fn foundation() -> Self {
pub fn mainnet() -> Self {
Self {
/*
* Misc
@@ -217,43 +217,35 @@ impl ChainSpec {
* Boot nodes
*/
boot_nodes: vec![],
chain_id: 1, // foundation chain id
chain_id: 1, // mainnet chain id
}
}
/// Returns a `ChainSpec` compatible with the Lighthouse testnet specification.
///
/// Spec v0.4.0
pub fn lighthouse_testnet() -> Self {
/*
* Lighthouse testnet bootnodes
*/
/// Returns a `ChainSpec` compatible with the specification suitable for 8 validators.
pub fn minimal() -> Self {
let genesis_slot = Slot::new(0);
// Note: these bootnodes are placeholders.
//
// Should be updated once static bootnodes exist.
let boot_nodes = vec!["/ip4/127.0.0.1/tcp/9000"
.parse()
.expect("correct multiaddr")];
Self {
boot_nodes,
chain_id: 2, // lighthouse testnet chain id
..ChainSpec::few_validators()
}
}
/// Returns a `ChainSpec` compatible with the specification suitable for 8 validators.
pub fn few_validators() -> Self {
let genesis_slot = Slot::new(0);
Self {
target_committee_size: 1,
chain_id: 2, // lighthouse testnet chain id
genesis_slot,
..ChainSpec::foundation()
shuffle_round_count: 10,
..ChainSpec::mainnet()
}
}
}
impl Default for ChainSpec {
fn default() -> Self {
Self::foundation()
Self::mainnet()
}
}
@@ -263,8 +255,8 @@ mod tests {
use int_to_bytes::int_to_bytes8;
#[test]
fn test_foundation_spec_can_be_constructed() {
let _ = ChainSpec::foundation();
fn test_mainnet_spec_can_be_constructed() {
let _ = ChainSpec::mainnet();
}
fn test_domain(domain_type: Domain, raw_domain: u32, spec: &ChainSpec) {
@@ -281,7 +273,7 @@ mod tests {
#[test]
fn test_get_domain() {
let spec = ChainSpec::foundation();
let spec = ChainSpec::mainnet();
test_domain(Domain::BeaconProposer, spec.domain_beacon_proposer, &spec);
test_domain(Domain::Randao, spec.domain_randao, &spec);

View File

@@ -63,7 +63,7 @@ mod tests {
cached_tree_hash_tests!(Fork);
fn test_genesis(epoch: Epoch) {
let mut spec = ChainSpec::foundation();
let mut spec = ChainSpec::mainnet();
let fork = Fork::genesis(epoch);

View File

@@ -31,7 +31,7 @@ pub struct HistoricalBatch<T: EthSpec> {
mod tests {
use super::*;
pub type FoundationHistoricalBatch = HistoricalBatch<FoundationEthSpec>;
pub type FoundationHistoricalBatch = HistoricalBatch<MainnetEthSpec>;
ssz_tests!(FoundationHistoricalBatch);
cached_tree_hash_tests!(FoundationHistoricalBatch);