22// Copyright 2023 The evmone Authors.
33// SPDX-License-Identifier: Apache-2.0
44
5- #include < gtest/gtest .h>
5+ #include < gmock/gmock .h>
66#include < intx/intx.hpp>
77#include < test/statetest/statetest.hpp>
88
99using namespace evmone ;
1010using namespace intx ;
11+ using namespace testing ;
1112
1213// TODO: Add specific test of loading nonce, chainId, r, s, v
1314
@@ -16,6 +17,7 @@ TEST(statetest_loader, tx_create_legacy)
1617 constexpr std::string_view input = R"( {
1718 "input": "b0b1",
1819 "gas": "0x9091",
20+ "chainId": "0x5",
1921 "value": "0xe0e1",
2022 "sender": "a0a1",
2123 "to": "",
@@ -30,6 +32,7 @@ TEST(statetest_loader, tx_create_legacy)
3032 EXPECT_EQ (tx.kind , state::Transaction::Kind::legacy);
3133 EXPECT_EQ (tx.data , (bytes{0xb0 , 0xb1 }));
3234 EXPECT_EQ (tx.gas_limit , 0x9091 );
35+ EXPECT_EQ (tx.chain_id , 5 );
3336 EXPECT_EQ (tx.value , 0xe0e1 );
3437 EXPECT_EQ (tx.sender , 0xa0a1_address);
3538 EXPECT_FALSE (tx.to .has_value ());
@@ -63,6 +66,7 @@ TEST(statetest_loader, tx_eip1559)
6366 EXPECT_EQ (tx.kind , state::Transaction::Kind::eip1559);
6467 EXPECT_EQ (tx.data , (bytes{0xb0 , 0xb1 }));
6568 EXPECT_EQ (tx.gas_limit , 0x9091 );
69+ EXPECT_EQ (tx.chain_id , 0 );
6670 EXPECT_EQ (tx.value , 0xe0e1 );
6771 EXPECT_EQ (tx.sender , 0xa0a1_address);
6872 EXPECT_EQ (tx.to , 0xc0c1_address);
@@ -133,8 +137,117 @@ TEST(statetest_loader, tx_confusing)
133137 "v": "1"
134138 })" ;
135139
136- EXPECT_THROW (
137- test::from_json<state::Transaction>(json::json::parse (input)), std::invalid_argument);
140+ EXPECT_THAT ([&] { test::from_json<state::Transaction>(json::json::parse (input)); },
141+ ThrowsMessage<std::invalid_argument>(
142+ " invalid transaction: contains both legacy and EIP-1559 fees" ));
143+ }
144+
145+ TEST (statetest_loader, tx_type_1)
146+ {
147+ constexpr std::string_view input = R"( {
148+ "input": "",
149+ "gas": "0",
150+ "type": "1",
151+ "value": "0",
152+ "sender": "",
153+ "to": "",
154+ "gasPrice": "0",
155+ "accessList": [
156+ {"address": "ac01", "storageKeys": []},
157+ {"address": "ac02", "storageKeys": ["fe", "00"]}
158+ ],
159+ "nonce": "0",
160+ "r": "0x1111111111111111111111111111111111111111111111111111111111111111",
161+ "s": "0x2222222222222222222222222222222222222222222222222222222222222222",
162+ "v": "1"
163+ })" ;
164+
165+ const auto tx = test::from_json<state::Transaction>(json::json::parse (input));
166+ EXPECT_EQ (tx.kind , state::Transaction::Kind::eip2930);
167+ EXPECT_TRUE (tx.data .empty ());
168+ EXPECT_EQ (tx.gas_limit , 0 );
169+ EXPECT_EQ (tx.value , 0 );
170+ EXPECT_EQ (tx.sender , 0x00_address);
171+ EXPECT_FALSE (tx.to .has_value ());
172+ EXPECT_EQ (tx.max_gas_price , 0 );
173+ EXPECT_EQ (tx.max_priority_gas_price , 0 );
174+ ASSERT_EQ (tx.access_list .size (), 2 );
175+ EXPECT_EQ (tx.access_list [0 ].first , 0xac01_address);
176+ EXPECT_EQ (tx.access_list [0 ].second .size (), 0 );
177+ EXPECT_EQ (tx.access_list [1 ].first , 0xac02_address);
178+ EXPECT_EQ (tx.access_list [1 ].second , (std::vector{0xfe_bytes32, 0x00_bytes32}));
179+ EXPECT_EQ (tx.nonce , 0 );
180+ EXPECT_EQ (tx.r , 0x1111111111111111111111111111111111111111111111111111111111111111_u256);
181+ EXPECT_EQ (tx.s , 0x2222222222222222222222222222222222222222222222222222222222222222_u256);
182+ EXPECT_EQ (tx.v , 1 );
183+ }
184+
185+ TEST (statetest_loader, invalid_tx_type)
186+ {
187+ {
188+ constexpr std::string_view input = R"( {
189+ "input": "",
190+ "gas": "0",
191+ "type": "2",
192+ "value": "0",
193+ "sender": "",
194+ "to": "",
195+ "gasPrice": "0",
196+ "accessList": [
197+ {"address": "ac01", "storageKeys": []},
198+ {"address": "ac02", "storageKeys": ["fe", "00"]}
199+ ],
200+ "nonce": "0",
201+ "r": "0x1111111111111111111111111111111111111111111111111111111111111111",
202+ "s": "0x2222222222222222222222222222222222222222222222222222222222222222",
203+ "v": "1"
204+ })" ;
205+
206+ EXPECT_THAT ([&] { test::from_json<state::Transaction>(json::json::parse (input)); },
207+ ThrowsMessage<std::invalid_argument>(" wrong transaction type" ));
208+ }
209+ {
210+ constexpr std::string_view input = R"( {
211+ "input": "",
212+ "gas": "0",
213+ "type": "1",
214+ "value": "0",
215+ "sender": "",
216+ "to": "",
217+ "gasPrice": "0",
218+ "nonce": "0",
219+ "r": "0x1111111111111111111111111111111111111111111111111111111111111111",
220+ "s": "0x2222222222222222222222222222222222222222222222222222222222222222",
221+ "v": "1"
222+ })" ;
223+
224+ EXPECT_THAT ([&] { test::from_json<state::Transaction>(json::json::parse (input)); },
225+ ThrowsMessage<std::invalid_argument>(" wrong transaction type" ));
226+ }
227+
228+ {
229+ constexpr std::string_view input = R"( {
230+ "input": "",
231+ "gas": "0",
232+ "type": "1",
233+ "value": "0",
234+ "sender": "",
235+ "to": "",
236+ "maxFeePerGas": "0",
237+ "maxPriorityFeePerGas": "0",
238+ "accessList": [
239+ {"address": "ac01", "storageKeys": []},
240+ {"address": "ac02", "storageKeys": ["fe", "00"]}
241+ ],
242+ "nonce": "0",
243+ "r": "0x1111111111111111111111111111111111111111111111111111111111111111",
244+ "s": "0x2222222222222222222222222222222222222222222222222222222222222222",
245+ "v": "1"
246+ })" ;
247+
248+ EXPECT_THAT ([&] { test::from_json<state::Transaction>(json::json::parse (input)); },
249+ ThrowsMessage<std::invalid_argument>(" wrong transaction type" ));
250+ }
138251}
139252
140253namespace evmone ::test
0 commit comments