More decl_error! migrations (#4427)

* Update assets to `decl_error`

* Update aura to `decl_error`

* Update authority discovery to `decl_error`

* Update collective to `decl_error`

* Update evm to `decl_error!`

* Fix error with replace

* Revert "Update authority discovery to `decl_error`"

This reverts commit 26e8f3c56656963d847e984c6f2c8e1f88014899.

* Revert "Update aura to `decl_error`"

This reverts commit 8f17c44ca8375a4a755710aaab7ad4d9522c4376.

* Update democracy to `decl_error`

* Update finality-tracker to `decl_error`

* Update grandpa to `decl_error`

* `assert` to `ensure` in dispatchable function
This commit is contained in:
Shawn Tabrizi
2019-12-18 10:50:21 +01:00
committed by Gavin Wood
parent 9550c845f3
commit 57c19bcf4e
6 changed files with 323 additions and 194 deletions
+50 -35
View File
@@ -24,13 +24,13 @@ mod backend;
pub use crate::backend::{Account, Log, Vicinity, Backend};
use sp_std::{vec::Vec, marker::PhantomData};
use frame_support::{dispatch, decl_module, decl_storage, decl_event};
use frame_support::{decl_module, decl_storage, decl_event, decl_error};
use frame_support::weights::{Weight, WeighData, ClassifyDispatch, DispatchClass, PaysFee};
use frame_support::traits::{Currency, WithdrawReason, ExistenceRequirement};
use frame_system::{self as system, ensure_signed};
use sp_runtime::ModuleId;
use frame_support::weights::SimpleDispatchInfo;
use sp_runtime::traits::{UniqueSaturatedInto, AccountIdConversion, SaturatedConversion};
use sp_runtime::traits::{UniqueSaturatedInto, AccountIdConversion, SaturatedConversion, ModuleDispatchError};
use sp_core::{U256, H256, H160};
use evm::{ExitReason, ExitSucceed, ExitError};
use evm::executor::StackExecutor;
@@ -137,21 +137,42 @@ decl_storage! {
}
}
decl_event!(
decl_event! {
/// EVM events
pub enum Event {
/// Ethereum events from contracts.
Log(Log),
}
);
}
decl_error! {
pub enum Error {
/// Not enough balance to perform action
BalanceLow,
/// Calculating total fee overflowed
FeeOverflow,
/// Calculating total payment overflowed
PaymentOverflow,
/// Withdraw fee failed
WithdrawFailed,
/// Call failed
ExitReasonFailed,
/// Call reverted
ExitReasonRevert,
/// Call returned VM fatal error
ExitReasonFatal,
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error;
fn deposit_event() = default;
#[weight = SimpleDispatchInfo::FixedNormal(10_000)]
fn deposit_balance(origin, value: BalanceOf<T>) -> dispatch::Result {
let sender = ensure_signed(origin)?;
fn deposit_balance(origin, value: BalanceOf<T>) {
let sender = ensure_signed(origin).map_err(|e| e.as_str())?;
let imbalance = T::Currency::withdraw(
&sender,
@@ -166,19 +187,17 @@ decl_module! {
Accounts::mutate(&address, |account| {
account.balance += bvalue;
});
Ok(())
}
#[weight = SimpleDispatchInfo::FixedNormal(10_000)]
fn withdraw_balance(origin, value: BalanceOf<T>) -> dispatch::Result {
let sender = ensure_signed(origin)?;
fn withdraw_balance(origin, value: BalanceOf<T>) {
let sender = ensure_signed(origin).map_err(|e| e.as_str())?;
let address = T::ConvertAccountId::convert_account_id(&sender);
let bvalue = U256::from(UniqueSaturatedInto::<u128>::unique_saturated_into(value));
let mut account = Accounts::get(&address);
account.balance = account.balance.checked_sub(bvalue)
.ok_or("Not enough balance to withdraw")?;
.ok_or(Error::BalanceLow)?;
let imbalance = T::Currency::withdraw(
&Self::account_id(),
@@ -190,15 +209,11 @@ decl_module! {
Accounts::insert(&address, account);
T::Currency::resolve_creating(&sender, imbalance);
Ok(())
}
#[weight = WeightForCallCreate::<T::FeeCalculator>::default()]
fn call(origin, target: H160, input: Vec<u8>, value: U256, gas_limit: u32)
-> dispatch::Result
{
let sender = ensure_signed(origin)?;
fn call(origin, target: H160, input: Vec<u8>, value: U256, gas_limit: u32) {
let sender = ensure_signed(origin).map_err(|e| e.as_str())?;
let source = T::ConvertAccountId::convert_account_id(&sender);
let gas_price = T::FeeCalculator::gas_price();
@@ -216,13 +231,13 @@ decl_module! {
);
let total_fee = gas_price.checked_mul(U256::from(gas_limit))
.ok_or("Calculating total fee overflowed")?;
.ok_or(Error::FeeOverflow)?;
if Accounts::get(&source).balance <
value.checked_add(total_fee).ok_or("Calculating total payment overflowed")?
value.checked_add(total_fee).ok_or(Error::PaymentOverflow)?
{
return Err("Not enough balance to pay transaction fee")
return Err(Error::BalanceLow)
}
executor.withdraw(source, total_fee).map_err(|_| "Withdraw fee failed")?;
executor.withdraw(source, total_fee).map_err(|_| Error::WithdrawFailed)?;
let reason = executor.transact_call(
source,
@@ -234,9 +249,9 @@ decl_module! {
let ret = match reason {
ExitReason::Succeed(_) => Ok(()),
ExitReason::Error(_) => Err("Execute message call failed"),
ExitReason::Revert(_) => Err("Execute message call reverted"),
ExitReason::Fatal(_) => Err("Execute message call returned VM fatal error"),
ExitReason::Error(_) => Err(Error::ExitReasonFailed),
ExitReason::Revert(_) => Err(Error::ExitReasonRevert),
ExitReason::Fatal(_) => Err(Error::ExitReasonFatal),
};
let actual_fee = executor.fee(gas_price);
executor.deposit(source, total_fee.saturating_sub(actual_fee));
@@ -244,12 +259,12 @@ decl_module! {
let (values, logs) = executor.deconstruct();
backend.apply(values, logs, true);
ret
return ret;
}
#[weight = WeightForCallCreate::<T::FeeCalculator>::default()]
fn create(origin, init: Vec<u8>, value: U256, gas_limit: u32) -> dispatch::Result {
let sender = ensure_signed(origin)?;
fn create(origin, init: Vec<u8>, value: U256, gas_limit: u32) {
let sender = ensure_signed(origin).map_err(|e| e.as_str())?;
let source = T::ConvertAccountId::convert_account_id(&sender);
let gas_price = T::FeeCalculator::gas_price();
@@ -267,13 +282,13 @@ decl_module! {
);
let total_fee = gas_price.checked_mul(U256::from(gas_limit))
.ok_or("Calculating total fee overflowed")?;
.ok_or(Error::FeeOverflow)?;
if Accounts::get(&source).balance <
value.checked_add(total_fee).ok_or("Calculating total payment overflowed")?
value.checked_add(total_fee).ok_or(Error::PaymentOverflow)?
{
return Err("Not enough balance to pay transaction fee")
return Err(Error::BalanceLow)
}
executor.withdraw(source, total_fee).map_err(|_| "Withdraw fee failed")?;
executor.withdraw(source, total_fee).map_err(|_| Error::WithdrawFailed)?;
let reason = executor.transact_create(
source,
@@ -284,9 +299,9 @@ decl_module! {
let ret = match reason {
ExitReason::Succeed(_) => Ok(()),
ExitReason::Error(_) => Err("Execute contract creation failed"),
ExitReason::Revert(_) => Err("Execute contract creation reverted"),
ExitReason::Fatal(_) => Err("Execute contract creation returned VM fatal error"),
ExitReason::Error(_) => Err(Error::ExitReasonFailed),
ExitReason::Revert(_) => Err(Error::ExitReasonRevert),
ExitReason::Fatal(_) => Err(Error::ExitReasonFatal),
};
let actual_fee = executor.fee(gas_price);
executor.deposit(source, total_fee.saturating_sub(actual_fee));
@@ -294,7 +309,7 @@ decl_module! {
let (values, logs) = executor.deconstruct();
backend.apply(values, logs, true);
ret
return ret;
}
}
}