revm_wiring/default/
tx.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use crate::{result::InvalidTransaction, Transaction};
use core::fmt::Debug;
use primitives::{Address, Bytes, TxKind, B256, U256};
use specification::eip2930::AccessList;
use specification::eip7702::AuthorizationList;
use std::vec::Vec;
use transaction::{
    eip7702::Authorization, CommonTxFields, Eip1559CommonTxFields, Eip1559Tx, Eip2930Tx, Eip4844Tx,
    Eip7702Tx, LegacyTx, TransactionType,
};

/// The transaction environment.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TxEnv {
    pub tx_type: TransactionType,
    /// Caller aka Author aka transaction signer.
    pub caller: Address,
    /// The gas limit of the transaction.
    pub gas_limit: u64,
    /// The gas price of the transaction.
    pub gas_price: U256,
    /// The destination of the transaction.
    pub transact_to: TxKind,
    /// The value sent to `transact_to`.
    pub value: U256,
    /// The data of the transaction.
    pub data: Bytes,

    /// The nonce of the transaction.
    pub nonce: u64,

    /// The chain ID of the transaction. If set to `None`, no checks are performed.
    ///
    /// Incorporated as part of the Spurious Dragon upgrade via [EIP-155].
    ///
    /// [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
    pub chain_id: Option<u64>,

    /// A list of addresses and storage keys that the transaction plans to access.
    ///
    /// Added in [EIP-2930].
    ///
    /// [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
    pub access_list: AccessList,

    /// The priority fee per gas.
    ///
    /// Incorporated as part of the London upgrade via [EIP-1559].
    ///
    /// [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559
    pub gas_priority_fee: Option<U256>,

    /// The list of blob versioned hashes. Per EIP there should be at least
    /// one blob present if [`Self::max_fee_per_blob_gas`] is `Some`.
    ///
    /// Incorporated as part of the Cancun upgrade via [EIP-4844].
    ///
    /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
    pub blob_hashes: Vec<B256>,

    /// The max fee per blob gas.
    ///
    /// Incorporated as part of the Cancun upgrade via [EIP-4844].
    ///
    /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
    pub max_fee_per_blob_gas: Option<U256>,

    /// List of authorizations, that contains the signature that authorizes this
    /// caller to place the code to signer account.
    ///
    /// Set EOA account code for one transaction
    ///
    /// [EIP-Set EOA account code for one transaction](https://eips.ethereum.org/EIPS/eip-7702)
    pub authorization_list: AuthorizationList,
}

impl Default for TxEnv {
    fn default() -> Self {
        Self {
            tx_type: TransactionType::Legacy,
            caller: Address::default(),
            gas_limit: u64::MAX,
            gas_price: U256::ZERO,
            transact_to: TxKind::Call(Address::default()),
            value: U256::ZERO,
            data: Bytes::default(),
            nonce: 0,
            chain_id: Some(1), // Mainnet chain ID is 1
            access_list: AccessList::default(),
            gas_priority_fee: Some(U256::ZERO),
            blob_hashes: Vec::new(),
            max_fee_per_blob_gas: Some(U256::ZERO),
            authorization_list: AuthorizationList::default(),
        }
    }
}

impl CommonTxFields for TxEnv {
    fn caller(&self) -> Address {
        self.caller
    }

    fn gas_limit(&self) -> u64 {
        self.gas_limit
    }

    fn value(&self) -> U256 {
        self.value
    }

    fn input(&self) -> &Bytes {
        &self.data
    }

    fn nonce(&self) -> u64 {
        self.nonce
    }
}

impl Eip1559CommonTxFields for TxEnv {
    type AccessList = AccessList;

    fn chain_id(&self) -> u64 {
        self.chain_id.unwrap_or_default()
    }

    fn max_fee_per_gas(&self) -> u128 {
        self.gas_price.to()
    }

    fn max_priority_fee_per_gas(&self) -> u128 {
        self.gas_priority_fee.unwrap_or_default().to()
    }

    fn access_list(&self) -> &Self::AccessList {
        &self.access_list
    }
}

impl LegacyTx for TxEnv {
    fn kind(&self) -> TxKind {
        self.transact_to
    }

    fn chain_id(&self) -> Option<u64> {
        self.chain_id
    }

    fn gas_price(&self) -> u128 {
        self.gas_price.try_into().unwrap_or(u128::MAX)
    }
}

impl Eip2930Tx for TxEnv {
    type AccessList = AccessList;

    fn access_list(&self) -> &Self::AccessList {
        &self.access_list
    }

    fn chain_id(&self) -> u64 {
        self.chain_id.unwrap_or_default()
    }

    fn gas_price(&self) -> u128 {
        self.gas_price.to()
    }

    fn kind(&self) -> TxKind {
        self.transact_to
    }
}

impl Eip1559Tx for TxEnv {
    fn kind(&self) -> TxKind {
        self.transact_to
    }
}

impl Eip4844Tx for TxEnv {
    fn destination(&self) -> Address {
        match self.transact_to {
            TxKind::Call(addr) => addr,
            TxKind::Create => panic!("Create transaction are not allowed in Eip4844"),
        }
    }

    fn blob_versioned_hashes(&self) -> &[B256] {
        &self.blob_hashes
    }

    fn max_fee_per_blob_gas(&self) -> u128 {
        self.max_fee_per_blob_gas.unwrap_or_default().to()
    }
}

impl Eip7702Tx for TxEnv {
    fn destination(&self) -> Address {
        match self.transact_to {
            TxKind::Call(addr) => addr,
            TxKind::Create => panic!("Create transaction are not allowed in Eip7702"),
        }
    }

    fn authorization_list_len(&self) -> usize {
        self.authorization_list.len()
    }

    fn authorization_list_iter(&self) -> impl Iterator<Item = impl Authorization> {
        self.authorization_list.recovered_iter()
    }
}

impl Transaction for TxEnv {
    type TransactionError = InvalidTransaction;
    type TransactionType = TransactionType;

    type AccessList = <Self::Eip2930 as Eip2930Tx>::AccessList;

    type Legacy = Self;

    type Eip1559 = Self;

    type Eip2930 = Self;

    type Eip4844 = Self;

    type Eip7702 = Self;

    fn tx_type(&self) -> Self::TransactionType {
        self.tx_type
    }

    fn legacy(&self) -> &Self::Legacy {
        self
    }

    fn eip2930(&self) -> &Self::Eip2930 {
        self
    }

    fn eip1559(&self) -> &Self::Eip1559 {
        self
    }

    fn eip4844(&self) -> &Self::Eip4844 {
        self
    }

    fn eip7702(&self) -> &Self::Eip7702 {
        self
    }
}