Skip to content

Commit

Permalink
test: Add some difficulty tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Sep 1, 2023
1 parent 2cd3222 commit 7d0e0ee
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ target_sources(
execution_state_test.cpp
instructions_test.cpp
state_bloom_filter_test.cpp
state_difficulty_test.cpp
state_mpt_hash_test.cpp
state_mpt_test.cpp
state_new_account_address_test.cpp
Expand Down
75 changes: 75 additions & 0 deletions test/unittests/state_difficulty_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2023 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include <gmock/gmock.h>
#include <test/state/ethash_difficulty.hpp>

using namespace evmone::state;

struct DifficultyTest
{
evmc_revision rev;
const char* name;
int64_t block_number;
int64_t difficulty;
int64_t timestamp;
int64_t parent_difficulty;
int64_t parent_timestamp;
bool parent_has_ommers;
};

/// Example difficulty tests from
/// https://github.com/ethereum/tests/blob/develop/DifficultyTests.
static constexpr DifficultyTest tests[] = {
{
EVMC_BYZANTIUM,
"DifficultyTest1",
0x0186a0,
0x69702c7f2c9fad14,
0x28d214819,
0x6963001f28ba95c2,
0x28d214818,
false,
},
{
EVMC_BYZANTIUM,
"DifficultyTest1038",
0x0dbba0,
0x79f2cbb8c97579b0,
0x72e440371,
0x79f2cbb8c97579b0,
0x72e44035d,
true,
},
{
EVMC_BERLIN,
"DifficultyTest1",
0x186a0,
0x56c67d1e106966c3,
0x63ed689e9,
0x56bba5a95b3dff04,
0x63ed689e8,
false,
},
{
EVMC_BERLIN,
"DifficultyTest1040",
0x10c8e0,
0x68f7512123928555,
0x617ec9fcc,
0x68f7512123928555,
0x617ec9fb8,
true,
},
};

TEST(state_difficulty, tests)
{
for (const auto& t : tests)
{
const auto difficulty = calculate_difficulty(t.parent_difficulty, t.parent_has_ommers,
t.parent_timestamp, t.timestamp, t.block_number, t.rev);
EXPECT_EQ(difficulty, t.difficulty) << t.rev << "/" << t.name;
}
}

0 comments on commit 7d0e0ee

Please sign in to comment.