diff --git a/eth2/types/src/attester_slashing/builder.rs b/eth2/types/src/attester_slashing/builder.rs index 54dfae959..6638eb2a5 100644 --- a/eth2/types/src/attester_slashing/builder.rs +++ b/eth2/types/src/attester_slashing/builder.rs @@ -40,7 +40,7 @@ impl AttesterSlashingBuilder { crosslink_data_root: hash_1, latest_crosslink: Crosslink { epoch, - shard_block_root: hash_1, + crosslink_data_root: hash_1, }, justified_epoch, justified_block_root: hash_1, @@ -59,7 +59,7 @@ impl AttesterSlashingBuilder { crosslink_data_root: hash_2, latest_crosslink: Crosslink { epoch, - shard_block_root: hash_2, + crosslink_data_root: hash_2, }, justified_epoch, justified_block_root: hash_2, diff --git a/eth2/types/src/beacon_block_body.rs b/eth2/types/src/beacon_block_body.rs index 13c82e42f..e7dec2e4b 100644 --- a/eth2/types/src/beacon_block_body.rs +++ b/eth2/types/src/beacon_block_body.rs @@ -1,4 +1,4 @@ -use super::{Attestation, AttesterSlashing, Deposit, ProposerSlashing, Transfer, VolutaryExit}; +use super::{Attestation, AttesterSlashing, Deposit, ProposerSlashing, Transfer, VoluntaryExit}; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; @@ -14,7 +14,7 @@ pub struct BeaconBlockBody { pub attester_slashings: Vec, pub attestations: Vec, pub deposits: Vec, - pub voluntary_exits: Vec, + pub voluntary_exits: Vec, pub transfers: Vec, } diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index d876adc62..2394c2c0d 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -2,6 +2,7 @@ use self::epoch_cache::EpochCache; use crate::test_utils::TestRandom; use crate::{validator_registry::get_active_validator_indices, *}; use bls::verify_proof_of_possession; +use helpers::*; use honey_badger_split::SplitExt; use int_to_bytes::int_to_bytes32; use log::{debug, error, trace}; @@ -16,6 +17,7 @@ pub use builder::BeaconStateBuilder; mod builder; mod epoch_cache; +pub mod helpers; mod tests; pub type Committee = Vec; @@ -44,6 +46,7 @@ pub enum Error { ShardOutOfBounds, UnableToShuffle, UnknownValidator, + InvalidBitfield, InsufficientRandaoMixes, InsufficientValidators, InsufficientBlockRoots, @@ -125,7 +128,7 @@ impl BeaconState { debug!("Creating genesis state (without validator processing)."); let initial_crosslink = Crosslink { epoch: spec.genesis_epoch, - shard_block_root: spec.zero_hash, + crosslink_data_root: spec.zero_hash, }; Ok(BeaconState { @@ -530,6 +533,10 @@ impl BeaconState { assert_eq!(*shard, attestation_data.shard, "Bad epoch cache build."); + if !verify_bitfield_length(&bitfield, committee.len()) { + return Err(Error::InvalidBitfield); + } + let mut participants = vec![]; for (i, validator_index) in committee.iter().enumerate() { if bitfield.get(i).unwrap() { @@ -559,23 +566,6 @@ impl BeaconState { .fold(0, |acc, i| acc + self.get_effective_balance(*i, spec)) } - /// Verify ``bitfield`` against the ``committee_size``. - /// - /// Spec v0.4.0 - pub fn verify_bitfield(&self, bitfield: &Bitfield, committee_size: usize) -> bool { - if bitfield.num_bytes() != ((committee_size + 7) / 8) { - return false; - } - - for i in committee_size..(bitfield.num_bytes() * 8) { - if bitfield.get(i).expect("Impossible due to previous check.") { - return false; - } - } - - true - } - /// Verify validity of ``slashable_attestation`` fields. /// /// Spec v0.4.0 @@ -600,7 +590,7 @@ impl BeaconState { } } - if !self.verify_bitfield( + if !verify_bitfield_length( &slashable_attestation.custody_bitfield, slashable_attestation.validator_indices.len(), ) { diff --git a/eth2/types/src/beacon_state/builder.rs b/eth2/types/src/beacon_state/builder.rs index 36b6468fc..dadce9cac 100644 --- a/eth2/types/src/beacon_state/builder.rs +++ b/eth2/types/src/beacon_state/builder.rs @@ -252,7 +252,7 @@ fn committee_to_pending_attestation( crosslink_data_root: Hash256::zero(), latest_crosslink: Crosslink { epoch: slot.epoch(spec.slots_per_epoch), - shard_block_root: Hash256::zero(), + crosslink_data_root: Hash256::zero(), }, justified_epoch, justified_block_root, diff --git a/eth2/types/src/beacon_state/helpers.rs b/eth2/types/src/beacon_state/helpers.rs index e69de29bb..c93b16f76 100644 --- a/eth2/types/src/beacon_state/helpers.rs +++ b/eth2/types/src/beacon_state/helpers.rs @@ -0,0 +1,20 @@ +use crate::*; + +/// Verify ``bitfield`` against the ``committee_size``. +/// +/// Is title `verify_bitfield` in spec. +/// +/// Spec v0.4.0 +pub fn verify_bitfield_length(bitfield: &Bitfield, committee_size: usize) -> bool { + if bitfield.num_bytes() != ((committee_size + 7) / 8) { + return false; + } + + for i in committee_size..(bitfield.num_bytes() * 8) { + if bitfield.get(i).expect("Impossible due to previous check.") { + return false; + } + } + + true +} diff --git a/eth2/types/src/crosslink.rs b/eth2/types/src/crosslink.rs index 142d0f894..f49195a75 100644 --- a/eth2/types/src/crosslink.rs +++ b/eth2/types/src/crosslink.rs @@ -13,7 +13,7 @@ use test_random_derive::TestRandom; )] pub struct Crosslink { pub epoch: Epoch, - pub shard_block_root: Hash256, + pub crosslink_data_root: Hash256, } #[cfg(test)] diff --git a/eth2/types/src/lib.rs b/eth2/types/src/lib.rs index f54a24b01..f88cc6a17 100644 --- a/eth2/types/src/lib.rs +++ b/eth2/types/src/lib.rs @@ -60,7 +60,7 @@ pub use crate::slot_epoch::{Epoch, Slot}; pub use crate::slot_height::SlotHeight; pub use crate::transfer::Transfer; pub use crate::validator::Validator; -pub use crate::voluntary_exit::VolutaryExit; +pub use crate::voluntary_exit::VoluntaryExit; pub type Hash256 = H256; pub type Address = H160; diff --git a/eth2/types/src/voluntary_exit.rs b/eth2/types/src/voluntary_exit.rs index 3cb0a85b7..58c3ae4c2 100644 --- a/eth2/types/src/voluntary_exit.rs +++ b/eth2/types/src/voluntary_exit.rs @@ -10,7 +10,7 @@ use test_random_derive::TestRandom; /// /// Spec v0.4.0 #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom, SignedRoot)] -pub struct VolutaryExit { +pub struct VoluntaryExit { pub epoch: Epoch, pub validator_index: u64, pub signature: Signature, @@ -25,7 +25,7 @@ mod tests { #[test] pub fn test_ssz_round_trip() { let mut rng = XorShiftRng::from_seed([42; 16]); - let original = VolutaryExit::random_for_test(&mut rng); + let original = VoluntaryExit::random_for_test(&mut rng); let bytes = ssz_encode(&original); let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap(); @@ -36,7 +36,7 @@ mod tests { #[test] pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); - let original = VolutaryExit::random_for_test(&mut rng); + let original = VoluntaryExit::random_for_test(&mut rng); let result = original.hash_tree_root_internal();