revm_context_interface/
result.rs

1//! Result of the EVM execution. Containing both execution result, state and errors.
2//!
3//! [`ExecutionResult`] is the result of the EVM execution.
4//!
5//! [`InvalidTransaction`] is the error that is returned when the transaction is invalid.
6//!
7//! [`InvalidHeader`] is the error that is returned when the header is invalid.
8//!
9//! [`SuccessReason`] is the reason that the transaction successfully completed.
10use crate::{context::ContextError, transaction::TransactionError};
11use core::fmt::{self, Debug};
12use database_interface::DBErrorMarker;
13use primitives::{Address, Bytes, Log, U256};
14use state::EvmState;
15use std::{boxed::Box, string::String, vec::Vec};
16
17/// Trait for the halt reason.
18pub trait HaltReasonTr: Clone + Debug + PartialEq + Eq + From<HaltReason> {}
19
20impl<T> HaltReasonTr for T where T: Clone + Debug + PartialEq + Eq + From<HaltReason> {}
21
22/// Tuple containing evm execution result and state.s
23#[derive(Clone, Debug, PartialEq, Eq, Hash)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25pub struct ExecResultAndState<R, S = EvmState> {
26    /// Execution result
27    pub result: R,
28    /// Output State.
29    pub state: S,
30}
31
32/// Type alias for backwards compatibility.
33pub type ResultAndState<H = HaltReason> = ExecResultAndState<ExecutionResult<H>>;
34
35/// Tuple containing multiple execution results and state.
36pub type ResultVecAndState<R, S> = ExecResultAndState<Vec<R>, S>;
37
38impl<R, S> ExecResultAndState<R, S> {
39    /// Creates new ResultAndState.
40    pub fn new(result: R, state: S) -> Self {
41        Self { result, state }
42    }
43}
44
45/// Result of a transaction execution
46#[derive(Clone, Debug, PartialEq, Eq, Hash)]
47#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
48pub enum ExecutionResult<HaltReasonTy = HaltReason> {
49    /// Returned successfully
50    Success {
51        /// Reason for the success.
52        reason: SuccessReason,
53        /// Gas used by the transaction.s
54        gas_used: u64,
55        /// Gas refunded by the transaction.
56        gas_refunded: u64,
57        /// Logs emitted by the transaction.
58        logs: Vec<Log>,
59        /// Output of the transaction.
60        output: Output,
61    },
62    /// Reverted by `REVERT` opcode that doesn't spend all gas
63    Revert {
64        /// Gas used by the transaction.
65        gas_used: u64,
66        /// Output of the transaction.
67        output: Bytes,
68    },
69    /// Reverted for various reasons and spend all gas
70    Halt {
71        /// Reason for the halt.
72        reason: HaltReasonTy,
73        /// Gas used by the transaction.
74        ///
75        /// Halting will spend all the gas, and will be equal to gas_limit.
76        gas_used: u64,
77    },
78}
79
80impl<HaltReasonTy> ExecutionResult<HaltReasonTy> {
81    /// Returns if transaction execution is successful.
82    ///
83    /// 1 indicates success, 0 indicates revert.
84    ///
85    /// <https://eips.ethereum.org/EIPS/eip-658>
86    pub fn is_success(&self) -> bool {
87        matches!(self, Self::Success { .. })
88    }
89
90    /// Maps a `DBError` to a new error type using the provided closure, leaving other variants unchanged.
91    pub fn map_haltreason<F, OHR>(self, op: F) -> ExecutionResult<OHR>
92    where
93        F: FnOnce(HaltReasonTy) -> OHR,
94    {
95        match self {
96            Self::Success {
97                reason,
98                gas_used,
99                gas_refunded,
100                logs,
101                output,
102            } => ExecutionResult::Success {
103                reason,
104                gas_used,
105                gas_refunded,
106                logs,
107                output,
108            },
109            Self::Revert { gas_used, output } => ExecutionResult::Revert { gas_used, output },
110            Self::Halt { reason, gas_used } => ExecutionResult::Halt {
111                reason: op(reason),
112                gas_used,
113            },
114        }
115    }
116
117    /// Returns created address if execution is Create transaction
118    /// and Contract was created.
119    pub fn created_address(&self) -> Option<Address> {
120        match self {
121            Self::Success { output, .. } => output.address().cloned(),
122            _ => None,
123        }
124    }
125
126    /// Returns true if execution result is a Halt.
127    pub fn is_halt(&self) -> bool {
128        matches!(self, Self::Halt { .. })
129    }
130
131    /// Returns the output data of the execution.
132    ///
133    /// Returns [`None`] if the execution was halted.
134    pub fn output(&self) -> Option<&Bytes> {
135        match self {
136            Self::Success { output, .. } => Some(output.data()),
137            Self::Revert { output, .. } => Some(output),
138            _ => None,
139        }
140    }
141
142    /// Consumes the type and returns the output data of the execution.
143    ///
144    /// Returns [`None`] if the execution was halted.
145    pub fn into_output(self) -> Option<Bytes> {
146        match self {
147            Self::Success { output, .. } => Some(output.into_data()),
148            Self::Revert { output, .. } => Some(output),
149            _ => None,
150        }
151    }
152
153    /// Returns the logs if execution is successful, or an empty list otherwise.
154    pub fn logs(&self) -> &[Log] {
155        match self {
156            Self::Success { logs, .. } => logs.as_slice(),
157            _ => &[],
158        }
159    }
160
161    /// Consumes [`self`] and returns the logs if execution is successful, or an empty list otherwise.
162    pub fn into_logs(self) -> Vec<Log> {
163        match self {
164            Self::Success { logs, .. } => logs,
165            _ => Vec::new(),
166        }
167    }
168
169    /// Returns the gas used.
170    pub fn gas_used(&self) -> u64 {
171        match *self {
172            Self::Success { gas_used, .. }
173            | Self::Revert { gas_used, .. }
174            | Self::Halt { gas_used, .. } => gas_used,
175        }
176    }
177}
178
179/// Output of a transaction execution
180#[derive(Debug, Clone, PartialEq, Eq, Hash)]
181#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
182pub enum Output {
183    /// Output of a call.
184    Call(Bytes),
185    /// Output of a create.
186    Create(Bytes, Option<Address>),
187}
188
189impl Output {
190    /// Returns the output data of the execution output.
191    pub fn into_data(self) -> Bytes {
192        match self {
193            Output::Call(data) => data,
194            Output::Create(data, _) => data,
195        }
196    }
197
198    /// Returns the output data of the execution output.
199    pub fn data(&self) -> &Bytes {
200        match self {
201            Output::Call(data) => data,
202            Output::Create(data, _) => data,
203        }
204    }
205
206    /// Returns the created address, if any.
207    pub fn address(&self) -> Option<&Address> {
208        match self {
209            Output::Call(_) => None,
210            Output::Create(_, address) => address.as_ref(),
211        }
212    }
213}
214
215/// Main EVM error
216#[derive(Debug, Clone, PartialEq, Eq)]
217#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
218pub enum EVMError<DBError, TransactionError = InvalidTransaction> {
219    /// Transaction validation error
220    Transaction(TransactionError),
221    /// Header validation error
222    Header(InvalidHeader),
223    /// Database error
224    Database(DBError),
225    /// Custom error
226    ///
227    /// Useful for handler registers where custom logic would want to return their own custom error.
228    Custom(String),
229}
230
231impl<DBError, TransactionValidationErrorT> From<ContextError<DBError>>
232    for EVMError<DBError, TransactionValidationErrorT>
233{
234    fn from(value: ContextError<DBError>) -> Self {
235        match value {
236            ContextError::Db(e) => Self::Database(e),
237            ContextError::Custom(e) => Self::Custom(e),
238        }
239    }
240}
241
242impl<DBError: DBErrorMarker, TX> From<DBError> for EVMError<DBError, TX> {
243    fn from(value: DBError) -> Self {
244        Self::Database(value)
245    }
246}
247
248/// Trait for converting a string to an [`EVMError::Custom`] error.
249pub trait FromStringError {
250    /// Converts a string to an [`EVMError::Custom`] error.
251    fn from_string(value: String) -> Self;
252}
253
254impl<DB, TX> FromStringError for EVMError<DB, TX> {
255    fn from_string(value: String) -> Self {
256        Self::Custom(value)
257    }
258}
259
260impl<DB, TXE: From<InvalidTransaction>> From<InvalidTransaction> for EVMError<DB, TXE> {
261    fn from(value: InvalidTransaction) -> Self {
262        Self::Transaction(TXE::from(value))
263    }
264}
265
266impl<DBError, TransactionValidationErrorT> EVMError<DBError, TransactionValidationErrorT> {
267    /// Maps a `DBError` to a new error type using the provided closure, leaving other variants unchanged.
268    pub fn map_db_err<F, E>(self, op: F) -> EVMError<E, TransactionValidationErrorT>
269    where
270        F: FnOnce(DBError) -> E,
271    {
272        match self {
273            Self::Transaction(e) => EVMError::Transaction(e),
274            Self::Header(e) => EVMError::Header(e),
275            Self::Database(e) => EVMError::Database(op(e)),
276            Self::Custom(e) => EVMError::Custom(e),
277        }
278    }
279}
280
281impl<DBError, TransactionValidationErrorT> core::error::Error
282    for EVMError<DBError, TransactionValidationErrorT>
283where
284    DBError: core::error::Error + 'static,
285    TransactionValidationErrorT: core::error::Error + 'static,
286{
287    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
288        match self {
289            Self::Transaction(e) => Some(e),
290            Self::Header(e) => Some(e),
291            Self::Database(e) => Some(e),
292            Self::Custom(_) => None,
293        }
294    }
295}
296
297impl<DBError, TransactionValidationErrorT> fmt::Display
298    for EVMError<DBError, TransactionValidationErrorT>
299where
300    DBError: fmt::Display,
301    TransactionValidationErrorT: fmt::Display,
302{
303    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
304        match self {
305            Self::Transaction(e) => write!(f, "transaction validation error: {e}"),
306            Self::Header(e) => write!(f, "header validation error: {e}"),
307            Self::Database(e) => write!(f, "database error: {e}"),
308            Self::Custom(e) => f.write_str(e),
309        }
310    }
311}
312
313impl<DBError, TransactionValidationErrorT> From<InvalidHeader>
314    for EVMError<DBError, TransactionValidationErrorT>
315{
316    fn from(value: InvalidHeader) -> Self {
317        Self::Header(value)
318    }
319}
320
321/// Transaction validation error.
322#[derive(Debug, Clone, PartialEq, Eq, Hash)]
323#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
324pub enum InvalidTransaction {
325    /// When using the EIP-1559 fee model introduced in the London upgrade, transactions specify two primary fee fields:
326    /// - `gas_max_fee`: The maximum total fee a user is willing to pay, inclusive of both base fee and priority fee.
327    /// - `gas_priority_fee`: The extra amount a user is willing to give directly to the miner, often referred to as the "tip".
328    ///
329    /// Provided `gas_priority_fee` exceeds the total `gas_max_fee`.
330    PriorityFeeGreaterThanMaxFee,
331    /// EIP-1559: `gas_price` is less than `basefee`.
332    GasPriceLessThanBasefee,
333    /// `gas_limit` in the tx is bigger than `block_gas_limit`.
334    CallerGasLimitMoreThanBlock,
335    /// Initial gas for a Call is bigger than `gas_limit`.
336    ///
337    /// Initial gas for a Call contains:
338    /// - initial stipend gas
339    /// - gas for access list and input data
340    CallGasCostMoreThanGasLimit {
341        /// Initial gas for a Call.
342        initial_gas: u64,
343        /// Gas limit for the transaction.
344        gas_limit: u64,
345    },
346    /// Gas floor calculated from EIP-7623 Increase calldata cost
347    /// is more than the gas limit.
348    ///
349    /// Tx data is too large to be executed.
350    GasFloorMoreThanGasLimit {
351        /// Gas floor calculated from EIP-7623 Increase calldata cost.
352        gas_floor: u64,
353        /// Gas limit for the transaction.
354        gas_limit: u64,
355    },
356    /// EIP-3607 Reject transactions from senders with deployed code
357    RejectCallerWithCode,
358    /// Transaction account does not have enough amount of ether to cover transferred value and gas_limit*gas_price.
359    LackOfFundForMaxFee {
360        /// Fee for the transaction.
361        fee: Box<U256>,
362        /// Balance of the sender.
363        balance: Box<U256>,
364    },
365    /// Overflow payment in transaction.
366    OverflowPaymentInTransaction,
367    /// Nonce overflows in transaction.
368    NonceOverflowInTransaction,
369    /// Nonce is too high.
370    NonceTooHigh {
371        /// Nonce of the transaction.
372        tx: u64,
373        /// Nonce of the state.
374        state: u64,
375    },
376    /// Nonce is too low.
377    NonceTooLow {
378        /// Nonce of the transaction.
379        tx: u64,
380        /// Nonce of the state.
381        state: u64,
382    },
383    /// EIP-3860: Limit and meter initcode
384    CreateInitCodeSizeLimit,
385    /// Transaction chain id does not match the config chain id.
386    InvalidChainId,
387    /// Missing chain id.
388    MissingChainId,
389    /// Transaction gas limit is greater than the cap.
390    TxGasLimitGreaterThanCap {
391        /// Transaction gas limit.
392        gas_limit: u64,
393        /// Gas limit cap.
394        cap: u64,
395    },
396    /// Access list is not supported for blocks before the Berlin hardfork.
397    AccessListNotSupported,
398    /// `max_fee_per_blob_gas` is not supported for blocks before the Cancun hardfork.
399    MaxFeePerBlobGasNotSupported,
400    /// `blob_hashes`/`blob_versioned_hashes` is not supported for blocks before the Cancun hardfork.
401    BlobVersionedHashesNotSupported,
402    /// Block `blob_gas_price` is greater than tx-specified `max_fee_per_blob_gas` after Cancun.
403    BlobGasPriceGreaterThanMax,
404    /// There should be at least one blob in Blob transaction.
405    EmptyBlobs,
406    /// Blob transaction can't be a create transaction.
407    ///
408    /// `to` must be present
409    BlobCreateTransaction,
410    /// Transaction has more then `max` blobs
411    TooManyBlobs {
412        /// Maximum number of blobs allowed.
413        max: usize,
414        /// Number of blobs in the transaction.
415        have: usize,
416    },
417    /// Blob transaction contains a versioned hash with an incorrect version
418    BlobVersionNotSupported,
419    /// EIP-7702 is not enabled.
420    AuthorizationListNotSupported,
421    /// EIP-7702 transaction has invalid fields set.
422    AuthorizationListInvalidFields,
423    /// Empty Authorization List is not allowed.
424    EmptyAuthorizationList,
425    /// EIP-2930 is not supported.
426    Eip2930NotSupported,
427    /// EIP-1559 is not supported.
428    Eip1559NotSupported,
429    /// EIP-4844 is not supported.
430    Eip4844NotSupported,
431    /// EIP-7702 is not supported.
432    Eip7702NotSupported,
433    /// EIP-7873 is not supported.
434    Eip7873NotSupported,
435    /// EIP-7873 initcode transaction should have `to` address.
436    Eip7873MissingTarget,
437}
438
439impl TransactionError for InvalidTransaction {}
440
441impl core::error::Error for InvalidTransaction {}
442
443impl fmt::Display for InvalidTransaction {
444    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
445        match self {
446            Self::PriorityFeeGreaterThanMaxFee => {
447                write!(f, "priority fee is greater than max fee")
448            }
449            Self::GasPriceLessThanBasefee => {
450                write!(f, "gas price is less than basefee")
451            }
452            Self::CallerGasLimitMoreThanBlock => {
453                write!(f, "caller gas limit exceeds the block gas limit")
454            }
455            Self::TxGasLimitGreaterThanCap { gas_limit, cap } => {
456                write!(
457                    f,
458                    "transaction gas limit ({gas_limit}) is greater than the cap ({cap})"
459                )
460            }
461            Self::CallGasCostMoreThanGasLimit {
462                initial_gas,
463                gas_limit,
464            } => {
465                write!(
466                    f,
467                    "call gas cost ({initial_gas}) exceeds the gas limit ({gas_limit})"
468                )
469            }
470            Self::GasFloorMoreThanGasLimit {
471                gas_floor,
472                gas_limit,
473            } => {
474                write!(
475                    f,
476                    "gas floor ({gas_floor}) exceeds the gas limit ({gas_limit})"
477                )
478            }
479            Self::RejectCallerWithCode => {
480                write!(f, "reject transactions from senders with deployed code")
481            }
482            Self::LackOfFundForMaxFee { fee, balance } => {
483                write!(f, "lack of funds ({balance}) for max fee ({fee})")
484            }
485            Self::OverflowPaymentInTransaction => {
486                write!(f, "overflow payment in transaction")
487            }
488            Self::NonceOverflowInTransaction => {
489                write!(f, "nonce overflow in transaction")
490            }
491            Self::NonceTooHigh { tx, state } => {
492                write!(f, "nonce {tx} too high, expected {state}")
493            }
494            Self::NonceTooLow { tx, state } => {
495                write!(f, "nonce {tx} too low, expected {state}")
496            }
497            Self::CreateInitCodeSizeLimit => {
498                write!(f, "create initcode size limit")
499            }
500            Self::InvalidChainId => write!(f, "invalid chain ID"),
501            Self::MissingChainId => write!(f, "missing chain ID"),
502            Self::AccessListNotSupported => write!(f, "access list not supported"),
503            Self::MaxFeePerBlobGasNotSupported => {
504                write!(f, "max fee per blob gas not supported")
505            }
506            Self::BlobVersionedHashesNotSupported => {
507                write!(f, "blob versioned hashes not supported")
508            }
509            Self::BlobGasPriceGreaterThanMax => {
510                write!(f, "blob gas price is greater than max fee per blob gas")
511            }
512            Self::EmptyBlobs => write!(f, "empty blobs"),
513            Self::BlobCreateTransaction => write!(f, "blob create transaction"),
514            Self::TooManyBlobs { max, have } => {
515                write!(f, "too many blobs, have {have}, max {max}")
516            }
517            Self::BlobVersionNotSupported => write!(f, "blob version not supported"),
518            Self::AuthorizationListNotSupported => write!(f, "authorization list not supported"),
519            Self::AuthorizationListInvalidFields => {
520                write!(f, "authorization list tx has invalid fields")
521            }
522            Self::EmptyAuthorizationList => write!(f, "empty authorization list"),
523            Self::Eip2930NotSupported => write!(f, "Eip2930 is not supported"),
524            Self::Eip1559NotSupported => write!(f, "Eip1559 is not supported"),
525            Self::Eip4844NotSupported => write!(f, "Eip4844 is not supported"),
526            Self::Eip7702NotSupported => write!(f, "Eip7702 is not supported"),
527            Self::Eip7873NotSupported => write!(f, "Eip7873 is not supported"),
528            Self::Eip7873MissingTarget => {
529                write!(f, "Eip7873 initcode transaction should have `to` address")
530            }
531        }
532    }
533}
534
535/// Errors related to misconfiguration of a [`crate::Block`].
536#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
537#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
538pub enum InvalidHeader {
539    /// `prevrandao` is not set for Merge and above.
540    PrevrandaoNotSet,
541    /// `excess_blob_gas` is not set for Cancun and above.
542    ExcessBlobGasNotSet,
543}
544
545impl core::error::Error for InvalidHeader {}
546
547impl fmt::Display for InvalidHeader {
548    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
549        match self {
550            Self::PrevrandaoNotSet => write!(f, "`prevrandao` not set"),
551            Self::ExcessBlobGasNotSet => write!(f, "`excess_blob_gas` not set"),
552        }
553    }
554}
555
556/// Reason a transaction successfully completed.
557#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
558#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
559pub enum SuccessReason {
560    /// Stop [`state::bytecode::opcode::STOP`] opcode.
561    Stop,
562    /// Return [`state::bytecode::opcode::RETURN`] opcode.
563    Return,
564    /// Self destruct opcode.
565    SelfDestruct,
566}
567
568/// Indicates that the EVM has experienced an exceptional halt.
569///
570/// This causes execution to immediately end with all gas being consumed.
571#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
572#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
573pub enum HaltReason {
574    /// Out of gas error.
575    OutOfGas(OutOfGasError),
576    /// Opcode not found error.
577    OpcodeNotFound,
578    /// Invalid FE opcode error.
579    InvalidFEOpcode,
580    /// Invalid jump destination.
581    InvalidJump,
582    /// The feature or opcode is not activated in hardfork.
583    NotActivated,
584    /// Attempting to pop a value from an empty stack.
585    StackUnderflow,
586    /// Attempting to push a value onto a full stack.
587    StackOverflow,
588    /// Invalid memory or storage offset for [`state::bytecode::opcode::RETURNDATACOPY`].
589    OutOfOffset,
590    /// Address collision during contract creation.
591    CreateCollision,
592    /// Precompile error.
593    PrecompileError,
594    /// Nonce overflow.
595    NonceOverflow,
596    /// Create init code size exceeds limit (runtime).
597    CreateContractSizeLimit,
598    /// Error on created contract that begins with EF
599    CreateContractStartingWithEF,
600    /// EIP-3860: Limit and meter initcode. Initcode size limit exceeded.
601    CreateInitCodeSizeLimit,
602
603    /* Internal Halts that can be only found inside Inspector */
604    /// Overflow payment. Not possible to happen on mainnet.
605    OverflowPayment,
606    /// State change during static call.
607    StateChangeDuringStaticCall,
608    /// Call not allowed inside static call.
609    CallNotAllowedInsideStatic,
610    /// Out of funds to pay for the call.
611    OutOfFunds,
612    /// Call is too deep.
613    CallTooDeep,
614}
615
616/// Out of gas errors.
617#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
618#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
619pub enum OutOfGasError {
620    /// Basic OOG error. Not enough gas to execute the opcode.
621    Basic,
622    /// Tried to expand past memory limit.
623    MemoryLimit,
624    /// Basic OOG error from memory expansion
625    Memory,
626    /// Precompile threw OOG error
627    Precompile,
628    /// When performing something that takes a U256 and casts down to a u64, if its too large this would fire
629    /// i.e. in `as_usize_or_fail`
630    InvalidOperand,
631    /// When performing SSTORE the gasleft is less than or equal to 2300
632    ReentrancySentry,
633}