Skip to content

Commit

Permalink
Implement apply_state flag and allow fetching return data and used g…
Browse files Browse the repository at this point in the history
…as (paritytech#6590)

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

* Bump spec version

* Implement apply_state flag and allow fetching return data and used gas

* Update evm version
  • Loading branch information
sorpaas authored Jul 14, 2020
1 parent 6add375 commit b06a383
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion frame/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sp-std = { version = "2.0.0-rc4", default-features = false, path = "../../primit
sp-io = { version = "2.0.0-rc4", default-features = false, path = "../../primitives/io" }
primitive-types = { version = "0.7.0", default-features = false, features = ["rlp"] }
rlp = { version = "0.4", default-features = false }
evm = { version = "0.16", default-features = false }
evm = { version = "0.17", default-features = false }
sha3 = { version = "0.8", default-features = false }

[features]
Expand Down
71 changes: 43 additions & 28 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,12 @@ decl_module! {
gas_limit,
gas_price,
nonce,
true,
)? {
ExitReason::Succeed(_) => {
(ExitReason::Succeed(_), _, _) => {
Module::<T>::deposit_event(Event::<T>::Executed(target));
},
ExitReason::Error(_) | ExitReason::Revert(_) | ExitReason::Fatal(_) => {
(_, _, _) => {
Module::<T>::deposit_event(Event::<T>::ExecutedFailed(target));
},
}
Expand Down Expand Up @@ -344,12 +345,13 @@ decl_module! {
value,
gas_limit,
gas_price,
nonce
nonce,
true,
)? {
(create_address, ExitReason::Succeed(_)) => {
(ExitReason::Succeed(_), create_address, _) => {
Module::<T>::deposit_event(Event::<T>::Created(create_address));
},
(create_address, _) => {
(_, create_address, _) => {
Module::<T>::deposit_event(Event::<T>::CreatedFailed(create_address));
},
}
Expand Down Expand Up @@ -380,12 +382,13 @@ decl_module! {
value,
gas_limit,
gas_price,
nonce
nonce,
true,
)? {
(create_address, ExitReason::Succeed(_)) => {
(ExitReason::Succeed(_), create_address, _) => {
Module::<T>::deposit_event(Event::<T>::Created(create_address));
},
(create_address, _) => {
(_, create_address, _) => {
Module::<T>::deposit_event(Event::<T>::CreatedFailed(create_address));
},
}
Expand Down Expand Up @@ -435,23 +438,26 @@ impl<T: Trait> Module<T> {
value: U256,
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>
) -> Result<(H160, ExitReason), Error<T>> {
nonce: Option<U256>,
apply_state: bool,
) -> Result<(ExitReason, H160, U256), Error<T>> {
Self::execute_evm(
source,
value,
gas_limit,
gas_price,
nonce,
apply_state,
|executor| {
(executor.create_address(
let address = executor.create_address(
evm::CreateScheme::Legacy { caller: source },
), executor.transact_create(
);
(executor.transact_create(
source,
value,
init,
gas_limit as usize,
))
), address)
},
)
}
Expand All @@ -464,25 +470,28 @@ impl<T: Trait> Module<T> {
value: U256,
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>
) -> Result<(H160, ExitReason), Error<T>> {
nonce: Option<U256>,
apply_state: bool,
) -> Result<(ExitReason, H160, U256), Error<T>> {
let code_hash = H256::from_slice(Keccak256::digest(&init).as_slice());
Self::execute_evm(
source,
value,
gas_limit,
gas_price,
nonce,
apply_state,
|executor| {
(executor.create_address(
let address = executor.create_address(
evm::CreateScheme::Create2 { caller: source, code_hash, salt },
), executor.transact_create2(
);
(executor.transact_create2(
source,
value,
init,
salt,
gas_limit as usize,
))
), address)
},
)
}
Expand All @@ -496,21 +505,23 @@ impl<T: Trait> Module<T> {
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>,
) -> Result<ExitReason, Error<T>> {
Ok(Self::execute_evm(
apply_state: bool,
) -> Result<(ExitReason, Vec<u8>, U256), Error<T>> {
Self::execute_evm(
source,
value,
gas_limit,
gas_price,
nonce,
|executor| ((), executor.transact_call(
apply_state,
|executor| executor.transact_call(
source,
target,
value,
input,
gas_limit as usize,
)),
)?.1)
),
)
}

/// Execute an EVM operation.
Expand All @@ -520,9 +531,10 @@ impl<T: Trait> Module<T> {
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>,
apply_state: bool,
f: F,
) -> Result<(R, ExitReason), Error<T>> where
F: FnOnce(&mut StackExecutor<Backend<T>>) -> (R, ExitReason),
) -> Result<(ExitReason, R, U256), Error<T>> where
F: FnOnce(&mut StackExecutor<Backend<T>>) -> (ExitReason, R),
{
let vicinity = Vicinity {
gas_price,
Expand Down Expand Up @@ -550,12 +562,15 @@ impl<T: Trait> Module<T> {

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

let used_gas = U256::from(executor.used_gas());
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);
if apply_state {
let (values, logs) = executor.deconstruct();
backend.apply(values, logs, true);
}

Ok((retv, reason))
Ok((retv, reason, used_gas))
}
}

0 comments on commit b06a383

Please sign in to comment.