revm_context_interface/
transaction.rs

1mod alloy_types;
2pub mod eip2930;
3pub mod eip7702;
4pub mod transaction_type;
5
6pub use eip2930::AccessListTr;
7pub use eip7702::AuthorizationTr;
8use specification::eip4844::GAS_PER_BLOB;
9pub use transaction_type::TransactionType;
10
11use auto_impl::auto_impl;
12use core::cmp::min;
13use core::fmt::Debug;
14use primitives::{Address, Bytes, TxKind, B256, U256};
15
16/// Transaction validity error types.
17pub trait TransactionError: Debug + core::error::Error {}
18
19/// Main Transaction trait that abstracts and specifies all transaction currently supported by Ethereum
20///
21/// Access to any associated type is gaited behind [`tx_type`][Transaction::tx_type] function.
22///
23/// It can be extended to support new transaction types and only transaction types can be
24/// deprecated by not returning tx_type.
25#[auto_impl(&, Box, Arc, Rc)]
26pub trait Transaction {
27    type AccessList: AccessListTr;
28    type Authorization: AuthorizationTr;
29
30    /// Returns the transaction type.
31    ///
32    /// Depending on this field other functions should be called.
33    fn tx_type(&self) -> u8;
34
35    /// Caller aka Author aka transaction signer.
36    ///
37    /// Note : Common field for all transactions.
38    fn caller(&self) -> Address;
39
40    /// The maximum amount of gas the transaction can use.
41    ///
42    /// Note : Common field for all transactions.
43    fn gas_limit(&self) -> u64;
44
45    /// The value sent to the receiver of [`TxKind::Call`][primitives::TxKind::Call].
46    ///
47    /// Note : Common field for all transactions.
48    fn value(&self) -> U256;
49
50    /// Returns the input data of the transaction.
51    ///
52    /// Note : Common field for all transactions.
53    fn input(&self) -> &Bytes;
54
55    /// The nonce of the transaction.
56    ///
57    /// Note : Common field for all transactions.
58    fn nonce(&self) -> u64;
59
60    /// Transaction kind. It can be Call or Create.
61    ///
62    /// Kind is applicable for: Legacy, EIP-2930, EIP-1559
63    /// And is Call for EIP-4844 and EIP-7702 transactions.
64    fn kind(&self) -> TxKind;
65
66    /// Chain Id is optional for legacy transactions.
67    ///
68    /// As it was introduced in EIP-155.
69    fn chain_id(&self) -> Option<u64>;
70
71    /// Gas price for the transaction.
72    /// It is only applicable for Legacy and EIP-2930 transactions.
73    /// For Eip1559 it is max_fee_per_gas.
74    fn gas_price(&self) -> u128;
75
76    /// Access list for the transaction.
77    ///
78    /// Introduced in EIP-2930.
79    fn access_list(&self) -> Option<&Self::AccessList>;
80
81    /// Returns vector of fixed size hash(32 bytes)
82    ///
83    /// Note : EIP-4844 transaction field.
84    fn blob_versioned_hashes(&self) -> &[B256];
85
86    /// Max fee per data gas
87    ///
88    /// Note : EIP-4844 transaction field.
89    fn max_fee_per_blob_gas(&self) -> u128;
90
91    /// Total gas for all blobs. Max number of blocks is already checked
92    /// so we dont need to check for overflow.
93    fn total_blob_gas(&self) -> u64 {
94        GAS_PER_BLOB * self.blob_versioned_hashes().len() as u64
95    }
96
97    /// Calculates the maximum [EIP-4844] `data_fee` of the transaction.
98    ///
99    /// This is used for ensuring that the user has at least enough funds to pay the
100    /// `max_fee_per_blob_gas * total_blob_gas`, on top of regular gas costs.
101    ///
102    /// See EIP-4844:
103    /// <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-4844.md#execution-layer-validation>
104    fn calc_max_data_fee(&self) -> U256 {
105        let blob_gas = U256::from(self.total_blob_gas());
106        let max_blob_fee = U256::from(self.max_fee_per_blob_gas());
107        max_blob_fee.saturating_mul(blob_gas)
108    }
109
110    /// Returns length of the authorization list.
111    ///
112    /// # Note
113    ///
114    /// Transaction is considered invalid if list is empty.
115    fn authorization_list_len(&self) -> usize;
116
117    /// List of authorizations, that contains the signature that authorizes this
118    /// caller to place the code to signer account.
119    ///
120    /// Set EOA account code for one transaction
121    ///
122    /// [EIP-Set EOA account code for one transaction](https://eips.ethereum.org/EIPS/eip-7702)
123    fn authorization_list(&self) -> impl Iterator<Item = &Self::Authorization>;
124
125    /// Returns maximum fee that can be paid for the transaction.
126    fn max_fee_per_gas(&self) -> u128 {
127        self.gas_price()
128    }
129
130    /// Maximum priority fee per gas.
131    fn max_priority_fee_per_gas(&self) -> Option<u128>;
132
133    /// Returns effective gas price is gas price field for Legacy and Eip2930 transaction.
134    ///
135    /// While for transactions after Eip1559 it is minimum of max_fee and `base + max_priority_fee`.
136    fn effective_gas_price(&self, base_fee: u128) -> u128 {
137        let max_fee = self.gas_price();
138        let Some(max_priority_fee) = self.max_priority_fee_per_gas() else {
139            return max_fee;
140        };
141        min(max_fee, base_fee.saturating_add(max_priority_fee))
142    }
143}
144
145#[auto_impl(&, &mut, Box, Arc)]
146pub trait TransactionGetter {
147    type Transaction: Transaction;
148
149    fn tx(&self) -> &Self::Transaction;
150}