Skip to main content

foundry_evm_core/backend/
error.rs

1use alloy_primitives::Address;
2use evm2::registry::HandlerError;
3
4pub use foundry_fork_db::{DatabaseError, DatabaseResult};
5
6pub type BackendResult<T> = Result<T, BackendError>;
7
8/// Errors of [`Backend`](super::Backend) operations.
9#[derive(Debug, thiserror::Error)]
10#[expect(missing_docs)]
11pub enum BackendError {
12    #[error("cheatcodes are not enabled for {0}; see `vm.allowCheatcodes(address)`")]
13    NoCheats(Address),
14    #[error(transparent)]
15    Database(#[from] DatabaseError),
16    #[error(transparent)]
17    State(#[from] evm2::DatabaseError),
18}
19
20/// Returns a report for a transaction evm2 rejected or failed to execute, wrapped in an
21/// "EVM error" context.
22pub fn handler_error(err: HandlerError) -> eyre::Report {
23    transaction_error(err).wrap_err("EVM error")
24}
25
26/// Returns a report for a transaction evm2 rejected or failed to execute.
27///
28/// Transaction validation errors use Foundry's own wording instead of evm2's.
29pub fn transaction_error(err: HandlerError) -> eyre::Report {
30    let reason = match &err {
31        HandlerError::InvalidNonce { expected, got } if got > expected => {
32            format!("nonce {got} too high, expected {expected}")
33        }
34        HandlerError::InvalidNonce { expected, got } => {
35            format!("nonce {got} too low, expected {expected}")
36        }
37        HandlerError::InvalidChainId { .. } => "invalid chain ID".to_string(),
38        HandlerError::MissingChainId => "missing chain ID".to_string(),
39        HandlerError::IntrinsicGasTooLow { required, got } => {
40            format!("call gas cost ({required}) exceeds the gas limit ({got})")
41        }
42        HandlerError::RejectCallerWithCode => {
43            "reject transactions from senders with deployed code".to_string()
44        }
45        HandlerError::GasLimitMoreThanBlock { .. } => {
46            "caller gas limit exceeds the block gas limit".to_string()
47        }
48        HandlerError::TxGasLimitGreaterThanCap { gas_limit, cap } => {
49            format!("transaction gas limit ({gas_limit}) is greater than the cap ({cap})")
50        }
51        HandlerError::CreateInitCodeSizeLimit { .. } => "create initcode size limit".to_string(),
52        HandlerError::FeeCapLessThanBaseFee { .. } => "gas price is less than basefee".to_string(),
53        HandlerError::EmptyAuthorizationList => "empty authorization list".to_string(),
54        HandlerError::BlobFeeCapLessThanBlobBaseFee { max_fee_per_blob_gas, blob_base_fee } => {
55            format!(
56                "blob gas price ({blob_base_fee}) is greater than max fee per blob gas \
57                 ({max_fee_per_blob_gas})"
58            )
59        }
60        HandlerError::TooManyBlobs { have, max } => {
61            format!("too many blobs, have {have}, max {max}")
62        }
63        HandlerError::PriorityFeeGreaterThanMaxFee => {
64            "priority fee is greater than max fee".to_string()
65        }
66        HandlerError::InsufficientFunds { .. }
67        | HandlerError::NonceOverflow
68        | HandlerError::EmptyBlobs
69        | HandlerError::BlobVersionNotSupported => err.to_string(),
70        _ => return eyre::Report::new(err),
71    };
72    eyre::Report::new(TransactionValidationError { reason, error: err })
73}
74
75/// A transaction evm2 rejected, with Foundry's wording of the reason. The evm2 error stays
76/// available to callers that downcast the report.
77#[derive(Debug, thiserror::Error)]
78#[error("transaction validation error: {reason}")]
79pub struct TransactionValidationError {
80    /// Foundry's wording of the rejection.
81    pub reason: String,
82    /// The evm2 rejection.
83    pub error: HandlerError,
84}