Skip to content

Commit 52f67b6

Browse files
committed
Implement apply_state flag and allow fetching return data and used gas (#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
1 parent 8751e20 commit 52f67b6

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ sp-std = { version = "2.0.0-rc4", default-features = false, path = "../../primit
2424
sp-io = { version = "2.0.0-rc4", default-features = false, path = "../../primitives/io" }
2525
primitive-types = { version = "0.7.0", default-features = false, features = ["rlp"] }
2626
rlp = { version = "0.4", default-features = false }
27-
evm = { version = "0.16", default-features = false }
27+
evm = { version = "0.17", default-features = false }
2828
sha3 = { version = "0.8", default-features = false }
2929

3030
[features]

src/lib.rs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,12 @@ decl_module! {
310310
gas_limit,
311311
gas_price,
312312
nonce,
313+
true,
313314
)? {
314-
ExitReason::Succeed(_) => {
315+
(ExitReason::Succeed(_), _, _) => {
315316
Module::<T>::deposit_event(Event::<T>::Executed(target));
316317
},
317-
ExitReason::Error(_) | ExitReason::Revert(_) | ExitReason::Fatal(_) => {
318+
(_, _, _) => {
318319
Module::<T>::deposit_event(Event::<T>::ExecutedFailed(target));
319320
},
320321
}
@@ -344,12 +345,13 @@ decl_module! {
344345
value,
345346
gas_limit,
346347
gas_price,
347-
nonce
348+
nonce,
349+
true,
348350
)? {
349-
(create_address, ExitReason::Succeed(_)) => {
351+
(ExitReason::Succeed(_), create_address, _) => {
350352
Module::<T>::deposit_event(Event::<T>::Created(create_address));
351353
},
352-
(create_address, _) => {
354+
(_, create_address, _) => {
353355
Module::<T>::deposit_event(Event::<T>::CreatedFailed(create_address));
354356
},
355357
}
@@ -380,12 +382,13 @@ decl_module! {
380382
value,
381383
gas_limit,
382384
gas_price,
383-
nonce
385+
nonce,
386+
true,
384387
)? {
385-
(create_address, ExitReason::Succeed(_)) => {
388+
(ExitReason::Succeed(_), create_address, _) => {
386389
Module::<T>::deposit_event(Event::<T>::Created(create_address));
387390
},
388-
(create_address, _) => {
391+
(_, create_address, _) => {
389392
Module::<T>::deposit_event(Event::<T>::CreatedFailed(create_address));
390393
},
391394
}
@@ -435,23 +438,26 @@ impl<T: Trait> Module<T> {
435438
value: U256,
436439
gas_limit: u32,
437440
gas_price: U256,
438-
nonce: Option<U256>
439-
) -> Result<(H160, ExitReason), Error<T>> {
441+
nonce: Option<U256>,
442+
apply_state: bool,
443+
) -> Result<(ExitReason, H160, U256), Error<T>> {
440444
Self::execute_evm(
441445
source,
442446
value,
443447
gas_limit,
444448
gas_price,
445449
nonce,
450+
apply_state,
446451
|executor| {
447-
(executor.create_address(
452+
let address = executor.create_address(
448453
evm::CreateScheme::Legacy { caller: source },
449-
), executor.transact_create(
454+
);
455+
(executor.transact_create(
450456
source,
451457
value,
452458
init,
453459
gas_limit as usize,
454-
))
460+
), address)
455461
},
456462
)
457463
}
@@ -464,25 +470,28 @@ impl<T: Trait> Module<T> {
464470
value: U256,
465471
gas_limit: u32,
466472
gas_price: U256,
467-
nonce: Option<U256>
468-
) -> Result<(H160, ExitReason), Error<T>> {
473+
nonce: Option<U256>,
474+
apply_state: bool,
475+
) -> Result<(ExitReason, H160, U256), Error<T>> {
469476
let code_hash = H256::from_slice(Keccak256::digest(&init).as_slice());
470477
Self::execute_evm(
471478
source,
472479
value,
473480
gas_limit,
474481
gas_price,
475482
nonce,
483+
apply_state,
476484
|executor| {
477-
(executor.create_address(
485+
let address = executor.create_address(
478486
evm::CreateScheme::Create2 { caller: source, code_hash, salt },
479-
), executor.transact_create2(
487+
);
488+
(executor.transact_create2(
480489
source,
481490
value,
482491
init,
483492
salt,
484493
gas_limit as usize,
485-
))
494+
), address)
486495
},
487496
)
488497
}
@@ -496,21 +505,23 @@ impl<T: Trait> Module<T> {
496505
gas_limit: u32,
497506
gas_price: U256,
498507
nonce: Option<U256>,
499-
) -> Result<ExitReason, Error<T>> {
500-
Ok(Self::execute_evm(
508+
apply_state: bool,
509+
) -> Result<(ExitReason, Vec<u8>, U256), Error<T>> {
510+
Self::execute_evm(
501511
source,
502512
value,
503513
gas_limit,
504514
gas_price,
505515
nonce,
506-
|executor| ((), executor.transact_call(
516+
apply_state,
517+
|executor| executor.transact_call(
507518
source,
508519
target,
509520
value,
510521
input,
511522
gas_limit as usize,
512-
)),
513-
)?.1)
523+
),
524+
)
514525
}
515526

516527
/// Execute an EVM operation.
@@ -520,9 +531,10 @@ impl<T: Trait> Module<T> {
520531
gas_limit: u32,
521532
gas_price: U256,
522533
nonce: Option<U256>,
534+
apply_state: bool,
523535
f: F,
524-
) -> Result<(R, ExitReason), Error<T>> where
525-
F: FnOnce(&mut StackExecutor<Backend<T>>) -> (R, ExitReason),
536+
) -> Result<(ExitReason, R, U256), Error<T>> where
537+
F: FnOnce(&mut StackExecutor<Backend<T>>) -> (ExitReason, R),
526538
{
527539
let vicinity = Vicinity {
528540
gas_price,
@@ -550,12 +562,15 @@ impl<T: Trait> Module<T> {
550562

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

565+
let used_gas = U256::from(executor.used_gas());
553566
let actual_fee = executor.fee(gas_price);
554567
executor.deposit(source, total_fee.saturating_sub(actual_fee));
555568

556-
let (values, logs) = executor.deconstruct();
557-
backend.apply(values, logs, true);
569+
if apply_state {
570+
let (values, logs) = executor.deconstruct();
571+
backend.apply(values, logs, true);
572+
}
558573

559-
Ok((retv, reason))
574+
Ok((retv, reason, used_gas))
560575
}
561576
}

0 commit comments

Comments
 (0)