From 2defe8e4ee4e8b0ad5ad856937ff73eb51f61981 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Wed, 7 Nov 2018 14:32:33 -0800 Subject: [PATCH] get tests passing (except one) --- beacon_chain/types/src/lib.rs | 9 +++++-- .../utils/boolean-bitfield/src/lib.rs | 16 ++++++------ .../validation/src/attestation_validation.rs | 7 +++-- .../validation/src/signature_verification.rs | 15 ++++++++--- .../tests/attestation_validation/helpers.rs | 2 +- .../tests/attestation_validation/tests.rs | 26 +++++-------------- 6 files changed, 36 insertions(+), 39 deletions(-) diff --git a/beacon_chain/types/src/lib.rs b/beacon_chain/types/src/lib.rs index e8e42e8e9..c531da299 100644 --- a/beacon_chain/types/src/lib.rs +++ b/beacon_chain/types/src/lib.rs @@ -15,7 +15,11 @@ pub mod validator_record; pub mod validator_registration; use self::boolean_bitfield::BooleanBitfield; -use self::ethereum_types::{H160, H256, U256}; +use self::ethereum_types::{ + H256, + H160, + U256 +}; use std::collections::HashMap; pub use active_state::ActiveState; @@ -32,7 +36,8 @@ pub use validator_registration::ValidatorRegistration; pub type Hash256 = H256; pub type Address = H160; pub type EthBalance = U256; -pub type Bitfield = BooleanBitfield; +pub type Bitfield = boolean_bitfield::BooleanBitfield; +pub type BitfieldError = boolean_bitfield::Error; /// Maps a (slot, shard_id) to attestation_indices. pub type AttesterMap = HashMap<(u64, u16), Vec>; diff --git a/beacon_chain/utils/boolean-bitfield/src/lib.rs b/beacon_chain/utils/boolean-bitfield/src/lib.rs index 4d562d95b..1f96a9afd 100644 --- a/beacon_chain/utils/boolean-bitfield/src/lib.rs +++ b/beacon_chain/utils/boolean-bitfield/src/lib.rs @@ -24,7 +24,7 @@ impl BooleanBitfield { } /// Create a new bitfield using the supplied `bytes` as input - pub fn from_bytes(bytes: &[u8]) -> Self { + pub fn from(bytes: &[u8]) -> Self { Self { 0: BitVec::from_bytes(bytes), } @@ -82,7 +82,7 @@ impl ssz::Decodable for BooleanBitfield { if len == 0 { Ok((BooleanBitfield::new(), index + ssz::LENGTH_BYTES)) } else { - let field = BooleanBitfield::from_bytes(&bytes[(index + 4)..(index + len + 4)]); + let field = BooleanBitfield::from(&bytes[(index + 4)..(index + len + 4)]); let index = index + ssz::LENGTH_BYTES + len; Ok((field, index)) } @@ -108,7 +108,7 @@ mod tests { #[test] fn test_get_from_bitfield() { - let field = BooleanBitfield::from_bytes(INPUT); + let field = BooleanBitfield::from(INPUT); let unset = field.get(0).unwrap(); assert!(!unset); let set = field.get(6).unwrap(); @@ -119,7 +119,7 @@ mod tests { #[test] fn test_set_for_bitfield() { - let mut field = BooleanBitfield::from_bytes(INPUT); + let mut field = BooleanBitfield::from(INPUT); let previous = field.set(10, true).unwrap(); assert!(!previous); let previous = field.get(10).unwrap(); @@ -132,7 +132,7 @@ mod tests { #[test] fn test_highest_set_bit() { - let field = BooleanBitfield::from_bytes(INPUT); + let field = BooleanBitfield::from(INPUT); assert_eq!(field.highest_set_bit().unwrap(), 14); let field = BooleanBitfield::new(); @@ -141,7 +141,7 @@ mod tests { #[test] fn test_len() { - let field = BooleanBitfield::from_bytes(INPUT); + let field = BooleanBitfield::from(INPUT); assert_eq!(field.len(), 16); let field = BooleanBitfield::new(); @@ -150,7 +150,7 @@ mod tests { #[test] fn test_num_set_bits() { - let field = BooleanBitfield::from_bytes(INPUT); + let field = BooleanBitfield::from(INPUT); assert_eq!(field.num_set_bits(), 2); let field = BooleanBitfield::new(); @@ -159,7 +159,7 @@ mod tests { #[test] fn test_to_bytes() { - let field = BooleanBitfield::from_bytes(INPUT); + let field = BooleanBitfield::from(INPUT); assert_eq!(field.to_bytes(), INPUT); let field = BooleanBitfield::new(); diff --git a/beacon_chain/validation/src/attestation_validation.rs b/beacon_chain/validation/src/attestation_validation.rs index d4e451d38..d63d74483 100644 --- a/beacon_chain/validation/src/attestation_validation.rs +++ b/beacon_chain/validation/src/attestation_validation.rs @@ -32,6 +32,7 @@ pub enum AttestationValidationError { NonZeroTrailingBits, BadAggregateSignature, DBError(String), + OutOfBoundsBitfieldIndex, } /// The context against which some attestation should be validated. @@ -198,10 +199,6 @@ where } } -fn bytes_for_bits(bits: usize) -> usize { - (bits.saturating_sub(1) / 8) + 1 -} - impl From for AttestationValidationError { fn from(e: ParentHashesError) -> Self { match e { @@ -242,6 +239,8 @@ impl From for AttestationValidationError { AttestationValidationError::NoPublicKeyForValidator } SignatureVerificationError::DBError(s) => AttestationValidationError::DBError(s), + SignatureVerificationError::OutOfBoundsBitfieldIndex + => AttestationValidationError::OutOfBoundsBitfieldIndex, } } } diff --git a/beacon_chain/validation/src/signature_verification.rs b/beacon_chain/validation/src/signature_verification.rs index fddaabb99..18a447267 100644 --- a/beacon_chain/validation/src/signature_verification.rs +++ b/beacon_chain/validation/src/signature_verification.rs @@ -1,7 +1,7 @@ use super::bls::{AggregatePublicKey, AggregateSignature}; use super::db::stores::{ValidatorStore, ValidatorStoreError}; use super::db::ClientDB; -use super::types::Bitfield; +use super::types::{Bitfield, BitfieldError}; use std::collections::HashSet; #[derive(Debug, PartialEq)] @@ -10,6 +10,13 @@ pub enum SignatureVerificationError { PublicKeyCorrupt, NoPublicKeyForValidator, DBError(String), + OutOfBoundsBitfieldIndex, +} + +impl From for SignatureVerificationError { + fn from(_error: BitfieldError) -> Self { + SignatureVerificationError::OutOfBoundsBitfieldIndex + } } /// Verify an aggregate signature across the supplied message. @@ -33,7 +40,7 @@ where let mut agg_pub_key = AggregatePublicKey::new(); for i in 0..attestation_indices.len() { - let voted = bitfield.get_bit(i); + let voted = bitfield.get(i)?; if voted { /* * De-reference the attestation index into a canonical ValidatorRecord index. @@ -123,7 +130,7 @@ mod tests { let attestation_indices: Vec = (0..all_keypairs.len()).collect(); let mut bitfield = Bitfield::new(); for i in 0..signing_keypairs.len() { - bitfield.set_bit(i, true); + bitfield.set(i, true).unwrap(); } let db = Arc::new(MemoryDB::open()); @@ -159,7 +166,7 @@ mod tests { * Add another validator to the bitfield, run validation will all other * parameters the same and assert that it fails. */ - bitfield.set_bit(signing_keypairs.len() + 1, true); + bitfield.set(signing_keypairs.len() + 1, true).unwrap(); let voters = verify_aggregate_signature_for_indices( &message, &agg_sig, diff --git a/beacon_chain/validation/tests/attestation_validation/helpers.rs b/beacon_chain/validation/tests/attestation_validation/helpers.rs index a148f9a69..a9c92ca5a 100644 --- a/beacon_chain/validation/tests/attestation_validation/helpers.rs +++ b/beacon_chain/validation/tests/attestation_validation/helpers.rs @@ -95,7 +95,7 @@ pub fn generate_attestation( * and sign the aggregate sig. */ if let Some(sk) = secret_key { - attester_bitfield.set_bit(i, true); + attester_bitfield.set(i, true).unwrap(); let sig = Signature::new(&attestation_message, sk); aggregate_sig.add(&sig); } diff --git a/beacon_chain/validation/tests/attestation_validation/tests.rs b/beacon_chain/validation/tests/attestation_validation/tests.rs index 7c9617070..4ea954c89 100644 --- a/beacon_chain/validation/tests/attestation_validation/tests.rs +++ b/beacon_chain/validation/tests/attestation_validation/tests.rs @@ -133,12 +133,8 @@ fn test_attestation_validation_invalid_bad_bitfield_length() { * of the bitfield. */ let one_byte_higher = rig.attester_count + 8; - rig.attestation - .attester_bitfield - .set_bit(one_byte_higher, true); - rig.attestation - .attester_bitfield - .set_bit(one_byte_higher, false); + rig.attestation.attester_bitfield.set(one_byte_higher, true).unwrap(); + rig.attestation.attester_bitfield.set(one_byte_higher, false).unwrap(); let result = rig.context.validate_attestation(&rig.attestation); assert_eq!(result, Err(AttestationValidationError::BadBitfieldLength)); @@ -149,9 +145,7 @@ fn test_attestation_validation_invalid_invalid_bitfield_end_bit() { let mut rig = generic_rig(); let one_bit_high = rig.attester_count + 1; - rig.attestation - .attester_bitfield - .set_bit(one_bit_high, true); + rig.attestation.attester_bitfield.set(one_bit_high, true).unwrap(); let result = rig.context.validate_attestation(&rig.attestation); assert_eq!( @@ -174,19 +168,11 @@ fn test_attestation_validation_invalid_invalid_bitfield_end_bit_with_irreguar_bi * bit in a bitfield and the byte length of that bitfield */ let one_bit_high = rig.attester_count + 1; - assert!( - one_bit_high % 8 != 0, - "the test is ineffective in this case." - ); - rig.attestation - .attester_bitfield - .set_bit(one_bit_high, true); + assert!(one_bit_high % 8 != 0, "the test is ineffective in this case."); + rig.attestation.attester_bitfield.set(one_bit_high, true).unwrap(); let result = rig.context.validate_attestation(&rig.attestation); - assert_eq!( - result, - Err(AttestationValidationError::InvalidBitfieldEndBits) - ); + assert_eq!(result, Err(AttestationValidationError::InvalidBitfieldEndBits)); } #[test]