22// Copyright 2023 The evmone Authors.
33// SPDX-License-Identifier: Apache-2.0
44
5+ #include " ../state/ethash_difficulty.hpp"
56#include " ../state/mpt_hash.hpp"
67#include " ../state/requests.hpp"
78#include " ../state/rlp.hpp"
@@ -107,8 +108,8 @@ TransitionResult apply_block(const TestState& state, evmc::VM& vm, const state::
107108 bloom, blob_gas_left, std::move (block_state)};
108109}
109110
110- bool validate_block (
111- evmc_revision rev, const TestBlock& test_block, const BlockHeader* parent_header) noexcept
111+ bool validate_block (evmc_revision rev, const TestBlock& test_block,
112+ const BlockHeader* parent_header, bool parent_has_ommers ) noexcept
112113{
113114 // NOTE: includes only block validity unrelated to individual txs. See `apply_block`.
114115
@@ -136,6 +137,12 @@ bool validate_block(
136137 if (test_block.block_info .gas_limit < 5000 )
137138 return false ;
138139
140+ if (test_block.block_info .difficulty != state::calculate_difficulty (parent_header->difficulty ,
141+ parent_has_ommers, parent_header->timestamp ,
142+ test_block.block_info .timestamp ,
143+ test_block.block_info .number , rev))
144+ return false ;
145+
139146 if (rev >= EVMC_PARIS && !test_block.block_info .ommers .empty ())
140147 return false ;
141148
@@ -257,11 +264,12 @@ void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
257264 struct BlockData
258265 {
259266 const BlockHeader* header;
267+ bool has_ommers = false ;
260268 TestState post_state;
261269 intx::uint256 total_difficulty;
262270 };
263271 std::unordered_map<hash256, BlockData> block_data{{{c.genesis_block_header .hash ,
264- {&c.genesis_block_header , c.pre_state , c.genesis_block_header .difficulty }}}};
272+ {&c.genesis_block_header , false , c.pre_state , c.genesis_block_header .difficulty }}}};
265273 const auto * canonical_state = &c.pre_state ;
266274 intx::uint256 max_total_difficulty = c.genesis_block_header .difficulty ;
267275
@@ -273,6 +281,8 @@ void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
273281 const auto parent_data_it = block_data.find (test_block.block_info .parent_hash );
274282 const auto * parent_header =
275283 parent_data_it != block_data.end () ? parent_data_it->second .header : nullptr ;
284+ const auto parent_has_ommers =
285+ parent_data_it != block_data.end () && parent_data_it->second .has_ommers ;
276286
277287 const auto rev = c.rev .get_revision (bi.timestamp );
278288
@@ -281,7 +291,7 @@ void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
281291
282292 if (test_block.valid )
283293 {
284- ASSERT_TRUE (validate_block (rev, test_block, parent_header))
294+ ASSERT_TRUE (validate_block (rev, test_block, parent_header, parent_has_ommers ))
285295 << " Expected block to be valid (validate_block)" ;
286296
287297 // Block being valid guarantees its parent was found.
@@ -296,7 +306,8 @@ void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
296306 block_hashes[test_block.expected_block_header .block_number ] =
297307 test_block.expected_block_header .hash ;
298308 const auto [inserted_it, _] = block_data.insert ({test_block.block_info .hash ,
299- {&test_block.expected_block_header , std::move (res.block_state ),
309+ {&test_block.expected_block_header , !test_block.block_info .ommers .empty (),
310+ std::move (res.block_state ),
300311 parent_data_it->second .total_difficulty +
301312 test_block.block_info .difficulty }});
302313 if (inserted_it->second .total_difficulty >= max_total_difficulty)
@@ -334,7 +345,7 @@ void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
334345 }
335346 else
336347 {
337- if (!validate_block (rev, test_block, parent_header))
348+ if (!validate_block (rev, test_block, parent_header, parent_has_ommers ))
338349 continue ;
339350
340351 // Block being valid guarantees its parent was found.
@@ -385,7 +396,6 @@ void run_blockchain_tests(std::span<const BlockchainTest> tests, evmc::VM& vm)
385396 print_state (std::get<TestState>(c.expectation .post_state )) :
386397 " " );
387398 }
388- // TODO: Add difficulty calculation verification.
389399}
390400
391401} // namespace evmone::test
0 commit comments