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
29 changed files with 113 additions and 112 deletions
+3 -3
View File
@@ -195,9 +195,9 @@ mod tests {
let beacon_node = Arc::new(SimulatedBeaconNode::default());
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_epoch = attest_slot / spec.epoch_length;
let attest_epoch = attest_slot / spec.slots_per_epoch;
let attest_shard = 12;
duties.insert_attestation_shard(attest_slot, attest_shard);
duties.set_validator_index(Some(2));
@@ -243,7 +243,7 @@ mod tests {
);
// 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());
assert_eq!(
attester.poll(),
+5 -5
View File
@@ -3,22 +3,22 @@ use std::collections::HashMap;
use types::{Epoch, Slot};
pub struct EpochMap {
epoch_length: u64,
slots_per_epoch: u64,
validator_index: Option<u64>,
map: HashMap<Epoch, (Slot, u64)>,
}
impl EpochMap {
pub fn new(epoch_length: u64) -> Self {
pub fn new(slots_per_epoch: u64) -> Self {
Self {
epoch_length,
slots_per_epoch,
validator_index: None,
map: HashMap::new(),
}
}
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));
}
@@ -29,7 +29,7 @@ impl EpochMap {
impl DutiesReader for EpochMap {
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) {
Some((attest_slot, attest_shard)) if *attest_slot == slot => Ok(Some(*attest_shard)),
+4 -4
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> {
let randao_reveal = {
// 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
.signer
@@ -233,9 +233,9 @@ mod tests {
let beacon_node = Arc::new(SimulatedBeaconNode::default());
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_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);
let epoch_map = Arc::new(epoch_map);
@@ -280,7 +280,7 @@ mod tests {
);
// 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);
assert_eq!(
block_proposer.poll(),
@@ -3,14 +3,14 @@ use std::collections::HashMap;
use types::{Epoch, Slot};
pub struct EpochMap {
epoch_length: u64,
slots_per_epoch: u64,
pub map: HashMap<Epoch, Slot>,
}
impl EpochMap {
pub fn new(epoch_length: u64) -> Self {
pub fn new(slots_per_epoch: u64) -> Self {
Self {
epoch_length,
slots_per_epoch,
map: HashMap::new(),
}
}
@@ -18,7 +18,7 @@ impl EpochMap {
impl DutiesReader for EpochMap {
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) {
Some(s) if *s == slot => Ok(true),
Some(s) if *s != slot => Ok(false),
+1 -1
View File
@@ -95,7 +95,7 @@ where
let active_validator_indices = get_active_validator_indices(
&current_state.validator_registry[..],
block_slot.epoch(spec.epoch_length),
block_slot.epoch(spec.slots_per_epoch),
);
for index in active_validator_indices {
+1 -1
View File
@@ -64,7 +64,7 @@ where
let active_validator_indices = get_active_validator_indices(
&current_state.validator_registry[..],
block_slot.epoch(spec.epoch_length),
block_slot.epoch(spec.slots_per_epoch),
);
for index in active_validator_indices {
@@ -185,7 +185,7 @@ fn per_block_processing_signature_optional(
proposer_slashing
.proposal_data_1
.slot
.epoch(spec.epoch_length),
.epoch(spec.slots_per_epoch),
spec.domain_proposal
)
),
@@ -201,7 +201,7 @@ fn per_block_processing_signature_optional(
proposer_slashing
.proposal_data_2
.slot
.epoch(spec.epoch_length),
.epoch(spec.slots_per_epoch),
spec.domain_proposal
)
),
@@ -341,14 +341,14 @@ fn validate_attestation_signature_optional(
) -> Result<(), AttestationValidationError> {
trace!(
"validate_attestation_signature_optional: attestation epoch: {}",
attestation.data.slot.epoch(spec.epoch_length)
attestation.data.slot.epoch(spec.slots_per_epoch)
);
ensure!(
attestation.data.slot + spec.min_attestation_inclusion_delay <= state.slot,
AttestationValidationError::IncludedTooEarly
);
ensure!(
attestation.data.slot + spec.epoch_length >= state.slot,
attestation.data.slot + spec.slots_per_epoch >= state.slot,
AttestationValidationError::IncludedTooLate
);
if attestation.data.slot >= state.current_epoch_start_slot(spec) {
@@ -369,7 +369,7 @@ fn validate_attestation_signature_optional(
attestation
.data
.justified_epoch
.start_slot(spec.epoch_length),
.start_slot(spec.slots_per_epoch),
&spec
)
.ok_or(AttestationValidationError::NoBlockRoot)?,
@@ -377,7 +377,7 @@ fn validate_attestation_signature_optional(
);
let potential_crosslink = Crosslink {
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!(
(attestation.data.latest_crosslink
@@ -407,7 +407,7 @@ fn validate_attestation_signature_optional(
PHASE_0_CUSTODY_BIT,
get_domain(
&state.fork,
attestation.data.slot.epoch(spec.epoch_length),
attestation.data.slot.epoch(spec.slots_per_epoch),
spec.domain_attestation,
)
),
@@ -76,7 +76,7 @@ impl EpochProcessable for BeaconState {
*/
let active_validator_indices = get_active_validator_indices(
&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);
@@ -90,7 +90,7 @@ impl EpochProcessable for BeaconState {
.latest_attestations
.par_iter()
.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)
})
.collect();
@@ -137,7 +137,7 @@ impl EpochProcessable for BeaconState {
.par_iter()
.filter(|a| {
//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)
})
.collect();
@@ -320,12 +320,12 @@ impl EpochProcessable for BeaconState {
let mut winning_root_for_shards: HashMap<u64, Result<WinningRoot, WinningRootError>> =
HashMap::new();
// 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.slot.saturating_sub(2 * spec.slots_per_epoch)..self.slot {
for slot in self.previous_epoch(spec).slot_iter(spec.slots_per_epoch) {
trace!(
"Finding winning root for slot: {} (epoch: {})",
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
@@ -506,7 +506,7 @@ impl EpochProcessable for BeaconState {
/*
* 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
// `self.balances`.
let crosslink_committees_at_slot =
@@ -615,7 +615,7 @@ impl EpochProcessable for BeaconState {
self.latest_attestations = self
.latest_attestations
.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()
.collect();
@@ -24,7 +24,7 @@ where
previous_block_root: Hash256,
spec: &ChainSpec,
) -> 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.advance_caches();
}
+13 -13
View File
@@ -337,7 +337,7 @@ impl BeaconState {
///
/// Spec v0.2.0
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()`.
@@ -363,14 +363,14 @@ impl BeaconState {
///
/// Spec v0.2.0
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`.
///
/// Spec v0.2.0
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.
@@ -386,10 +386,10 @@ impl BeaconState {
std::cmp::max(
1,
std::cmp::min(
spec.shard_count / spec.epoch_length,
active_validator_count as u64 / spec.epoch_length / spec.target_committee_size,
spec.shard_count / spec.slots_per_epoch,
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``.
@@ -520,11 +520,11 @@ impl BeaconState {
slot: Slot,
spec: &ChainSpec,
) -> 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 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()])
}
@@ -565,7 +565,7 @@ impl BeaconState {
registry_change: bool,
spec: &ChainSpec,
) -> 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 previous_epoch = self.previous_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) =
self.get_committee_params_at_slot(slot, registry_change, spec)?;
let offset = slot.as_u64() % spec.epoch_length;
let committees_per_slot = committees_per_epoch / spec.epoch_length;
let offset = slot.as_u64() % spec.slots_per_epoch;
let committees_per_slot = committees_per_epoch / spec.slots_per_epoch;
let slot_start_shard =
(shuffling_start_shard + committees_per_slot * offset) % spec.shard_count;
@@ -835,7 +835,7 @@ impl BeaconState {
proof_of_possession.verify(
&proof_of_possession_data.hash_tree_root(),
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,
)
}
@@ -1296,7 +1296,7 @@ impl BeaconState {
bitfield: &Bitfield,
spec: &ChainSpec,
) -> 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 cache = self.cache(relative_epoch)?;
+10 -9
View File
@@ -137,7 +137,7 @@ impl BeaconStateBuilder {
pub fn teleport_to_end_of_epoch(&mut self, epoch: Epoch) {
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.validator_registry_update_epoch = epoch - 1;
@@ -171,11 +171,11 @@ impl BeaconStateBuilder {
let current_epoch = state.current_epoch(&self.spec);
let previous_epoch = state.previous_epoch(&self.spec);
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
.slot_iter(self.spec.epoch_length)
.slot_iter(self.spec.slots_per_epoch)
.take(current_epoch_depth);
for slot in previous_epoch_slots.chain(current_epoch_slots) {
@@ -219,7 +219,8 @@ fn committee_to_pending_attestation(
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 {
state.previous_justified_epoch
@@ -229,16 +230,16 @@ fn committee_to_pending_attestation(
let epoch_boundary_root = if is_previous_epoch {
*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()
} else {
*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()
};
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();
PendingAttestation {
@@ -250,7 +251,7 @@ fn committee_to_pending_attestation(
epoch_boundary_root,
shard_block_root: Hash256::zero(),
latest_crosslink: Crosslink {
epoch: slot.epoch(spec.epoch_length),
epoch: slot.epoch(spec.slots_per_epoch),
shard_block_root: Hash256::zero(),
},
justified_epoch,
+3 -3
View File
@@ -32,14 +32,14 @@ impl EpochCache {
spec: &ChainSpec,
) -> Result<EpochCache, Error> {
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 shard_committee_index_map: ShardCommitteeIndexMap = HashMap::new();
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(
slot,
false,
+2 -2
View File
@@ -35,8 +35,8 @@ pub fn get_attestation_participants_consistency() {
for slot in state
.slot
.epoch(spec.epoch_length)
.slot_iter(spec.epoch_length)
.epoch(spec.slots_per_epoch)
.slot_iter(spec.slots_per_epoch)
{
let committees = state.get_crosslink_committees_at_slot(slot, &spec).unwrap();
+2 -2
View File
@@ -38,14 +38,14 @@ impl ProposerSlashingBuilder {
proposal_1.signature = {
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;
signer(proposer_index, &message[..], epoch, domain)
};
proposal_2.signature = {
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;
signer(proposer_index, &message[..], epoch, domain)
};
+4 -4
View File
@@ -24,7 +24,7 @@ impl SlashableAttestation {
///
/// Spec v0.4.0
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``.
@@ -33,8 +33,8 @@ impl SlashableAttestation {
pub fn is_surround_vote(&self, other: &SlashableAttestation, spec: &ChainSpec) -> bool {
let source_epoch_1 = self.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_2 = other.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.slots_per_epoch);
(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 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
}
+15 -15
View File
@@ -35,8 +35,8 @@ impl Slot {
Slot(slot)
}
pub fn epoch(self, epoch_length: u64) -> Epoch {
Epoch::from(self.0 / epoch_length)
pub fn epoch(self, slots_per_epoch: u64) -> Epoch {
Epoch::from(self.0 / slots_per_epoch)
}
pub fn height(self, genesis_slot: Slot) -> SlotHeight {
@@ -57,24 +57,24 @@ impl Epoch {
Epoch(u64::max_value())
}
pub fn start_slot(self, epoch_length: u64) -> Slot {
Slot::from(self.0.saturating_mul(epoch_length))
pub fn start_slot(self, slots_per_epoch: u64) -> Slot {
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(
self.0
.saturating_add(1)
.saturating_mul(epoch_length)
.saturating_mul(slots_per_epoch)
.saturating_sub(1),
)
}
pub fn slot_iter(&self, epoch_length: u64) -> SlotIter {
pub fn slot_iter(&self, slots_per_epoch: u64) -> SlotIter {
SlotIter {
current_iteration: 0,
epoch: self,
epoch_length,
slots_per_epoch,
}
}
}
@@ -82,17 +82,17 @@ impl Epoch {
pub struct SlotIter<'a> {
current_iteration: u64,
epoch: &'a Epoch,
epoch_length: u64,
slots_per_epoch: u64,
}
impl<'a> Iterator for SlotIter<'a> {
type Item = Slot;
fn next(&mut self) -> Option<Slot> {
if self.current_iteration >= self.epoch_length {
if self.current_iteration >= self.slots_per_epoch {
None
} 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;
self.current_iteration += 1;
Some(start_slot + previous)
@@ -119,18 +119,18 @@ mod epoch_tests {
#[test]
fn slot_iter() {
let epoch_length = 8;
let slots_per_epoch = 8;
let epoch = Epoch::new(0);
let mut slots = vec![];
for slot in epoch.slot_iter(epoch_length) {
for slot in epoch.slot_iter(slots_per_epoch) {
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])
}
}
+2 -2
View File
@@ -23,8 +23,8 @@ impl SlotHeight {
Slot::from(self.0.saturating_add(genesis_slot.as_u64()))
}
pub fn epoch(self, genesis_slot: u64, epoch_length: u64) -> Epoch {
Epoch::from(self.0.saturating_add(genesis_slot) / epoch_length)
pub fn epoch(self, genesis_slot: u64, slots_per_epoch: u64) -> Epoch {
Epoch::from(self.0.saturating_add(genesis_slot) / slots_per_epoch)
}
pub fn max_value() -> SlotHeight {