Skip to content

Commit

Permalink
statetest: Reuse Transaction loading in TestMultiTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast authored and axic committed Feb 13, 2023
1 parent b76bbc5 commit 53055e6
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<state::Transaction>(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<bytes>(j.at("input"));
o.gas_limit = from_json<int64_t>(j.at("gas"));
o.value = from_json<intx::uint256>(j.at("value"));
o.sender = from_json<evmc::address>(j.at("sender"));

if (!j.at("to").get<std::string>().empty())
o.to = from_json<evmc::address>(j.at("to"));
if (const auto& to = j.at("to"); !to.get<std::string>().empty())
o.to = from_json<evmc::address>(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<intx::uint256>(j.at("gasPrice"));
o.max_gas_price = from_json<intx::uint256>(*gas_price_it);
o.max_priority_gas_price = o.max_gas_price;
}
else
Expand All @@ -206,30 +202,26 @@ state::Transaction from_json<state::Transaction>(const json::json& j)
o.max_gas_price = from_json<intx::uint256>(j.at("maxFeePerGas"));
o.max_priority_gas_price = from_json<intx::uint256>(j.at("maxPriorityFeePerGas"));
}
}

template <>
state::Transaction from_json<state::Transaction>(const json::json& j)
{
state::Transaction o;
from_json_tx_common(j, o);
o.data = from_json<bytes>(j.at("input"));
o.gas_limit = from_json<int64_t>(j.at("gas"));
o.value = from_json<intx::uint256>(j.at("value"));

if (j.contains("accessList"))
o.access_list = from_json<state::AccessList>(j.at("accessList"));
if (const auto ac_it = j.find("accessList"); ac_it != j.end())
o.access_list = from_json<state::AccessList>(*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<intx::uint256>(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<intx::uint256>(j.at("maxFeePerGas"));
o.max_priority_gas_price = from_json<intx::uint256>(j.at("maxPriorityFeePerGas"));
}
o.sender = from_json<evmc::address>(j.at("sender"));
if (!j.at("to").get<std::string>().empty())
o.to = from_json<evmc::address>(j["to"]);
from_json_tx_common(j, o);

for (const auto& j_data : j.at("data"))
o.inputs.emplace_back(from_json<bytes>(j_data));
Expand Down

0 comments on commit 53055e6

Please sign in to comment.