Skip to content

Commit 72be9a5

Browse files
committed
state: Implement difficulty calculation for pre Byzantium rev
1 parent 781875d commit 72be9a5

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

test/state/ethash_difficulty.cpp

+25-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@ int64_t get_bomb_delay(evmc_revision rev) noexcept
2828
return 9'700'000;
2929
}
3030
}
31+
32+
int64_t calculate_difficulty_pre_byzantium(int64_t parent_difficulty, int64_t parent_timestamp,
33+
int64_t current_timestamp, int64_t block_number, evmc_revision rev)
34+
{
35+
// According to https://eips.ethereum.org/EIPS/eip-2
36+
const auto period_count = block_number / 100'000;
37+
const auto offset = parent_difficulty / 2048;
38+
39+
auto diff = parent_difficulty;
40+
41+
if (rev < EVMC_HOMESTEAD)
42+
diff += offset * (current_timestamp - parent_timestamp < 13 ? 1 : -1);
43+
else
44+
diff += offset * std::max(1 - (current_timestamp - parent_timestamp) / 10, int64_t{-99});
45+
46+
if (period_count > 2)
47+
diff += 2 << (block_number / 100'000 - 3);
48+
else if (period_count == 2)
49+
diff += 1;
50+
51+
return diff;
52+
}
53+
3154
} // namespace
3255

3356
int64_t calculate_difficulty(int64_t parent_difficulty, bool parent_has_ommers,
@@ -39,9 +62,9 @@ int64_t calculate_difficulty(int64_t parent_difficulty, bool parent_has_ommers,
3962
if (rev >= EVMC_PARIS)
4063
return 0; // No difficulty after the Merge.
4164

42-
// TODO: Implement for older revisions
4365
if (rev < EVMC_BYZANTIUM)
44-
return 0x020000;
66+
return calculate_difficulty_pre_byzantium(
67+
parent_difficulty, parent_timestamp, current_timestamp, block_number, rev);
4568

4669
static constexpr auto min_difficulty = int64_t{1} << 17;
4770

test/unittests/state_difficulty_test.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,66 @@ struct DifficultyTest // NOLINT(clang-analyzer-optin.performance.Padding)
2222
/// Example difficulty tests from
2323
/// https://github.com/ethereum/tests/blob/develop/DifficultyTests.
2424
static constexpr DifficultyTest tests[] = {
25+
{
26+
EVMC_FRONTIER,
27+
"DifficultyTest1",
28+
0x0186a0,
29+
0x6a8d5758858f3fb6,
30+
0x75311a08b,
31+
0x6a8007579a9bec39,
32+
0x75311a08a,
33+
false,
34+
},
35+
{
36+
EVMC_FRONTIER,
37+
"DifficultyTest1040",
38+
0x10c8e0,
39+
0x3857fe2e57047922,
40+
0x3558049dc,
41+
0x385f0a0f98f79614,
42+
0x3558049c8,
43+
true,
44+
},
45+
{
46+
EVMC_FRONTIER,
47+
"DifficultyTest1031",
48+
0x030d40,
49+
0x7b435a6e9d83b81e,
50+
0x2442b7295,
51+
0x7b52c4c7366a856d,
52+
0x2442b7281,
53+
true,
54+
},
55+
{
56+
EVMC_HOMESTEAD,
57+
"DifficultyTest1",
58+
0x0186a0,
59+
0x6ab7534e3bcfec27,
60+
0x68450ae0d,
61+
0x6aa9fe0e7a00ac12,
62+
0x68450ae0c,
63+
false,
64+
},
65+
{
66+
EVMC_HOMESTEAD,
67+
"DifficultyTest1040",
68+
0x10c8e0,
69+
0x024bf6f60ecc847d,
70+
0x7b389fe96,
71+
0x024c407e1e905487,
72+
0x7b389fe82,
73+
true,
74+
},
75+
{
76+
EVMC_HOMESTEAD,
77+
"DifficultyTest1031",
78+
0x030d40,
79+
0x39699ae4587a0b05,
80+
0x3b907b4f4,
81+
0x3970c8fd78291026,
82+
0x3b907b4e0,
83+
true,
84+
},
2585
{
2686
EVMC_BYZANTIUM,
2787
"DifficultyTest1",

0 commit comments

Comments
 (0)