Skip to content
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
15 changes: 12 additions & 3 deletions crates/rpc/rpc-eth-api/src/helpers/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use reth_revm::{database::StateProviderDatabase, db::State};
use reth_rpc_convert::{RpcConvert, RpcTxReq};
use reth_rpc_eth_types::{
cache::db::StateProviderTraitObjWrapper,
error::FromEthApiError,
error::{AsEthApiError, FromEthApiError},
simulate::{self, EthSimulateError},
EthApiError, StateCacheDb,
};
Expand Down Expand Up @@ -159,6 +159,13 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
.context_for_next_block(&parent, this.next_env_attributes(&parent)?)
.map_err(RethError::other)
.map_err(Self::Error::from_eth_err)?;
let map_err = |e: EthApiError| -> Self::Error {
match e.as_simulate_error() {
Some(sim_err) => Self::Error::from_eth_err(EthApiError::other(sim_err)),
None => Self::Error::from_eth_err(e),
}
};
Comment on lines +162 to +167
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't too bad either


let (result, results) = if trace_transfers {
// prepare inspector to capture transfer inside the evm so they are recorded
// and included in logs
Expand All @@ -173,7 +180,8 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
default_gas_limit,
chain_id,
this.converter(),
)?
)
.map_err(map_err)?
} else {
let evm = this.evm_config().evm_with_env(&mut db, evm_env);
let builder = this.evm_config().create_block_builder(evm, &parent, ctx);
Expand All @@ -183,7 +191,8 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA
default_gas_limit,
chain_id,
this.converter(),
)?
)
.map_err(map_err)?
};

parent = result.block.clone_sealed_header();
Expand Down
28 changes: 27 additions & 1 deletion crates/rpc/rpc-eth-types/src/error/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Helper traits to wrap generic l1 errors, in network specific error type configured in
//! `reth_rpc_eth_api::EthApiTypes`.

use crate::{EthApiError, RevertError};
use crate::{simulate::EthSimulateError, EthApiError, RevertError};
use alloy_primitives::Bytes;
use reth_errors::ProviderError;
use reth_evm::{ConfigureEvm, EvmErrorFor, HaltReasonFor};
Expand Down Expand Up @@ -74,6 +74,32 @@ pub trait AsEthApiError {

false
}

/// Returns [`EthSimulateError`] if this error maps to a simulate-specific error code.
fn as_simulate_error(&self) -> Option<EthSimulateError> {
Comment on lines +78 to +79
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, this should actually work

let err = self.as_err()?;
match err {
EthApiError::InvalidTransaction(tx_err) => match tx_err {
RpcInvalidTransactionError::NonceTooLow { tx, state } => {
Some(EthSimulateError::NonceTooLow { tx: *tx, state: *state })
}
RpcInvalidTransactionError::NonceTooHigh => Some(EthSimulateError::NonceTooHigh),
RpcInvalidTransactionError::FeeCapTooLow => {
Some(EthSimulateError::BaseFeePerGasTooLow)
}
RpcInvalidTransactionError::GasTooLow => Some(EthSimulateError::IntrinsicGasTooLow),
RpcInvalidTransactionError::InsufficientFunds { cost, balance } => {
Some(EthSimulateError::InsufficientFunds { cost: *cost, balance: *balance })
}
RpcInvalidTransactionError::SenderNoEOA => Some(EthSimulateError::SenderNotEOA),
RpcInvalidTransactionError::MaxInitCodeSizeExceeded => {
Some(EthSimulateError::MaxInitCodeSizeExceeded)
}
_ => None,
},
_ => None,
}
}
}

impl AsEthApiError for EthApiError {
Expand Down
59 changes: 57 additions & 2 deletions crates/rpc/rpc-eth-types/src/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use reth_storage_api::noop::NoopProvider;
use revm::{
context::Block,
context_interface::result::ExecutionResult,
primitives::{Address, Bytes, TxKind},
primitives::{Address, Bytes, TxKind, U256},
Database,
};

Expand All @@ -36,12 +36,67 @@ pub enum EthSimulateError {
/// Max gas limit for entire operation exceeded.
#[error("Client adjustable limit reached")]
GasLimitReached,
/// Block number in sequence did not increase.
#[error("Block number in sequence did not increase")]
BlockNumberInvalid,
/// Block timestamp in sequence did not increase or stay the same.
#[error("Block timestamp in sequence did not increase")]
BlockTimestampInvalid,
/// Transaction nonce is too low.
#[error("nonce too low: next nonce {state}, tx nonce {tx}")]
NonceTooLow {
/// Transaction nonce.
tx: u64,
/// Current state nonce.
state: u64,
},
/// Transaction nonce is too high.
#[error("nonce too high")]
NonceTooHigh,
/// Transaction's baseFeePerGas is too low.
#[error("max fee per gas less than block base fee")]
BaseFeePerGasTooLow,
/// Not enough gas provided to pay for intrinsic gas.
#[error("intrinsic gas too low")]
IntrinsicGasTooLow,
/// Insufficient funds to pay for gas fees and value.
#[error("insufficient funds for gas * price + value: have {balance} want {cost}")]
InsufficientFunds {
/// Transaction cost.
cost: U256,
/// Sender balance.
balance: U256,
},
/// Sender is not an EOA.
#[error("sender is not an EOA")]
SenderNotEOA,
/// Max init code size exceeded.
#[error("max initcode size exceeded")]
MaxInitCodeSizeExceeded,
/// `MovePrecompileToAddress` referenced itself in replacement.
#[error("MovePrecompileToAddress referenced itself")]
PrecompileSelfReference,
/// Multiple `MovePrecompileToAddress` referencing the same address.
#[error("Multiple MovePrecompileToAddress referencing the same address")]
PrecompileDuplicateAddress,
}

impl EthSimulateError {
const fn error_code(&self) -> i32 {
/// Returns the JSON-RPC error code for a `eth_simulateV1` error.
pub const fn error_code(&self) -> i32 {
match self {
Self::NonceTooLow { .. } => -38010,
Self::NonceTooHigh => -38011,
Self::BaseFeePerGasTooLow => -38012,
Self::IntrinsicGasTooLow => -38013,
Self::InsufficientFunds { .. } => -38014,
Self::BlockGasLimitExceeded => -38015,
Self::BlockNumberInvalid => -38020,
Self::BlockTimestampInvalid => -38021,
Self::PrecompileSelfReference => -38022,
Self::PrecompileDuplicateAddress => -38023,
Self::SenderNotEOA => -38024,
Self::MaxInitCodeSizeExceeded => -38025,
Comment on lines +88 to +99
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be also great to check if any of those codes should apply here

/// Returns the rpc error code for this error.
pub const fn error_code(&self) -> i32 {
match self {
Self::InvalidChainId |
Self::GasTooLow |
Self::GasTooHigh |
Self::GasRequiredExceedsAllowance { .. } |
Self::NonceTooLow { .. } |
Self::NonceTooHigh { .. } |
Self::FeeCapTooLow |
Self::FeeCapVeryHigh => EthRpcErrorCode::InvalidInput.code(),
Self::Revert(_) => EthRpcErrorCode::ExecutionError.code(),
_ => EthRpcErrorCode::TransactionRejected.code(),
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree, let me take a look at it

Copy link
Contributor Author

@figtracer figtracer Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i think we can leave it like this, there's nothing else to be added here

     pub const fn error_code(&self) -> i32 { 
         match self { 
             Self::InvalidChainId | 
             Self::GasTooLow | 
             Self::GasTooHigh | 
             Self::GasRequiredExceedsAllowance { .. } | 
             ...
        } 
     }

the generic json-rpc codes stay at RpcInvalidTransactionError and the conversion for (if needed) is handled by as_simulate_error(). the ones that don't match are specific

Self::GasLimitReached => -38026,
}
}
Expand Down
Loading