Update types to use Slot newtype

All dependant functions haven't yet been updated.
This commit is contained in:
Paul Hauner 2019-02-05 19:27:12 +11:00
parent dbe9112848
commit 2aa7d80a5f
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
12 changed files with 59 additions and 59 deletions

View File

@ -1,5 +1,5 @@
use super::{AttestationDataAndCustodyBit, Hash256};
use crate::test_utils::TestRandom;
use crate::{AttestationDataAndCustodyBit, Hash256, Slot};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
@ -17,13 +17,13 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = {
#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash)]
pub struct AttestationData {
pub slot: u64,
pub slot: Slot,
pub shard: u64,
pub beacon_block_root: Hash256,
pub epoch_boundary_root: Hash256,
pub shard_block_root: Hash256,
pub latest_crosslink_root: Hash256,
pub justified_slot: u64,
pub justified_slot: Slot,
pub justified_block_root: Hash256,
}
@ -32,13 +32,13 @@ impl Eq for AttestationData {}
impl AttestationData {
pub fn zero() -> Self {
Self {
slot: 0,
slot: Slot::from(0_u64),
shard: 0,
beacon_block_root: Hash256::zero(),
epoch_boundary_root: Hash256::zero(),
shard_block_root: Hash256::zero(),
latest_crosslink_root: Hash256::zero(),
justified_slot: 0,
justified_slot: Slot::from(0_u64),
justified_block_root: Hash256::zero(),
}
}
@ -71,14 +71,14 @@ impl Encodable for AttestationData {
impl Decodable for AttestationData {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (slot, i) = u64::ssz_decode(bytes, i)?;
let (shard, i) = u64::ssz_decode(bytes, i)?;
let (beacon_block_root, i) = Hash256::ssz_decode(bytes, i)?;
let (epoch_boundary_root, i) = Hash256::ssz_decode(bytes, i)?;
let (shard_block_root, i) = Hash256::ssz_decode(bytes, i)?;
let (latest_crosslink_root, i) = Hash256::ssz_decode(bytes, i)?;
let (justified_slot, i) = u64::ssz_decode(bytes, i)?;
let (justified_block_root, i) = Hash256::ssz_decode(bytes, i)?;
let (slot, i) = <_>::ssz_decode(bytes, i)?;
let (shard, i) = <_>::ssz_decode(bytes, i)?;
let (beacon_block_root, i) = <_>::ssz_decode(bytes, i)?;
let (epoch_boundary_root, i) = <_>::ssz_decode(bytes, i)?;
let (shard_block_root, i) = <_>::ssz_decode(bytes, i)?;
let (latest_crosslink_root, i) = <_>::ssz_decode(bytes, i)?;
let (justified_slot, i) = <_>::ssz_decode(bytes, i)?;
let (justified_block_root, i) = <_>::ssz_decode(bytes, i)?;
let attestation_data = AttestationData {
slot,

View File

@ -1,5 +1,5 @@
use super::{BeaconBlockBody, ChainSpec, Eth1Data, Hash256, ProposalSignedData};
use crate::test_utils::TestRandom;
use crate::{BeaconBlockBody, ChainSpec, Eth1Data, Hash256, ProposalSignedData, Slot};
use bls::Signature;
use rand::RngCore;
use serde_derive::Serialize;
@ -7,7 +7,7 @@ use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize)]
pub struct BeaconBlock {
pub slot: u64,
pub slot: Slot,
pub parent_root: Hash256,
pub state_root: Hash256,
pub randao_reveal: Signature,

View File

@ -2,7 +2,7 @@ use crate::test_utils::TestRandom;
use crate::{
validator::StatusFlags, validator_registry::get_active_validator_indices, AggregatePublicKey,
Attestation, AttestationData, BeaconBlock, Bitfield, ChainSpec, Crosslink, Eth1Data,
Eth1DataVote, Exit, Fork, Hash256, PendingAttestation, PublicKey, Signature, Validator,
Eth1DataVote, Exit, Fork, Hash256, PendingAttestation, PublicKey, Signature, Slot, Validator,
};
use bls::bls_verify_aggregate;
use honey_badger_split::SplitExt;
@ -151,14 +151,14 @@ type CustodyChallenge = usize;
#[derive(Debug, PartialEq, Clone, Default, Serialize)]
pub struct BeaconState {
// Misc
pub slot: u64,
pub slot: Slot,
pub genesis_time: u64,
pub fork_data: Fork,
// Validator registry
pub validator_registry: Vec<Validator>,
pub validator_balances: Vec<u64>,
pub validator_registry_update_slot: u64,
pub validator_registry_update_slot: Slot,
pub validator_registry_exit_count: u64,
pub validator_registry_delta_chain_tip: Hash256,
@ -167,8 +167,8 @@ pub struct BeaconState {
pub latest_vdf_outputs: Vec<Hash256>,
pub previous_epoch_start_shard: u64,
pub current_epoch_start_shard: u64,
pub previous_epoch_calculation_slot: u64,
pub current_epoch_calculation_slot: u64,
pub previous_epoch_calculation_slot: Slot,
pub current_epoch_calculation_slot: Slot,
pub previous_epoch_seed: Hash256,
pub current_epoch_seed: Hash256,
@ -176,10 +176,10 @@ pub struct BeaconState {
pub custody_challenges: Vec<CustodyChallenge>,
// Finality
pub previous_justified_slot: u64,
pub justified_slot: u64,
pub previous_justified_slot: Slot,
pub justified_slot: Slot,
pub justification_bitfield: u64,
pub finalized_slot: u64,
pub finalized_slot: Slot,
// Recent state
pub latest_crosslinks: Vec<Crosslink>,

View File

@ -1,12 +1,12 @@
use super::Hash256;
use crate::test_utils::TestRandom;
use crate::{Hash256, Slot};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Crosslink {
pub slot: u64,
pub slot: Slot,
pub shard_block_root: Hash256,
}
@ -14,7 +14,7 @@ impl Crosslink {
/// Generates a new instance where `dynasty` and `hash` are both zero.
pub fn zero() -> Self {
Self {
slot: 0,
slot: Slot::from(0_u64),
shard_block_root: Hash256::zero(),
}
}

View File

@ -1,4 +1,4 @@
use crate::test_utils::TestRandom;
use crate::{test_utils::TestRandom, Slot};
use bls::Signature;
use rand::RngCore;
use serde_derive::Serialize;
@ -6,7 +6,7 @@ use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize)]
pub struct Exit {
pub slot: u64,
pub slot: Slot,
pub validator_index: u32,
pub signature: Signature,
}

View File

@ -1,4 +1,4 @@
use crate::test_utils::TestRandom;
use crate::{test_utils::TestRandom, Slot};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
@ -7,7 +7,7 @@ use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
pub struct Fork {
pub pre_fork_version: u64,
pub post_fork_version: u64,
pub fork_slot: u64,
pub fork_slot: Slot,
}
impl Encodable for Fork {

View File

@ -55,6 +55,7 @@ pub use crate::proposal_signed_data::ProposalSignedData;
pub use crate::proposer_slashing::ProposerSlashing;
pub use crate::shard_committee::ShardCommittee;
pub use crate::slashable_vote_data::SlashableVoteData;
pub use crate::slot_epoch::{Epoch, Slot};
pub use crate::spec::ChainSpec;
pub use crate::special_record::{SpecialRecord, SpecialRecordKind};
pub use crate::validator::{StatusFlags as ValidatorStatusFlags, Validator};

View File

@ -1,5 +1,5 @@
use super::{AttestationData, Bitfield};
use crate::test_utils::TestRandom;
use crate::{AttestationData, Bitfield, Slot};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
@ -9,7 +9,7 @@ pub struct PendingAttestation {
pub data: AttestationData,
pub aggregation_bitfield: Bitfield,
pub custody_bitfield: Bitfield,
pub slot_included: u64,
pub slot_included: Slot,
}
impl Encodable for PendingAttestation {

View File

@ -1,12 +1,12 @@
use super::Hash256;
use crate::test_utils::TestRandom;
use crate::{Hash256, Slot};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
#[derive(Debug, PartialEq, Clone, Default, Serialize)]
pub struct ProposalSignedData {
pub slot: u64,
pub slot: Slot,
pub shard: u64,
pub block_root: Hash256,
}

View File

@ -1,4 +1,4 @@
use crate::test_utils::TestRandom;
use crate::{test_utils::TestRandom, Slot};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
@ -7,7 +7,7 @@ use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
pub struct ShardReassignmentRecord {
pub validator_index: u64,
pub shard: u64,
pub slot: u64,
pub slot: Slot,
}
impl Encodable for ShardReassignmentRecord {

View File

@ -1,5 +1,4 @@
use super::Hash256;
use crate::{test_utils::TestRandom, PublicKey};
use crate::{test_utils::TestRandom, Hash256, PublicKey, Slot};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
@ -47,20 +46,20 @@ fn status_flag_from_byte(flag: u8) -> Result<Option<StatusFlags>, StatusFlagsDec
pub struct Validator {
pub pubkey: PublicKey,
pub withdrawal_credentials: Hash256,
pub proposer_slots: u64,
pub activation_slot: u64,
pub exit_slot: u64,
pub withdrawal_slot: u64,
pub penalized_slot: u64,
pub proposer_slots: Slot,
pub activation_slot: Slot,
pub exit_slot: Slot,
pub withdrawal_slot: Slot,
pub penalized_slot: Slot,
pub exit_count: u64,
pub status_flags: Option<StatusFlags>,
pub latest_custody_reseed_slot: u64,
pub penultimate_custody_reseed_slot: u64,
pub latest_custody_reseed_slot: Slot,
pub penultimate_custody_reseed_slot: Slot,
}
impl Validator {
/// This predicate indicates if the validator represented by this record is considered "active" at `slot`.
pub fn is_active_at(&self, slot: u64) -> bool {
pub fn is_active_at(&self, slot: Slot) -> bool {
self.activation_slot <= slot && slot < self.exit_slot
}
}
@ -71,15 +70,15 @@ impl Default for Validator {
Self {
pubkey: PublicKey::default(),
withdrawal_credentials: Hash256::default(),
proposer_slots: 0,
activation_slot: std::u64::MAX,
exit_slot: std::u64::MAX,
withdrawal_slot: std::u64::MAX,
penalized_slot: std::u64::MAX,
proposer_slots: Slot::from(0_u64),
activation_slot: Slot::from(std::u64::MAX),
exit_slot: Slot::from(std::u64::MAX),
withdrawal_slot: Slot::from(std::u64::MAX),
penalized_slot: Slot::from(std::u64::MAX),
exit_count: 0,
status_flags: None,
latest_custody_reseed_slot: 0, // NOTE: is `GENESIS_SLOT`
penultimate_custody_reseed_slot: 0, // NOTE: is `GENESIS_SLOT`
latest_custody_reseed_slot: Slot::from(0_u64), // NOTE: is `GENESIS_SLOT`
penultimate_custody_reseed_slot: Slot::from(0_u64), // NOTE: is `GENESIS_SLOT`
}
}
}
@ -203,10 +202,11 @@ mod tests {
let activation_slot = u64::random_for_test(&mut rng);
let exit_slot = activation_slot + 234;
validator.activation_slot = activation_slot;
validator.exit_slot = exit_slot;
validator.activation_slot = Slot::from(activation_slot);
validator.exit_slot = Slot::from(exit_slot);
for slot in (activation_slot - 100)..(exit_slot + 100) {
let slot = Slot::from(slot);
if slot < activation_slot {
assert!(!validator.is_active_at(slot));
} else if slot >= exit_slot {

View File

@ -1,5 +1,4 @@
use super::Hash256;
use crate::test_utils::TestRandom;
use crate::{test_utils::TestRandom, Hash256, Slot};
use bls::PublicKey;
use rand::RngCore;
use serde_derive::Serialize;
@ -11,7 +10,7 @@ pub struct ValidatorRegistryDeltaBlock {
pub latest_registry_delta_root: Hash256,
pub validator_index: u32,
pub pubkey: PublicKey,
pub slot: u64,
pub slot: Slot,
pub flag: u64,
}
@ -22,7 +21,7 @@ impl Default for ValidatorRegistryDeltaBlock {
latest_registry_delta_root: Hash256::zero(),
validator_index: std::u32::MAX,
pubkey: PublicKey::default(),
slot: std::u64::MAX,
slot: Slot::from(std::u64::MAX),
flag: std::u64::MAX,
}
}