Skip to content

Commit

Permalink
Fix loading difficulty for BlockInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Mar 28, 2023
1 parent fd99568 commit 913cf29
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
26 changes: 16 additions & 10 deletions test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ inline uint64_t calculate_current_base_fee_eip1559(
return base_fee;
}

namespace
{
bytes32 load_bytes32(const json::json::const_iterator& json_it)
{
// Special case to handle "0". Required by exec-spec-tests.
// TODO: Get rid of it.
if (json_it->is_string() && json_it->get<std::string>() == "0")
return 0x00_bytes32;
else
return from_json<evmc::bytes32>(*json_it);
}
} // namespace

template <>
state::BlockInfo from_json<state::BlockInfo>(const json::json& j)
{
Expand All @@ -148,18 +161,11 @@ 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())
{
// 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);
}
difficulty = load_bytes32(prev_randao_it);
else if (current_difficulty_it != j.end())
difficulty = from_json<evmc::bytes32>(*current_difficulty_it);
difficulty = load_bytes32(current_difficulty_it);
else if (parent_difficulty_it != j.end())
difficulty = from_json<evmc::bytes32>(*parent_difficulty_it);
difficulty = load_bytes32(parent_difficulty_it);

uint64_t base_fee = 0;
if (j.contains("currentBaseFee"))
Expand Down
58 changes: 58 additions & 0 deletions test/unittests/statetest_loader_block_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,64 @@ TEST(statetest_loader, block_info_dec)
EXPECT_EQ(bi.number, 1);
}

TEST(statetest_loader, block_info_0_current_difficulty)
{
constexpr std::string_view input = R"({
"currentCoinbase": "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentGasLimit": "100000000000000000",
"currentNumber": "1",
"currentTimestamp": "1000",
"currentDifficulty": "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_parent_difficulty)
{
constexpr std::string_view input = R"({
"currentCoinbase": "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentGasLimit": "100000000000000000",
"currentNumber": "1",
"currentTimestamp": "1000",
"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"({
Expand Down

0 comments on commit 913cf29

Please sign in to comment.