Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

pallet-evm: return Ok(()) when EVM execution fails #6493

Merged
merged 11 commits into from
Jul 4, 2020
2 changes: 1 addition & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 254,
spec_version: 255,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
73 changes: 44 additions & 29 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

mod backend;
mod tests;

pub use crate::backend::{Account, Log, Vicinity, Backend};

Expand Down Expand Up @@ -159,7 +160,7 @@ decl_storage! {
trait Store for Module<T: Trait> as EVM {
Accounts get(fn accounts): map hasher(blake2_128_concat) H160 => Account;
AccountCodes get(fn account_codes): map hasher(blake2_128_concat) H160 => Vec<u8>;
AccountStorages get(fn account_storages):
AccountStorages get(fn account_storages):
double_map hasher(blake2_128_concat) H160, hasher(blake2_128_concat) H256 => H256;
}

Expand Down Expand Up @@ -190,6 +191,12 @@ decl_event! {
Log(Log),
/// A contract has been created at given address.
Created(H160),
/// A contract was attempted to be created, but the execution failed.
CreatedFailed(H160),
/// A contract has been executed successfully with states applied.
Executed(H160),
/// A contract has been executed with errors. States are reverted with only gas fees applied.
ExecutedFailed(H160),
/// A deposit has been made at a given address.
BalanceDeposit(AccountId, H160, U256),
/// A withdrawal has been made from a given address.
Expand All @@ -209,12 +216,6 @@ decl_error! {
WithdrawFailed,
/// Gas price is too low.
GasPriceTooLow,
/// Call failed
ExitReasonFailed,
/// Call reverted
ExitReasonRevert,
/// Call returned VM fatal error
ExitReasonFatal,
/// Nonce is invalid
InvalidNonce,
}
Expand Down Expand Up @@ -289,15 +290,24 @@ decl_module! {
let sender = ensure_signed(origin)?;
let source = T::ConvertAccountId::convert_account_id(&sender);

Self::execute_call(
match Self::execute_call(
source,
target,
input,
value,
gas_limit,
gas_price,
nonce,
).map_err(Into::into)
)? {
ExitReason::Succeed(_) => {
Module::<T>::deposit_event(Event::<T>::Executed(target));
},
_ => {
Module::<T>::deposit_event(Event::<T>::ExecutedFailed(target));
},
}

Ok(())
}

/// Issue an EVM create operation. This is similar to a contract creation transaction in
Expand All @@ -316,16 +326,22 @@ decl_module! {
let sender = ensure_signed(origin)?;
let source = T::ConvertAccountId::convert_account_id(&sender);

let create_address = Self::execute_create(
match Self::execute_create(
source,
init,
value,
gas_limit,
gas_price,
nonce
)?;
)? {
(create_address, ExitReason::Succeed(_)) => {
Module::<T>::deposit_event(Event::<T>::Created(create_address));
},
(create_address, _) => {
Module::<T>::deposit_event(Event::<T>::CreatedFailed(create_address));
},
}

Module::<T>::deposit_event(Event::<T>::Created(create_address));
Ok(())
}

Expand All @@ -345,17 +361,23 @@ decl_module! {
let sender = ensure_signed(origin)?;
let source = T::ConvertAccountId::convert_account_id(&sender);

let create_address = Self::execute_create2(
match Self::execute_create2(
source,
init,
salt,
value,
gas_limit,
gas_price,
nonce
)?;
)? {
(create_address, ExitReason::Succeed(_)) => {
Module::<T>::deposit_event(Event::<T>::Created(create_address));
},
(create_address, _) => {
Module::<T>::deposit_event(Event::<T>::CreatedFailed(create_address));
},
}

Module::<T>::deposit_event(Event::<T>::Created(create_address));
Ok(())
}
}
Expand Down Expand Up @@ -402,7 +424,7 @@ impl<T: Trait> Module<T> {
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>
) -> Result<H160, Error<T>> {
) -> Result<(H160, ExitReason), Error<T>> {
Self::execute_evm(
source,
value,
Expand Down Expand Up @@ -431,7 +453,7 @@ impl<T: Trait> Module<T> {
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>
) -> Result<H160, Error<T>> {
) -> Result<(H160, ExitReason), Error<T>> {
let code_hash = H256::from_slice(Keccak256::digest(&init).as_slice());
Self::execute_evm(
source,
Expand Down Expand Up @@ -462,8 +484,8 @@ impl<T: Trait> Module<T> {
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>,
) -> Result<(), Error<T>> {
Self::execute_evm(
) -> Result<ExitReason, Error<T>> {
Ok(Self::execute_evm(
source,
value,
gas_limit,
Expand All @@ -476,7 +498,7 @@ impl<T: Trait> Module<T> {
input,
gas_limit as usize,
)),
)
)?.1)
}

/// Execute an EVM operation.
Expand All @@ -487,7 +509,7 @@ impl<T: Trait> Module<T> {
gas_price: U256,
nonce: Option<U256>,
f: F,
) -> Result<R, Error<T>> where
) -> Result<(R, ExitReason), Error<T>> where
F: FnOnce(&mut StackExecutor<Backend<T>>) -> (R, ExitReason),
{
let vicinity = Vicinity {
Expand Down Expand Up @@ -516,19 +538,12 @@ impl<T: Trait> Module<T> {

let (retv, reason) = f(&mut executor);

let ret = match reason {
ExitReason::Succeed(_) => Ok(retv),
ExitReason::Error(_) => Err(Error::<T>::ExitReasonFailed),
ExitReason::Revert(_) => Err(Error::<T>::ExitReasonRevert),
ExitReason::Fatal(_) => Err(Error::<T>::ExitReasonFatal),
};

let actual_fee = executor.fee(gas_price);
executor.deposit(source, total_fee.saturating_sub(actual_fee));

let (values, logs) = executor.deconstruct();
backend.apply(values, logs, true);

ret
Ok((retv, reason))
}
}
168 changes: 168 additions & 0 deletions frame/evm/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#![cfg(test)]

use super::*;

use std::{str::FromStr, collections::BTreeMap};
use frame_support::{
assert_ok, impl_outer_origin, parameter_types, impl_outer_dispatch,
};
use sp_core::H256;
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
use sp_runtime::{
Perbill,
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};

impl_outer_origin! {
pub enum Origin for Test where system = frame_system {}
}

impl_outer_dispatch! {
pub enum OuterCall for Test where origin: Origin {
self::EVM,
}
}

// For testing the pallet, we construct most of a mock runtime. This means
// first constructing a configuration type (`Test`) which `impl`s each of the
// configuration traits of pallets we want to use.
#[derive(Clone, Eq, PartialEq)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
impl frame_system::Trait for Test {
type BaseCallFilter = ();
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Call = OuterCall;
type Hashing = BlakeTwo256;
type AccountId = H256;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
type BlockExecutionWeight = ();
type ExtrinsicBaseWeight = ();
type MaximumExtrinsicWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type ModuleToIndex = ();
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
}

parameter_types! {
pub const ExistentialDeposit: u64 = 1;
}
impl pallet_balances::Trait for Test {
type Balance = u64;
type DustRemoval = ();
type Event = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
}

parameter_types! {
pub const MinimumPeriod: u64 = 1000;
}
impl pallet_timestamp::Trait for Test {
type Moment = u64;
type OnTimestampSet = ();
type MinimumPeriod = MinimumPeriod;
}

/// Fixed gas price of `0`.
pub struct FixedGasPrice;
impl FeeCalculator for FixedGasPrice {
fn min_gas_price() -> U256 {
// Gas price is always one token per gas.
0.into()
}
}
parameter_types! {
pub const EVMModuleId: ModuleId = ModuleId(*b"py/evmpa");
}
impl Trait for Test {
type ModuleId = EVMModuleId;
type FeeCalculator = FixedGasPrice;
type ConvertAccountId = HashTruncateConvertAccountId<BlakeTwo256>;
type Currency = Balances;
type Event = Event<Test>;
type Precompiles = ();
}

type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type EVM = Module<Test>;

// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();

let mut accounts = BTreeMap::new();
accounts.insert(
H160::from_str("1000000000000000000000000000000000000001").unwrap(),
GenesisAccount {
nonce: U256::from(1),
balance: U256::from(1000000),
storage: Default::default(),
code: vec![
0x00, // STOP
],
}
);
accounts.insert(
H160::from_str("1000000000000000000000000000000000000002").unwrap(),
GenesisAccount {
nonce: U256::from(1),
balance: U256::from(1000000),
storage: Default::default(),
code: vec![
0xff, // INVALID
],
}
);

// We use default for brevity, but you can configure as desired if needed.
pallet_balances::GenesisConfig::<Test>::default().assimilate_storage(&mut t).unwrap();
GenesisConfig { accounts }.assimilate_storage(&mut t).unwrap();
t.into()
}

#[test]
fn fail_call_return_ok() {
new_test_ext().execute_with(|| {
assert_ok!(EVM::call(
Origin::signed(H256::default()),
H160::from_str("1000000000000000000000000000000000000001").unwrap(),
Vec::new(),
U256::default(),
1000000,
U256::default(),
None,
));

assert_ok!(EVM::call(
Origin::signed(H256::default()),
H160::from_str("1000000000000000000000000000000000000002").unwrap(),
Vec::new(),
U256::default(),
1000000,
U256::default(),
None,
));
});
}