diff --git a/eth2/types/src/attestation_data.rs b/eth2/types/src/attestation_data.rs index e2140527b6..bf8bc708e4 100644 --- a/eth2/types/src/attestation_data.rs +++ b/eth2/types/src/attestation_data.rs @@ -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, diff --git a/eth2/types/src/beacon_block.rs b/eth2/types/src/beacon_block.rs index 3e2c51ede7..2e844859a3 100644 --- a/eth2/types/src/beacon_block.rs +++ b/eth2/types/src/beacon_block.rs @@ -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, diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 39787ab32e..362460c0ce 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -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, pub validator_balances: Vec, - 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, 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, // 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, diff --git a/eth2/types/src/crosslink.rs b/eth2/types/src/crosslink.rs index f727d7b438..ca53baad78 100644 --- a/eth2/types/src/crosslink.rs +++ b/eth2/types/src/crosslink.rs @@ -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(), } } diff --git a/eth2/types/src/exit.rs b/eth2/types/src/exit.rs index 97f1fd2868..846d204949 100644 --- a/eth2/types/src/exit.rs +++ b/eth2/types/src/exit.rs @@ -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, } diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index c5a06caea9..c147c03bc5 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -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 { diff --git a/eth2/types/src/lib.rs b/eth2/types/src/lib.rs index c74fcd989c..631d6fc580 100644 --- a/eth2/types/src/lib.rs +++ b/eth2/types/src/lib.rs @@ -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}; diff --git a/eth2/types/src/pending_attestation.rs b/eth2/types/src/pending_attestation.rs index d2af528266..7957ca93c1 100644 --- a/eth2/types/src/pending_attestation.rs +++ b/eth2/types/src/pending_attestation.rs @@ -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 { diff --git a/eth2/types/src/proposal_signed_data.rs b/eth2/types/src/proposal_signed_data.rs index 829a16987d..c57eb1e2ad 100644 --- a/eth2/types/src/proposal_signed_data.rs +++ b/eth2/types/src/proposal_signed_data.rs @@ -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, } diff --git a/eth2/types/src/shard_reassignment_record.rs b/eth2/types/src/shard_reassignment_record.rs index a239233df9..61f68ac051 100644 --- a/eth2/types/src/shard_reassignment_record.rs +++ b/eth2/types/src/shard_reassignment_record.rs @@ -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 { diff --git a/eth2/types/src/validator.rs b/eth2/types/src/validator.rs index 5c7d0ad308..2f535adad8 100644 --- a/eth2/types/src/validator.rs +++ b/eth2/types/src/validator.rs @@ -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, 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, - 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 { diff --git a/eth2/types/src/validator_registry_delta_block.rs b/eth2/types/src/validator_registry_delta_block.rs index 196da754d0..3142e02637 100644 --- a/eth2/types/src/validator_registry_delta_block.rs +++ b/eth2/types/src/validator_registry_delta_block.rs @@ -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, } }