Skip to content

Commit 898ea5c

Browse files
committed
Fix loading difficulty for BlockInfo
1 parent fd99568 commit 898ea5c

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

test/statetest/statetest_loader.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ address from_json<address>(const json::json& j)
7575
template <>
7676
hash256 from_json<hash256>(const json::json& j)
7777
{
78-
return evmc::from_hex<hash256>(j.get<std::string>()).value();
78+
// Special case to handle "0". Required by exec-spec-tests.
79+
// TODO: Get rid of it.
80+
if (j.is_string() && (j == "0" || j == "0x0"))
81+
return 0x00_bytes32;
82+
else
83+
return evmc::from_hex<hash256>(j.get<std::string>()).value();
7984
}
8085

8186
template <>
@@ -148,18 +153,11 @@ state::BlockInfo from_json<state::BlockInfo>(const json::json& j)
148153
const auto current_difficulty_it = j.find("currentDifficulty");
149154
const auto parent_difficulty_it = j.find("parentDifficulty");
150155
if (prev_randao_it != j.end())
151-
{
152-
// Special case to handle "0". Required by exec-spec-tests.
153-
// TODO: Get rid of it.
154-
if (prev_randao_it->is_string() && prev_randao_it->get<std::string>() == "0")
155-
difficulty = 0x0000000000000000000000000000000000000000000000000000000000000000_bytes32;
156-
else
157-
difficulty = from_json<evmc::bytes32>(*prev_randao_it);
158-
}
156+
difficulty = from_json<bytes32>(*prev_randao_it);
159157
else if (current_difficulty_it != j.end())
160-
difficulty = from_json<evmc::bytes32>(*current_difficulty_it);
158+
difficulty = from_json<bytes32>(*current_difficulty_it);
161159
else if (parent_difficulty_it != j.end())
162-
difficulty = from_json<evmc::bytes32>(*parent_difficulty_it);
160+
difficulty = from_json<bytes32>(*parent_difficulty_it);
163161

164162
uint64_t base_fee = 0;
165163
if (j.contains("currentBaseFee"))

test/unittests/statetest_loader_block_info_test.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,64 @@ TEST(statetest_loader, block_info_dec)
9191
EXPECT_EQ(bi.number, 1);
9292
}
9393

94+
TEST(statetest_loader, block_info_0_current_difficulty)
95+
{
96+
constexpr std::string_view input = R"({
97+
"currentCoinbase": "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
98+
"currentGasLimit": "100000000000000000",
99+
"currentNumber": "1",
100+
"currentTimestamp": "1000",
101+
"currentDifficulty": "0",
102+
"parentBaseFee": "7",
103+
"parentGasUsed": "0",
104+
"parentGasLimit": "100000000000000000",
105+
"parentTimstamp": "0",
106+
"blockHashes": {
107+
"0": "0xc305d826e3784046a7e9d31128ef98d3e96133fe454c16ef630574d967dfdb1a"
108+
},
109+
"ommers": [],
110+
"withdrawals": [],
111+
"parentUncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
112+
})";
113+
114+
const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
115+
EXPECT_EQ(bi.coinbase, 0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba_address);
116+
EXPECT_EQ(bi.prev_randao, 0x00_bytes32);
117+
EXPECT_EQ(bi.gas_limit, 100000000000000000);
118+
EXPECT_EQ(bi.base_fee, 7);
119+
EXPECT_EQ(bi.timestamp, 1000);
120+
EXPECT_EQ(bi.number, 1);
121+
}
122+
123+
TEST(statetest_loader, block_info_0_parent_difficulty)
124+
{
125+
constexpr std::string_view input = R"({
126+
"currentCoinbase": "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
127+
"currentGasLimit": "100000000000000000",
128+
"currentNumber": "1",
129+
"currentTimestamp": "1000",
130+
"parentDifficulty": "0x0",
131+
"parentBaseFee": "7",
132+
"parentGasUsed": "0",
133+
"parentGasLimit": "100000000000000000",
134+
"parentTimstamp": "0",
135+
"blockHashes": {
136+
"0": "0xc305d826e3784046a7e9d31128ef98d3e96133fe454c16ef630574d967dfdb1a"
137+
},
138+
"ommers": [],
139+
"withdrawals": [],
140+
"parentUncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
141+
})";
142+
143+
const auto bi = test::from_json<state::BlockInfo>(json::json::parse(input));
144+
EXPECT_EQ(bi.coinbase, 0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba_address);
145+
EXPECT_EQ(bi.prev_randao, 0x00_bytes32);
146+
EXPECT_EQ(bi.gas_limit, 100000000000000000);
147+
EXPECT_EQ(bi.base_fee, 7);
148+
EXPECT_EQ(bi.timestamp, 1000);
149+
EXPECT_EQ(bi.number, 1);
150+
}
151+
94152
TEST(statetest_loader, block_info_0_random)
95153
{
96154
constexpr std::string_view input = R"({

0 commit comments

Comments
 (0)