Implement error type specific to ValidatorStore

This commit is contained in:
Paul Hauner 2018-09-24 18:08:00 +10:00
parent 029a69a4a5
commit f9f5a9e49c
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
4 changed files with 31 additions and 26 deletions

View File

@ -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<DBError> for StoreError {
fn from(error: DBError) -> Self {
StoreError::DBError(error.message)
}
}

View File

@ -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<DBError> for ValidatorStoreError {
fn from(error: DBError) -> Self {
ValidatorStoreError::DBError(error.message)
}
}
#[derive(Debug, PartialEq)]
enum KeyPrefixes {
PublicKey,
@ -48,16 +60,16 @@ impl<T: ClientDB> ValidatorStore<T> {
}
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<Option<PublicKey>, StoreError>
-> Result<Option<PublicKey>, 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<T: ClientDB> ValidatorStore<T> {
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));
}
}

View File

@ -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::{

View File

@ -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<T>(message: &[u8],
}
}
impl From<StoreError> for SignatureVerificationError {
fn from(error: StoreError) -> Self {
impl From<ValidatorStoreError> for SignatureVerificationError {
fn from(error: ValidatorStoreError) -> Self {
match error {
StoreError::DBError(s) =>
ValidatorStoreError::DBError(s) =>
SignatureVerificationError::DBError(s),
StoreError::DecodeError =>
ValidatorStoreError::DecodeError =>
SignatureVerificationError::PublicKeyCorrupt,
}
}