From d0ef459658749215c7c0efed6c08a98e423423de Mon Sep 17 00:00:00 2001 From: rodiazet Date: Wed, 29 Mar 2023 18:21:03 +0200 Subject: [PATCH] statetest: Fix loading difficulty for BlockInfo (#603) --- test/statetest/statetest_loader.cpp | 20 +++---- .../statetest_loader_block_info_test.cpp | 58 +++++++++++++++++++ 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/test/statetest/statetest_loader.cpp b/test/statetest/statetest_loader.cpp index bdcce19065..f7f18c8237 100644 --- a/test/statetest/statetest_loader.cpp +++ b/test/statetest/statetest_loader.cpp @@ -75,7 +75,12 @@ address from_json
(const json::json& j) template <> hash256 from_json(const json::json& j) { - return evmc::from_hex(j.get()).value(); + // Special case to handle "0". Required by exec-spec-tests. + // TODO: Get rid of it. + if (j.is_string() && (j == "0" || j == "0x0")) + return 0x00_bytes32; + else + return evmc::from_hex(j.get()).value(); } template <> @@ -148,18 +153,11 @@ state::BlockInfo from_json(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() == "0") - difficulty = 0x0000000000000000000000000000000000000000000000000000000000000000_bytes32; - else - difficulty = from_json(*prev_randao_it); - } + difficulty = from_json(*prev_randao_it); else if (current_difficulty_it != j.end()) - difficulty = from_json(*current_difficulty_it); + difficulty = from_json(*current_difficulty_it); else if (parent_difficulty_it != j.end()) - difficulty = from_json(*parent_difficulty_it); + difficulty = from_json(*parent_difficulty_it); uint64_t base_fee = 0; if (j.contains("currentBaseFee")) diff --git a/test/unittests/statetest_loader_block_info_test.cpp b/test/unittests/statetest_loader_block_info_test.cpp index 5003958cfe..65d4a982f4 100644 --- a/test/unittests/statetest_loader_block_info_test.cpp +++ b/test/unittests/statetest_loader_block_info_test.cpp @@ -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(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": "0x0", + "parentBaseFee": "7", + "parentGasUsed": "0", + "parentGasLimit": "100000000000000000", + "parentTimstamp": "0", + "blockHashes": { + "0": "0xc305d826e3784046a7e9d31128ef98d3e96133fe454c16ef630574d967dfdb1a" + }, + "ommers": [], + "withdrawals": [], + "parentUncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" + })"; + + const auto bi = test::from_json(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"({