revm_statetest_types/
transaction.rs1use crate::{deserializer::deserialize_maybe_empty, TestAuthorization};
2use revm::{
3 context::TransactionType,
4 context_interface::transaction::AccessList,
5 primitives::{Address, Bytes, B256, U256},
6};
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct TransactionParts {
13 #[serde(rename = "type")]
15 pub tx_type: Option<u8>,
16 pub data: Vec<Bytes>,
18 pub gas_limit: Vec<U256>,
20 pub gas_price: Option<U256>,
22 pub nonce: U256,
24 pub secret_key: B256,
26 #[serde(default)]
28 pub sender: Option<Address>,
29 #[serde(default, deserialize_with = "deserialize_maybe_empty")]
31 pub to: Option<Address>,
32 pub value: Vec<U256>,
34 pub max_fee_per_gas: Option<U256>,
36 pub max_priority_fee_per_gas: Option<U256>,
38 pub initcodes: Option<Vec<Bytes>>,
40 #[serde(default)]
42 pub access_lists: Vec<Option<AccessList>>,
43 pub authorization_list: Option<Vec<TestAuthorization>>,
45 #[serde(default)]
47 pub blob_versioned_hashes: Vec<B256>,
48 pub max_fee_per_blob_gas: Option<U256>,
50}
51
52impl TransactionParts {
53 pub fn tx_type(&self, access_list_index: usize) -> Option<TransactionType> {
61 if let Some(tx_type) = self.tx_type {
62 return Some(TransactionType::from(tx_type));
63 }
64
65 let mut tx_type = TransactionType::Legacy;
66
67 if let Some(access_list) = self.access_lists.get(access_list_index) {
69 if access_list.is_some() {
70 tx_type = TransactionType::Eip2930;
71 }
72 }
73
74 if self.max_fee_per_gas.is_some() {
76 tx_type = TransactionType::Eip1559;
77 }
78
79 if self.max_fee_per_blob_gas.is_some() {
81 self.to?;
83 return Some(TransactionType::Eip4844);
84 }
85
86 if self.authorization_list.is_some() {
88 self.to?;
90 return Some(TransactionType::Eip7702);
91 }
92
93 Some(tx_type)
102 }
103}
104
105#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
107#[serde(rename_all = "camelCase", deny_unknown_fields)]
108pub struct TxPartIndices {
109 pub data: usize,
111 pub gas: usize,
113 pub value: usize,
115}
116
117#[cfg(test)]
118mod test {
119
120 use super::*;
121
122 #[test]
123 fn decode_tx_parts() {
124 let tx = r#"{
125 "nonce": "0x00",
126 "maxPriorityFeePerGas": "0x00",
127 "maxFeePerGas": "0x07",
128 "gasLimit": [
129 "0x0423ff"
130 ],
131 "to": "0x0000000000000000000000000000000000001000",
132 "value": [
133 "0x00"
134 ],
135 "data": [
136 "0x"
137 ],
138 "accessLists": [
139 [
140 {
141 "address": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71",
142 "storageKeys": [
143 "0x0000000000000000000000000000000000000000000000000000000000000000"
144 ]
145 }
146 ]
147 ],
148 "authorizationList": [
149 {
150 "chainId": "0x00",
151 "address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
152 "nonce": "0x00",
153 "v": "0x01",
154 "r": "0x5a8cac98fd240d8ef83c22db4a061ffa0facb1801245283cc05fc809d8b92837",
155 "s": "0x1c3162fe11d91bc24d4fa00fb19ca34531e0eacdf8142c804be44058d5b8244f",
156 "signer": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71"
157 }
158 ],
159 "sender": "0x8a0a19589531694250d570040a0c4b74576919b8",
160 "secretKey": "0x9e7645d0cfd9c3a04eb7a9db59a4eb7d359f2e75c9164a9d6b9a7d54e1b6a36f"
161 }"#;
162
163 let _: TransactionParts = serde_json::from_str(tx).unwrap();
164 }
165}