Merge pull request #234 from sigp/test-harness-fix

Fix test_harness, increase speed
This commit is contained in:
Age Manning 2019-02-18 10:10:01 +11:00 committed by GitHub
commit a4beb81b83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 400 additions and 145 deletions

View File

@ -1,3 +1,4 @@
use crate::cached_beacon_state::CachedBeaconState;
use state_processing::validate_attestation_without_signature; use state_processing::validate_attestation_without_signature;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use types::{ use types::{
@ -76,12 +77,12 @@ impl AttestationAggregator {
/// - The signature is verified against that of the validator at `validator_index`. /// - The signature is verified against that of the validator at `validator_index`.
pub fn process_free_attestation( pub fn process_free_attestation(
&mut self, &mut self,
state: &BeaconState, cached_state: &CachedBeaconState,
free_attestation: &FreeAttestation, free_attestation: &FreeAttestation,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<Outcome, BeaconStateError> { ) -> Result<Outcome, BeaconStateError> {
let (slot, shard, committee_index) = some_or_invalid!( let (slot, shard, committee_index) = some_or_invalid!(
state.attestation_slot_and_shard_for_validator( cached_state.attestation_slot_and_shard_for_validator(
free_attestation.validator_index as usize, free_attestation.validator_index as usize,
spec, spec,
)?, )?,
@ -104,7 +105,8 @@ impl AttestationAggregator {
let signable_message = free_attestation.data.signable_message(PHASE_0_CUSTODY_BIT); let signable_message = free_attestation.data.signable_message(PHASE_0_CUSTODY_BIT);
let validator_record = some_or_invalid!( let validator_record = some_or_invalid!(
state cached_state
.state
.validator_registry .validator_registry
.get(free_attestation.validator_index as usize), .get(free_attestation.validator_index as usize),
Message::BadValidatorIndex Message::BadValidatorIndex

View File

@ -1,4 +1,5 @@
use crate::attestation_aggregator::{AttestationAggregator, Outcome as AggregationOutcome}; use crate::attestation_aggregator::{AttestationAggregator, Outcome as AggregationOutcome};
use crate::cached_beacon_state::CachedBeaconState;
use crate::checkpoint::CheckPoint; use crate::checkpoint::CheckPoint;
use db::{ use db::{
stores::{BeaconBlockStore, BeaconStateStore}, stores::{BeaconBlockStore, BeaconStateStore},
@ -69,6 +70,7 @@ pub struct BeaconChain<T: ClientDB + Sized, U: SlotClock, F: ForkChoice> {
canonical_head: RwLock<CheckPoint>, canonical_head: RwLock<CheckPoint>,
finalized_head: RwLock<CheckPoint>, finalized_head: RwLock<CheckPoint>,
pub state: RwLock<BeaconState>, pub state: RwLock<BeaconState>,
pub cached_state: RwLock<CachedBeaconState>,
pub spec: ChainSpec, pub spec: ChainSpec,
pub fork_choice: RwLock<F>, pub fork_choice: RwLock<F>,
} }
@ -107,6 +109,11 @@ where
let block_root = genesis_block.canonical_root(); let block_root = genesis_block.canonical_root();
block_store.put(&block_root, &ssz_encode(&genesis_block)[..])?; block_store.put(&block_root, &ssz_encode(&genesis_block)[..])?;
let cached_state = RwLock::new(CachedBeaconState::from_beacon_state(
genesis_state.clone(),
spec.clone(),
)?);
let finalized_head = RwLock::new(CheckPoint::new( let finalized_head = RwLock::new(CheckPoint::new(
genesis_block.clone(), genesis_block.clone(),
block_root, block_root,
@ -127,6 +134,7 @@ where
slot_clock, slot_clock,
attestation_aggregator, attestation_aggregator,
state: RwLock::new(genesis_state.clone()), state: RwLock::new(genesis_state.clone()),
cached_state,
finalized_head, finalized_head,
canonical_head, canonical_head,
spec, spec,
@ -253,6 +261,7 @@ where
/// Information is read from the present `beacon_state` shuffling, so only information from the /// Information is read from the present `beacon_state` shuffling, so only information from the
/// present and prior epoch is available. /// present and prior epoch is available.
pub fn block_proposer(&self, slot: Slot) -> Result<usize, BeaconStateError> { pub fn block_proposer(&self, slot: Slot) -> Result<usize, BeaconStateError> {
trace!("BeaconChain::block_proposer: slot: {}", slot);
let index = self let index = self
.state .state
.read() .read()
@ -274,8 +283,12 @@ where
&self, &self,
validator_index: usize, validator_index: usize,
) -> Result<Option<(Slot, u64)>, BeaconStateError> { ) -> Result<Option<(Slot, u64)>, BeaconStateError> {
trace!(
"BeaconChain::validator_attestion_slot_and_shard: validator_index: {}",
validator_index
);
if let Some((slot, shard, _committee)) = self if let Some((slot, shard, _committee)) = self
.state .cached_state
.read() .read()
.attestation_slot_and_shard_for_validator(validator_index, &self.spec)? .attestation_slot_and_shard_for_validator(validator_index, &self.spec)?
{ {
@ -287,6 +300,7 @@ where
/// Produce an `AttestationData` that is valid for the present `slot` and given `shard`. /// Produce an `AttestationData` that is valid for the present `slot` and given `shard`.
pub fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, Error> { pub fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, Error> {
trace!("BeaconChain::produce_attestation_data: shard: {}", shard);
let justified_epoch = self.justified_epoch(); let justified_epoch = self.justified_epoch();
let justified_block_root = *self let justified_block_root = *self
.state .state
@ -332,9 +346,7 @@ where
let aggregation_outcome = self let aggregation_outcome = self
.attestation_aggregator .attestation_aggregator
.write() .write()
.process_free_attestation(&self.state.read(), &free_attestation, &self.spec)?; .process_free_attestation(&self.cached_state.read(), &free_attestation, &self.spec)?;
// TODO: Check this comment
//.map_err(|e| e.into())?;
// return if the attestation is invalid // return if the attestation is invalid
if !aggregation_outcome.valid { if !aggregation_outcome.valid {
@ -489,6 +501,9 @@ where
); );
// Update the local state variable. // Update the local state variable.
*self.state.write() = state.clone(); *self.state.write() = state.clone();
// Update the cached state variable.
*self.cached_state.write() =
CachedBeaconState::from_beacon_state(state.clone(), self.spec.clone())?;
} }
Ok(BlockProcessingOutcome::ValidBlock(ValidBlock::Processed)) Ok(BlockProcessingOutcome::ValidBlock(ValidBlock::Processed))
@ -537,9 +552,15 @@ where
}, },
}; };
state trace!("BeaconChain::produce_block: updating state for new block.",);
.per_block_processing_without_verifying_block_signature(&block, &self.spec)
.ok()?; let result =
state.per_block_processing_without_verifying_block_signature(&block, &self.spec);
trace!(
"BeaconNode::produce_block: state processing result: {:?}",
result
);
result.ok()?;
let state_root = state.canonical_root(); let state_root = state.canonical_root();

View File

@ -0,0 +1,150 @@
use log::{debug, trace};
use std::collections::HashMap;
use types::{beacon_state::BeaconStateError, BeaconState, ChainSpec, Epoch, Slot};
pub const CACHE_PREVIOUS: bool = false;
pub const CACHE_CURRENT: bool = true;
pub const CACHE_NEXT: bool = false;
pub type CrosslinkCommittees = Vec<(Vec<usize>, u64)>;
pub type Shard = u64;
pub type CommitteeIndex = u64;
pub type AttestationDuty = (Slot, Shard, CommitteeIndex);
pub type AttestationDutyMap = HashMap<u64, AttestationDuty>;
// TODO: CachedBeaconState is presently duplicating `BeaconState` and `ChainSpec`. This is a
// massive memory waste, switch them to references.
pub struct CachedBeaconState {
pub state: BeaconState,
committees: Vec<Vec<CrosslinkCommittees>>,
attestation_duties: Vec<AttestationDutyMap>,
next_epoch: Epoch,
current_epoch: Epoch,
previous_epoch: Epoch,
spec: ChainSpec,
}
impl CachedBeaconState {
pub fn from_beacon_state(
state: BeaconState,
spec: ChainSpec,
) -> Result<Self, BeaconStateError> {
let current_epoch = state.current_epoch(&spec);
let previous_epoch = if current_epoch == spec.genesis_epoch {
current_epoch
} else {
current_epoch.saturating_sub(1_u64)
};
let next_epoch = state.next_epoch(&spec);
let mut committees: Vec<Vec<CrosslinkCommittees>> = Vec::with_capacity(3);
let mut attestation_duties: Vec<AttestationDutyMap> = Vec::with_capacity(3);
if CACHE_PREVIOUS {
debug!("from_beacon_state: building previous epoch cache.");
let cache = build_epoch_cache(&state, previous_epoch, &spec)?;
committees.push(cache.committees);
attestation_duties.push(cache.attestation_duty_map);
} else {
committees.push(vec![]);
attestation_duties.push(HashMap::new());
}
if CACHE_CURRENT {
debug!("from_beacon_state: building current epoch cache.");
let cache = build_epoch_cache(&state, current_epoch, &spec)?;
committees.push(cache.committees);
attestation_duties.push(cache.attestation_duty_map);
} else {
committees.push(vec![]);
attestation_duties.push(HashMap::new());
}
if CACHE_NEXT {
debug!("from_beacon_state: building next epoch cache.");
let cache = build_epoch_cache(&state, next_epoch, &spec)?;
committees.push(cache.committees);
attestation_duties.push(cache.attestation_duty_map);
} else {
committees.push(vec![]);
attestation_duties.push(HashMap::new());
}
Ok(Self {
state,
committees,
attestation_duties,
next_epoch,
current_epoch,
previous_epoch,
spec,
})
}
fn slot_to_cache_index(&self, slot: Slot) -> Option<usize> {
trace!("slot_to_cache_index: cache lookup");
match slot.epoch(self.spec.epoch_length) {
epoch if (epoch == self.previous_epoch) & CACHE_PREVIOUS => Some(0),
epoch if (epoch == self.current_epoch) & CACHE_CURRENT => Some(1),
epoch if (epoch == self.next_epoch) & CACHE_NEXT => Some(2),
_ => None,
}
}
/// Returns the `slot`, `shard` and `committee_index` for which a validator must produce an
/// attestation.
///
/// Cached method.
///
/// Spec v0.2.0
pub fn attestation_slot_and_shard_for_validator(
&self,
validator_index: usize,
_spec: &ChainSpec,
) -> Result<Option<(Slot, u64, u64)>, BeaconStateError> {
// Get the result for this epoch.
let cache_index = self
.slot_to_cache_index(self.state.slot)
.expect("Current epoch should always have a cache index.");
let duties = self.attestation_duties[cache_index]
.get(&(validator_index as u64))
.and_then(|tuple| Some(*tuple));
Ok(duties)
}
}
struct EpochCacheResult {
committees: Vec<CrosslinkCommittees>,
attestation_duty_map: AttestationDutyMap,
}
fn build_epoch_cache(
state: &BeaconState,
epoch: Epoch,
spec: &ChainSpec,
) -> Result<EpochCacheResult, BeaconStateError> {
let mut epoch_committees: Vec<CrosslinkCommittees> =
Vec::with_capacity(spec.epoch_length as usize);
let mut attestation_duty_map: AttestationDutyMap = HashMap::new();
for slot in epoch.slot_iter(spec.epoch_length) {
let slot_committees = state.get_crosslink_committees_at_slot(slot, false, spec)?;
for (committee, shard) in slot_committees {
for (committee_index, validator_index) in committee.iter().enumerate() {
attestation_duty_map.insert(
*validator_index as u64,
(slot, shard, committee_index as u64),
);
}
}
epoch_committees.push(state.get_crosslink_committees_at_slot(slot, false, spec)?)
}
Ok(EpochCacheResult {
committees: epoch_committees,
attestation_duty_map,
})
}

View File

@ -1,5 +1,6 @@
mod attestation_aggregator; mod attestation_aggregator;
mod beacon_chain; mod beacon_chain;
mod cached_beacon_state;
mod checkpoint; mod checkpoint;
pub use self::beacon_chain::{BeaconChain, Error}; pub use self::beacon_chain::{BeaconChain, Error};

View File

@ -128,7 +128,18 @@ impl BeaconChainHarness {
pub fn increment_beacon_chain_slot(&mut self) -> Slot { pub fn increment_beacon_chain_slot(&mut self) -> Slot {
let slot = self.beacon_chain.present_slot() + 1; let slot = self.beacon_chain.present_slot() + 1;
debug!("Incrementing BeaconChain slot to {}.", slot); let nth_slot = slot
- slot
.epoch(self.spec.epoch_length)
.start_slot(self.spec.epoch_length);
let nth_epoch = slot.epoch(self.spec.epoch_length) - self.spec.genesis_epoch;
debug!(
"Advancing BeaconChain to slot {}, epoch {} (epoch height: {}, slot {} in epoch.).",
slot,
slot.epoch(self.spec.epoch_length),
nth_epoch,
nth_slot
);
self.beacon_chain.slot_clock.set_slot(slot.as_u64()); self.beacon_chain.slot_clock.set_slot(slot.as_u64());
self.beacon_chain.advance_state(slot).unwrap(); self.beacon_chain.advance_state(slot).unwrap();
@ -209,6 +220,7 @@ impl BeaconChainHarness {
self.increment_beacon_chain_slot(); self.increment_beacon_chain_slot();
// Produce a new block. // Produce a new block.
debug!("Producing block...");
let block = self.produce_block(); let block = self.produce_block();
debug!("Submitting block for processing..."); debug!("Submitting block for processing...");
self.beacon_chain.process_block(block).unwrap(); self.beacon_chain.process_block(block).unwrap();

View File

@ -1,19 +1,14 @@
use env_logger::{Builder, Env}; use env_logger::{Builder, Env};
use log::debug; use log::debug;
use test_harness::BeaconChainHarness; use test_harness::BeaconChainHarness;
use types::{ChainSpec, Slot}; use types::ChainSpec;
#[test] #[test]
#[ignore]
fn it_can_build_on_genesis_block() { fn it_can_build_on_genesis_block() {
let mut spec = ChainSpec::foundation(); Builder::from_env(Env::default().default_filter_or("info")).init();
spec.genesis_slot = Slot::new(spec.epoch_length * 8);
/* let spec = ChainSpec::few_validators();
spec.shard_count = spec.shard_count / 8; let validator_count = 8;
spec.target_committee_size = spec.target_committee_size / 8;
*/
let validator_count = 1000;
let mut harness = BeaconChainHarness::new(spec, validator_count as usize); let mut harness = BeaconChainHarness::new(spec, validator_count as usize);
@ -23,21 +18,22 @@ fn it_can_build_on_genesis_block() {
#[test] #[test]
#[ignore] #[ignore]
fn it_can_produce_past_first_epoch_boundary() { fn it_can_produce_past_first_epoch_boundary() {
Builder::from_env(Env::default().default_filter_or("debug")).init(); Builder::from_env(Env::default().default_filter_or("info")).init();
let validator_count = 100; let spec = ChainSpec::few_validators();
let validator_count = 8;
debug!("Starting harness build..."); debug!("Starting harness build...");
let mut harness = BeaconChainHarness::new(ChainSpec::foundation(), validator_count); let mut harness = BeaconChainHarness::new(spec, validator_count);
debug!("Harness built, tests starting.."); debug!("Harness built, tests starting..");
let blocks = harness.spec.epoch_length * 3 + 1; let blocks = harness.spec.epoch_length * 2 + 1;
for i in 0..blocks { for i in 0..blocks {
harness.advance_chain_with_block(); harness.advance_chain_with_block();
debug!("Produced block {}/{}.", i, blocks); debug!("Produced block {}/{}.", i + 1, blocks);
} }
let dump = harness.chain_dump().expect("Chain dump failed."); let dump = harness.chain_dump().expect("Chain dump failed.");

View File

@ -1,7 +1,7 @@
use crate::SlotProcessingError; use crate::SlotProcessingError;
use hashing::hash; use hashing::hash;
use int_to_bytes::int_to_bytes32; use int_to_bytes::int_to_bytes32;
use log::debug; use log::{debug, trace};
use ssz::{ssz_encode, TreeHash}; use ssz::{ssz_encode, TreeHash};
use types::{ use types::{
beacon_state::{AttestationParticipantsError, BeaconStateError}, beacon_state::{AttestationParticipantsError, BeaconStateError},
@ -219,6 +219,8 @@ fn per_block_processing_signature_optional(
Error::MaxAttestationsExceeded Error::MaxAttestationsExceeded
); );
debug!("Verifying {} attestations.", block.body.attestations.len());
for attestation in &block.body.attestations { for attestation in &block.body.attestations {
validate_attestation(&state, attestation, spec)?; validate_attestation(&state, attestation, spec)?;
@ -231,11 +233,6 @@ fn per_block_processing_signature_optional(
state.latest_attestations.push(pending_attestation); state.latest_attestations.push(pending_attestation);
} }
debug!(
"{} attestations verified & processed.",
block.body.attestations.len()
);
/* /*
* Deposits * Deposits
*/ */
@ -312,6 +309,10 @@ fn validate_attestation_signature_optional(
spec: &ChainSpec, spec: &ChainSpec,
verify_signature: bool, verify_signature: bool,
) -> Result<(), AttestationValidationError> { ) -> Result<(), AttestationValidationError> {
trace!(
"validate_attestation_signature_optional: attestation epoch: {}",
attestation.data.slot.epoch(spec.epoch_length)
);
ensure!( ensure!(
attestation.data.slot + spec.min_attestation_inclusion_delay <= state.slot, attestation.data.slot + spec.min_attestation_inclusion_delay <= state.slot,
AttestationValidationError::IncludedTooEarly AttestationValidationError::IncludedTooEarly

View File

@ -144,8 +144,10 @@ impl EpochProcessable for BeaconState {
let previous_epoch_attester_indices = let previous_epoch_attester_indices =
self.get_attestation_participants_union(&previous_epoch_attestations[..], spec)?; self.get_attestation_participants_union(&previous_epoch_attestations[..], spec)?;
let previous_total_balance = let previous_total_balance = self.get_total_balance(
self.get_total_balance(&previous_epoch_attester_indices[..], spec); &get_active_validator_indices(&self.validator_registry, previous_epoch),
spec,
);
/* /*
* Validators targetting the previous justified slot * Validators targetting the previous justified slot
@ -315,6 +317,11 @@ impl EpochProcessable for BeaconState {
// for slot in self.slot.saturating_sub(2 * spec.epoch_length)..self.slot { // for slot in self.slot.saturating_sub(2 * spec.epoch_length)..self.slot {
for slot in self.previous_epoch(spec).slot_iter(spec.epoch_length) { for slot in self.previous_epoch(spec).slot_iter(spec.epoch_length) {
trace!(
"Finding winning root for slot: {} (epoch: {})",
slot,
slot.epoch(spec.epoch_length)
);
let crosslink_committees_at_slot = let crosslink_committees_at_slot =
self.get_crosslink_committees_at_slot(slot, false, spec)?; self.get_crosslink_committees_at_slot(slot, false, spec)?;
@ -352,7 +359,8 @@ impl EpochProcessable for BeaconState {
/* /*
* Rewards and Penalities * Rewards and Penalities
*/ */
let base_reward_quotient = previous_total_balance.integer_sqrt(); let base_reward_quotient =
previous_total_balance.integer_sqrt() / spec.base_reward_quotient;
if base_reward_quotient == 0 { if base_reward_quotient == 0 {
return Err(Error::BaseRewardQuotientIsZero); return Err(Error::BaseRewardQuotientIsZero);
} }
@ -539,6 +547,12 @@ impl EpochProcessable for BeaconState {
*/ */
self.previous_calculation_epoch = self.current_calculation_epoch; self.previous_calculation_epoch = self.current_calculation_epoch;
self.previous_epoch_start_shard = self.current_epoch_start_shard; self.previous_epoch_start_shard = self.current_epoch_start_shard;
debug!(
"setting previous_epoch_seed to : {}",
self.current_epoch_seed
);
self.previous_epoch_seed = self.current_epoch_seed; self.previous_epoch_seed = self.current_epoch_seed;
let should_update_validator_registy = if self.finalized_epoch let should_update_validator_registy = if self.finalized_epoch
@ -553,6 +567,7 @@ impl EpochProcessable for BeaconState {
}; };
if should_update_validator_registy { if should_update_validator_registy {
trace!("updating validator registry.");
self.update_validator_registry(spec); self.update_validator_registry(spec);
self.current_calculation_epoch = next_epoch; self.current_calculation_epoch = next_epoch;
@ -561,6 +576,7 @@ impl EpochProcessable for BeaconState {
% spec.shard_count; % spec.shard_count;
self.current_epoch_seed = self.generate_seed(self.current_calculation_epoch, spec)? self.current_epoch_seed = self.generate_seed(self.current_calculation_epoch, spec)?
} else { } else {
trace!("not updating validator registry.");
let epochs_since_last_registry_update = let epochs_since_last_registry_update =
current_epoch - self.validator_registry_update_epoch; current_epoch - self.validator_registry_update_epoch;
if (epochs_since_last_registry_update > 1) if (epochs_since_last_registry_update > 1)

View File

@ -6,6 +6,7 @@ use crate::{
}; };
use bls::verify_proof_of_possession; use bls::verify_proof_of_possession;
use honey_badger_split::SplitExt; use honey_badger_split::SplitExt;
use log::trace;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
@ -204,7 +205,12 @@ impl BeaconState {
/// ///
/// Spec v0.2.0 /// Spec v0.2.0
pub fn previous_epoch(&self, spec: &ChainSpec) -> Epoch { pub fn previous_epoch(&self, spec: &ChainSpec) -> Epoch {
self.current_epoch(spec).saturating_sub(1_u64) let current_epoch = self.current_epoch(&spec);
if current_epoch == spec.genesis_epoch {
current_epoch
} else {
current_epoch - 1
}
} }
/// The epoch following `self.current_epoch()`. /// The epoch following `self.current_epoch()`.
@ -261,11 +267,25 @@ impl BeaconState {
let active_validator_indices = let active_validator_indices =
get_active_validator_indices(&self.validator_registry, epoch); get_active_validator_indices(&self.validator_registry, epoch);
if active_validator_indices.is_empty() {
return None;
}
trace!(
"get_shuffling: active_validator_indices.len() == {}",
active_validator_indices.len()
);
let committees_per_epoch = let committees_per_epoch =
self.get_epoch_committee_count(active_validator_indices.len(), spec); self.get_epoch_committee_count(active_validator_indices.len(), spec);
let mut shuffled_active_validator_indices = trace!(
Vec::with_capacity(active_validator_indices.len()); "get_shuffling: active_validator_indices.len() == {}, committees_per_epoch: {}",
active_validator_indices.len(),
committees_per_epoch
);
let mut shuffled_active_validator_indices = vec![0; active_validator_indices.len()];
for &i in &active_validator_indices { for &i in &active_validator_indices {
let shuffled_i = get_permutated_index( let shuffled_i = get_permutated_index(
i, i,
@ -319,9 +339,17 @@ impl BeaconState {
+ 1; + 1;
let latest_index_root = current_epoch + spec.entry_exit_delay; let latest_index_root = current_epoch + spec.entry_exit_delay;
trace!(
"get_active_index_root: epoch: {}, earliest: {}, latest: {}",
epoch,
earliest_index_root,
latest_index_root
);
if (epoch >= earliest_index_root) & (epoch <= latest_index_root) { if (epoch >= earliest_index_root) & (epoch <= latest_index_root) {
Some(self.latest_index_roots[epoch.as_usize() % spec.latest_index_roots_length]) Some(self.latest_index_roots[epoch.as_usize() % spec.latest_index_roots_length])
} else { } else {
trace!("get_active_index_root: epoch out of range.");
None None
} }
} }
@ -366,29 +394,28 @@ impl BeaconState {
) -> Result<Vec<(Vec<usize>, u64)>, BeaconStateError> { ) -> Result<Vec<(Vec<usize>, u64)>, BeaconStateError> {
let epoch = slot.epoch(spec.epoch_length); let epoch = slot.epoch(spec.epoch_length);
let current_epoch = self.current_epoch(spec); let current_epoch = self.current_epoch(spec);
let previous_epoch = if current_epoch == spec.genesis_epoch { let previous_epoch = self.previous_epoch(spec);
current_epoch
} else {
current_epoch.saturating_sub(1_u64)
};
let next_epoch = self.next_epoch(spec); let next_epoch = self.next_epoch(spec);
let (committees_per_epoch, seed, shuffling_epoch, shuffling_start_shard) = let (committees_per_epoch, seed, shuffling_epoch, shuffling_start_shard) =
if epoch == previous_epoch { if epoch == current_epoch {
( trace!("get_crosslink_committees_at_slot: current_epoch");
self.get_previous_epoch_committee_count(spec),
self.previous_epoch_seed,
self.previous_calculation_epoch,
self.previous_epoch_start_shard,
)
} else if epoch == current_epoch {
( (
self.get_current_epoch_committee_count(spec), self.get_current_epoch_committee_count(spec),
self.current_epoch_seed, self.current_epoch_seed,
self.current_calculation_epoch, self.current_calculation_epoch,
self.current_epoch_start_shard, self.current_epoch_start_shard,
) )
} else if epoch == previous_epoch {
trace!("get_crosslink_committees_at_slot: previous_epoch");
(
self.get_previous_epoch_committee_count(spec),
self.previous_epoch_seed,
self.previous_calculation_epoch,
self.previous_epoch_start_shard,
)
} else if epoch == next_epoch { } else if epoch == next_epoch {
trace!("get_crosslink_committees_at_slot: next_epoch");
let current_committees_per_epoch = self.get_current_epoch_committee_count(spec); let current_committees_per_epoch = self.get_current_epoch_committee_count(spec);
let epochs_since_last_registry_update = let epochs_since_last_registry_update =
current_epoch - self.validator_registry_update_epoch; current_epoch - self.validator_registry_update_epoch;
@ -425,6 +452,13 @@ impl BeaconState {
let slot_start_shard = let slot_start_shard =
(shuffling_start_shard + committees_per_slot * offset) % spec.shard_count; (shuffling_start_shard + committees_per_slot * offset) % spec.shard_count;
trace!(
"get_crosslink_committees_at_slot: committees_per_slot: {}, slot_start_shard: {}, seed: {}",
committees_per_slot,
slot_start_shard,
seed
);
let mut crosslinks_at_slot = vec![]; let mut crosslinks_at_slot = vec![];
for i in 0..committees_per_slot { for i in 0..committees_per_slot {
let tuple = ( let tuple = (
@ -476,6 +510,11 @@ impl BeaconState {
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<usize, BeaconStateError> { ) -> Result<usize, BeaconStateError> {
let committees = self.get_crosslink_committees_at_slot(slot, false, spec)?; let committees = self.get_crosslink_committees_at_slot(slot, false, spec)?;
trace!(
"get_beacon_proposer_index: slot: {}, committees_count: {}",
slot,
committees.len()
);
committees committees
.first() .first()
.ok_or(BeaconStateError::InsufficientValidators) .ok_or(BeaconStateError::InsufficientValidators)

View File

@ -1,7 +1,96 @@
use crate::{Address, ChainSpec, Epoch, Hash256, Signature, Slot}; use crate::{Address, Epoch, Hash256, Slot};
use bls::Signature;
const GWEI: u64 = 1_000_000_000; const GWEI: u64 = 1_000_000_000;
/// Holds all the "constants" for a BeaconChain.
///
/// Spec v0.2.0
#[derive(PartialEq, Debug, Clone)]
pub struct ChainSpec {
/*
* Misc
*/
pub shard_count: u64,
pub target_committee_size: u64,
pub max_balance_churn_quotient: u64,
pub beacon_chain_shard_number: u64,
pub max_indices_per_slashable_vote: u64,
pub max_withdrawals_per_epoch: u64,
pub shuffle_round_count: u8,
/*
* Deposit contract
*/
pub deposit_contract_address: Address,
pub deposit_contract_tree_depth: u64,
/*
* Gwei values
*/
pub min_deposit_amount: u64,
pub max_deposit_amount: u64,
pub fork_choice_balance_increment: u64,
pub ejection_balance: u64,
/*
* Initial Values
*/
pub genesis_fork_version: u64,
pub genesis_slot: Slot,
pub genesis_epoch: Epoch,
pub genesis_start_shard: u64,
pub far_future_epoch: Epoch,
pub zero_hash: Hash256,
pub empty_signature: Signature,
pub bls_withdrawal_prefix_byte: u8,
/*
* Time parameters
*/
pub slot_duration: u64,
pub min_attestation_inclusion_delay: u64,
pub epoch_length: u64,
pub seed_lookahead: Epoch,
pub entry_exit_delay: u64,
pub eth1_data_voting_period: u64,
pub min_validator_withdrawal_epochs: Epoch,
/*
* State list lengths
*/
pub latest_block_roots_length: usize,
pub latest_randao_mixes_length: usize,
pub latest_index_roots_length: usize,
pub latest_penalized_exit_length: usize,
/*
* Reward and penalty quotients
*/
pub base_reward_quotient: u64,
pub whistleblower_reward_quotient: u64,
pub includer_reward_quotient: u64,
pub inactivity_penalty_quotient: u64,
/*
* Max operations per block
*/
pub max_proposer_slashings: u64,
pub max_attester_slashings: u64,
pub max_attestations: u64,
pub max_deposits: u64,
pub max_exits: u64,
/*
* Signature domains
*/
pub domain_deposit: u64,
pub domain_attestation: u64,
pub domain_proposal: u64,
pub domain_exit: u64,
pub domain_randao: u64,
}
impl ChainSpec { impl ChainSpec {
/// Returns a `ChainSpec` compatible with the specification from Ethereum Foundation. /// Returns a `ChainSpec` compatible with the specification from Ethereum Foundation.
/// ///
@ -100,6 +189,26 @@ impl ChainSpec {
} }
} }
impl ChainSpec {
/// Returns a `ChainSpec` compatible with the specification suitable for 8 validators.
///
/// Spec v0.2.0
pub fn few_validators() -> Self {
let genesis_slot = Slot::new(2_u64.pow(19));
let epoch_length = 8;
let genesis_epoch = genesis_slot.epoch(epoch_length);
Self {
shard_count: 1,
target_committee_size: 1,
genesis_slot,
genesis_epoch,
epoch_length,
..ChainSpec::foundation()
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -8,6 +8,7 @@ pub mod beacon_block;
pub mod beacon_block_body; pub mod beacon_block_body;
pub mod beacon_state; pub mod beacon_state;
pub mod casper_slashing; pub mod casper_slashing;
pub mod chain_spec;
pub mod crosslink; pub mod crosslink;
pub mod deposit; pub mod deposit;
pub mod deposit_data; pub mod deposit_data;
@ -28,7 +29,6 @@ pub mod slashable_vote_data;
pub mod slot_epoch_macros; pub mod slot_epoch_macros;
pub mod slot_epoch; pub mod slot_epoch;
pub mod slot_height; pub mod slot_height;
pub mod spec;
pub mod validator; pub mod validator;
pub mod validator_registry; pub mod validator_registry;
pub mod validator_registry_delta_block; pub mod validator_registry_delta_block;
@ -44,6 +44,7 @@ pub use crate::beacon_block::BeaconBlock;
pub use crate::beacon_block_body::BeaconBlockBody; pub use crate::beacon_block_body::BeaconBlockBody;
pub use crate::beacon_state::BeaconState; pub use crate::beacon_state::BeaconState;
pub use crate::casper_slashing::CasperSlashing; pub use crate::casper_slashing::CasperSlashing;
pub use crate::chain_spec::ChainSpec;
pub use crate::crosslink::Crosslink; pub use crate::crosslink::Crosslink;
pub use crate::deposit::Deposit; pub use crate::deposit::Deposit;
pub use crate::deposit_data::DepositData; pub use crate::deposit_data::DepositData;
@ -60,7 +61,6 @@ pub use crate::slashable_attestation::SlashableAttestation;
pub use crate::slashable_vote_data::SlashableVoteData; pub use crate::slashable_vote_data::SlashableVoteData;
pub use crate::slot_epoch::{Epoch, Slot}; pub use crate::slot_epoch::{Epoch, Slot};
pub use crate::slot_height::SlotHeight; pub use crate::slot_height::SlotHeight;
pub use crate::spec::ChainSpec;
pub use crate::validator::{StatusFlags as ValidatorStatusFlags, Validator}; pub use crate::validator::{StatusFlags as ValidatorStatusFlags, Validator};
pub use crate::validator_registry_delta_block::ValidatorRegistryDeltaBlock; pub use crate::validator_registry_delta_block::ValidatorRegistryDeltaBlock;

View File

@ -1,92 +0,0 @@
mod foundation;
use crate::{Address, Epoch, Hash256, Slot};
use bls::Signature;
/// Holds all the "constants" for a BeaconChain.
///
/// Spec v0.2.0
#[derive(PartialEq, Debug, Clone)]
pub struct ChainSpec {
/*
* Misc
*/
pub shard_count: u64,
pub target_committee_size: u64,
pub max_balance_churn_quotient: u64,
pub beacon_chain_shard_number: u64,
pub max_indices_per_slashable_vote: u64,
pub max_withdrawals_per_epoch: u64,
pub shuffle_round_count: u8,
/*
* Deposit contract
*/
pub deposit_contract_address: Address,
pub deposit_contract_tree_depth: u64,
/*
* Gwei values
*/
pub min_deposit_amount: u64,
pub max_deposit_amount: u64,
pub fork_choice_balance_increment: u64,
pub ejection_balance: u64,
/*
* Initial Values
*/
pub genesis_fork_version: u64,
pub genesis_slot: Slot,
pub genesis_epoch: Epoch,
pub genesis_start_shard: u64,
pub far_future_epoch: Epoch,
pub zero_hash: Hash256,
pub empty_signature: Signature,
pub bls_withdrawal_prefix_byte: u8,
/*
* Time parameters
*/
pub slot_duration: u64,
pub min_attestation_inclusion_delay: u64,
pub epoch_length: u64,
pub seed_lookahead: Epoch,
pub entry_exit_delay: u64,
pub eth1_data_voting_period: u64,
pub min_validator_withdrawal_epochs: Epoch,
/*
* State list lengths
*/
pub latest_block_roots_length: usize,
pub latest_randao_mixes_length: usize,
pub latest_index_roots_length: usize,
pub latest_penalized_exit_length: usize,
/*
* Reward and penalty quotients
*/
pub base_reward_quotient: u64,
pub whistleblower_reward_quotient: u64,
pub includer_reward_quotient: u64,
pub inactivity_penalty_quotient: u64,
/*
* Max operations per block
*/
pub max_proposer_slashings: u64,
pub max_attester_slashings: u64,
pub max_attestations: u64,
pub max_deposits: u64,
pub max_exits: u64,
/*
* Signature domains
*/
pub domain_deposit: u64,
pub domain_attestation: u64,
pub domain_proposal: u64,
pub domain_exit: u64,
pub domain_randao: u64,
}