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 an [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 halts 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 results in an [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 an [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 /// Missing enveloped transaction bytes for non-deposit transaction.
45 ///
46 /// Non-deposit transactions on Optimism must have `enveloped_tx` field set
47 /// to properly calculate L1 costs.
48 MissingEnvelopedTx,
49}
50
51impl TransactionError for OpTransactionError {}
52
53impl Display for OpTransactionError {
54 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
55 match self {
56 Self::Base(error) => error.fmt(f),
57 Self::DepositSystemTxPostRegolith => {
58 write!(
59 f,
60 "deposit system transactions post regolith hardfork are not supported"
61 )
62 }
63 Self::HaltedDepositPostRegolith => {
64 write!(
65 f,
66 "deposit transaction halted post-regolith; error will be bubbled up to main return handler"
67 )
68 }
69 Self::MissingEnvelopedTx => {
70 write!(
71 f,
72 "missing enveloped transaction bytes for non-deposit transaction"
73 )
74 }
75 }
76 }
77}
78
79impl core::error::Error for OpTransactionError {}
80
81impl From<InvalidTransaction> for OpTransactionError {
82 fn from(value: InvalidTransaction) -> Self {
83 Self::Base(value)
84 }
85}
86
87impl<DBError> From<OpTransactionError> for EVMError<DBError, OpTransactionError> {
88 fn from(value: OpTransactionError) -> Self {
89 Self::Transaction(value)
90 }
91}
92
93#[cfg(test)]
94mod test {
95 use super::*;
96 use std::string::ToString;
97
98 #[test]
99 fn test_display_op_errors() {
100 assert_eq!(
101 OpTransactionError::Base(InvalidTransaction::NonceTooHigh { tx: 2, state: 1 })
102 .to_string(),
103 "nonce 2 too high, expected 1"
104 );
105 assert_eq!(
106 OpTransactionError::DepositSystemTxPostRegolith.to_string(),
107 "deposit system transactions post regolith hardfork are not supported"
108 );
109 assert_eq!(
110 OpTransactionError::HaltedDepositPostRegolith.to_string(),
111 "deposit transaction halted post-regolith; error will be bubbled up to main return handler"
112 );
113 assert_eq!(
114 OpTransactionError::MissingEnvelopedTx.to_string(),
115 "missing enveloped transaction bytes for non-deposit transaction"
116 );
117 }
118
119 #[cfg(feature = "serde")]
120 #[test]
121 fn test_serialize_json_op_transaction_error() {
122 let response = r#""DepositSystemTxPostRegolith""#;
123
124 let op_transaction_error: OpTransactionError = serde_json::from_str(response).unwrap();
125 assert_eq!(
126 op_transaction_error,
127 OpTransactionError::DepositSystemTxPostRegolith
128 );
129 }
130}