From f9f5a9e49c257d969bf35fff008cc7ae85520e04 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 24 Sep 2018 18:08:00 +1000 Subject: [PATCH] Implement error type specific to ValidatorStore --- lighthouse/db/stores/mod.rs | 17 ++++--------- lighthouse/db/stores/validator_store.rs | 24 ++++++++++++++----- .../validation/attestation_validation.rs | 6 +++-- lighthouse/state/validation/signatures.rs | 10 ++++---- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/lighthouse/db/stores/mod.rs b/lighthouse/db/stores/mod.rs index 93250e7db..7c17653d3 100644 --- a/lighthouse/db/stores/mod.rs +++ b/lighthouse/db/stores/mod.rs @@ -9,7 +9,10 @@ mod validator_store; pub use self::block_store::BlockStore; pub use self::pow_chain_store::PoWChainStore; -pub use self::validator_store::ValidatorStore; +pub use self::validator_store::{ + ValidatorStore, + ValidatorStoreError, +}; use super::bls; @@ -22,15 +25,3 @@ pub const COLUMNS: [&str; 3] = [ POW_CHAIN_DB_COLUMN, VALIDATOR_DB_COLUMN, ]; - -#[derive(Debug, PartialEq)] -pub enum StoreError { - DBError(String), - DecodeError, -} - -impl From for StoreError { - fn from(error: DBError) -> Self { - StoreError::DBError(error.message) - } -} diff --git a/lighthouse/db/stores/validator_store.rs b/lighthouse/db/stores/validator_store.rs index 25e589308..e3c604b3a 100644 --- a/lighthouse/db/stores/validator_store.rs +++ b/lighthouse/db/stores/validator_store.rs @@ -7,11 +7,23 @@ use self::bytes::{ use std::sync::Arc; use super::{ ClientDB, - StoreError, + DBError, }; use super::VALIDATOR_DB_COLUMN as DB_COLUMN; use super::bls::PublicKey; +#[derive(Debug, PartialEq)] +pub enum ValidatorStoreError { + DBError(String), + DecodeError, +} + +impl From for ValidatorStoreError { + fn from(error: DBError) -> Self { + ValidatorStoreError::DBError(error.message) + } +} + #[derive(Debug, PartialEq)] enum KeyPrefixes { PublicKey, @@ -48,16 +60,16 @@ impl ValidatorStore { } pub fn put_public_key_by_index(&self, index: usize, public_key: &PublicKey) - -> Result<(), StoreError> + -> Result<(), ValidatorStoreError> { let key = self.get_db_key_for_index(KeyPrefixes::PublicKey, index); let val = public_key.as_bytes(); self.db.put(DB_COLUMN, &key[..], &val[..]) - .map_err(|e| StoreError::from(e)) + .map_err(|e| ValidatorStoreError::from(e)) } pub fn get_public_key_by_index(&self, index: usize) - -> Result, StoreError> + -> Result, ValidatorStoreError> { let key = self.get_db_key_for_index(KeyPrefixes::PublicKey, index); let val = self.db.get(DB_COLUMN, &key[..])?; @@ -66,7 +78,7 @@ impl ValidatorStore { Some(val) => { match PublicKey::from_bytes(&val) { Ok(key) => Ok(Some(key)), - Err(_) => Err(StoreError::DecodeError), + Err(_) => Err(ValidatorStoreError::DecodeError), } } } @@ -121,6 +133,6 @@ mod tests { db.put(DB_COLUMN, &key[..], "cats".as_bytes()).unwrap(); assert_eq!(store.get_public_key_by_index(42), - Err(StoreError::DecodeError)); + Err(ValidatorStoreError::DecodeError)); } } diff --git a/lighthouse/state/validation/attestation_validation.rs b/lighthouse/state/validation/attestation_validation.rs index 0061f7a9f..694b897ab 100644 --- a/lighthouse/state/validation/attestation_validation.rs +++ b/lighthouse/state/validation/attestation_validation.rs @@ -3,11 +3,13 @@ use super::attestation_parent_hashes::{ attestation_parent_hashes, ParentHashesError, }; -use super::db::ClientDB; +use super::db::{ + ClientDB, + DBError +}; use super::db::stores::{ BlockStore, ValidatorStore, - StoreError, }; use super::ssz::SszStream; use super::bls::{ diff --git a/lighthouse/state/validation/signatures.rs b/lighthouse/state/validation/signatures.rs index ad00e0e3f..30f2339b2 100644 --- a/lighthouse/state/validation/signatures.rs +++ b/lighthouse/state/validation/signatures.rs @@ -6,7 +6,7 @@ use super::bls::{ use super::db::ClientDB; use super::db::stores::{ ValidatorStore, - StoreError, + ValidatorStoreError, }; use super::utils::types::Bitfield; @@ -46,12 +46,12 @@ fn verify_aggregate_signature_for_indices(message: &[u8], } } -impl From for SignatureVerificationError { - fn from(error: StoreError) -> Self { +impl From for SignatureVerificationError { + fn from(error: ValidatorStoreError) -> Self { match error { - StoreError::DBError(s) => + ValidatorStoreError::DBError(s) => SignatureVerificationError::DBError(s), - StoreError::DecodeError => + ValidatorStoreError::DecodeError => SignatureVerificationError::PublicKeyCorrupt, } }