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

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)?;

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,

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,

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();

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)
};

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
}

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])
}
}

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 {