Implement error type specific to ValidatorStore
This commit is contained in:
parent
029a69a4a5
commit
f9f5a9e49c
@ -9,7 +9,10 @@ mod validator_store;
|
|||||||
|
|
||||||
pub use self::block_store::BlockStore;
|
pub use self::block_store::BlockStore;
|
||||||
pub use self::pow_chain_store::PoWChainStore;
|
pub use self::pow_chain_store::PoWChainStore;
|
||||||
pub use self::validator_store::ValidatorStore;
|
pub use self::validator_store::{
|
||||||
|
ValidatorStore,
|
||||||
|
ValidatorStoreError,
|
||||||
|
};
|
||||||
|
|
||||||
use super::bls;
|
use super::bls;
|
||||||
|
|
||||||
@ -22,15 +25,3 @@ pub const COLUMNS: [&str; 3] = [
|
|||||||
POW_CHAIN_DB_COLUMN,
|
POW_CHAIN_DB_COLUMN,
|
||||||
VALIDATOR_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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -7,11 +7,23 @@ use self::bytes::{
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use super::{
|
use super::{
|
||||||
ClientDB,
|
ClientDB,
|
||||||
StoreError,
|
DBError,
|
||||||
};
|
};
|
||||||
use super::VALIDATOR_DB_COLUMN as DB_COLUMN;
|
use super::VALIDATOR_DB_COLUMN as DB_COLUMN;
|
||||||
use super::bls::PublicKey;
|
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)]
|
#[derive(Debug, PartialEq)]
|
||||||
enum KeyPrefixes {
|
enum KeyPrefixes {
|
||||||
PublicKey,
|
PublicKey,
|
||||||
@ -48,16 +60,16 @@ impl<T: ClientDB> ValidatorStore<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn put_public_key_by_index(&self, index: usize, public_key: &PublicKey)
|
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 key = self.get_db_key_for_index(KeyPrefixes::PublicKey, index);
|
||||||
let val = public_key.as_bytes();
|
let val = public_key.as_bytes();
|
||||||
self.db.put(DB_COLUMN, &key[..], &val[..])
|
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)
|
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 key = self.get_db_key_for_index(KeyPrefixes::PublicKey, index);
|
||||||
let val = self.db.get(DB_COLUMN, &key[..])?;
|
let val = self.db.get(DB_COLUMN, &key[..])?;
|
||||||
@ -66,7 +78,7 @@ impl<T: ClientDB> ValidatorStore<T> {
|
|||||||
Some(val) => {
|
Some(val) => {
|
||||||
match PublicKey::from_bytes(&val) {
|
match PublicKey::from_bytes(&val) {
|
||||||
Ok(key) => Ok(Some(key)),
|
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();
|
db.put(DB_COLUMN, &key[..], "cats".as_bytes()).unwrap();
|
||||||
|
|
||||||
assert_eq!(store.get_public_key_by_index(42),
|
assert_eq!(store.get_public_key_by_index(42),
|
||||||
Err(StoreError::DecodeError));
|
Err(ValidatorStoreError::DecodeError));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,13 @@ use super::attestation_parent_hashes::{
|
|||||||
attestation_parent_hashes,
|
attestation_parent_hashes,
|
||||||
ParentHashesError,
|
ParentHashesError,
|
||||||
};
|
};
|
||||||
use super::db::ClientDB;
|
use super::db::{
|
||||||
|
ClientDB,
|
||||||
|
DBError
|
||||||
|
};
|
||||||
use super::db::stores::{
|
use super::db::stores::{
|
||||||
BlockStore,
|
BlockStore,
|
||||||
ValidatorStore,
|
ValidatorStore,
|
||||||
StoreError,
|
|
||||||
};
|
};
|
||||||
use super::ssz::SszStream;
|
use super::ssz::SszStream;
|
||||||
use super::bls::{
|
use super::bls::{
|
||||||
|
@ -6,7 +6,7 @@ use super::bls::{
|
|||||||
use super::db::ClientDB;
|
use super::db::ClientDB;
|
||||||
use super::db::stores::{
|
use super::db::stores::{
|
||||||
ValidatorStore,
|
ValidatorStore,
|
||||||
StoreError,
|
ValidatorStoreError,
|
||||||
};
|
};
|
||||||
use super::utils::types::Bitfield;
|
use super::utils::types::Bitfield;
|
||||||
|
|
||||||
@ -46,12 +46,12 @@ fn verify_aggregate_signature_for_indices<T>(message: &[u8],
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<StoreError> for SignatureVerificationError {
|
impl From<ValidatorStoreError> for SignatureVerificationError {
|
||||||
fn from(error: StoreError) -> Self {
|
fn from(error: ValidatorStoreError) -> Self {
|
||||||
match error {
|
match error {
|
||||||
StoreError::DBError(s) =>
|
ValidatorStoreError::DBError(s) =>
|
||||||
SignatureVerificationError::DBError(s),
|
SignatureVerificationError::DBError(s),
|
||||||
StoreError::DecodeError =>
|
ValidatorStoreError::DecodeError =>
|
||||||
SignatureVerificationError::PublicKeyCorrupt,
|
SignatureVerificationError::PublicKeyCorrupt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user