From 30bf97539cecf24e861ee9abf7d244899c4b88dd Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 22 Sep 2018 21:59:44 +1000 Subject: [PATCH] Create unique errors for att. parent hashes --- .../transition/attestation_parent_hashes.rs | 29 ++++++++++--------- lighthouse/state/transition/mod.rs | 4 +-- lighthouse/utils/errors.rs | 9 ------ 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/lighthouse/state/transition/attestation_parent_hashes.rs b/lighthouse/state/transition/attestation_parent_hashes.rs index 2ba4712d5..f98414c39 100644 --- a/lighthouse/state/transition/attestation_parent_hashes.rs +++ b/lighthouse/state/transition/attestation_parent_hashes.rs @@ -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, ParameterError> + -> Result, 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(); diff --git a/lighthouse/state/transition/mod.rs b/lighthouse/state/transition/mod.rs index 9fe381757..751079671 100644 --- a/lighthouse/state/transition/mod.rs +++ b/lighthouse/state/transition/mod.rs @@ -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; diff --git a/lighthouse/utils/errors.rs b/lighthouse/utils/errors.rs index 2a25b29b8..b464f6a48 100644 --- a/lighthouse/utils/errors.rs +++ b/lighthouse/utils/errors.rs @@ -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,