get tests passing (except one)

This commit is contained in:
Alex Stokes 2018-11-07 14:32:33 -08:00
parent a9decd3aed
commit 37b05e1a5b
6 changed files with 36 additions and 39 deletions

View File

@ -15,7 +15,11 @@ pub mod validator_record;
pub mod validator_registration; pub mod validator_registration;
use self::boolean_bitfield::BooleanBitfield; use self::boolean_bitfield::BooleanBitfield;
use self::ethereum_types::{H160, H256, U256}; use self::ethereum_types::{
H256,
H160,
U256
};
use std::collections::HashMap; use std::collections::HashMap;
pub use active_state::ActiveState; pub use active_state::ActiveState;
@ -32,7 +36,8 @@ pub use validator_registration::ValidatorRegistration;
pub type Hash256 = H256; pub type Hash256 = H256;
pub type Address = H160; pub type Address = H160;
pub type EthBalance = U256; 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. /// Maps a (slot, shard_id) to attestation_indices.
pub type AttesterMap = HashMap<(u64, u16), Vec<usize>>; pub type AttesterMap = HashMap<(u64, u16), Vec<usize>>;

View File

@ -24,7 +24,7 @@ impl BooleanBitfield {
} }
/// Create a new bitfield using the supplied `bytes` as input /// Create a new bitfield using the supplied `bytes` as input
pub fn from_bytes(bytes: &[u8]) -> Self { pub fn from(bytes: &[u8]) -> Self {
Self { Self {
0: BitVec::from_bytes(bytes), 0: BitVec::from_bytes(bytes),
} }
@ -82,7 +82,7 @@ impl ssz::Decodable for BooleanBitfield {
if len == 0 { if len == 0 {
Ok((BooleanBitfield::new(), index + ssz::LENGTH_BYTES)) Ok((BooleanBitfield::new(), index + ssz::LENGTH_BYTES))
} else { } 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; let index = index + ssz::LENGTH_BYTES + len;
Ok((field, index)) Ok((field, index))
} }
@ -108,7 +108,7 @@ mod tests {
#[test] #[test]
fn test_get_from_bitfield() { fn test_get_from_bitfield() {
let field = BooleanBitfield::from_bytes(INPUT); let field = BooleanBitfield::from(INPUT);
let unset = field.get(0).unwrap(); let unset = field.get(0).unwrap();
assert!(!unset); assert!(!unset);
let set = field.get(6).unwrap(); let set = field.get(6).unwrap();
@ -119,7 +119,7 @@ mod tests {
#[test] #[test]
fn test_set_for_bitfield() { 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(); let previous = field.set(10, true).unwrap();
assert!(!previous); assert!(!previous);
let previous = field.get(10).unwrap(); let previous = field.get(10).unwrap();
@ -132,7 +132,7 @@ mod tests {
#[test] #[test]
fn test_highest_set_bit() { fn test_highest_set_bit() {
let field = BooleanBitfield::from_bytes(INPUT); let field = BooleanBitfield::from(INPUT);
assert_eq!(field.highest_set_bit().unwrap(), 14); assert_eq!(field.highest_set_bit().unwrap(), 14);
let field = BooleanBitfield::new(); let field = BooleanBitfield::new();
@ -141,7 +141,7 @@ mod tests {
#[test] #[test]
fn test_len() { fn test_len() {
let field = BooleanBitfield::from_bytes(INPUT); let field = BooleanBitfield::from(INPUT);
assert_eq!(field.len(), 16); assert_eq!(field.len(), 16);
let field = BooleanBitfield::new(); let field = BooleanBitfield::new();
@ -150,7 +150,7 @@ mod tests {
#[test] #[test]
fn test_num_set_bits() { fn test_num_set_bits() {
let field = BooleanBitfield::from_bytes(INPUT); let field = BooleanBitfield::from(INPUT);
assert_eq!(field.num_set_bits(), 2); assert_eq!(field.num_set_bits(), 2);
let field = BooleanBitfield::new(); let field = BooleanBitfield::new();
@ -159,7 +159,7 @@ mod tests {
#[test] #[test]
fn test_to_bytes() { fn test_to_bytes() {
let field = BooleanBitfield::from_bytes(INPUT); let field = BooleanBitfield::from(INPUT);
assert_eq!(field.to_bytes(), INPUT); assert_eq!(field.to_bytes(), INPUT);
let field = BooleanBitfield::new(); let field = BooleanBitfield::new();

View File

@ -32,6 +32,7 @@ pub enum AttestationValidationError {
NonZeroTrailingBits, NonZeroTrailingBits,
BadAggregateSignature, BadAggregateSignature,
DBError(String), DBError(String),
OutOfBoundsBitfieldIndex,
} }
/// The context against which some attestation should be validated. /// 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<ParentHashesError> for AttestationValidationError { impl From<ParentHashesError> for AttestationValidationError {
fn from(e: ParentHashesError) -> Self { fn from(e: ParentHashesError) -> Self {
match e { match e {
@ -242,6 +239,8 @@ impl From<SignatureVerificationError> for AttestationValidationError {
AttestationValidationError::NoPublicKeyForValidator AttestationValidationError::NoPublicKeyForValidator
} }
SignatureVerificationError::DBError(s) => AttestationValidationError::DBError(s), SignatureVerificationError::DBError(s) => AttestationValidationError::DBError(s),
SignatureVerificationError::OutOfBoundsBitfieldIndex
=> AttestationValidationError::OutOfBoundsBitfieldIndex,
} }
} }
} }

View File

@ -1,7 +1,7 @@
use super::bls::{AggregatePublicKey, AggregateSignature}; use super::bls::{AggregatePublicKey, AggregateSignature};
use super::db::stores::{ValidatorStore, ValidatorStoreError}; use super::db::stores::{ValidatorStore, ValidatorStoreError};
use super::db::ClientDB; use super::db::ClientDB;
use super::types::Bitfield; use super::types::{Bitfield, BitfieldError};
use std::collections::HashSet; use std::collections::HashSet;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -10,6 +10,13 @@ pub enum SignatureVerificationError {
PublicKeyCorrupt, PublicKeyCorrupt,
NoPublicKeyForValidator, NoPublicKeyForValidator,
DBError(String), DBError(String),
OutOfBoundsBitfieldIndex,
}
impl From<BitfieldError> for SignatureVerificationError {
fn from(_error: BitfieldError) -> Self {
SignatureVerificationError::OutOfBoundsBitfieldIndex
}
} }
/// Verify an aggregate signature across the supplied message. /// Verify an aggregate signature across the supplied message.
@ -33,7 +40,7 @@ where
let mut agg_pub_key = AggregatePublicKey::new(); let mut agg_pub_key = AggregatePublicKey::new();
for i in 0..attestation_indices.len() { for i in 0..attestation_indices.len() {
let voted = bitfield.get_bit(i); let voted = bitfield.get(i)?;
if voted { if voted {
/* /*
* De-reference the attestation index into a canonical ValidatorRecord index. * De-reference the attestation index into a canonical ValidatorRecord index.
@ -123,7 +130,7 @@ mod tests {
let attestation_indices: Vec<usize> = (0..all_keypairs.len()).collect(); let attestation_indices: Vec<usize> = (0..all_keypairs.len()).collect();
let mut bitfield = Bitfield::new(); let mut bitfield = Bitfield::new();
for i in 0..signing_keypairs.len() { for i in 0..signing_keypairs.len() {
bitfield.set_bit(i, true); bitfield.set(i, true).unwrap();
} }
let db = Arc::new(MemoryDB::open()); let db = Arc::new(MemoryDB::open());
@ -159,7 +166,7 @@ mod tests {
* Add another validator to the bitfield, run validation will all other * Add another validator to the bitfield, run validation will all other
* parameters the same and assert that it fails. * 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( let voters = verify_aggregate_signature_for_indices(
&message, &message,
&agg_sig, &agg_sig,

View File

@ -95,7 +95,7 @@ pub fn generate_attestation(
* and sign the aggregate sig. * and sign the aggregate sig.
*/ */
if let Some(sk) = secret_key { 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); let sig = Signature::new(&attestation_message, sk);
aggregate_sig.add(&sig); aggregate_sig.add(&sig);
} }

View File

@ -133,12 +133,8 @@ fn test_attestation_validation_invalid_bad_bitfield_length() {
* of the bitfield. * of the bitfield.
*/ */
let one_byte_higher = rig.attester_count + 8; let one_byte_higher = rig.attester_count + 8;
rig.attestation rig.attestation.attester_bitfield.set(one_byte_higher, true).unwrap();
.attester_bitfield rig.attestation.attester_bitfield.set(one_byte_higher, false).unwrap();
.set_bit(one_byte_higher, true);
rig.attestation
.attester_bitfield
.set_bit(one_byte_higher, false);
let result = rig.context.validate_attestation(&rig.attestation); let result = rig.context.validate_attestation(&rig.attestation);
assert_eq!(result, Err(AttestationValidationError::BadBitfieldLength)); 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 mut rig = generic_rig();
let one_bit_high = rig.attester_count + 1; let one_bit_high = rig.attester_count + 1;
rig.attestation rig.attestation.attester_bitfield.set(one_bit_high, true).unwrap();
.attester_bitfield
.set_bit(one_bit_high, true);
let result = rig.context.validate_attestation(&rig.attestation); let result = rig.context.validate_attestation(&rig.attestation);
assert_eq!( 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 * bit in a bitfield and the byte length of that bitfield
*/ */
let one_bit_high = rig.attester_count + 1; let one_bit_high = rig.attester_count + 1;
assert!( assert!(one_bit_high % 8 != 0, "the test is ineffective in this case.");
one_bit_high % 8 != 0, rig.attestation.attester_bitfield.set(one_bit_high, true).unwrap();
"the test is ineffective in this case."
);
rig.attestation
.attester_bitfield
.set_bit(one_bit_high, true);
let result = rig.context.validate_attestation(&rig.attestation); let result = rig.context.validate_attestation(&rig.attestation);
assert_eq!( assert_eq!(result, Err(AttestationValidationError::InvalidBitfieldEndBits));
result,
Err(AttestationValidationError::InvalidBitfieldEndBits)
);
} }
#[test] #[test]