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 committed Feb 8, 2023
1 parent 895f56f commit ba88fda
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
2 changes: 1 addition & 1 deletion test/state/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 23 additions & 24 deletions test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,15 @@ template <>
state::Transaction from_json<state::Transaction>(const json::json& j)
{
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 @@ -207,29 +204,31 @@ state::Transaction from_json<state::Transaction>(const json::json& j)
o.max_priority_gas_price = from_json<intx::uint256>(j.at("maxPriorityFeePerGas"));
}

if (j.contains("accessList"))
o.access_list = from_json<state::AccessList>(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<bytes>(*input_it);

if (const auto gas_it = j.find("gas"); gas_it != j.end())
o.gas_limit = from_json<int64_t>(*gas_it);

if (const auto value_it = j.find("value"); value_it != j.end() && !value_it->is_array())
o.value = from_json<intx::uint256>(*value_it);

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"]);
const auto base_tx = from_json<state::Transaction>(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<bytes>(j_data));
Expand Down
26 changes: 26 additions & 0 deletions test/unittests/statetest_loader_tx_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<state::Transaction>(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());
}

0 comments on commit ba88fda

Please sign in to comment.