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)
94 }
95}
96
97#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
99#[serde(rename_all = "camelCase", deny_unknown_fields)]
100pub struct TxPartIndices {
101 pub data: usize,
103 pub gas: usize,
105 pub value: usize,
107}
108
109#[cfg(test)]
110mod test {
111
112 use super::*;
113
114 #[test]
115 fn decode_tx_parts() {
116 let tx = r#"{
117 "nonce": "0x00",
118 "maxPriorityFeePerGas": "0x00",
119 "maxFeePerGas": "0x07",
120 "gasLimit": [
121 "0x0423ff"
122 ],
123 "to": "0x0000000000000000000000000000000000001000",
124 "value": [
125 "0x00"
126 ],
127 "data": [
128 "0x"
129 ],
130 "accessLists": [
131 [
132 {
133 "address": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71",
134 "storageKeys": [
135 "0x0000000000000000000000000000000000000000000000000000000000000000"
136 ]
137 }
138 ]
139 ],
140 "authorizationList": [
141 {
142 "chainId": "0x00",
143 "address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
144 "nonce": "0x00",
145 "v": "0x01",
146 "r": "0x5a8cac98fd240d8ef83c22db4a061ffa0facb1801245283cc05fc809d8b92837",
147 "s": "0x1c3162fe11d91bc24d4fa00fb19ca34531e0eacdf8142c804be44058d5b8244f",
148 "signer": "0x6389e7f33ce3b1e94e4325ef02829cd12297ef71"
149 }
150 ],
151 "sender": "0x8a0a19589531694250d570040a0c4b74576919b8",
152 "secretKey": "0x9e7645d0cfd9c3a04eb7a9db59a4eb7d359f2e75c9164a9d6b9a7d54e1b6a36f"
153 }"#;
154
155 let _: TransactionParts = serde_json::from_str(tx).unwrap();
156 }
157}