Create unique errors for att. parent hashes

This commit is contained in:
Paul Hauner 2018-09-22 21:59:44 +10:00
parent 890aaf7335
commit 30bf97539c
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
3 changed files with 17 additions and 25 deletions

View File

@ -1,5 +1,12 @@
use super::Hash256;
use super::ParameterError;
pub enum ParentHashesError {
BadCurrentHashes,
BadObliqueHashes,
SlotTooHigh,
SlotTooLow,
IntWrapping,
}
/// This function is used to select the hashes used in
/// the signing of an AttestationRecord.
@ -18,23 +25,20 @@ pub fn attestation_parent_hashes(
attestation_slot: u64,
current_hashes: &[Hash256],
oblique_hashes: &[Hash256])
-> Result<Vec<Hash256>, ParameterError>
-> Result<Vec<Hash256>, ParentHashesError>
{
// This cast places a limit on cycle_length. If you change it, check math
// for overflow.
let cycle_length: u64 = u64::from(cycle_length);
if current_hashes.len() as u64 != (cycle_length * 2) {
return Err(ParameterError::InvalidInput(String::from(
"current_hashes.len() must equal cycle_length * 2")));
}
if attestation_slot >= block_slot {
return Err(ParameterError::InvalidInput(String::from(
"attestation_slot must be less than block_slot")));
return Err(ParentHashesError::BadCurrentHashes);
}
if oblique_hashes.len() as u64 > cycle_length {
return Err(ParameterError::InvalidInput(String::from(
"oblique_hashes.len() must be <= cycle_length * 2")));
return Err(ParentHashesError::BadObliqueHashes);
}
if attestation_slot >= block_slot {
return Err(ParentHashesError::SlotTooHigh);
}
/*
@ -44,8 +48,7 @@ pub fn attestation_parent_hashes(
let attestation_distance = block_slot - attestation_slot;
if attestation_distance > cycle_length {
return Err(ParameterError::InvalidInput(String::from(
"attestation_slot must be withing one cycle of block_slot")));
return Err(ParentHashesError::SlotTooLow);
}
/*
@ -63,7 +66,7 @@ pub fn attestation_parent_hashes(
*/
let end = start.checked_add(cycle_length)
.and_then(|x| x.checked_sub(oblique_hashes.len() as u64))
.ok_or(ParameterError::IntWrapping)?;
.ok_or(ParentHashesError::IntWrapping)?;
let mut hashes = Vec::new();

View File

@ -1,8 +1,6 @@
use super::utils::types::Hash256;
use super::utils::errors::ParameterError;
mod attestation_parent_hashes;
pub mod attestation_parent_hashes;
mod shuffling;
pub use self::attestation_parent_hashes::attestation_parent_hashes;
pub use self::shuffling::shuffle;

View File

@ -1,14 +1,5 @@
// Collection of custom errors
#[derive(Debug,PartialEq)]
pub enum AttestationValidationError {
SlotTooHigh,
SlotTooLow(String),
IncorrectBitField,
NonZeroTrailingBits,
AggregateSignatureFail
}
#[derive(Debug,PartialEq)]
pub enum ParameterError {
IntWrapping,