Skip to content

refactor(levm): give more details in TxValidationError #3408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cmd/ef_tests/blockchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ hex.workspace = true
lazy_static.workspace = true
tokio = { workspace = true, features = ["full"] }
datatest-stable = "0.2.9"
regex = "1.11.1"

[lib]
path = "./lib.rs"
Expand Down
12 changes: 8 additions & 4 deletions cmd/ef_tests/blockchain/deserialize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::types::{BlockChainExpectedException, BlockExpectedException};
use serde::{Deserialize, Deserializer};

pub const SENDER_NOT_EOA_REGEX: &str = "Sender account .* shouldn't be a contract";
pub const PRIORITY_GREATER_THAN_MAX_FEE_PER_GAS_REGEX: &str =
"Priority fee .* is greater than max fee per gas .*";

pub fn deserialize_block_expected_exception<'de, D>(
deserializer: D,
) -> Result<Option<Vec<BlockChainExpectedException>>, D::Error>
Expand Down Expand Up @@ -45,12 +49,12 @@ where
"Insufficient account funds".to_string(),
)
}
"TransactionException.SENDER_NOT_EOA" => BlockChainExpectedException::TxtException(
"Sender account shouldn't be a contract".to_string(),
),
"TransactionException.SENDER_NOT_EOA" => {
BlockChainExpectedException::TxtException(SENDER_NOT_EOA_REGEX.to_string())
}
"TransactionException.PRIORITY_GREATER_THAN_MAX_FEE_PER_GAS" => {
BlockChainExpectedException::TxtException(
"Priority fee is greater than max fee per gas".to_string(),
PRIORITY_GREATER_THAN_MAX_FEE_PER_GAS_REGEX.to_string(),
)
}
"TransactionException.GAS_ALLOWANCE_EXCEEDED" => {
Expand Down
17 changes: 14 additions & 3 deletions cmd/ef_tests/blockchain/test_runner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashMap, path::Path};

use crate::{
deserialize::{PRIORITY_GREATER_THAN_MAX_FEE_PER_GAS_REGEX, SENDER_NOT_EOA_REGEX},
network::Network,
types::{BlockChainExpectedException, BlockExpectedException, BlockWithRLP, TestUnit},
};
Expand All @@ -19,6 +20,7 @@ use ethrex_common::{
use ethrex_rlp::decode::RLPDecode;
use ethrex_storage::{EngineType, Store};
use ethrex_vm::{EvmEngine, EvmError};
use regex::Regex;
use zkvm_interface::io::ProgramInput;

pub fn parse_and_execute(
Expand Down Expand Up @@ -130,7 +132,8 @@ fn exception_is_expected(
) = (exception, returned_error)
{
return match_alternative_revm_exception_msg(expected_error_msg, error_msg)
|| (expected_error_msg.to_lowercase() == error_msg.to_lowercase());
|| (expected_error_msg.to_lowercase() == error_msg.to_lowercase())
|| match_expected_regex(expected_error_msg, error_msg);
}
matches!(
(exception, &returned_error),
Expand Down Expand Up @@ -184,7 +187,7 @@ fn match_alternative_revm_exception_msg(expected_msg: &String, msg: &str) -> boo
(msg, expected_msg.as_str()),
(
"reject transactions from senders with deployed code",
"Sender account shouldn't be a contract"
SENDER_NOT_EOA_REGEX
) | (
"call gas cost exceeds the gas limit",
"Intrinsic gas too low"
Expand All @@ -205,11 +208,19 @@ fn match_alternative_revm_exception_msg(expected_msg: &String, msg: &str) -> boo
)
| (
"priority fee is greater than max fee",
"Priority fee is greater than max fee per gas"
PRIORITY_GREATER_THAN_MAX_FEE_PER_GAS_REGEX
)
| ("create initcode size limit", "Initcode size exceeded")
) || (msg.starts_with("lack of funds") && expected_msg == "Insufficient account funds")
}

fn match_expected_regex(expected_error_regex: &str, error_msg: &str) -> bool {
let Ok(regex) = Regex::new(expected_error_regex) else {
return false;
};
regex.is_match(error_msg)
}

/// Tests the rlp decoding of a block
fn exception_in_rlp_decoding(block_fixture: &BlockWithRLP) -> bool {
// NOTE: There is a test which validates that an EIP-7702 transaction is not allowed to
Expand Down
27 changes: 21 additions & 6 deletions cmd/ef_tests/state/runner/levm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,16 @@ fn exception_is_expected(
VMError::TxValidation(TxValidationError::InsufficientAccountFunds)
) | (
TransactionExpectedException::PriorityGreaterThanMaxFeePerGas,
VMError::TxValidation(TxValidationError::PriorityGreaterThanMaxFeePerGas)
VMError::TxValidation(TxValidationError::PriorityGreaterThanMaxFeePerGas {
priority_fee: _,
max_fee_per_gas: _
})
) | (
TransactionExpectedException::GasLimitPriceProductOverflow,
VMError::TxValidation(TxValidationError::GasLimitPriceProductOverflow)
) | (
TransactionExpectedException::SenderNotEoa,
VMError::TxValidation(TxValidationError::SenderNotEOA)
VMError::TxValidation(TxValidationError::SenderNotEOA(_))
) | (
TransactionExpectedException::InsufficientMaxFeePerGas,
VMError::TxValidation(TxValidationError::InsufficientMaxFeePerGas)
Expand All @@ -291,13 +294,19 @@ fn exception_is_expected(
VMError::TxValidation(TxValidationError::NonceIsMax)
) | (
TransactionExpectedException::GasAllowanceExceeded,
VMError::TxValidation(TxValidationError::GasAllowanceExceeded)
VMError::TxValidation(TxValidationError::GasAllowanceExceeded {
block_gas_limit: _,
tx_gas_limit: _
})
) | (
TransactionExpectedException::Type3TxPreFork,
VMError::TxValidation(TxValidationError::Type3TxPreFork)
) | (
TransactionExpectedException::Type3TxBlobCountExceeded,
VMError::TxValidation(TxValidationError::Type3TxBlobCountExceeded)
VMError::TxValidation(TxValidationError::Type3TxBlobCountExceeded {
max_blob_count: _,
actual_blob_count: _
})
) | (
TransactionExpectedException::Type3TxZeroBlobs,
VMError::TxValidation(TxValidationError::Type3TxZeroBlobs)
Expand All @@ -309,10 +318,16 @@ fn exception_is_expected(
VMError::TxValidation(TxValidationError::Type3TxInvalidBlobVersionedHash)
) | (
TransactionExpectedException::InsufficientMaxFeePerBlobGas,
VMError::TxValidation(TxValidationError::InsufficientMaxFeePerBlobGas)
VMError::TxValidation(TxValidationError::InsufficientMaxFeePerBlobGas {
base_fee_per_blob_gas: _,
tx_max_fee_per_blob_gas: _,
})
) | (
TransactionExpectedException::InitcodeSizeExceeded,
VMError::TxValidation(TxValidationError::InitcodeSizeExceeded)
VMError::TxValidation(TxValidationError::InitcodeSizeExceeded {
max_size: _,
actual_size: _
})
) | (
TransactionExpectedException::Type4TxContractCreation,
VMError::TxValidation(TxValidationError::Type4TxContractCreation)
Expand Down
44 changes: 31 additions & 13 deletions crates/vm/levm/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bytes::Bytes;
use derive_more::derive::Display;
use ethrex_common::types::Log;
use ethrex_common::{Address, U256, types::Log};
use serde::{Deserialize, Serialize};
use thiserror;

Expand Down Expand Up @@ -73,34 +73,52 @@ pub enum ExceptionalHalt {
// If any change is made here without changing the mapper it will break some hive tests.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, Serialize, Deserialize)]
pub enum TxValidationError {
#[error("Sender account shouldn't be a contract")]
SenderNotEOA,
#[error("Sender account {0} shouldn't be a contract")]
SenderNotEOA(Address),
#[error("Insufficient account funds")]
InsufficientAccountFunds,
#[error("Nonce is max")]
NonceIsMax,
#[error("Nonce mismatch: expected {expected}, got {actual}")]
NonceMismatch { expected: u64, actual: u64 },
#[error("Initcode size exceeded")]
InitcodeSizeExceeded,
#[error("Priority fee is greater than max fee per gas")]
PriorityGreaterThanMaxFeePerGas,
#[error("Initcode size exceeded, max size: {max_size}, actual size: {actual_size}")]
InitcodeSizeExceeded { max_size: usize, actual_size: usize },
#[error("Priority fee {priority_fee} is greater than max fee per gas {max_fee_per_gas}")]
PriorityGreaterThanMaxFeePerGas {
priority_fee: U256,
max_fee_per_gas: U256,
},
#[error("Intrinsic gas too low")]
IntrinsicGasTooLow,
#[error("Gas allowance exceeded")]
GasAllowanceExceeded,
#[error(
"Gas allowance exceeded. Block gas limit: {block_gas_limit}, transaction gas limit: {tx_gas_limit}"
)]
GasAllowanceExceeded {
block_gas_limit: u64,
tx_gas_limit: u64,
},
#[error("Insufficient max fee per gas")]
InsufficientMaxFeePerGas,
#[error("Insufficient max fee per blob gas")]
InsufficientMaxFeePerBlobGas,
#[error(
"Insufficient max fee per blob gas. Expected at least {base_fee_per_blob_gas}, got: {tx_max_fee_per_blob_gas}"
)]
InsufficientMaxFeePerBlobGas {
base_fee_per_blob_gas: U256,
tx_max_fee_per_blob_gas: U256,
},
#[error("Type 3 transactions are not supported before the Cancun fork")]
Type3TxPreFork,
#[error("Type 3 transaction without blobs")]
Type3TxZeroBlobs,
#[error("Invalid blob versioned hash")]
Type3TxInvalidBlobVersionedHash,
#[error("Blob count exceeded")]
Type3TxBlobCountExceeded,
#[error(
"Blob count exceeded. Max blob count: {max_blob_count}, actual blob count: {actual_blob_count}"
)]
Type3TxBlobCountExceeded {
max_blob_count: usize,
actual_blob_count: usize,
},
#[error("Contract creation in blob transaction")]
Type3TxContractCreation,
#[error("Type 4 transactions are not supported before the Prague fork")]
Expand Down
63 changes: 41 additions & 22 deletions crates/vm/levm/src/hooks/default_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,16 @@ impl Hook for DefaultHook {
vm.env.tx_max_fee_per_gas,
) {
if tx_max_priority_fee > tx_max_fee_per_gas {
return Err(TxValidationError::PriorityGreaterThanMaxFeePerGas.into());
return Err(TxValidationError::PriorityGreaterThanMaxFeePerGas {
priority_fee: tx_max_priority_fee,
max_fee_per_gas: tx_max_fee_per_gas,
}
.into());
}
}

// (9) SENDER_NOT_EOA
validate_sender(vm.db.get_account(sender_address)?)?;
validate_sender(sender_address, vm.db.get_account(sender_address)?)?;

// (10) GAS_ALLOWANCE_EXCEEDED
validate_gas_allowance(vm)?;
Expand Down Expand Up @@ -263,20 +267,27 @@ pub fn validate_max_fee_per_blob_gas(
vm: &mut VM<'_>,
tx_max_fee_per_blob_gas: U256,
) -> Result<(), VMError> {
if tx_max_fee_per_blob_gas
< get_base_fee_per_blob_gas(vm.env.block_excess_blob_gas, &vm.env.config)?
{
return Err(TxValidationError::InsufficientMaxFeePerBlobGas.into());
let base_fee_per_blob_gas =
get_base_fee_per_blob_gas(vm.env.block_excess_blob_gas, &vm.env.config)?;
if tx_max_fee_per_blob_gas < base_fee_per_blob_gas {
return Err(TxValidationError::InsufficientMaxFeePerBlobGas {
base_fee_per_blob_gas,
tx_max_fee_per_blob_gas,
}
.into());
}
Ok(())
}

pub fn validate_init_code_size(vm: &mut VM<'_>) -> Result<(), VMError> {
// [EIP-3860] - INITCODE_SIZE_EXCEEDED
if vm.current_call_frame()?.calldata.len() > INIT_CODE_MAX_SIZE
&& vm.env.config.fork >= Fork::Shanghai
{
return Err(TxValidationError::InitcodeSizeExceeded.into());
let code_size = vm.current_call_frame()?.calldata.len();
if code_size > INIT_CODE_MAX_SIZE && vm.env.config.fork >= Fork::Shanghai {
return Err(TxValidationError::InitcodeSizeExceeded {
max_size: INIT_CODE_MAX_SIZE,
actual_size: code_size,
}
.into());
}
Ok(())
}
Expand Down Expand Up @@ -312,15 +323,20 @@ pub fn validate_4844_tx(vm: &mut VM<'_>) -> Result<(), VMError> {
}

// (14) TYPE_3_TX_BLOB_COUNT_EXCEEDED
if blob_hashes.len()
> vm.env
.config
.blob_schedule
.max
.try_into()
.map_err(|_| InternalError::TypeConversion)?
{
return Err(TxValidationError::Type3TxBlobCountExceeded.into());
let max_blob_count = vm
.env
.config
.blob_schedule
.max
.try_into()
.map_err(|_| InternalError::TypeConversion)?;
let blob_count = blob_hashes.len();
if blob_count > max_blob_count {
return Err(TxValidationError::Type3TxBlobCountExceeded {
max_blob_count,
actual_blob_count: blob_count,
}
.into());
}

// (15) TYPE_3_TX_CONTRACT_CREATION
Expand Down Expand Up @@ -369,16 +385,19 @@ pub fn validate_type_4_tx(vm: &mut VM<'_>) -> Result<(), VMError> {
vm.eip7702_set_access_code()
}

pub fn validate_sender(sender_account: &Account) -> Result<(), VMError> {
pub fn validate_sender(sender_address: Address, sender_account: &Account) -> Result<(), VMError> {
if sender_account.has_code() && !has_delegation(sender_account)? {
return Err(TxValidationError::SenderNotEOA.into());
return Err(TxValidationError::SenderNotEOA(sender_address).into());
}
Ok(())
}

pub fn validate_gas_allowance(vm: &mut VM<'_>) -> Result<(), TxValidationError> {
if vm.env.gas_limit > vm.env.block_gas_limit {
return Err(TxValidationError::GasAllowanceExceeded);
return Err(TxValidationError::GasAllowanceExceeded {
block_gas_limit: vm.env.block_gas_limit,
tx_gas_limit: vm.env.gas_limit,
});
}
Ok(())
}
Expand Down
Loading