Further v0.4.0 updates to types crate

This commit is contained in:
Paul Hauner 2019-03-06 10:21:55 +11:00
parent 96ec53c6a8
commit 0be8e57fd3
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
8 changed files with 39 additions and 29 deletions

View File

@ -40,7 +40,7 @@ impl AttesterSlashingBuilder {
crosslink_data_root: hash_1, crosslink_data_root: hash_1,
latest_crosslink: Crosslink { latest_crosslink: Crosslink {
epoch, epoch,
shard_block_root: hash_1, crosslink_data_root: hash_1,
}, },
justified_epoch, justified_epoch,
justified_block_root: hash_1, justified_block_root: hash_1,
@ -59,7 +59,7 @@ impl AttesterSlashingBuilder {
crosslink_data_root: hash_2, crosslink_data_root: hash_2,
latest_crosslink: Crosslink { latest_crosslink: Crosslink {
epoch, epoch,
shard_block_root: hash_2, crosslink_data_root: hash_2,
}, },
justified_epoch, justified_epoch,
justified_block_root: hash_2, justified_block_root: hash_2,

View File

@ -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 crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
@ -14,7 +14,7 @@ pub struct BeaconBlockBody {
pub attester_slashings: Vec<AttesterSlashing>, pub attester_slashings: Vec<AttesterSlashing>,
pub attestations: Vec<Attestation>, pub attestations: Vec<Attestation>,
pub deposits: Vec<Deposit>, pub deposits: Vec<Deposit>,
pub voluntary_exits: Vec<VolutaryExit>, pub voluntary_exits: Vec<VoluntaryExit>,
pub transfers: Vec<Transfer>, pub transfers: Vec<Transfer>,
} }

View File

@ -2,6 +2,7 @@ use self::epoch_cache::EpochCache;
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use crate::{validator_registry::get_active_validator_indices, *}; use crate::{validator_registry::get_active_validator_indices, *};
use bls::verify_proof_of_possession; use bls::verify_proof_of_possession;
use helpers::*;
use honey_badger_split::SplitExt; use honey_badger_split::SplitExt;
use int_to_bytes::int_to_bytes32; use int_to_bytes::int_to_bytes32;
use log::{debug, error, trace}; use log::{debug, error, trace};
@ -16,6 +17,7 @@ pub use builder::BeaconStateBuilder;
mod builder; mod builder;
mod epoch_cache; mod epoch_cache;
pub mod helpers;
mod tests; mod tests;
pub type Committee = Vec<usize>; pub type Committee = Vec<usize>;
@ -44,6 +46,7 @@ pub enum Error {
ShardOutOfBounds, ShardOutOfBounds,
UnableToShuffle, UnableToShuffle,
UnknownValidator, UnknownValidator,
InvalidBitfield,
InsufficientRandaoMixes, InsufficientRandaoMixes,
InsufficientValidators, InsufficientValidators,
InsufficientBlockRoots, InsufficientBlockRoots,
@ -125,7 +128,7 @@ impl BeaconState {
debug!("Creating genesis state (without validator processing)."); debug!("Creating genesis state (without validator processing).");
let initial_crosslink = Crosslink { let initial_crosslink = Crosslink {
epoch: spec.genesis_epoch, epoch: spec.genesis_epoch,
shard_block_root: spec.zero_hash, crosslink_data_root: spec.zero_hash,
}; };
Ok(BeaconState { Ok(BeaconState {
@ -530,6 +533,10 @@ impl BeaconState {
assert_eq!(*shard, attestation_data.shard, "Bad epoch cache build."); 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![]; let mut participants = vec![];
for (i, validator_index) in committee.iter().enumerate() { for (i, validator_index) in committee.iter().enumerate() {
if bitfield.get(i).unwrap() { if bitfield.get(i).unwrap() {
@ -559,23 +566,6 @@ impl BeaconState {
.fold(0, |acc, i| acc + self.get_effective_balance(*i, spec)) .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. /// Verify validity of ``slashable_attestation`` fields.
/// ///
/// Spec v0.4.0 /// Spec v0.4.0
@ -600,7 +590,7 @@ impl BeaconState {
} }
} }
if !self.verify_bitfield( if !verify_bitfield_length(
&slashable_attestation.custody_bitfield, &slashable_attestation.custody_bitfield,
slashable_attestation.validator_indices.len(), slashable_attestation.validator_indices.len(),
) { ) {

View File

@ -252,7 +252,7 @@ fn committee_to_pending_attestation(
crosslink_data_root: Hash256::zero(), crosslink_data_root: Hash256::zero(),
latest_crosslink: Crosslink { latest_crosslink: Crosslink {
epoch: slot.epoch(spec.slots_per_epoch), epoch: slot.epoch(spec.slots_per_epoch),
shard_block_root: Hash256::zero(), crosslink_data_root: Hash256::zero(),
}, },
justified_epoch, justified_epoch,
justified_block_root, justified_block_root,

View File

@ -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
}

View File

@ -13,7 +13,7 @@ use test_random_derive::TestRandom;
)] )]
pub struct Crosslink { pub struct Crosslink {
pub epoch: Epoch, pub epoch: Epoch,
pub shard_block_root: Hash256, pub crosslink_data_root: Hash256,
} }
#[cfg(test)] #[cfg(test)]

View File

@ -60,7 +60,7 @@ pub use crate::slot_epoch::{Epoch, Slot};
pub use crate::slot_height::SlotHeight; pub use crate::slot_height::SlotHeight;
pub use crate::transfer::Transfer; pub use crate::transfer::Transfer;
pub use crate::validator::Validator; pub use crate::validator::Validator;
pub use crate::voluntary_exit::VolutaryExit; pub use crate::voluntary_exit::VoluntaryExit;
pub type Hash256 = H256; pub type Hash256 = H256;
pub type Address = H160; pub type Address = H160;

View File

@ -10,7 +10,7 @@ use test_random_derive::TestRandom;
/// ///
/// Spec v0.4.0 /// Spec v0.4.0
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom, SignedRoot)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom, SignedRoot)]
pub struct VolutaryExit { pub struct VoluntaryExit {
pub epoch: Epoch, pub epoch: Epoch,
pub validator_index: u64, pub validator_index: u64,
pub signature: Signature, pub signature: Signature,
@ -25,7 +25,7 @@ mod tests {
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
let mut rng = XorShiftRng::from_seed([42; 16]); 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 bytes = ssz_encode(&original);
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap(); let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
@ -36,7 +36,7 @@ mod tests {
#[test] #[test]
pub fn test_hash_tree_root_internal() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); 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(); let result = original.hash_tree_root_internal();