Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #710 from input-output-hk/errors
Browse files Browse the repository at this point in the history
Errors improvements
  • Loading branch information
NicolasDP authored May 31, 2019
2 parents f8c4231 + 900e82e commit 5a0a5ab
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 260 deletions.
6 changes: 6 additions & 0 deletions chain-impl-mockchain/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ pub type Secret = key::AccountSecretKey;

/// The public ledger of all accounts associated with their current state
pub type Ledger = account::Ledger<Identifier>;

impl std::fmt::Display for Identifier {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.0.fmt(f)
}
}
38 changes: 8 additions & 30 deletions chain-impl-mockchain/src/accounting/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,16 @@
use crate::value::*;
use imhamt::{Hamt, InsertError, UpdateError};
use std::collections::hash_map::DefaultHasher;
use std::fmt::{self, Display, Formatter};
use std::hash::Hash;

/// Possible errors during an account operation
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LedgerError {
NonExistent,
AlreadyExists,
NeedTotalWithdrawal,
NonZero,
ValueError(ValueError),
}

impl Display for LedgerError {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), fmt::Error> {
match self {
LedgerError::NonExistent => "Account does not exist",
LedgerError::AlreadyExists => "Account already exists",
LedgerError::NeedTotalWithdrawal => {
"Operation counter reached its maximum and next operation must be full withdrawal"
}
LedgerError::NonZero => "Removed account is not empty",
LedgerError::ValueError(_) => "Value calculation failed",
}
.fmt(formatter)
}
}

impl From<ValueError> for LedgerError {
fn from(e: ValueError) -> Self {
LedgerError::ValueError(e)
}
custom_error! {
#[derive(Clone, PartialEq, Eq)]
pub LedgerError
NonExistent = "Account does not exist",
AlreadyExists = "Account already exists",
NeedTotalWithdrawal = "Operation counter reached its maximum and next operation must be full withdrawal",
NonZero = "Removed account is not empty",
ValueError{ source: ValueError } = "Value calculation failed",
}

impl From<UpdateError<LedgerError>> for LedgerError {
Expand Down
3 changes: 0 additions & 3 deletions chain-impl-mockchain/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,13 @@ impl Readable for Header {
}
AnyBlockVersion::Supported(BlockVersion::KesVrfproof) => {
let node_id = StakePoolId::read(buf)?;
dbg!(&node_id);
let vrf_proof = {
let bytes = <[u8;<Curve25519_2HashDH as VerifiableRandomFunction>::VERIFIED_RANDOM_SIZE]>::read(buf)?;

<Curve25519_2HashDH as VerifiableRandomFunction>::VerifiedRandomOutput::from_bytes_unverified(&bytes)
.ok_or(ReadError::StructureInvalid("VRF Proof".to_string()))
}?;
dbg!(&vrf_proof);
let kes_proof = deserialize_signature(buf).map(KESSignature)?;
dbg!(&kes_proof);

Proof::GenesisPraos(GenesisPraosProof {
node_id: node_id,
Expand Down
29 changes: 23 additions & 6 deletions chain-impl-mockchain/src/leadership/genesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ pub struct GenesisLeaderSelection {
active_slots_coeff: ActiveSlotsCoeff,
}

custom_error! {GenesisError
InvalidEpoch { expected: Epoch, actual: Epoch } = "Wrong epoch, expected epoch {expected} but received block at epoch {actual}",
TotalStakeIsZero = "Total stake is null",
}

impl GenesisLeaderSelection {
pub fn new(epoch: Epoch, ledger: &Ledger) -> Self {
GenesisLeaderSelection {
Expand All @@ -49,8 +54,13 @@ impl GenesisLeaderSelection {
date: BlockDate,
) -> Result<Option<Witness>, Error> {
if date.epoch != self.epoch {
// TODO: add more error details: invalid Date
return Err(Error::new(ErrorKind::Failure));
return Err(Error::new_(
ErrorKind::Failure,
GenesisError::InvalidEpoch {
actual: date.epoch,
expected: self.epoch,
},
));
}

let stake_snapshot = &self.distribution;
Expand All @@ -62,8 +72,10 @@ impl GenesisLeaderSelection {
let total_stake: Value = stake_snapshot.total_stake();

if total_stake == Value::zero() {
// TODO: give more info about the error here...
return Err(Error::new(ErrorKind::Failure));
return Err(Error::new_(
ErrorKind::Failure,
GenesisError::TotalStakeIsZero,
));
}

let percent_stake = PercentStake {
Expand All @@ -83,8 +95,13 @@ impl GenesisLeaderSelection {

pub(crate) fn verify(&self, block_header: &Header) -> Verification {
if block_header.block_date().epoch != self.epoch {
// TODO: add more error details: invalid Date
return Verification::Failure(Error::new(ErrorKind::Failure));
return Verification::Failure(Error::new_(
ErrorKind::Failure,
GenesisError::InvalidEpoch {
expected: self.epoch,
actual: block_header.block_date().epoch,
},
));
}

let stake_snapshot = &self.distribution;
Expand Down
10 changes: 8 additions & 2 deletions chain-impl-mockchain/src/leadership/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ impl LeadershipConsensus {
LeadershipConsensus::Bft(_) if block_version == BlockVersion::Ed25519Signed => {
Verification::Success
}
LeadershipConsensus::GenesisPraos(_) if block_version == BlockVersion::KesVrfproof => {
Verification::Success
}
_ => Verification::Failure(Error::new(ErrorKind::IncompatibleBlockVersion)),
}
}
Expand Down Expand Up @@ -232,10 +235,13 @@ impl Error {
}
}

pub fn new_(kind: ErrorKind, cause: Box<dyn std::error::Error>) -> Self {
pub fn new_<E>(kind: ErrorKind, cause: E) -> Self
where
E: std::error::Error + 'static,
{
Error {
kind: kind,
cause: Some(cause),
cause: Some(Box::new(cause)),
}
}
}
Expand Down
Loading

0 comments on commit 5a0a5ab

Please sign in to comment.