Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load BlockInfo fixes required by exec-spec-tests #583

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/statetest/statetest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ struct StateTransitionTest
template <typename T>
T from_json(const json::json& j) = delete;

template <>
uint64_t from_json<uint64_t>(const json::json& j);

template <>
int64_t from_json<int64_t>(const json::json& j);
chfast marked this conversation as resolved.
Show resolved Hide resolved

template <>
state::BlockInfo from_json<state::BlockInfo>(const json::json& j);

Expand Down
41 changes: 38 additions & 3 deletions test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,44 @@ uint8_t from_json<uint8_t>(const json::json& j)
return static_cast<uint8_t>(ret);
}

template <typename T>
static std::optional<T> integer_from_json(const json::json& j)
{
if (j.is_number_integer())
return j.get<T>();

if (!j.is_string())
return {};

const auto s = j.get<std::string>();
size_t num_processed = 0;
T v = 0;
if constexpr (std::is_same_v<T, uint64_t>)
v = std::stoull(s, &num_processed, 0);
else
v = std::stoll(s, &num_processed, 0);

if (num_processed == 0 || num_processed != s.size())
return {};
return v;
}

template <>
int64_t from_json<int64_t>(const json::json& j)
{
return static_cast<int64_t>(std::stoll(j.get<std::string>(), nullptr, 16));
const auto v = integer_from_json<int64_t>(j);
if (!v.has_value())
throw std::invalid_argument("from_json<int64_t>: must be integer or string of integer");
return *v;
}

template <>
uint64_t from_json<uint64_t>(const json::json& j)
{
return static_cast<uint64_t>(std::stoull(j.get<std::string>(), nullptr, 16));
const auto v = integer_from_json<uint64_t>(j);
if (!v.has_value())
throw std::invalid_argument("from_json<uint64_t>: must be integer or string of integer");
return *v;
}

template <>
Expand Down Expand Up @@ -120,7 +148,14 @@ state::BlockInfo from_json<state::BlockInfo>(const json::json& j)
const auto current_difficulty_it = j.find("currentDifficulty");
const auto parent_difficulty_it = j.find("parentDifficulty");
if (prev_randao_it != j.end())
difficulty = from_json<evmc::bytes32>(*prev_randao_it);
{
// Special case to handle "0". Required by exec-spec-tests.
// TODO: Get rid of it.
if (prev_randao_it->is_string() && prev_randao_it->get<std::string>() == "0")
difficulty = 0x0000000000000000000000000000000000000000000000000000000000000000_bytes32;
else
difficulty = from_json<evmc::bytes32>(*prev_randao_it);
}
else if (current_difficulty_it != j.end())
difficulty = from_json<evmc::bytes32>(*current_difficulty_it);
else if (parent_difficulty_it != j.end())
Expand Down
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ target_sources(
state_mpt_test.cpp
state_new_account_address_test.cpp
state_rlp_test.cpp
statetest_loader_block_info_test.cpp
statetest_loader_test.cpp
statetest_loader_tx_test.cpp
statetest_logs_hash_test.cpp
Expand Down
114 changes: 114 additions & 0 deletions test/unittests/statetest_loader_block_info_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2023 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <test/statetest/statetest.hpp>

using namespace evmone;

TEST(statetest_loader, block_info)
{
constexpr std::string_view input = R"({
"currentCoinbase": "0x1111111111111111111111111111111111111111",
"currentDifficulty": "0x0",
"currentGasLimit": "0x0",
"currentNumber": "0",
"currentTimestamp": "0",
"currentBaseFee": "7",
"currentRandom": "0x00",
"withdrawals": []
})";

const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
EXPECT_EQ(bi.coinbase, 0x1111111111111111111111111111111111111111_address);
EXPECT_EQ(bi.prev_randao, 0x00_bytes32);
EXPECT_EQ(bi.gas_limit, 0x0);
EXPECT_EQ(bi.base_fee, 7);
EXPECT_EQ(bi.timestamp, 0);
EXPECT_EQ(bi.number, 0);
}

TEST(statetest_loader, block_info_hex)
{
constexpr std::string_view input = R"({
"currentCoinbase": "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentGasLimit": "0x16345785D8A0000",
"currentNumber": "1",
"currentTimestamp": "0x3E8",
"currentRandom": "0x00",
"currentDifficulty": "1",
"parentDifficulty": "0",
"parentBaseFee": "7",
"parentGasUsed": "0",
"parentGasLimit": "0x16345785D8A0000",
"parentTimstamp": "0",
"blockHashes": {
"0": "0xc305d826e3784046a7e9d31128ef98d3e96133fe454c16ef630574d967dfdb1a"
},
"ommers": [],
"withdrawals": [],
"parentUncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
})";

const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
EXPECT_EQ(bi.coinbase, 0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba_address);
EXPECT_EQ(bi.prev_randao, 0x00_bytes32);
EXPECT_EQ(bi.gas_limit, 100000000000000000);
EXPECT_EQ(bi.base_fee, 7);
EXPECT_EQ(bi.timestamp, 1000);
EXPECT_EQ(bi.number, 1);
}

TEST(statetest_loader, block_info_dec)
{
constexpr std::string_view input = R"({
"currentCoinbase": "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentGasLimit": "100000000000000000",
"currentNumber": "1",
"currentTimestamp": "1000",
"currentRandom": "0x00",
"currentDifficulty": "0",
"parentDifficulty": "0",
"parentBaseFee": "7",
"parentGasUsed": "0",
"parentGasLimit": "100000000000000000",
"parentTimstamp": "0",
"blockHashes": {
"0": "0xc305d826e3784046a7e9d31128ef98d3e96133fe454c16ef630574d967dfdb1a"
},
"ommers": [],
"withdrawals": [],
"parentUncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
})";

const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
EXPECT_EQ(bi.coinbase, 0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba_address);
EXPECT_EQ(bi.prev_randao, 0x00_bytes32);
EXPECT_EQ(bi.gas_limit, 100000000000000000);
EXPECT_EQ(bi.base_fee, 7);
EXPECT_EQ(bi.timestamp, 1000);
EXPECT_EQ(bi.number, 1);
}

TEST(statetest_loader, block_info_0_random)
{
constexpr std::string_view input = R"({
"currentCoinbase": "0x1111111111111111111111111111111111111111",
"currentDifficulty": "0x0",
"currentGasLimit": "0x0",
"currentNumber": "0",
"currentTimestamp": "0",
"currentBaseFee": "7",
"currentRandom": "0",
"withdrawals": []
})";

const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
EXPECT_EQ(bi.coinbase, 0x1111111111111111111111111111111111111111_address);
EXPECT_EQ(bi.prev_randao, 0x00_bytes32);
EXPECT_EQ(bi.gas_limit, 0x0);
EXPECT_EQ(bi.base_fee, 7);
EXPECT_EQ(bi.timestamp, 0);
EXPECT_EQ(bi.number, 0);
}
59 changes: 57 additions & 2 deletions test/unittests/statetest_loader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,66 @@
#include <test/statetest/statetest.hpp>

using namespace evmone;
using namespace evmone::test;

TEST(json_loader, uint64_t)
{
using json::basic_json;

EXPECT_EQ(from_json<uint64_t>(basic_json("0x00000005")), 5);
EXPECT_EQ(from_json<uint64_t>(basic_json("5")), 5);
EXPECT_EQ(from_json<uint64_t>(basic_json(7)), 7);

EXPECT_EQ(from_json<uint64_t>(basic_json("0xffffffffffffffff")),
std::numeric_limits<uint64_t>::max());
EXPECT_EQ(from_json<uint64_t>(basic_json("18446744073709551615")),
std::numeric_limits<uint64_t>::max());
EXPECT_THROW(from_json<uint64_t>(basic_json("0x10000000000000000")), std::out_of_range);
EXPECT_THROW(from_json<uint64_t>(basic_json("18446744073709551616")), std::out_of_range);
EXPECT_EQ(from_json<uint64_t>(basic_json(std::numeric_limits<uint64_t>::max())),
std::numeric_limits<uint64_t>::max());

// Octal is also supported.
EXPECT_EQ(from_json<uint64_t>(basic_json("0777")), 0777);

EXPECT_THROW(from_json<uint64_t>(basic_json("0x000000000000000k")), std::invalid_argument);
EXPECT_THROW(from_json<uint64_t>(basic_json("k")), std::invalid_argument);
EXPECT_THROW(from_json<uint64_t>(basic_json("")), std::invalid_argument);
}

TEST(json_loader, int64_t)
{
using json::basic_json;

EXPECT_EQ(from_json<int64_t>(basic_json("0x00000005")), 5);
EXPECT_EQ(from_json<int64_t>(basic_json("-0x5")), -5);
EXPECT_EQ(from_json<int64_t>(basic_json("-5")), -5);

EXPECT_EQ(from_json<int64_t>(basic_json(-7)), -7);
EXPECT_EQ(from_json<int64_t>(basic_json(0xffffffffffffffff)), -1);

EXPECT_EQ(
from_json<int64_t>(basic_json("0x7fffffffffffffff")), std::numeric_limits<int64_t>::max());
EXPECT_EQ(
from_json<int64_t>(basic_json("9223372036854775807")), std::numeric_limits<int64_t>::max());
EXPECT_EQ(from_json<int64_t>(basic_json("-9223372036854775808")),
std::numeric_limits<int64_t>::min());
EXPECT_THROW(from_json<int64_t>(basic_json("0xffffffffffffffff")), std::out_of_range);
EXPECT_THROW(from_json<int64_t>(basic_json("9223372036854775808")), std::out_of_range);
EXPECT_THROW(from_json<int64_t>(basic_json("-9223372036854775809")), std::out_of_range);

// Octal is also supported.
EXPECT_EQ(from_json<int64_t>(basic_json("0777")), 0777);

EXPECT_THROW(from_json<int64_t>(basic_json("0x000000000000000k")), std::invalid_argument);
EXPECT_THROW(from_json<int64_t>(basic_json("k")), std::invalid_argument);
EXPECT_THROW(from_json<int64_t>(basic_json("")), std::invalid_argument);
}

TEST(statetest_loader, load_empty_test)
{
std::istringstream s{"{}"};
EXPECT_THROW(test::load_state_test(s), std::invalid_argument);
EXPECT_THROW(load_state_test(s), std::invalid_argument);
}

TEST(statetest_loader, load_minimal_test)
Expand All @@ -36,7 +91,7 @@ TEST(statetest_loader, load_minimal_test)
}
}
})"};
const test::StateTransitionTest st = test::load_state_test(s);
const StateTransitionTest st = load_state_test(s);
// TODO: should add some comparison operator to State, BlockInfo, AccessList
EXPECT_EQ(st.pre_state.get_accounts().size(), 0);
EXPECT_EQ(st.block.number, 0);
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/statetest_loader_tx_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ TEST(statetest_loader, tx_create_legacy)
{
constexpr std::string_view input = R"({
"input": "b0b1",
"gas": "9091",
"gas": "0x9091",
"value": "0xe0e1",
"sender": "a0a1",
"to": "",
Expand Down Expand Up @@ -46,7 +46,7 @@ TEST(statetest_loader, tx_eip1559)
{
constexpr std::string_view input = R"({
"input": "b0b1",
"gas": "9091",
"gas": "0x9091",
chfast marked this conversation as resolved.
Show resolved Hide resolved
"value": "0xe0e1",
"sender": "a0a1",
"to": "c0c1",
Expand Down