Rename BeaconStateTypes to EthSpec

This commit is contained in:
Paul Hauner
2019-05-10 14:47:09 +10:00
parent 75b310a078
commit ce8ebeccbc
60 changed files with 223 additions and 235 deletions

View File

@@ -67,7 +67,7 @@ pub enum Error {
)]
pub struct BeaconState<T>
where
T: BeaconStateTypes,
T: EthSpec,
{
// Misc
pub slot: Slot,
@@ -140,7 +140,7 @@ where
pub tree_hash_cache: TreeHashCache,
}
impl<T: BeaconStateTypes> BeaconState<T> {
impl<T: EthSpec> BeaconState<T> {
/// Produce the first state of the Beacon Chain.
///
/// This does not fully build a genesis beacon state, it omits processing of initial validator

View File

@@ -3,7 +3,7 @@ use fixed_len_vec::typenum::{Unsigned, U1024, U8, U8192};
use serde_derive::{Deserialize, Serialize};
use std::fmt::Debug;
pub trait BeaconStateTypes:
pub trait EthSpec:
'static + Default + Sync + Send + Clone + Debug + PartialEq + serde::de::DeserializeOwned
{
type ShardCount: Unsigned + Clone + Sync + Send + Debug + PartialEq;
@@ -54,9 +54,9 @@ pub trait BeaconStateTypes:
///
/// Spec v0.5.1
#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
pub struct FoundationStateTypes;
pub struct FoundationEthSpec;
impl BeaconStateTypes for FoundationStateTypes {
impl EthSpec for FoundationEthSpec {
type ShardCount = U1024;
type SlotsPerHistoricalRoot = U8192;
type LatestRandaoMixesLength = U8192;
@@ -68,15 +68,15 @@ impl BeaconStateTypes for FoundationStateTypes {
}
}
pub type FoundationBeaconState = BeaconState<FoundationStateTypes>;
pub type FoundationBeaconState = BeaconState<FoundationEthSpec>;
/// Ethereum Foundation specifications, modified to be suitable for < 1000 validators.
///
/// Spec v0.5.1
#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
pub struct FewValidatorsStateTypes;
pub struct FewValidatorsEthSpec;
impl BeaconStateTypes for FewValidatorsStateTypes {
impl EthSpec for FewValidatorsEthSpec {
type ShardCount = U8;
type SlotsPerHistoricalRoot = U8192;
type LatestRandaoMixesLength = U8192;
@@ -88,15 +88,15 @@ impl BeaconStateTypes for FewValidatorsStateTypes {
}
}
pub type FewValidatorsBeaconState = BeaconState<FewValidatorsStateTypes>;
pub type FewValidatorsBeaconState = BeaconState<FewValidatorsEthSpec>;
/// Specifications suitable for a small-scale (< 1000 validators) lighthouse testnet.
///
/// Spec v0.5.1
#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
pub struct LighthouseTestnetStateTypes;
pub struct LighthouseTestnetEthSpec;
impl BeaconStateTypes for LighthouseTestnetStateTypes {
impl EthSpec for LighthouseTestnetEthSpec {
type ShardCount = U8;
type SlotsPerHistoricalRoot = U8192;
type LatestRandaoMixesLength = U8192;
@@ -108,4 +108,4 @@ impl BeaconStateTypes for LighthouseTestnetStateTypes {
}
}
pub type LighthouseTestnetBeaconState = BeaconState<LighthouseTestnetStateTypes>;
pub type LighthouseTestnetBeaconState = BeaconState<LighthouseTestnetEthSpec>;

View File

@@ -28,7 +28,7 @@ pub struct EpochCache {
impl EpochCache {
/// Return a new, fully initialized cache.
pub fn initialized<T: BeaconStateTypes>(
pub fn initialized<T: EthSpec>(
state: &BeaconState<T>,
relative_epoch: RelativeEpoch,
spec: &ChainSpec,
@@ -200,7 +200,7 @@ pub struct EpochCrosslinkCommitteesBuilder {
impl EpochCrosslinkCommitteesBuilder {
/// Instantiates a builder that will build for the `state`'s previous epoch.
pub fn for_previous_epoch<T: BeaconStateTypes>(
pub fn for_previous_epoch<T: EthSpec>(
state: &BeaconState<T>,
active_validator_indices: Vec<usize>,
spec: &ChainSpec,
@@ -215,7 +215,7 @@ impl EpochCrosslinkCommitteesBuilder {
}
/// Instantiates a builder that will build for the `state`'s next epoch.
pub fn for_current_epoch<T: BeaconStateTypes>(
pub fn for_current_epoch<T: EthSpec>(
state: &BeaconState<T>,
active_validator_indices: Vec<usize>,
spec: &ChainSpec,
@@ -233,7 +233,7 @@ impl EpochCrosslinkCommitteesBuilder {
///
/// Note: there are two possible epoch builds for the next epoch, one where there is a registry
/// change and one where there is not.
pub fn for_next_epoch<T: BeaconStateTypes>(
pub fn for_next_epoch<T: EthSpec>(
state: &BeaconState<T>,
active_validator_indices: Vec<usize>,
registry_change: bool,

View File

@@ -1,11 +1,11 @@
#![cfg(test)]
use super::*;
use crate::beacon_state::FewValidatorsStateTypes;
use crate::beacon_state::FewValidatorsEthSpec;
use crate::test_utils::*;
use swap_or_not_shuffle::shuffle_list;
fn do_sane_cache_test<T: BeaconStateTypes>(
fn do_sane_cache_test<T: EthSpec>(
state: BeaconState<T>,
epoch: Epoch,
relative_epoch: RelativeEpoch,
@@ -65,10 +65,7 @@ fn do_sane_cache_test<T: BeaconStateTypes>(
}
}
fn setup_sane_cache_test<T: BeaconStateTypes>(
validator_count: usize,
spec: &ChainSpec,
) -> BeaconState<T> {
fn setup_sane_cache_test<T: EthSpec>(validator_count: usize, spec: &ChainSpec) -> BeaconState<T> {
let mut builder =
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(validator_count, spec);
@@ -102,11 +99,11 @@ fn setup_sane_cache_test<T: BeaconStateTypes>(
#[test]
fn builds_sane_current_epoch_cache() {
let mut spec = FewValidatorsStateTypes::spec();
let mut spec = FewValidatorsEthSpec::spec();
spec.shard_count = 4;
let validator_count = (spec.shard_count * spec.target_committee_size) + 1;
let state: BeaconState<FewValidatorsStateTypes> =
let state: BeaconState<FewValidatorsEthSpec> =
setup_sane_cache_test(validator_count as usize, &spec);
do_sane_cache_test(
@@ -122,11 +119,11 @@ fn builds_sane_current_epoch_cache() {
#[test]
fn builds_sane_previous_epoch_cache() {
let mut spec = FewValidatorsStateTypes::spec();
let mut spec = FewValidatorsEthSpec::spec();
spec.shard_count = 2;
let validator_count = (spec.shard_count * spec.target_committee_size) + 1;
let state: BeaconState<FewValidatorsStateTypes> =
let state: BeaconState<FewValidatorsEthSpec> =
setup_sane_cache_test(validator_count as usize, &spec);
do_sane_cache_test(
@@ -142,11 +139,11 @@ fn builds_sane_previous_epoch_cache() {
#[test]
fn builds_sane_next_without_update_epoch_cache() {
let mut spec = FewValidatorsStateTypes::spec();
let mut spec = FewValidatorsEthSpec::spec();
spec.shard_count = 2;
let validator_count = (spec.shard_count * spec.target_committee_size) + 1;
let mut state: BeaconState<FewValidatorsStateTypes> =
let mut state: BeaconState<FewValidatorsEthSpec> =
setup_sane_cache_test(validator_count as usize, &spec);
state.validator_registry_update_epoch = state.slot.epoch(spec.slots_per_epoch);

View File

@@ -1,6 +1,6 @@
#![cfg(test)]
use super::*;
use crate::beacon_state::FewValidatorsStateTypes;
use crate::beacon_state::FewValidatorsEthSpec;
use crate::test_utils::*;
ssz_tests!(FoundationBeaconState);
@@ -11,7 +11,7 @@ cached_tree_hash_tests!(FoundationBeaconState);
/// 1. Using the cache before it's built fails.
/// 2. Using the cache after it's build passes.
/// 3. Using the cache after it's dropped fails.
fn test_cache_initialization<'a, T: BeaconStateTypes>(
fn test_cache_initialization<'a, T: EthSpec>(
state: &'a mut BeaconState<T>,
relative_epoch: RelativeEpoch,
spec: &ChainSpec,
@@ -46,9 +46,9 @@ fn test_cache_initialization<'a, T: BeaconStateTypes>(
#[test]
fn cache_initialization() {
let spec = FewValidatorsStateTypes::spec();
let spec = FewValidatorsEthSpec::spec();
let builder: TestingBeaconStateBuilder<FewValidatorsStateTypes> =
let builder: TestingBeaconStateBuilder<FewValidatorsEthSpec> =
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(16, &spec);
let (mut state, _keypairs) = builder.build();

View File

@@ -22,7 +22,7 @@ use tree_hash_derive::{CachedTreeHash, TreeHash};
CachedTreeHash,
TestRandom,
)]
pub struct HistoricalBatch<T: BeaconStateTypes> {
pub struct HistoricalBatch<T: EthSpec> {
pub block_roots: FixedLenVec<Hash256, T::SlotsPerHistoricalRoot>,
pub state_roots: FixedLenVec<Hash256, T::SlotsPerHistoricalRoot>,
}
@@ -31,7 +31,7 @@ pub struct HistoricalBatch<T: BeaconStateTypes> {
mod tests {
use super::*;
pub type FoundationHistoricalBatch = HistoricalBatch<FoundationStateTypes>;
pub type FoundationHistoricalBatch = HistoricalBatch<FoundationEthSpec>;
ssz_tests!(FoundationHistoricalBatch);
cached_tree_hash_tests!(FoundationHistoricalBatch);

View File

@@ -12,7 +12,7 @@ pub struct TestingAttestationBuilder {
impl TestingAttestationBuilder {
/// Create a new attestation builder.
pub fn new<T: BeaconStateTypes>(
pub fn new<T: EthSpec>(
state: &BeaconState<T>,
committee: &[usize],
slot: Slot,

View File

@@ -10,7 +10,7 @@ pub struct TestingAttestationDataBuilder {
impl TestingAttestationDataBuilder {
/// Configures a new `AttestationData` which attests to all of the same parameters as the
/// state.
pub fn new<T: BeaconStateTypes>(
pub fn new<T: EthSpec>(
state: &BeaconState<T>,
shard: u64,
slot: Slot,

View File

@@ -82,7 +82,7 @@ impl TestingBeaconBlockBuilder {
///
/// Note: the signed messages of the split committees will be identical -- it would be possible
/// to aggregate these split attestations.
pub fn insert_attestations<T: BeaconStateTypes>(
pub fn insert_attestations<T: EthSpec>(
&mut self,
state: &BeaconState<T>,
secret_keys: &[&SecretKey],
@@ -171,7 +171,7 @@ impl TestingBeaconBlockBuilder {
}
/// Insert a `Valid` deposit into the state.
pub fn insert_deposit<T: BeaconStateTypes>(
pub fn insert_deposit<T: EthSpec>(
&mut self,
amount: u64,
index: u64,
@@ -193,7 +193,7 @@ impl TestingBeaconBlockBuilder {
}
/// Insert a `Valid` exit into the state.
pub fn insert_exit<T: BeaconStateTypes>(
pub fn insert_exit<T: EthSpec>(
&mut self,
state: &BeaconState<T>,
validator_index: u64,
@@ -214,7 +214,7 @@ impl TestingBeaconBlockBuilder {
///
/// Note: this will set the validator to be withdrawable by directly modifying the state
/// validator registry. This _may_ cause problems historic hashes, etc.
pub fn insert_transfer<T: BeaconStateTypes>(
pub fn insert_transfer<T: EthSpec>(
&mut self,
state: &BeaconState<T>,
from: u64,

View File

@@ -25,12 +25,12 @@ pub fn keypairs_path() -> PathBuf {
///
/// This struct should **never be used for production purposes.**
#[derive(Clone)]
pub struct TestingBeaconStateBuilder<T: BeaconStateTypes> {
pub struct TestingBeaconStateBuilder<T: EthSpec> {
state: BeaconState<T>,
keypairs: Vec<Keypair>,
}
impl<T: BeaconStateTypes> TestingBeaconStateBuilder<T> {
impl<T: EthSpec> TestingBeaconStateBuilder<T> {
/// Attempts to load validators from a file in `$HOME/.lighthouse/keypairs.raw_keypairs`. If
/// the file is unavailable, it generates the keys at runtime.
///

View File

@@ -16,7 +16,7 @@ impl TestingPendingAttestationBuilder {
///
/// * The aggregation and custody bitfields will all be empty, they need to be set with
/// `Self::add_committee_participation`.
pub fn new<T: BeaconStateTypes>(
pub fn new<T: EthSpec>(
state: &BeaconState<T>,
shard: u64,
slot: Slot,