revm_context_interface/
transaction.rs

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