revm_statetest_types/
transaction.rs1use 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#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct TransactionParts {
11 #[serde(rename = "type")]
13 pub tx_type: Option<u8>,
14 pub data: Vec<Bytes>,
16 pub gas_limit: Vec<U256>,
18 pub gas_price: Option<U256>,
20 pub nonce: U256,
22 pub secret_key: B256,
24 #[serde(default)]
26 pub sender: Option<Address>,
27 #[serde(default, deserialize_with = "deserialize_maybe_empty")]
29 pub to: Option<Address>,
30 pub value: Vec<U256>,
32 pub max_fee_per_gas: Option<U256>,
34 pub max_priority_fee_per_gas: Option<U256>,
36 pub initcodes: Option<Vec<Bytes>>,
38 #[serde(default)]
40 pub access_lists: Vec<Option<AccessList>>,
41 pub authorization_list: Option<Vec<TestAuthorization>>,
43 #[serde(default)]
45 pub blob_versioned_hashes: Vec<B256>,
46 pub max_fee_per_blob_gas: Option<U256>,
48}
49
50impl TransactionParts {
51 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 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 self.max_fee_per_gas.is_some() {
74 tx_type = TransactionType::Eip1559;
75 }
76
77 if self.max_fee_per_blob_gas.is_some() {
79 self.to?;
81 return Some(TransactionType::Eip4844);
82 }
83
84 if self.authorization_list.is_some() {
86 self.to?;
88 return Some(TransactionType::Eip7702);
89 }
90
91 Some(tx_type)
92 }
93}
94
95#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
97#[serde(rename_all = "camelCase", deny_unknown_fields)]
98pub struct TxPartIndices {
99 pub data: usize,
101 pub gas: usize,
103 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}