Skip to content

Commit f25b014

Browse files
fix: patch runner to not exit on calculating weight (polkadot-evm#1813)
* fix: patch runner to not exit on calculating weight * chore: fix tests * fix: test args * chore fmt
1 parent b742feb commit f25b014

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

frame/ethereum/src/tests/eip1559.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! Consensus extension module tests for BABE consensus.
1919
2020
use super::*;
21-
use evm::{ExitReason, ExitRevert, ExitSucceed};
21+
use evm::{ExitError, ExitReason, ExitRevert, ExitSucceed};
2222
use fp_ethereum::{TransactionData, ValidatedTransaction};
2323
use frame_support::{dispatch::DispatchClass, traits::Get, weights::Weight};
2424
use pallet_evm::{AddressMapping, GasWeightMapping};
@@ -577,6 +577,8 @@ fn proof_size_weight_limit_validation_works() {
577577
let alice = &pairs[0];
578578

579579
ext.execute_with(|| {
580+
System::set_block_number(1);
581+
580582
let mut tx = EIP1559UnsignedTransaction {
581583
nonce: U256::from(2),
582584
max_priority_fee_per_gas: U256::zero(),
@@ -596,11 +598,21 @@ fn proof_size_weight_limit_validation_works() {
596598
// Gas limit cannot afford the extra byte and thus is expected to exhaust.
597599
tx.input = vec![0u8; (weight_limit.proof_size() + 1) as usize];
598600
let tx = tx.sign(&alice.private_key, None);
601+
let transaction_hash = tx.hash();
599602

600-
// Execute
601-
assert!(
602-
Ethereum::transact(RawOrigin::EthereumTransaction(alice.address).into(), tx,).is_err()
603-
);
603+
// Execute - transaction is applied but execution fails with OutOfGas
604+
assert_ok!(Ethereum::apply_validated_transaction(
605+
alice.address,
606+
tx,
607+
None
608+
));
609+
System::assert_last_event(RuntimeEvent::Ethereum(Event::Executed {
610+
from: alice.address,
611+
to: alice.address,
612+
transaction_hash,
613+
exit_reason: ExitReason::Error(ExitError::OutOfGas),
614+
extra_data: vec![],
615+
}));
604616
});
605617
}
606618

frame/ethereum/src/tests/eip2930.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! Consensus extension module tests for BABE consensus.
1919
2020
use super::*;
21-
use evm::{ExitReason, ExitRevert, ExitSucceed};
21+
use evm::{ExitError, ExitReason, ExitRevert, ExitSucceed};
2222
use fp_ethereum::{TransactionData, ValidatedTransaction};
2323
use frame_support::{
2424
dispatch::{DispatchClass, GetDispatchInfo},
@@ -501,6 +501,8 @@ fn proof_size_weight_limit_validation_works() {
501501
let alice = &pairs[0];
502502

503503
ext.execute_with(|| {
504+
System::set_block_number(1);
505+
504506
let mut tx = EIP2930UnsignedTransaction {
505507
nonce: U256::from(2),
506508
gas_price: U256::from(1),
@@ -519,11 +521,21 @@ fn proof_size_weight_limit_validation_works() {
519521
// Gas limit cannot afford the extra byte and thus is expected to exhaust.
520522
tx.input = vec![0u8; (weight_limit.proof_size() + 1) as usize];
521523
let tx = tx.sign(&alice.private_key, None);
524+
let transaction_hash = tx.hash();
522525

523-
// Execute
524-
assert!(
525-
Ethereum::transact(RawOrigin::EthereumTransaction(alice.address).into(), tx,).is_err()
526-
);
526+
// Execute - transaction is applied but execution fails with OutOfGas
527+
assert_ok!(Ethereum::apply_validated_transaction(
528+
alice.address,
529+
tx,
530+
None
531+
));
532+
System::assert_last_event(RuntimeEvent::Ethereum(Event::Executed {
533+
from: alice.address,
534+
to: alice.address,
535+
transaction_hash,
536+
exit_reason: ExitReason::Error(ExitError::OutOfGas),
537+
extra_data: vec![],
538+
}));
527539
});
528540
}
529541

frame/ethereum/src/tests/legacy.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! Consensus extension module tests for BABE consensus.
1919
2020
use super::*;
21-
use evm::{ExitReason, ExitRevert, ExitSucceed};
21+
use evm::{ExitError, ExitReason, ExitRevert, ExitSucceed};
2222
use fp_ethereum::{TransactionData, ValidatedTransaction};
2323
use frame_support::{
2424
dispatch::{DispatchClass, GetDispatchInfo, Pays, PostDispatchInfo},
@@ -645,6 +645,8 @@ fn proof_size_weight_limit_validation_works() {
645645
let alice = &pairs[0];
646646

647647
ext.execute_with(|| {
648+
System::set_block_number(1);
649+
648650
let mut tx = LegacyUnsignedTransaction {
649651
nonce: U256::from(2),
650652
gas_price: U256::from(1),
@@ -663,11 +665,21 @@ fn proof_size_weight_limit_validation_works() {
663665
// Gas limit cannot afford the extra byte and thus is expected to exhaust.
664666
tx.input = vec![0u8; (weight_limit.proof_size() + 1) as usize];
665667
let tx = tx.sign(&alice.private_key);
668+
let transaction_hash = tx.hash();
666669

667-
// Execute
668-
assert!(
669-
Ethereum::transact(RawOrigin::EthereumTransaction(alice.address).into(), tx,).is_err()
670-
);
670+
// Execute - transaction is applied but execution fails with OutOfGas
671+
assert_ok!(Ethereum::apply_validated_transaction(
672+
alice.address,
673+
tx,
674+
None
675+
));
676+
System::assert_last_event(RuntimeEvent::Ethereum(Event::Executed {
677+
from: alice.address,
678+
to: alice.address,
679+
transaction_hash,
680+
exit_reason: ExitReason::Error(ExitError::OutOfGas),
681+
extra_data: vec![],
682+
}));
671683
});
672684
}
673685

frame/evm/src/runner/stack.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,21 @@ where
189189
{
190190
// Used to record the external costs in the evm through the StackState implementation
191191
let maybe_weight_info =
192-
WeightInfo::new_from_weight_limit(weight_limit, proof_size_base_cost).map_err(
193-
|_| RunnerError {
194-
error: Error::<T>::GasLimitTooLow,
195-
weight,
196-
},
197-
)?;
192+
match WeightInfo::new_from_weight_limit(weight_limit, proof_size_base_cost) {
193+
Ok(weight_info) => weight_info,
194+
Err(_) => {
195+
return Ok(ExecutionInfoV2 {
196+
exit_reason: ExitError::OutOfGas.into(),
197+
value: Default::default(),
198+
used_gas: fp_evm::UsedGas {
199+
standard: gas_limit.into(),
200+
effective: gas_limit.into(),
201+
},
202+
weight_info: None,
203+
logs: Default::default(),
204+
})
205+
}
206+
};
198207
// The precompile check is only used for transactional invocations. However, here we always
199208
// execute the check, because the check has side effects.
200209
match precompiles.is_precompile(source, gas_limit) {

0 commit comments

Comments
 (0)