get tests passing (except one)

This commit is contained in:
Alex Stokes
2018-11-07 14:32:33 -08:00
committed by mjkeating
parent 3a26f73cf2
commit 2defe8e4ee
6 changed files with 36 additions and 39 deletions

View File

@@ -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<ParentHashesError> for AttestationValidationError {
fn from(e: ParentHashesError) -> Self {
match e {
@@ -242,6 +239,8 @@ impl From<SignatureVerificationError> for AttestationValidationError {
AttestationValidationError::NoPublicKeyForValidator
}
SignatureVerificationError::DBError(s) => AttestationValidationError::DBError(s),
SignatureVerificationError::OutOfBoundsBitfieldIndex
=> AttestationValidationError::OutOfBoundsBitfieldIndex,
}
}
}

View File

@@ -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<BitfieldError> 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<usize> = (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,

View File

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

View File

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