Do project-wide s/epoch_length/slots_per_epoch/g

This commit is contained in:
Paul Hauner 2019-03-04 17:51:54 +11:00
parent a1af65ce1a
commit 663d39739f
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
29 changed files with 113 additions and 112 deletions

View File

@ -312,7 +312,7 @@ where
.state .state
.read() .read()
.get_block_root( .get_block_root(
justified_epoch.start_slot(self.spec.epoch_length), justified_epoch.start_slot(self.spec.slots_per_epoch),
&self.spec, &self.spec,
) )
.ok_or_else(|| Error::BadRecentBlockRoots)?; .ok_or_else(|| Error::BadRecentBlockRoots)?;
@ -333,7 +333,7 @@ where
epoch_boundary_root, epoch_boundary_root,
shard_block_root: Hash256::zero(), shard_block_root: Hash256::zero(),
latest_crosslink: Crosslink { latest_crosslink: Crosslink {
epoch: self.state.read().slot.epoch(self.spec.epoch_length), epoch: self.state.read().slot.epoch(self.spec.slots_per_epoch),
shard_block_root: Hash256::zero(), shard_block_root: Hash256::zero(),
}, },
justified_epoch, justified_epoch,

View File

@ -27,7 +27,7 @@ fork: tchaikovsky
version: 1.0 version: 1.0
test_cases: test_cases:
- config: - config:
epoch_length: 64 slots_per_epoch: 64
deposits_for_chain_start: 1000 deposits_for_chain_start: 1000
num_slots: 64 num_slots: 64
skip_slots: [2, 3] skip_slots: [2, 3]

View File

@ -11,7 +11,7 @@ fn mid_epoch_state_transition(c: &mut Criterion) {
let validator_count = 1000; let validator_count = 1000;
let mut rig = BeaconChainHarness::new(ChainSpec::foundation(), validator_count); let mut rig = BeaconChainHarness::new(ChainSpec::foundation(), validator_count);
let epoch_depth = (rig.spec.epoch_length * 2) + (rig.spec.epoch_length / 2); let epoch_depth = (rig.spec.slots_per_epoch * 2) + (rig.spec.slots_per_epoch / 2);
for _ in 0..epoch_depth { for _ in 0..epoch_depth {
rig.advance_chain_with_block(); rig.advance_chain_with_block();
@ -19,7 +19,7 @@ fn mid_epoch_state_transition(c: &mut Criterion) {
let state = rig.beacon_chain.state.read().clone(); let state = rig.beacon_chain.state.read().clone();
assert!((state.slot + 1) % rig.spec.epoch_length != 0); assert!((state.slot + 1) % rig.spec.slots_per_epoch != 0);
c.bench_function("mid-epoch state transition 10k validators", move |b| { c.bench_function("mid-epoch state transition 10k validators", move |b| {
let state = state.clone(); let state = state.clone();
@ -36,7 +36,7 @@ fn epoch_boundary_state_transition(c: &mut Criterion) {
let validator_count = 10000; let validator_count = 10000;
let mut rig = BeaconChainHarness::new(ChainSpec::foundation(), validator_count); let mut rig = BeaconChainHarness::new(ChainSpec::foundation(), validator_count);
let epoch_depth = rig.spec.epoch_length * 2; let epoch_depth = rig.spec.slots_per_epoch * 2;
for _ in 0..(epoch_depth - 1) { for _ in 0..(epoch_depth - 1) {
rig.advance_chain_with_block(); rig.advance_chain_with_block();
@ -44,7 +44,7 @@ fn epoch_boundary_state_transition(c: &mut Criterion) {
let state = rig.beacon_chain.state.read().clone(); let state = rig.beacon_chain.state.read().clone();
assert_eq!((state.slot + 1) % rig.spec.epoch_length, 0); assert_eq!((state.slot + 1) % rig.spec.slots_per_epoch, 0);
c.bench( c.bench(
"routines", "routines",

View File

@ -5,7 +5,7 @@ fork: tchaikovsky
version: 1.0 version: 1.0
test_cases: test_cases:
- config: - config:
epoch_length: 64 slots_per_epoch: 64
deposits_for_chain_start: 1000 deposits_for_chain_start: 1000
num_slots: 64 num_slots: 64
skip_slots: [2, 3] skip_slots: [2, 3]

View File

@ -125,13 +125,13 @@ impl BeaconChainHarness {
let nth_slot = slot let nth_slot = slot
- slot - slot
.epoch(self.spec.epoch_length) .epoch(self.spec.slots_per_epoch)
.start_slot(self.spec.epoch_length); .start_slot(self.spec.slots_per_epoch);
let nth_epoch = slot.epoch(self.spec.epoch_length) - self.spec.genesis_epoch; let nth_epoch = slot.epoch(self.spec.slots_per_epoch) - self.spec.genesis_epoch;
debug!( debug!(
"Advancing BeaconChain to slot {}, epoch {} (epoch height: {}, slot {} in epoch.).", "Advancing BeaconChain to slot {}, epoch {} (epoch height: {}, slot {} in epoch.).",
slot, slot,
slot.epoch(self.spec.epoch_length), slot.epoch(self.spec.slots_per_epoch),
nth_epoch, nth_epoch,
nth_slot nth_slot
); );

View File

@ -13,7 +13,7 @@ pub struct Config {
/// Initial validators. /// Initial validators.
pub deposits_for_chain_start: usize, pub deposits_for_chain_start: usize,
/// Number of slots in an epoch. /// Number of slots in an epoch.
pub epoch_length: Option<u64>, pub slots_per_epoch: Option<u64>,
/// Number of slots to build before ending execution. /// Number of slots to build before ending execution.
pub num_slots: u64, pub num_slots: u64,
/// Number of slots that should be skipped due to inactive validator. /// Number of slots that should be skipped due to inactive validator.
@ -34,7 +34,7 @@ impl Config {
Self { Self {
deposits_for_chain_start: as_usize(&yaml, "deposits_for_chain_start") deposits_for_chain_start: as_usize(&yaml, "deposits_for_chain_start")
.expect("Must specify validator count"), .expect("Must specify validator count"),
epoch_length: as_u64(&yaml, "epoch_length"), slots_per_epoch: as_u64(&yaml, "slots_per_epoch"),
num_slots: as_u64(&yaml, "num_slots").expect("Must specify `config.num_slots`"), num_slots: as_u64(&yaml, "num_slots").expect("Must specify `config.num_slots`"),
skip_slots: as_vec_u64(yaml, "skip_slots"), skip_slots: as_vec_u64(yaml, "skip_slots"),
deposits: parse_deposits(&yaml), deposits: parse_deposits(&yaml),

View File

@ -54,12 +54,12 @@ impl TestCase {
/// Return a `ChainSpec::foundation()`. /// Return a `ChainSpec::foundation()`.
/// ///
/// If specified in `config`, returns it with a modified `epoch_length`. /// If specified in `config`, returns it with a modified `slots_per_epoch`.
fn spec(&self) -> ChainSpec { fn spec(&self) -> ChainSpec {
let mut spec = ChainSpec::foundation(); let mut spec = ChainSpec::foundation();
if let Some(n) = self.config.epoch_length { if let Some(n) = self.config.slots_per_epoch {
spec.epoch_length = n; spec.slots_per_epoch = n;
} }
spec spec
@ -174,7 +174,7 @@ impl TestCase {
for state_check in state_checks { for state_check in state_checks {
let adjusted_state_slot = let adjusted_state_slot =
state.slot - spec.genesis_epoch.start_slot(spec.epoch_length); state.slot - spec.genesis_epoch.start_slot(spec.slots_per_epoch);
if state_check.slot == adjusted_state_slot { if state_check.slot == adjusted_state_slot {
state_check.assert_valid(state, spec); state_check.assert_valid(state, spec);

View File

@ -36,13 +36,13 @@ impl StateCheck {
/// ///
/// Panics with an error message if any test fails. /// Panics with an error message if any test fails.
pub fn assert_valid(&self, state: &BeaconState, spec: &ChainSpec) { pub fn assert_valid(&self, state: &BeaconState, spec: &ChainSpec) {
let state_epoch = state.slot.epoch(spec.epoch_length); let state_epoch = state.slot.epoch(spec.slots_per_epoch);
info!("Running state check for slot height {}.", self.slot); info!("Running state check for slot height {}.", self.slot);
assert_eq!( assert_eq!(
self.slot, self.slot,
state.slot - spec.genesis_epoch.start_slot(spec.epoch_length), state.slot - spec.genesis_epoch.start_slot(spec.slots_per_epoch),
"State slot is invalid." "State slot is invalid."
); );

View File

@ -29,7 +29,7 @@ fn it_can_produce_past_first_epoch_boundary() {
debug!("Harness built, tests starting.."); debug!("Harness built, tests starting..");
let blocks = harness.spec.epoch_length * 2 + 1; let blocks = harness.spec.slots_per_epoch * 2 + 1;
for i in 0..blocks { for i in 0..blocks {
harness.advance_chain_with_block(); harness.advance_chain_with_block();

View File

@ -195,9 +195,9 @@ mod tests {
let beacon_node = Arc::new(SimulatedBeaconNode::default()); let beacon_node = Arc::new(SimulatedBeaconNode::default());
let signer = Arc::new(LocalSigner::new(Keypair::random())); let signer = Arc::new(LocalSigner::new(Keypair::random()));
let mut duties = EpochMap::new(spec.epoch_length); let mut duties = EpochMap::new(spec.slots_per_epoch);
let attest_slot = Slot::new(100); let attest_slot = Slot::new(100);
let attest_epoch = attest_slot / spec.epoch_length; let attest_epoch = attest_slot / spec.slots_per_epoch;
let attest_shard = 12; let attest_shard = 12;
duties.insert_attestation_shard(attest_slot, attest_shard); duties.insert_attestation_shard(attest_slot, attest_shard);
duties.set_validator_index(Some(2)); duties.set_validator_index(Some(2));
@ -243,7 +243,7 @@ mod tests {
); );
// In an epoch without known duties... // In an epoch without known duties...
let slot = (attest_epoch + 1) * spec.epoch_length; let slot = (attest_epoch + 1) * spec.slots_per_epoch;
slot_clock.set_slot(slot.into()); slot_clock.set_slot(slot.into());
assert_eq!( assert_eq!(
attester.poll(), attester.poll(),

View File

@ -3,22 +3,22 @@ use std::collections::HashMap;
use types::{Epoch, Slot}; use types::{Epoch, Slot};
pub struct EpochMap { pub struct EpochMap {
epoch_length: u64, slots_per_epoch: u64,
validator_index: Option<u64>, validator_index: Option<u64>,
map: HashMap<Epoch, (Slot, u64)>, map: HashMap<Epoch, (Slot, u64)>,
} }
impl EpochMap { impl EpochMap {
pub fn new(epoch_length: u64) -> Self { pub fn new(slots_per_epoch: u64) -> Self {
Self { Self {
epoch_length, slots_per_epoch,
validator_index: None, validator_index: None,
map: HashMap::new(), map: HashMap::new(),
} }
} }
pub fn insert_attestation_shard(&mut self, slot: Slot, shard: u64) { pub fn insert_attestation_shard(&mut self, slot: Slot, shard: u64) {
let epoch = slot.epoch(self.epoch_length); let epoch = slot.epoch(self.slots_per_epoch);
self.map.insert(epoch, (slot, shard)); self.map.insert(epoch, (slot, shard));
} }
@ -29,7 +29,7 @@ impl EpochMap {
impl DutiesReader for EpochMap { impl DutiesReader for EpochMap {
fn attestation_shard(&self, slot: Slot) -> Result<Option<u64>, DutiesReaderError> { fn attestation_shard(&self, slot: Slot) -> Result<Option<u64>, DutiesReaderError> {
let epoch = slot.epoch(self.epoch_length); let epoch = slot.epoch(self.slots_per_epoch);
match self.map.get(&epoch) { match self.map.get(&epoch) {
Some((attest_slot, attest_shard)) if *attest_slot == slot => Ok(Some(*attest_shard)), Some((attest_slot, attest_shard)) if *attest_slot == slot => Ok(Some(*attest_shard)),

View File

@ -132,7 +132,7 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U
fn produce_block(&mut self, slot: Slot) -> Result<PollOutcome, Error> { fn produce_block(&mut self, slot: Slot) -> Result<PollOutcome, Error> {
let randao_reveal = { let randao_reveal = {
// TODO: add domain, etc to this message. Also ensure result matches `into_to_bytes32`. // TODO: add domain, etc to this message. Also ensure result matches `into_to_bytes32`.
let message = int_to_bytes32(slot.epoch(self.spec.epoch_length).as_u64()); let message = int_to_bytes32(slot.epoch(self.spec.slots_per_epoch).as_u64());
match self match self
.signer .signer
@ -233,9 +233,9 @@ mod tests {
let beacon_node = Arc::new(SimulatedBeaconNode::default()); let beacon_node = Arc::new(SimulatedBeaconNode::default());
let signer = Arc::new(LocalSigner::new(Keypair::random())); let signer = Arc::new(LocalSigner::new(Keypair::random()));
let mut epoch_map = EpochMap::new(spec.epoch_length); let mut epoch_map = EpochMap::new(spec.slots_per_epoch);
let produce_slot = Slot::new(100); let produce_slot = Slot::new(100);
let produce_epoch = produce_slot.epoch(spec.epoch_length); let produce_epoch = produce_slot.epoch(spec.slots_per_epoch);
epoch_map.map.insert(produce_epoch, produce_slot); epoch_map.map.insert(produce_epoch, produce_slot);
let epoch_map = Arc::new(epoch_map); let epoch_map = Arc::new(epoch_map);
@ -280,7 +280,7 @@ mod tests {
); );
// In an epoch without known duties... // In an epoch without known duties...
let slot = (produce_epoch.as_u64() + 1) * spec.epoch_length; let slot = (produce_epoch.as_u64() + 1) * spec.slots_per_epoch;
slot_clock.set_slot(slot); slot_clock.set_slot(slot);
assert_eq!( assert_eq!(
block_proposer.poll(), block_proposer.poll(),

View File

@ -3,14 +3,14 @@ use std::collections::HashMap;
use types::{Epoch, Slot}; use types::{Epoch, Slot};
pub struct EpochMap { pub struct EpochMap {
epoch_length: u64, slots_per_epoch: u64,
pub map: HashMap<Epoch, Slot>, pub map: HashMap<Epoch, Slot>,
} }
impl EpochMap { impl EpochMap {
pub fn new(epoch_length: u64) -> Self { pub fn new(slots_per_epoch: u64) -> Self {
Self { Self {
epoch_length, slots_per_epoch,
map: HashMap::new(), map: HashMap::new(),
} }
} }
@ -18,7 +18,7 @@ impl EpochMap {
impl DutiesReader for EpochMap { impl DutiesReader for EpochMap {
fn is_block_production_slot(&self, slot: Slot) -> Result<bool, DutiesReaderError> { fn is_block_production_slot(&self, slot: Slot) -> Result<bool, DutiesReaderError> {
let epoch = slot.epoch(self.epoch_length); let epoch = slot.epoch(self.slots_per_epoch);
match self.map.get(&epoch) { match self.map.get(&epoch) {
Some(s) if *s == slot => Ok(true), Some(s) if *s == slot => Ok(true),
Some(s) if *s != slot => Ok(false), Some(s) if *s != slot => Ok(false),

View File

@ -95,7 +95,7 @@ where
let active_validator_indices = get_active_validator_indices( let active_validator_indices = get_active_validator_indices(
&current_state.validator_registry[..], &current_state.validator_registry[..],
block_slot.epoch(spec.epoch_length), block_slot.epoch(spec.slots_per_epoch),
); );
for index in active_validator_indices { for index in active_validator_indices {

View File

@ -64,7 +64,7 @@ where
let active_validator_indices = get_active_validator_indices( let active_validator_indices = get_active_validator_indices(
&current_state.validator_registry[..], &current_state.validator_registry[..],
block_slot.epoch(spec.epoch_length), block_slot.epoch(spec.slots_per_epoch),
); );
for index in active_validator_indices { for index in active_validator_indices {

View File

@ -185,7 +185,7 @@ fn per_block_processing_signature_optional(
proposer_slashing proposer_slashing
.proposal_data_1 .proposal_data_1
.slot .slot
.epoch(spec.epoch_length), .epoch(spec.slots_per_epoch),
spec.domain_proposal spec.domain_proposal
) )
), ),
@ -201,7 +201,7 @@ fn per_block_processing_signature_optional(
proposer_slashing proposer_slashing
.proposal_data_2 .proposal_data_2
.slot .slot
.epoch(spec.epoch_length), .epoch(spec.slots_per_epoch),
spec.domain_proposal spec.domain_proposal
) )
), ),
@ -341,14 +341,14 @@ fn validate_attestation_signature_optional(
) -> Result<(), AttestationValidationError> { ) -> Result<(), AttestationValidationError> {
trace!( trace!(
"validate_attestation_signature_optional: attestation epoch: {}", "validate_attestation_signature_optional: attestation epoch: {}",
attestation.data.slot.epoch(spec.epoch_length) attestation.data.slot.epoch(spec.slots_per_epoch)
); );
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
); );
ensure!( ensure!(
attestation.data.slot + spec.epoch_length >= state.slot, attestation.data.slot + spec.slots_per_epoch >= state.slot,
AttestationValidationError::IncludedTooLate AttestationValidationError::IncludedTooLate
); );
if attestation.data.slot >= state.current_epoch_start_slot(spec) { if attestation.data.slot >= state.current_epoch_start_slot(spec) {
@ -369,7 +369,7 @@ fn validate_attestation_signature_optional(
attestation attestation
.data .data
.justified_epoch .justified_epoch
.start_slot(spec.epoch_length), .start_slot(spec.slots_per_epoch),
&spec &spec
) )
.ok_or(AttestationValidationError::NoBlockRoot)?, .ok_or(AttestationValidationError::NoBlockRoot)?,
@ -377,7 +377,7 @@ fn validate_attestation_signature_optional(
); );
let potential_crosslink = Crosslink { let potential_crosslink = Crosslink {
shard_block_root: attestation.data.shard_block_root, shard_block_root: attestation.data.shard_block_root,
epoch: attestation.data.slot.epoch(spec.epoch_length), epoch: attestation.data.slot.epoch(spec.slots_per_epoch),
}; };
ensure!( ensure!(
(attestation.data.latest_crosslink (attestation.data.latest_crosslink
@ -407,7 +407,7 @@ fn validate_attestation_signature_optional(
PHASE_0_CUSTODY_BIT, PHASE_0_CUSTODY_BIT,
get_domain( get_domain(
&state.fork, &state.fork,
attestation.data.slot.epoch(spec.epoch_length), attestation.data.slot.epoch(spec.slots_per_epoch),
spec.domain_attestation, spec.domain_attestation,
) )
), ),

View File

@ -76,7 +76,7 @@ impl EpochProcessable for BeaconState {
*/ */
let active_validator_indices = get_active_validator_indices( let active_validator_indices = get_active_validator_indices(
&self.validator_registry, &self.validator_registry,
self.slot.epoch(spec.epoch_length), self.slot.epoch(spec.slots_per_epoch),
); );
let current_total_balance = self.get_total_balance(&active_validator_indices[..], spec); let current_total_balance = self.get_total_balance(&active_validator_indices[..], spec);
@ -90,7 +90,7 @@ impl EpochProcessable for BeaconState {
.latest_attestations .latest_attestations
.par_iter() .par_iter()
.filter(|a| { .filter(|a| {
(a.data.slot / spec.epoch_length).epoch(spec.epoch_length) (a.data.slot / spec.slots_per_epoch).epoch(spec.slots_per_epoch)
== self.current_epoch(spec) == self.current_epoch(spec)
}) })
.collect(); .collect();
@ -137,7 +137,7 @@ impl EpochProcessable for BeaconState {
.par_iter() .par_iter()
.filter(|a| { .filter(|a| {
//TODO: ensure these saturating subs are correct. //TODO: ensure these saturating subs are correct.
(a.data.slot / spec.epoch_length).epoch(spec.epoch_length) (a.data.slot / spec.slots_per_epoch).epoch(spec.slots_per_epoch)
== self.previous_epoch(spec) == self.previous_epoch(spec)
}) })
.collect(); .collect();
@ -320,12 +320,12 @@ impl EpochProcessable for BeaconState {
let mut winning_root_for_shards: HashMap<u64, Result<WinningRoot, WinningRootError>> = let mut winning_root_for_shards: HashMap<u64, Result<WinningRoot, WinningRootError>> =
HashMap::new(); HashMap::new();
// for slot in self.slot.saturating_sub(2 * spec.epoch_length)..self.slot { // for slot in self.slot.saturating_sub(2 * spec.slots_per_epoch)..self.slot {
for slot in self.previous_epoch(spec).slot_iter(spec.epoch_length) { for slot in self.previous_epoch(spec).slot_iter(spec.slots_per_epoch) {
trace!( trace!(
"Finding winning root for slot: {} (epoch: {})", "Finding winning root for slot: {} (epoch: {})",
slot, slot,
slot.epoch(spec.epoch_length) slot.epoch(spec.slots_per_epoch)
); );
// Clone is used to remove the borrow. It becomes an issue later when trying to mutate // Clone is used to remove the borrow. It becomes an issue later when trying to mutate
@ -506,7 +506,7 @@ impl EpochProcessable for BeaconState {
/* /*
* Crosslinks * Crosslinks
*/ */
for slot in self.previous_epoch(spec).slot_iter(spec.epoch_length) { for slot in self.previous_epoch(spec).slot_iter(spec.slots_per_epoch) {
// Clone is used to remove the borrow. It becomes an issue later when trying to mutate // Clone is used to remove the borrow. It becomes an issue later when trying to mutate
// `self.balances`. // `self.balances`.
let crosslink_committees_at_slot = let crosslink_committees_at_slot =
@ -615,7 +615,7 @@ impl EpochProcessable for BeaconState {
self.latest_attestations = self self.latest_attestations = self
.latest_attestations .latest_attestations
.iter() .iter()
.filter(|a| a.data.slot.epoch(spec.epoch_length) >= current_epoch) .filter(|a| a.data.slot.epoch(spec.slots_per_epoch) >= current_epoch)
.cloned() .cloned()
.collect(); .collect();

View File

@ -24,7 +24,7 @@ where
previous_block_root: Hash256, previous_block_root: Hash256,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<(), Error> { ) -> Result<(), Error> {
if (self.slot + 1) % spec.epoch_length == 0 { if (self.slot + 1) % spec.slots_per_epoch == 0 {
self.per_epoch_processing(spec)?; self.per_epoch_processing(spec)?;
self.advance_caches(); self.advance_caches();
} }

View File

@ -337,7 +337,7 @@ impl BeaconState {
/// ///
/// Spec v0.2.0 /// Spec v0.2.0
pub fn current_epoch(&self, spec: &ChainSpec) -> Epoch { pub fn current_epoch(&self, spec: &ChainSpec) -> Epoch {
self.slot.epoch(spec.epoch_length) self.slot.epoch(spec.slots_per_epoch)
} }
/// The epoch prior to `self.current_epoch()`. /// The epoch prior to `self.current_epoch()`.
@ -363,14 +363,14 @@ impl BeaconState {
/// ///
/// Spec v0.2.0 /// Spec v0.2.0
pub fn current_epoch_start_slot(&self, spec: &ChainSpec) -> Slot { pub fn current_epoch_start_slot(&self, spec: &ChainSpec) -> Slot {
self.current_epoch(spec).start_slot(spec.epoch_length) self.current_epoch(spec).start_slot(spec.slots_per_epoch)
} }
/// The first slot of the epoch preceeding the one corresponding to `self.slot`. /// The first slot of the epoch preceeding the one corresponding to `self.slot`.
/// ///
/// Spec v0.2.0 /// Spec v0.2.0
pub fn previous_epoch_start_slot(&self, spec: &ChainSpec) -> Slot { pub fn previous_epoch_start_slot(&self, spec: &ChainSpec) -> Slot {
self.previous_epoch(spec).start_slot(spec.epoch_length) self.previous_epoch(spec).start_slot(spec.slots_per_epoch)
} }
/// Return the number of committees in one epoch. /// Return the number of committees in one epoch.
@ -386,10 +386,10 @@ impl BeaconState {
std::cmp::max( std::cmp::max(
1, 1,
std::cmp::min( std::cmp::min(
spec.shard_count / spec.epoch_length, spec.shard_count / spec.slots_per_epoch,
active_validator_count as u64 / spec.epoch_length / spec.target_committee_size, active_validator_count as u64 / spec.slots_per_epoch / spec.target_committee_size,
), ),
) * spec.epoch_length ) * spec.slots_per_epoch
} }
/// Shuffle ``validators`` into crosslink committees seeded by ``seed`` and ``epoch``. /// Shuffle ``validators`` into crosslink committees seeded by ``seed`` and ``epoch``.
@ -520,11 +520,11 @@ impl BeaconState {
slot: Slot, slot: Slot,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<&CrosslinkCommittees, Error> { ) -> Result<&CrosslinkCommittees, Error> {
let epoch = slot.epoch(spec.epoch_length); let epoch = slot.epoch(spec.slots_per_epoch);
let relative_epoch = self.relative_epoch(epoch, spec)?; let relative_epoch = self.relative_epoch(epoch, spec)?;
let cache = self.cache(relative_epoch)?; let cache = self.cache(relative_epoch)?;
let slot_offset = slot - epoch.start_slot(spec.epoch_length); let slot_offset = slot - epoch.start_slot(spec.slots_per_epoch);
Ok(&cache.committees[slot_offset.as_usize()]) Ok(&cache.committees[slot_offset.as_usize()])
} }
@ -565,7 +565,7 @@ impl BeaconState {
registry_change: bool, registry_change: bool,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<(u64, Hash256, Epoch, u64), Error> { ) -> Result<(u64, Hash256, Epoch, u64), Error> {
let epoch = slot.epoch(spec.epoch_length); let epoch = slot.epoch(spec.slots_per_epoch);
let current_epoch = self.current_epoch(spec); let current_epoch = self.current_epoch(spec);
let previous_epoch = self.previous_epoch(spec); let previous_epoch = self.previous_epoch(spec);
let next_epoch = self.next_epoch(spec); let next_epoch = self.next_epoch(spec);
@ -639,8 +639,8 @@ impl BeaconState {
let (committees_per_epoch, _seed, _shuffling_epoch, shuffling_start_shard) = let (committees_per_epoch, _seed, _shuffling_epoch, shuffling_start_shard) =
self.get_committee_params_at_slot(slot, registry_change, spec)?; self.get_committee_params_at_slot(slot, registry_change, spec)?;
let offset = slot.as_u64() % spec.epoch_length; let offset = slot.as_u64() % spec.slots_per_epoch;
let committees_per_slot = committees_per_epoch / spec.epoch_length; let committees_per_slot = committees_per_epoch / spec.slots_per_epoch;
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;
@ -835,7 +835,7 @@ impl BeaconState {
proof_of_possession.verify( proof_of_possession.verify(
&proof_of_possession_data.hash_tree_root(), &proof_of_possession_data.hash_tree_root(),
self.fork self.fork
.get_domain(self.slot.epoch(spec.epoch_length), spec.domain_deposit), .get_domain(self.slot.epoch(spec.slots_per_epoch), spec.domain_deposit),
&pubkey, &pubkey,
) )
} }
@ -1296,7 +1296,7 @@ impl BeaconState {
bitfield: &Bitfield, bitfield: &Bitfield,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<Vec<usize>, Error> { ) -> Result<Vec<usize>, Error> {
let epoch = attestation_data.slot.epoch(spec.epoch_length); let epoch = attestation_data.slot.epoch(spec.slots_per_epoch);
let relative_epoch = self.relative_epoch(epoch, spec)?; let relative_epoch = self.relative_epoch(epoch, spec)?;
let cache = self.cache(relative_epoch)?; let cache = self.cache(relative_epoch)?;

View File

@ -137,7 +137,7 @@ impl BeaconStateBuilder {
pub fn teleport_to_end_of_epoch(&mut self, epoch: Epoch) { pub fn teleport_to_end_of_epoch(&mut self, epoch: Epoch) {
let state = self.state.as_mut().expect("Genesis required"); let state = self.state.as_mut().expect("Genesis required");
let slot = epoch.end_slot(self.spec.epoch_length); let slot = epoch.end_slot(self.spec.slots_per_epoch);
state.slot = slot; state.slot = slot;
state.validator_registry_update_epoch = epoch - 1; state.validator_registry_update_epoch = epoch - 1;
@ -171,11 +171,11 @@ impl BeaconStateBuilder {
let current_epoch = state.current_epoch(&self.spec); let current_epoch = state.current_epoch(&self.spec);
let previous_epoch = state.previous_epoch(&self.spec); let previous_epoch = state.previous_epoch(&self.spec);
let current_epoch_depth = let current_epoch_depth =
(state.slot - current_epoch.end_slot(self.spec.epoch_length)).as_usize(); (state.slot - current_epoch.end_slot(self.spec.slots_per_epoch)).as_usize();
let previous_epoch_slots = previous_epoch.slot_iter(self.spec.epoch_length); let previous_epoch_slots = previous_epoch.slot_iter(self.spec.slots_per_epoch);
let current_epoch_slots = current_epoch let current_epoch_slots = current_epoch
.slot_iter(self.spec.epoch_length) .slot_iter(self.spec.slots_per_epoch)
.take(current_epoch_depth); .take(current_epoch_depth);
for slot in previous_epoch_slots.chain(current_epoch_slots) { for slot in previous_epoch_slots.chain(current_epoch_slots) {
@ -219,7 +219,8 @@ fn committee_to_pending_attestation(
custody_bitfield.set(i, true); custody_bitfield.set(i, true);
} }
let is_previous_epoch = state.slot.epoch(spec.epoch_length) != slot.epoch(spec.epoch_length); let is_previous_epoch =
state.slot.epoch(spec.slots_per_epoch) != slot.epoch(spec.slots_per_epoch);
let justified_epoch = if is_previous_epoch { let justified_epoch = if is_previous_epoch {
state.previous_justified_epoch state.previous_justified_epoch
@ -229,16 +230,16 @@ fn committee_to_pending_attestation(
let epoch_boundary_root = if is_previous_epoch { let epoch_boundary_root = if is_previous_epoch {
*state *state
.get_block_root(previous_epoch.start_slot(spec.epoch_length), spec) .get_block_root(previous_epoch.start_slot(spec.slots_per_epoch), spec)
.unwrap() .unwrap()
} else { } else {
*state *state
.get_block_root(current_epoch.start_slot(spec.epoch_length), spec) .get_block_root(current_epoch.start_slot(spec.slots_per_epoch), spec)
.unwrap() .unwrap()
}; };
let justified_block_root = *state let justified_block_root = *state
.get_block_root(justified_epoch.start_slot(spec.epoch_length), &spec) .get_block_root(justified_epoch.start_slot(spec.slots_per_epoch), &spec)
.unwrap(); .unwrap();
PendingAttestation { PendingAttestation {
@ -250,7 +251,7 @@ fn committee_to_pending_attestation(
epoch_boundary_root, epoch_boundary_root,
shard_block_root: Hash256::zero(), shard_block_root: Hash256::zero(),
latest_crosslink: Crosslink { latest_crosslink: Crosslink {
epoch: slot.epoch(spec.epoch_length), epoch: slot.epoch(spec.slots_per_epoch),
shard_block_root: Hash256::zero(), shard_block_root: Hash256::zero(),
}, },
justified_epoch, justified_epoch,

View File

@ -32,14 +32,14 @@ impl EpochCache {
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<EpochCache, Error> { ) -> Result<EpochCache, Error> {
let mut epoch_committees: Vec<CrosslinkCommittees> = let mut epoch_committees: Vec<CrosslinkCommittees> =
Vec::with_capacity(spec.epoch_length as usize); Vec::with_capacity(spec.slots_per_epoch as usize);
let mut attestation_duty_map: AttestationDutyMap = HashMap::new(); let mut attestation_duty_map: AttestationDutyMap = HashMap::new();
let mut shard_committee_index_map: ShardCommitteeIndexMap = HashMap::new(); let mut shard_committee_index_map: ShardCommitteeIndexMap = HashMap::new();
let shuffling = let shuffling =
state.get_shuffling_for_slot(epoch.start_slot(spec.epoch_length), false, spec)?; state.get_shuffling_for_slot(epoch.start_slot(spec.slots_per_epoch), false, spec)?;
for (epoch_committeess_index, slot) in epoch.slot_iter(spec.epoch_length).enumerate() { for (epoch_committeess_index, slot) in epoch.slot_iter(spec.slots_per_epoch).enumerate() {
let slot_committees = state.calculate_crosslink_committees_at_slot( let slot_committees = state.calculate_crosslink_committees_at_slot(
slot, slot,
false, false,

View File

@ -35,8 +35,8 @@ pub fn get_attestation_participants_consistency() {
for slot in state for slot in state
.slot .slot
.epoch(spec.epoch_length) .epoch(spec.slots_per_epoch)
.slot_iter(spec.epoch_length) .slot_iter(spec.slots_per_epoch)
{ {
let committees = state.get_crosslink_committees_at_slot(slot, &spec).unwrap(); let committees = state.get_crosslink_committees_at_slot(slot, &spec).unwrap();

View File

@ -38,14 +38,14 @@ impl ProposerSlashingBuilder {
proposal_1.signature = { proposal_1.signature = {
let message = proposal_1.signed_root(); let message = proposal_1.signed_root();
let epoch = slot.epoch(spec.epoch_length); let epoch = slot.epoch(spec.slots_per_epoch);
let domain = spec.domain_proposal; let domain = spec.domain_proposal;
signer(proposer_index, &message[..], epoch, domain) signer(proposer_index, &message[..], epoch, domain)
}; };
proposal_2.signature = { proposal_2.signature = {
let message = proposal_2.signed_root(); let message = proposal_2.signed_root();
let epoch = slot.epoch(spec.epoch_length); let epoch = slot.epoch(spec.slots_per_epoch);
let domain = spec.domain_proposal; let domain = spec.domain_proposal;
signer(proposer_index, &message[..], epoch, domain) signer(proposer_index, &message[..], epoch, domain)
}; };

View File

@ -24,7 +24,7 @@ impl SlashableAttestation {
/// ///
/// Spec v0.4.0 /// Spec v0.4.0
pub fn is_double_vote(&self, other: &SlashableAttestation, spec: &ChainSpec) -> bool { pub fn is_double_vote(&self, other: &SlashableAttestation, spec: &ChainSpec) -> bool {
self.data.slot.epoch(spec.epoch_length) == other.data.slot.epoch(spec.epoch_length) self.data.slot.epoch(spec.slots_per_epoch) == other.data.slot.epoch(spec.slots_per_epoch)
} }
/// Check if ``attestation_data_1`` surrounds ``attestation_data_2``. /// Check if ``attestation_data_1`` surrounds ``attestation_data_2``.
@ -33,8 +33,8 @@ impl SlashableAttestation {
pub fn is_surround_vote(&self, other: &SlashableAttestation, spec: &ChainSpec) -> bool { pub fn is_surround_vote(&self, other: &SlashableAttestation, spec: &ChainSpec) -> bool {
let source_epoch_1 = self.data.justified_epoch; let source_epoch_1 = self.data.justified_epoch;
let source_epoch_2 = other.data.justified_epoch; let source_epoch_2 = other.data.justified_epoch;
let target_epoch_1 = self.data.slot.epoch(spec.epoch_length); let target_epoch_1 = self.data.slot.epoch(spec.slots_per_epoch);
let target_epoch_2 = other.data.slot.epoch(spec.epoch_length); let target_epoch_2 = other.data.slot.epoch(spec.slots_per_epoch);
(source_epoch_1 < source_epoch_2) & (target_epoch_2 < target_epoch_1) (source_epoch_1 < source_epoch_2) & (target_epoch_2 < target_epoch_1)
} }
@ -151,7 +151,7 @@ mod tests {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let mut slashable_vote = SlashableAttestation::random_for_test(&mut rng); let mut slashable_vote = SlashableAttestation::random_for_test(&mut rng);
slashable_vote.data.slot = Slot::new(slot_factor * spec.epoch_length); slashable_vote.data.slot = Slot::new(slot_factor * spec.slots_per_epoch);
slashable_vote.data.justified_epoch = Epoch::new(justified_epoch); slashable_vote.data.justified_epoch = Epoch::new(justified_epoch);
slashable_vote slashable_vote
} }

View File

@ -35,8 +35,8 @@ impl Slot {
Slot(slot) Slot(slot)
} }
pub fn epoch(self, epoch_length: u64) -> Epoch { pub fn epoch(self, slots_per_epoch: u64) -> Epoch {
Epoch::from(self.0 / epoch_length) Epoch::from(self.0 / slots_per_epoch)
} }
pub fn height(self, genesis_slot: Slot) -> SlotHeight { pub fn height(self, genesis_slot: Slot) -> SlotHeight {
@ -57,24 +57,24 @@ impl Epoch {
Epoch(u64::max_value()) Epoch(u64::max_value())
} }
pub fn start_slot(self, epoch_length: u64) -> Slot { pub fn start_slot(self, slots_per_epoch: u64) -> Slot {
Slot::from(self.0.saturating_mul(epoch_length)) Slot::from(self.0.saturating_mul(slots_per_epoch))
} }
pub fn end_slot(self, epoch_length: u64) -> Slot { pub fn end_slot(self, slots_per_epoch: u64) -> Slot {
Slot::from( Slot::from(
self.0 self.0
.saturating_add(1) .saturating_add(1)
.saturating_mul(epoch_length) .saturating_mul(slots_per_epoch)
.saturating_sub(1), .saturating_sub(1),
) )
} }
pub fn slot_iter(&self, epoch_length: u64) -> SlotIter { pub fn slot_iter(&self, slots_per_epoch: u64) -> SlotIter {
SlotIter { SlotIter {
current_iteration: 0, current_iteration: 0,
epoch: self, epoch: self,
epoch_length, slots_per_epoch,
} }
} }
} }
@ -82,17 +82,17 @@ impl Epoch {
pub struct SlotIter<'a> { pub struct SlotIter<'a> {
current_iteration: u64, current_iteration: u64,
epoch: &'a Epoch, epoch: &'a Epoch,
epoch_length: u64, slots_per_epoch: u64,
} }
impl<'a> Iterator for SlotIter<'a> { impl<'a> Iterator for SlotIter<'a> {
type Item = Slot; type Item = Slot;
fn next(&mut self) -> Option<Slot> { fn next(&mut self) -> Option<Slot> {
if self.current_iteration >= self.epoch_length { if self.current_iteration >= self.slots_per_epoch {
None None
} else { } else {
let start_slot = self.epoch.start_slot(self.epoch_length); let start_slot = self.epoch.start_slot(self.slots_per_epoch);
let previous = self.current_iteration; let previous = self.current_iteration;
self.current_iteration += 1; self.current_iteration += 1;
Some(start_slot + previous) Some(start_slot + previous)
@ -119,18 +119,18 @@ mod epoch_tests {
#[test] #[test]
fn slot_iter() { fn slot_iter() {
let epoch_length = 8; let slots_per_epoch = 8;
let epoch = Epoch::new(0); let epoch = Epoch::new(0);
let mut slots = vec![]; let mut slots = vec![];
for slot in epoch.slot_iter(epoch_length) { for slot in epoch.slot_iter(slots_per_epoch) {
slots.push(slot); slots.push(slot);
} }
assert_eq!(slots.len(), epoch_length as usize); assert_eq!(slots.len(), slots_per_epoch as usize);
for i in 0..epoch_length { for i in 0..slots_per_epoch {
assert_eq!(Slot::from(i), slots[i as usize]) assert_eq!(Slot::from(i), slots[i as usize])
} }
} }

View File

@ -23,8 +23,8 @@ impl SlotHeight {
Slot::from(self.0.saturating_add(genesis_slot.as_u64())) Slot::from(self.0.saturating_add(genesis_slot.as_u64()))
} }
pub fn epoch(self, genesis_slot: u64, epoch_length: u64) -> Epoch { pub fn epoch(self, genesis_slot: u64, slots_per_epoch: u64) -> Epoch {
Epoch::from(self.0.saturating_add(genesis_slot) / epoch_length) Epoch::from(self.0.saturating_add(genesis_slot) / slots_per_epoch)
} }
pub fn max_value() -> SlotHeight { pub fn max_value() -> SlotHeight {

View File

@ -32,14 +32,14 @@ pub enum EpochDutiesMapError {
/// Maps an `epoch` to some `EpochDuties` for a single validator. /// Maps an `epoch` to some `EpochDuties` for a single validator.
pub struct EpochDutiesMap { pub struct EpochDutiesMap {
pub epoch_length: u64, pub slots_per_epoch: u64,
pub map: RwLock<HashMap<Epoch, EpochDuties>>, pub map: RwLock<HashMap<Epoch, EpochDuties>>,
} }
impl EpochDutiesMap { impl EpochDutiesMap {
pub fn new(epoch_length: u64) -> Self { pub fn new(slots_per_epoch: u64) -> Self {
Self { Self {
epoch_length, slots_per_epoch,
map: RwLock::new(HashMap::new()), map: RwLock::new(HashMap::new()),
} }
} }
@ -67,7 +67,7 @@ impl EpochDutiesMap {
impl DutiesReader for EpochDutiesMap { impl DutiesReader for EpochDutiesMap {
fn is_block_production_slot(&self, slot: Slot) -> Result<bool, DutiesReaderError> { fn is_block_production_slot(&self, slot: Slot) -> Result<bool, DutiesReaderError> {
let epoch = slot.epoch(self.epoch_length); let epoch = slot.epoch(self.slots_per_epoch);
let map = self.map.read().map_err(|_| DutiesReaderError::Poisoned)?; let map = self.map.read().map_err(|_| DutiesReaderError::Poisoned)?;
let duties = map let duties = map

View File

@ -61,7 +61,7 @@ impl<T: SlotClock, U: BeaconNode> DutiesManager<T, U> {
.map_err(|_| Error::SlotClockError)? .map_err(|_| Error::SlotClockError)?
.ok_or(Error::SlotUnknowable)?; .ok_or(Error::SlotUnknowable)?;
let epoch = slot.epoch(self.spec.epoch_length); let epoch = slot.epoch(self.spec.slots_per_epoch);
if let Some(duties) = self.beacon_node.request_shuffling(epoch, &self.pubkey)? { if let Some(duties) = self.beacon_node.request_shuffling(epoch, &self.pubkey)? {
// If these duties were known, check to see if they're updates or identical. // If these duties were known, check to see if they're updates or identical.
@ -112,7 +112,7 @@ mod tests {
#[test] #[test]
pub fn polling() { pub fn polling() {
let spec = Arc::new(ChainSpec::foundation()); let spec = Arc::new(ChainSpec::foundation());
let duties_map = Arc::new(EpochDutiesMap::new(spec.epoch_length)); let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch));
let keypair = Keypair::random(); let keypair = Keypair::random();
let slot_clock = Arc::new(TestingSlotClock::new(0)); let slot_clock = Arc::new(TestingSlotClock::new(0));
let beacon_node = Arc::new(TestBeaconNode::default()); let beacon_node = Arc::new(TestBeaconNode::default());

View File

@ -111,7 +111,7 @@ fn main() {
for keypair in keypairs { for keypair in keypairs {
info!(log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id()); info!(log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id());
let duties_map = Arc::new(EpochDutiesMap::new(spec.epoch_length)); let duties_map = Arc::new(EpochDutiesMap::new(spec.slots_per_epoch));
// Spawn a new thread to maintain the validator's `EpochDuties`. // Spawn a new thread to maintain the validator's `EpochDuties`.
let duties_manager_thread = { let duties_manager_thread = {