diff --git a/test/state/state.hpp b/test/state/state.hpp index 10311a528..27a92bcaa 100644 --- a/test/state/state.hpp +++ b/test/state/state.hpp @@ -85,7 +85,7 @@ struct Transaction Kind kind = Kind::legacy; bytes data; - int64_t gas_limit; + int64_t gas_limit = 0; intx::uint256 max_gas_price; intx::uint256 max_priority_gas_price; address sender; diff --git a/test/statetest/statetest_loader.cpp b/test/statetest/statetest_loader.cpp index 1e1688b6f..d564af1c3 100644 --- a/test/statetest/statetest_loader.cpp +++ b/test/statetest/statetest_loader.cpp @@ -186,18 +186,15 @@ template <> state::Transaction from_json(const json::json& j) { state::Transaction o; - o.data = from_json(j.at("input")); - o.gas_limit = from_json(j.at("gas")); - o.value = from_json(j.at("value")); o.sender = from_json(j.at("sender")); - if (!j.at("to").get().empty()) - o.to = from_json(j.at("to")); + if (const auto& to = j.at("to"); !to.get().empty()) + o.to = from_json(to); - if (j.contains("gasPrice")) + if (const auto gas_price_it = j.find("gasPrice"); gas_price_it != j.end()) { o.kind = state::Transaction::Kind::legacy; - o.max_gas_price = from_json(j.at("gasPrice")); + o.max_gas_price = from_json(*gas_price_it); o.max_priority_gas_price = o.max_gas_price; } else @@ -207,29 +204,31 @@ state::Transaction from_json(const json::json& j) o.max_priority_gas_price = from_json(j.at("maxPriorityFeePerGas")); } - if (j.contains("accessList")) - o.access_list = from_json(j.at("accessList")); + // The following fields are optional or arrays in a TestMultiTransaction: + + if (const auto input_it = j.find("input"); input_it != j.end()) + o.data = from_json(*input_it); + + if (const auto gas_it = j.find("gas"); gas_it != j.end()) + o.gas_limit = from_json(*gas_it); + + if (const auto value_it = j.find("value"); value_it != j.end() && !value_it->is_array()) + o.value = from_json(*value_it); + + if (const auto ac_it = j.find("accessList"); ac_it != j.end()) + o.access_list = from_json(*ac_it); return o; } static void from_json(const json::json& j, TestMultiTransaction& o) { - if (j.contains("gasPrice")) - { - o.kind = state::Transaction::Kind::legacy; - o.max_gas_price = from_json(j.at("gasPrice")); - o.max_priority_gas_price = o.max_gas_price; - } - else - { - o.kind = state::Transaction::Kind::eip1559; - o.max_gas_price = from_json(j.at("maxFeePerGas")); - o.max_priority_gas_price = from_json(j.at("maxPriorityFeePerGas")); - } - o.sender = from_json(j.at("sender")); - if (!j.at("to").get().empty()) - o.to = from_json(j["to"]); + const auto base_tx = from_json(j); + o.kind = base_tx.kind; + o.sender = base_tx.sender; + o.to = base_tx.to; + o.max_gas_price = base_tx.max_gas_price; + o.max_priority_gas_price = base_tx.max_priority_gas_price; for (const auto& j_data : j.at("data")) o.inputs.emplace_back(from_json(j_data)); diff --git a/test/unittests/statetest_loader_tx_test.cpp b/test/unittests/statetest_loader_tx_test.cpp index 495879caa..e99287240 100644 --- a/test/unittests/statetest_loader_tx_test.cpp +++ b/test/unittests/statetest_loader_tx_test.cpp @@ -78,3 +78,29 @@ TEST(statetest_loader, tx_access_list) EXPECT_EQ(tx.access_list[1].first, 0xac02_address); EXPECT_EQ(tx.access_list[1].second, (std::vector{0xfe_bytes32, 0x00_bytes32})); } + +TEST(statetest_loader, multi_tx_base) +{ + constexpr std::string_view input = R"({ + "sender" : "0xaa01", + "to" : "0xaa02", + "gasPrice" : "0x0a", + + "accessLists" : [], + "data" : ["dd"], + "gasLimit" : ["0x01"], + "value" : ["0x02"] + })"; + + const auto base_tx = test::from_json(json::json::parse(input)); + EXPECT_EQ(base_tx.kind, state::Transaction::Kind::legacy); + EXPECT_EQ(base_tx.sender, 0xaa01_address); + EXPECT_EQ(base_tx.to, 0xaa02_address); + EXPECT_EQ(base_tx.max_gas_price, 0x0a); + EXPECT_EQ(base_tx.max_priority_gas_price, 0x0a); + + EXPECT_EQ(base_tx.data.size(), 0); + EXPECT_EQ(base_tx.gas_limit, 0); + EXPECT_EQ(base_tx.value, 0); + EXPECT_TRUE(base_tx.access_list.empty()); +}