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

Commit

Permalink
general improvements in the error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDP committed May 31, 2019
1 parent a050505 commit 59e9e60
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 257 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
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 59e9e60

Please sign in to comment.