Skip to content

Commit 8f216bd

Browse files
rodiazetchfast
andcommitted
test: Adjust difficulty if below minimum 0x20000
Co-authored-by: Paweł Bylica <[email protected]>
1 parent ffd7390 commit 8f216bd

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

test/state/ethash_difficulty.cpp

+23-16
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,10 @@ int64_t calculate_difficulty_pre_byzantium(int64_t parent_difficulty, int64_t pa
5151
return diff;
5252
}
5353

54-
} // namespace
55-
56-
int64_t calculate_difficulty(int64_t parent_difficulty, bool parent_has_ommers,
54+
int64_t calculate_difficulty_since_byzantium(int64_t parent_difficulty, bool parent_has_ommers,
5755
int64_t parent_timestamp, int64_t current_timestamp, int64_t block_number,
5856
evmc_revision rev) noexcept
5957
{
60-
// The calculation follows Ethereum Yellow Paper section 4.3.4. "Block Header Validity".
61-
62-
if (rev >= EVMC_PARIS)
63-
return 0; // No difficulty after the Merge.
64-
65-
if (rev < EVMC_BYZANTIUM)
66-
return calculate_difficulty_pre_byzantium(
67-
parent_difficulty, parent_timestamp, current_timestamp, block_number, rev);
68-
69-
static constexpr auto min_difficulty = int64_t{1} << 17;
70-
7158
const auto delay = get_bomb_delay(rev);
7259
const auto fake_block_number = std::max(int64_t{0}, block_number - delay);
7360
const auto p = (fake_block_number / 100'000) - 2;
@@ -79,7 +66,27 @@ int64_t calculate_difficulty(int64_t parent_difficulty, bool parent_has_ommers,
7966
assert(timestamp_diff > 0);
8067
const auto sigma_2 = std::max(y - timestamp_diff / 9, int64_t{-99});
8168
const auto x = parent_difficulty / 2048;
82-
const auto difficulty = parent_difficulty + x * sigma_2 + epsilon;
83-
return std::max(min_difficulty, difficulty);
69+
return parent_difficulty + x * sigma_2 + epsilon;
70+
}
71+
} // namespace
72+
73+
int64_t calculate_difficulty(int64_t parent_difficulty, bool parent_has_ommers,
74+
int64_t parent_timestamp, int64_t current_timestamp, int64_t block_number,
75+
evmc_revision rev) noexcept
76+
{
77+
// The calculation follows Ethereum Yellow Paper section 4.3.4. "Block Header Validity".
78+
static constexpr int64_t MIN_DIFFICULTY = 0x20000;
79+
80+
if (rev >= EVMC_PARIS)
81+
return 0; // No difficulty after the Merge.
82+
83+
const auto difficulty =
84+
(rev < EVMC_BYZANTIUM) ?
85+
calculate_difficulty_pre_byzantium(
86+
parent_difficulty, parent_timestamp, current_timestamp, block_number, rev) :
87+
calculate_difficulty_since_byzantium(parent_difficulty, parent_has_ommers,
88+
parent_timestamp, current_timestamp, block_number, rev);
89+
90+
return std::max(MIN_DIFFICULTY, difficulty);
8491
}
8592
} // namespace evmone::state

test/unittests/state_difficulty_test.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,47 @@ static constexpr DifficultyTest tests[] = {
122122
0x617ec9fb8,
123123
true,
124124
},
125+
{
126+
EVMC_FRONTIER,
127+
"min_difficulty_frontier",
128+
1,
129+
0x20000,
130+
1100,
131+
0x20000,
132+
1000,
133+
false,
134+
},
135+
{
136+
EVMC_HOMESTEAD,
137+
"min_difficulty_homestead",
138+
1,
139+
0x20000,
140+
2000,
141+
0x21999,
142+
1000,
143+
false,
144+
},
145+
{
146+
EVMC_BYZANTIUM,
147+
"min_difficulty_byzantium",
148+
3'000'001,
149+
0x20000,
150+
10060,
151+
0x20139,
152+
10000,
153+
false,
154+
},
155+
{
156+
// Calculated difficulty is exactly 0x20000 without min cap.
157+
EVMC_BYZANTIUM,
158+
"min_difficulty_byzantium2",
159+
3'000'001,
160+
0x20000,
161+
10060,
162+
0x20140,
163+
10000,
164+
false,
165+
},
125166
};
126167

127168
TEST(state_difficulty, tests)

0 commit comments

Comments
 (0)