op_revm/transaction/
error.rs

1//! Contains the `[OpTransactionError]` type.
2use core::fmt::Display;
3use revm::context_interface::{
4    result::{EVMError, InvalidTransaction},
5    transaction::TransactionError,
6};
7
8/// Optimism transaction validation error.
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub enum OpTransactionError {
12    /// Base transaction error.
13    Base(InvalidTransaction),
14    /// System transactions are not supported post-regolith hardfork.
15    ///
16    /// Before the Regolith hardfork, there was a special field in the `Deposit` transaction
17    /// type that differentiated between `system` and `user` deposit transactions. This field
18    /// was deprecated in the Regolith hardfork, and this error is thrown if a `Deposit` transaction
19    /// is found with this field set to `true` after the hardfork activation.
20    ///
21    /// In addition, this error is internal, and bubbles up into a [OpHaltReason::FailedDeposit][crate::OpHaltReason::FailedDeposit] error
22    /// in the `revm` handler for the consumer to easily handle. This is due to a state transition
23    /// rule on OP Stack chains where, if for any reason a deposit transaction fails, the transaction
24    /// must still be included in the block, the sender nonce is bumped, the `mint` value persists, and
25    /// special gas accounting rules are applied. Normally on L1, [EVMError::Transaction] errors
26    /// are cause for non-inclusion, so a special [OpHaltReason][crate::OpHaltReason] variant was introduced to handle this
27    /// case for failed deposit transactions.
28    DepositSystemTxPostRegolith,
29    /// Deposit transaction haults bubble up to the global main return handler, wiping state and
30    /// only increasing the nonce + persisting the mint value.
31    ///
32    /// This is a catch-all error for any deposit transaction that is results in a [OpHaltReason][crate::OpHaltReason] error
33    /// post-regolith hardfork. This allows for a consumer to easily handle special cases where
34    /// a deposit transaction fails during validation, but must still be included in the block.
35    ///
36    /// In addition, this error is internal, and bubbles up into a [OpHaltReason::FailedDeposit][crate::OpHaltReason::FailedDeposit] error
37    /// in the `revm` handler for the consumer to easily handle. This is due to a state transition
38    /// rule on OP Stack chains where, if for any reason a deposit transaction fails, the transaction
39    /// must still be included in the block, the sender nonce is bumped, the `mint` value persists, and
40    /// special gas accounting rules are applied. Normally on L1, [EVMError::Transaction] errors
41    /// are cause for non-inclusion, so a special [OpHaltReason][crate::OpHaltReason] variant was introduced to handle this
42    /// case for failed deposit transactions.
43    HaltedDepositPostRegolith,
44}
45
46impl TransactionError for OpTransactionError {}
47
48impl Display for OpTransactionError {
49    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
50        match self {
51            Self::Base(error) => error.fmt(f),
52            Self::DepositSystemTxPostRegolith => {
53                write!(
54                    f,
55                    "deposit system transactions post regolith hardfork are not supported"
56                )
57            }
58            Self::HaltedDepositPostRegolith => {
59                write!(
60                    f,
61                    "deposit transaction halted post-regolith; error will be bubbled up to main return handler"
62                )
63            }
64        }
65    }
66}
67
68impl core::error::Error for OpTransactionError {}
69
70impl From<InvalidTransaction> for OpTransactionError {
71    fn from(value: InvalidTransaction) -> Self {
72        Self::Base(value)
73    }
74}
75
76impl<DBError> From<OpTransactionError> for EVMError<DBError, OpTransactionError> {
77    fn from(value: OpTransactionError) -> Self {
78        Self::Transaction(value)
79    }
80}
81
82#[cfg(test)]
83mod test {
84    use super::*;
85    use std::string::ToString;
86
87    #[test]
88    fn test_display_op_errors() {
89        assert_eq!(
90            OpTransactionError::DepositSystemTxPostRegolith.to_string(),
91            "deposit system transactions post regolith hardfork are not supported"
92        );
93        assert_eq!(
94            OpTransactionError::HaltedDepositPostRegolith.to_string(),
95            "deposit transaction halted post-regolith; error will be bubbled up to main return handler"
96        )
97    }
98
99    #[cfg(feature = "serde")]
100    #[test]
101    fn test_serialize_json_op_transaction_error() {
102        let response = r#""DepositSystemTxPostRegolith""#;
103
104        let op_transaction_error: OpTransactionError = serde_json::from_str(response).unwrap();
105        assert_eq!(
106            op_transaction_error,
107            OpTransactionError::DepositSystemTxPostRegolith
108        );
109    }
110}