Skip to main content

revm_statetest_types/
transaction.rs

1use crate::{deserializer::deserialize_maybe_empty, TestAuthorization};
2use context::TransactionType;
3use context_interface::transaction::AccessList;
4use primitives::{Address, Bytes, B256, U256};
5use serde::{Deserialize, Serialize};
6
7/// Transaction parts.
8#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct TransactionParts {
11    /// Transaction type (0=Legacy, 1=EIP-2930, 2=EIP-1559, 3=EIP-4844, 4=EIP-7702)
12    #[serde(rename = "type")]
13    pub tx_type: Option<u8>,
14    /// Transaction data/input (multiple variants for different test cases)
15    pub data: Vec<Bytes>,
16    /// Gas limit values (multiple variants for different test cases)
17    pub gas_limit: Vec<U256>,
18    /// Gas price (for legacy and EIP-2930 transactions)
19    pub gas_price: Option<U256>,
20    /// Transaction nonce
21    pub nonce: U256,
22    /// Private key for signing the transaction
23    pub secret_key: B256,
24    /// if sender is not present we need to derive it from secret key.
25    #[serde(default)]
26    pub sender: Option<Address>,
27    /// Recipient address (None for contract creation)
28    #[serde(default, deserialize_with = "deserialize_maybe_empty")]
29    pub to: Option<Address>,
30    /// Ether value to transfer (multiple variants for different test cases)
31    pub value: Vec<U256>,
32    /// Maximum fee per gas (EIP-1559 transactions)
33    pub max_fee_per_gas: Option<U256>,
34    /// Maximum priority fee per gas (EIP-1559 transactions)
35    pub max_priority_fee_per_gas: Option<U256>,
36    /// Initcodes for contract creation (EIP-7873)
37    pub initcodes: Option<Vec<Bytes>>,
38    /// Access lists for different test cases (EIP-2930)
39    #[serde(default)]
40    pub access_lists: Vec<Option<AccessList>>,
41    /// Authorization list (EIP-7702)
42    pub authorization_list: Option<Vec<TestAuthorization>>,
43    /// Blob versioned hashes (EIP-4844)
44    #[serde(default)]
45    pub blob_versioned_hashes: Vec<B256>,
46    /// Maximum fee per blob gas (EIP-4844)
47    pub max_fee_per_blob_gas: Option<U256>,
48}
49
50impl TransactionParts {
51    /// Returns the transaction type.   
52    ///
53    /// As this information is derived from the fields it is not stored in the struct.
54    ///
55    /// Returns `None` if the transaction is invalid:
56    ///   * It has both blob gas and no destination.
57    ///   * It has authorization list and no destination.
58    pub fn tx_type(&self, access_list_index: usize) -> Option<TransactionType> {
59        if let Some(tx_type) = self.tx_type {
60            return Some(TransactionType::from(tx_type));
61        }
62
63        let mut tx_type = TransactionType::Legacy;
64
65        // If it has access list it is EIP-2930 tx
66        if let Some(access_list) = self.access_lists.get(access_list_index) {
67            if access_list.is_some() {
68                tx_type = TransactionType::Eip2930;
69            }
70        }
71
72        // If there is max_fee it is EIP-1559 tx
73        if self.max_fee_per_gas.is_some() {
74            tx_type = TransactionType::Eip1559;
75        }
76
77        // If it has max_fee_per_blob_gas it is EIP-4844 tx
78        if self.max_fee_per_blob_gas.is_some() {
79            // target need to be present for EIP-4844 tx
80            self.to?;
81            return Some(TransactionType::Eip4844);
82        }
83
84        // And if it has authorization list it is EIP-7702 tx
85        if self.authorization_list.is_some() {
86            // Target need to be present for EIP-7702 tx
87            self.to?;
88            return Some(TransactionType::Eip7702);
89        }
90
91        Some(tx_type)
92    }
93}
94
95/// Transaction part indices.
96#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
97#[serde(rename_all = "camelCase", deny_unknown_fields)]
98pub struct TxPartIndices {
99    /// Index into the data array
100    pub data: usize,
101    /// Index into the gas_limit array
102    pub gas: usize,
103    /// Index into the value array
104    pub value: usize,
105}
106
107#[cfg(test)]
108mod test {
109
110    use super::*;
111
112    #[test]
113    fn decode_tx_parts() {
114        let tx = r#"{
115            "nonce": "0x00",
116            "maxPriorityFeePerGas": "0x00",
117            "maxFeePerGas": "0x07",
118            "gasLimit": [
119                "0x0423ff"
120            ],
121            "to": "0x0000000000000000000000000000000000001000",
122            "value": [
123                "0x00"
124            ],
125            "data": [
126                "0x"
127            ],
128            "accessLists": [
129                [
130                    {
131                        "address": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71",
132                        "storageKeys": [
133                            "0x0000000000000000000000000000000000000000000000000000000000000000"
134                        ]
135                    }
136                ]
137            ],
138            "authorizationList": [
139                {
140                    "chainId": "0x00",
141                    "address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
142                    "nonce": "0x00",
143                    "v": "0x01",
144                    "r": "0x5a8cac98fd240d8ef83c22db4a061ffa0facb1801245283cc05fc809d8b92837",
145                    "s": "0x1c3162fe11d91bc24d4fa00fb19ca34531e0eacdf8142c804be44058d5b8244f",
146                    "signer": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71"
147                }
148            ],
149            "sender": "0x8a0a19589531694250d570040a0c4b74576919b8",
150            "secretKey": "0x9e7645d0cfd9c3a04eb7a9db59a4eb7d359f2e75c9164a9d6b9a7d54e1b6a36f"
151        }"#;
152
153        let _: TransactionParts = serde_json::from_str(tx).unwrap();
154    }
155}