Add arithmetic dispatch errors. (#8726)

* Add arithmetic dispatch errors.

* Replace custom overflow errors.

* Replace custom underflow and division by zero errors.

* Replace overflow/underflow in token error.

* Add token and arithmetic errors in dispatch error equality test.

* Trigger CI.
This commit is contained in:
Shaun Wang
2021-05-10 20:14:02 +12:00
committed by GitHub
parent 655ebc95fb
commit 2a38b23062
12 changed files with 88 additions and 60 deletions
@@ -20,7 +20,7 @@
use super::*;
use sp_std::marker::PhantomData;
use sp_runtime::{TokenError, traits::{CheckedAdd, Zero}};
use sp_runtime::{TokenError, ArithmeticError, traits::{CheckedAdd, Zero}};
use super::super::Imbalance as ImbalanceT;
use crate::traits::misc::{SameOrOther, TryDrop};
use crate::dispatch::{DispatchResult, DispatchError};
@@ -221,7 +221,7 @@ pub trait Unbalanced<AccountId>: Inspect<AccountId> {
-> Result<Self::Balance, DispatchError>
{
let old_balance = Self::balance(who);
let new_balance = old_balance.checked_add(&amount).ok_or(TokenError::Overflow)?;
let new_balance = old_balance.checked_add(&amount).ok_or(ArithmeticError::Overflow)?;
if new_balance < Self::minimum_balance() {
Err(TokenError::BelowMinimum)?
}
@@ -20,7 +20,7 @@
use super::*;
use sp_std::marker::PhantomData;
use sp_runtime::{TokenError, traits::{Zero, CheckedAdd}};
use sp_runtime::{ArithmeticError, TokenError, traits::{Zero, CheckedAdd}};
use sp_arithmetic::traits::Saturating;
use crate::dispatch::{DispatchError, DispatchResult};
use crate::traits::misc::{SameOrOther, TryDrop};
@@ -236,7 +236,7 @@ pub trait Unbalanced<AccountId>: Inspect<AccountId> {
-> Result<Self::Balance, DispatchError>
{
let old_balance = Self::balance(asset, who);
let new_balance = old_balance.checked_add(&amount).ok_or(TokenError::Overflow)?;
let new_balance = old_balance.checked_add(&amount).ok_or(ArithmeticError::Overflow)?;
if new_balance < Self::minimum_balance(asset) {
Err(TokenError::BelowMinimum)?
}
@@ -20,7 +20,7 @@
use codec::{Encode, Decode, FullCodec};
use sp_core::RuntimeDebug;
use sp_arithmetic::traits::{Zero, AtLeast32BitUnsigned};
use sp_runtime::TokenError;
use sp_runtime::{DispatchError, ArithmeticError, TokenError};
/// One of a number of consequences of withdrawing a fungible from an account.
#[derive(Copy, Clone, Eq, PartialEq)]
@@ -50,17 +50,17 @@ pub enum WithdrawConsequence<Balance> {
}
impl<Balance: Zero> WithdrawConsequence<Balance> {
/// Convert the type into a `Result` with `TokenError` as the error or the additional `Balance`
/// Convert the type into a `Result` with `DispatchError` as the error or the additional `Balance`
/// by which the account will be reduced.
pub fn into_result(self) -> Result<Balance, TokenError> {
pub fn into_result(self) -> Result<Balance, DispatchError> {
use WithdrawConsequence::*;
match self {
NoFunds => Err(TokenError::NoFunds),
WouldDie => Err(TokenError::WouldDie),
UnknownAsset => Err(TokenError::UnknownAsset),
Underflow => Err(TokenError::Underflow),
Overflow => Err(TokenError::Overflow),
Frozen => Err(TokenError::Frozen),
NoFunds => Err(TokenError::NoFunds.into()),
WouldDie => Err(TokenError::WouldDie.into()),
UnknownAsset => Err(TokenError::UnknownAsset.into()),
Underflow => Err(ArithmeticError::Underflow.into()),
Overflow => Err(ArithmeticError::Overflow.into()),
Frozen => Err(TokenError::Frozen.into()),
ReducedToZero(result) => Ok(result),
Success => Ok(Zero::zero()),
}
@@ -90,13 +90,13 @@ pub enum DepositConsequence {
impl DepositConsequence {
/// Convert the type into a `Result` with `TokenError` as the error.
pub fn into_result(self) -> Result<(), TokenError> {
pub fn into_result(self) -> Result<(), DispatchError> {
use DepositConsequence::*;
Err(match self {
BelowMinimum => TokenError::BelowMinimum,
CannotCreate => TokenError::CannotCreate,
UnknownAsset => TokenError::UnknownAsset,
Overflow => TokenError::Overflow,
BelowMinimum => TokenError::BelowMinimum.into(),
CannotCreate => TokenError::CannotCreate.into(),
UnknownAsset => TokenError::UnknownAsset.into(),
Overflow => ArithmeticError::Overflow.into(),
Success => return Ok(()),
})
}