Skip to content

NDRS-974: Get rid of failure #1077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ bitflags = "1"
blake2 = { version = "0.9.0", default-features = false }
datasize = { version = "0.2.4", default-features = false }
ed25519-dalek = { version = "1.0.0", default-features = false, features = ["rand", "u64_backend"] }
# TODO: Replace failure with thiserror once no_std support is landed https://github.com/dtolnay/thiserror/pull/64
failure = { version = "0.1.8", default-features = false, features = ["failure_derive"] }
thiserror = { version = "1.0.20", default-features = false, optional = true }
hex = { version = "0.4.2", default-features = false }
hex_fmt = "0.3.0"
k256 = { version = "0.4.2", default-features = false, features = ["ecdsa", "zeroize"] }
Expand All @@ -33,6 +32,7 @@ serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.59", default-features = false }
uint = { version = "0.8.3", default-features = false }
once_cell = "1.5.2"
displaydoc = { version = "0.1", default-features = false, optional = true }

[dev-dependencies]
bincode = "1.3.1"
Expand All @@ -44,7 +44,7 @@ version-sync = "0.9"
serde_test = "1.0.117"

[features]
default = ["base16/alloc", "serde/alloc", "serde_json/alloc"]
default = ["base16/alloc", "serde/alloc", "serde_json/alloc", "displaydoc"]
std = [
"base16/std",
"base64/std",
Expand All @@ -54,7 +54,8 @@ std = [
"k256/std",
"serde/std",
"serde_json/std",
"schemars"
"schemars",
"thiserror"
]
gens = ["std", "proptest/std"]

Expand Down
77 changes: 58 additions & 19 deletions types/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ use blake2::{
VarBlake2b,
};
use datasize::DataSize;
use failure::Fail;
use rand::{
distributions::{Distribution, Standard},
Rng,
};
#[cfg(feature = "std")]
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
use serde::{de::Error as SerdeError, Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "std")]
use thiserror::Error;

use crate::{
bytesrepr::{Error, FromBytes, ToBytes, U8_SERIALIZED_LENGTH},
Expand Down Expand Up @@ -106,22 +107,33 @@ impl TryFrom<u32> for ActionType {
/// Errors that can occur while changing action thresholds (i.e. the total [`Weight`]s of signing
/// [`AccountHash`]s required to perform various actions) on an account.
#[repr(i32)]
#[derive(Debug, Fail, PartialEq, Eq, Copy, Clone)]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum SetThresholdFailure {
/// Setting the key-management threshold to a value lower than the deployment threshold is
/// disallowed.
#[fail(display = "New threshold should be greater than or equal to deployment threshold")]
#[cfg_attr(
feature = "std",
error("New threshold should be greater than or equal to deployment threshold")
)]
KeyManagementThreshold = 1,
/// Setting the deployment threshold to a value greater than any other threshold is disallowed.
#[fail(display = "New threshold should be lower than or equal to key management threshold")]
#[cfg_attr(
feature = "std",
error("New threshold should be lower than or equal to key management threshold")
)]
DeploymentThreshold = 2,
/// Caller doesn't have sufficient permissions to set new thresholds.
#[fail(display = "Unable to set action threshold due to insufficient permissions")]
#[cfg_attr(
feature = "std",
error("Unable to set action threshold due to insufficient permissions")
)]
PermissionDeniedError = 3,
/// Setting a threshold to a value greater than the total weight of associated keys is
/// disallowed.
#[fail(
display = "New threshold should be lower or equal than total weight of associated keys"
#[cfg_attr(
feature = "std",
error("New threshold should be lower or equal than total weight of associated keys")
)]
InsufficientTotalWeight = 4,
}
Expand Down Expand Up @@ -396,19 +408,29 @@ impl Distribution<AccountHash> for Standard {
}

/// Errors that can occur while adding a new [`AccountHash`] to an account's associated keys map.
#[derive(PartialEq, Eq, Fail, Debug, Copy, Clone)]
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
#[cfg_attr(feature = "std", derive(Error))]
#[repr(i32)]
pub enum AddKeyFailure {
/// There are already [`MAX_ASSOCIATED_KEYS`] [`AccountHash`]s associated with the given
/// account.
#[fail(display = "Unable to add new associated key because maximum amount of keys is reached")]
#[cfg_attr(
feature = "std",
error("Unable to add new associated key because maximum amount of keys is reached")
)]
MaxKeysLimit = 1,
/// The given [`AccountHash`] is already associated with the given account.
#[fail(display = "Unable to add new associated key because given key already exists")]
#[cfg_attr(
feature = "std",
error("Unable to add new associated key because given key already exists")
)]
DuplicateKey = 2,
/// Caller doesn't have sufficient permissions to associate a new [`AccountHash`] with the
/// given account.
#[fail(display = "Unable to add new associated key due to insufficient permissions")]
#[cfg_attr(
feature = "std",
error("Unable to add new associated key due to insufficient permissions")
)]
PermissionDenied = 3,
}

Expand All @@ -428,19 +450,26 @@ impl TryFrom<i32> for AddKeyFailure {
}

/// Errors that can occur while removing a [`AccountHash`] from an account's associated keys map.
#[derive(Fail, Debug, Eq, PartialEq, Copy, Clone)]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "std", derive(Error))]
#[repr(i32)]
pub enum RemoveKeyFailure {
/// The given [`AccountHash`] is not associated with the given account.
#[fail(display = "Unable to remove a key that does not exist")]
#[cfg_attr(feature = "std", error("Unable to remove a key that does not exist"))]
MissingKey = 1,
/// Caller doesn't have sufficient permissions to remove an associated [`AccountHash`] from the
/// given account.
#[fail(display = "Unable to remove associated key due to insufficient permissions")]
#[cfg_attr(
feature = "std",
error("Unable to remove associated key due to insufficient permissions")
)]
PermissionDenied = 2,
/// Removing the given associated [`AccountHash`] would cause the total weight of all remaining
/// `AccountHash`s to fall below one of the action thresholds for the given account.
#[fail(display = "Unable to remove a key which would violate action threshold constraints")]
#[cfg_attr(
feature = "std",
error("Unable to remove a key which would violate action threshold constraints")
)]
ThresholdViolation = 3,
}

Expand All @@ -465,20 +494,30 @@ impl TryFrom<i32> for RemoveKeyFailure {

/// Errors that can occur while updating the [`Weight`] of a [`AccountHash`] in an account's
/// associated keys map.
#[derive(PartialEq, Eq, Fail, Debug, Copy, Clone)]
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
#[cfg_attr(feature = "std", derive(Error))]
#[repr(i32)]
pub enum UpdateKeyFailure {
/// The given [`AccountHash`] is not associated with the given account.
#[fail(display = "Unable to update the value under an associated key that does not exist")]
#[cfg_attr(
feature = "std",
error("Unable to update the value under an associated key that does not exist")
)]
MissingKey = 1,
/// Caller doesn't have sufficient permissions to update an associated [`AccountHash`] from the
/// given account.
#[fail(display = "Unable to update associated key due to insufficient permissions")]
#[cfg_attr(
feature = "std",
error("Unable to update associated key due to insufficient permissions")
)]
PermissionDenied = 2,
/// Updating the [`Weight`] of the given associated [`AccountHash`] would cause the total
/// weight of all `AccountHash`s to fall below one of the action thresholds for the given
/// account.
#[fail(display = "Unable to update weight that would fall below any of action thresholds")]
#[cfg_attr(
feature = "std",
error("Unable to update weight that would fall below any of action thresholds")
)]
ThresholdViolation = 3,
}

Expand Down
14 changes: 8 additions & 6 deletions types/src/bytesrepr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ use alloc::{
use core::any;
use core::{mem, ptr::NonNull};

use failure::Fail;
use num_integer::Integer;
use num_rational::Ratio;
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use thiserror::Error;

pub use bytes::Bytes;

Expand Down Expand Up @@ -98,20 +99,21 @@ pub fn allocate_buffer<T: ToBytes>(to_be_serialized: &T) -> Result<Vec<u8>, Erro
}

/// Serialization and deserialization errors.
#[derive(Debug, Fail, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "std", derive(Error))]
#[repr(u8)]
pub enum Error {
/// Early end of stream while deserializing.
#[fail(display = "Deserialization error: early end of stream")]
#[cfg_attr(feature = "std", error("Deserialization error: early end of stream"))]
EarlyEndOfStream = 0,
/// Formatting error while deserializing.
#[fail(display = "Deserialization error: formatting")]
#[cfg_attr(feature = "std", error("Deserialization error: formatting"))]
Formatting,
/// Not all input bytes were consumed in [`deserialize`].
#[fail(display = "Deserialization error: left-over bytes")]
#[cfg_attr(feature = "std", error("Deserialization error: left-over bytes"))]
LeftOverBytes,
/// Out of memory error.
#[fail(display = "Serialization error: out of memory")]
#[cfg_attr(feature = "std", error("Serialization error: out of memory"))]
OutOfMemory,
}

Expand Down
10 changes: 6 additions & 4 deletions types/src/cl_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use alloc::{string::String, vec::Vec};
use core::fmt;

use datasize::DataSize;
use failure::Fail;
#[cfg(feature = "std")]
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
use serde::{de::Error as SerdeError, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value;
#[cfg(feature = "std")]
use thiserror::Error;

use crate::{
bytesrepr::{self, Bytes, FromBytes, ToBytes, U32_SERIALIZED_LENGTH},
Expand Down Expand Up @@ -39,13 +40,14 @@ impl fmt::Display for CLTypeMismatch {
}

/// Error relating to [`CLValue`] operations.
#[derive(Fail, PartialEq, Eq, Clone, Debug)]
#[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum CLValueError {
/// An error while serializing or deserializing the underlying data.
#[fail(display = "CLValue error: {}", _0)]
#[cfg_attr(feature = "std", error("CLValue error: {}", _0))]
Serialization(bytesrepr::Error),
/// A type mismatch while trying to convert a [`CLValue`] into a given type.
#[fail(display = "Type mismatch: {}", _0)]
#[cfg_attr(feature = "std", error("Type mismatch: {}", _0))]
Type(CLTypeMismatch),
}

Expand Down
16 changes: 12 additions & 4 deletions types/src/semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use alloc::vec::Vec;
use core::{convert::TryFrom, fmt, num::ParseIntError};

use datasize::DataSize;
use failure::Fail;
use serde::{Deserialize, Serialize};

#[cfg(not(feature = "std"))]
use displaydoc::Display;
#[cfg(feature = "std")]
use thiserror::Error;

use crate::bytesrepr::{self, Error, FromBytes, ToBytes, U32_SERIALIZED_LENGTH};

/// Length of SemVer when serialized
Expand Down Expand Up @@ -81,11 +85,15 @@ impl fmt::Display for SemVer {
}
}

#[derive(Fail, Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Error))]
#[cfg_attr(not(feature = "std"), derive(Display))]
pub enum ParseSemVerError {
#[fail(display = "Invalid version format")]
/// Invalid version format
#[cfg_attr(feature = "std", error("Invalid version format"))]
InvalidVersionFormat,
#[fail(display = "{}", _0)]
/// {0}
#[cfg_attr(feature = "std", error("{}", _0))]
ParseIntError(ParseIntError),
}

Expand Down
Loading