From 53055e62fcd2af359ce59e63c675517e6f32c1c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 8 Feb 2023 13:00:22 +0100 Subject: [PATCH] statetest: Reuse Transaction loading in TestMultiTransaction --- test/statetest/statetest_loader.cpp | 46 ++++++++++++----------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/test/statetest/statetest_loader.cpp b/test/statetest/statetest_loader.cpp index 1e1688b6f1..c0a7579eb3 100644 --- a/test/statetest/statetest_loader.cpp +++ b/test/statetest/statetest_loader.cpp @@ -182,22 +182,18 @@ evmc_revision to_rev(std::string_view s) throw std::invalid_argument{"unknown revision: " + std::string{s}}; } -template <> -state::Transaction from_json(const json::json& j) +/// Load common parts of Transaction or TestMultiTransaction. +static void from_json_tx_common(const json::json& j, state::Transaction& o) { - 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 @@ -206,30 +202,26 @@ state::Transaction from_json(const json::json& j) o.max_gas_price = from_json(j.at("maxFeePerGas")); o.max_priority_gas_price = from_json(j.at("maxPriorityFeePerGas")); } +} + +template <> +state::Transaction from_json(const json::json& j) +{ + state::Transaction o; + from_json_tx_common(j, o); + o.data = from_json(j.at("input")); + o.gas_limit = from_json(j.at("gas")); + o.value = from_json(j.at("value")); - if (j.contains("accessList")) - o.access_list = from_json(j.at("accessList")); + 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"]); + from_json_tx_common(j, o); for (const auto& j_data : j.at("data")) o.inputs.emplace_back(from_json(j_data));