Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Adjust difficulty if below minimum 0x20000 #803

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions test/state/ethash_difficulty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,10 @@
return diff;
}

} // namespace

int64_t calculate_difficulty(int64_t parent_difficulty, bool parent_has_ommers,
int64_t calculate_difficulty_since_byzantium(int64_t parent_difficulty, bool parent_has_ommers,

Check warning on line 54 in test/state/ethash_difficulty.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/ethash_difficulty.cpp#L54

Added line #L54 was not covered by tests
int64_t parent_timestamp, int64_t current_timestamp, int64_t block_number,
evmc_revision rev) noexcept
{
// The calculation follows Ethereum Yellow Paper section 4.3.4. "Block Header Validity".

if (rev >= EVMC_PARIS)
return 0; // No difficulty after the Merge.

if (rev < EVMC_BYZANTIUM)
return calculate_difficulty_pre_byzantium(
parent_difficulty, parent_timestamp, current_timestamp, block_number, rev);

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

const auto delay = get_bomb_delay(rev);
const auto fake_block_number = std::max(int64_t{0}, block_number - delay);
const auto p = (fake_block_number / 100'000) - 2;
Expand All @@ -79,7 +66,27 @@
assert(timestamp_diff > 0);
const auto sigma_2 = std::max(y - timestamp_diff / 9, int64_t{-99});
const auto x = parent_difficulty / 2048;
const auto difficulty = parent_difficulty + x * sigma_2 + epsilon;
return std::max(min_difficulty, difficulty);
return parent_difficulty + x * sigma_2 + epsilon;

Check warning on line 69 in test/state/ethash_difficulty.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/ethash_difficulty.cpp#L69

Added line #L69 was not covered by tests
}
} // namespace

int64_t calculate_difficulty(int64_t parent_difficulty, bool parent_has_ommers,

Check warning on line 73 in test/state/ethash_difficulty.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/ethash_difficulty.cpp#L73

Added line #L73 was not covered by tests
int64_t parent_timestamp, int64_t current_timestamp, int64_t block_number,
evmc_revision rev) noexcept
{
// The calculation follows Ethereum Yellow Paper section 4.3.4. "Block Header Validity".
static constexpr int64_t MIN_DIFFICULTY = 0x20000;

if (rev >= EVMC_PARIS)

Check warning on line 80 in test/state/ethash_difficulty.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/ethash_difficulty.cpp#L80

Added line #L80 was not covered by tests
return 0; // No difficulty after the Merge.

const auto difficulty =
(rev < EVMC_BYZANTIUM) ?
calculate_difficulty_pre_byzantium(

Check warning on line 85 in test/state/ethash_difficulty.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/ethash_difficulty.cpp#L84-L85

Added lines #L84 - L85 were not covered by tests
parent_difficulty, parent_timestamp, current_timestamp, block_number, rev) :
calculate_difficulty_since_byzantium(parent_difficulty, parent_has_ommers,
parent_timestamp, current_timestamp, block_number, rev);

Check warning on line 88 in test/state/ethash_difficulty.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/ethash_difficulty.cpp#L87-L88

Added lines #L87 - L88 were not covered by tests

return std::max(MIN_DIFFICULTY, difficulty);

Check warning on line 90 in test/state/ethash_difficulty.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/ethash_difficulty.cpp#L90

Added line #L90 was not covered by tests
}
} // namespace evmone::state
41 changes: 41 additions & 0 deletions test/unittests/state_difficulty_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,47 @@ static constexpr DifficultyTest tests[] = {
0x617ec9fb8,
true,
},
{
EVMC_FRONTIER,
"min_difficulty_frontier",
1,
0x20000,
1100,
0x20000,
1000,
false,
},
{
EVMC_HOMESTEAD,
"min_difficulty_homestead",
1,
0x20000,
2000,
0x21999,
1000,
false,
},
{
EVMC_BYZANTIUM,
"min_difficulty_byzantium",
3'000'001,
0x20000,
10060,
0x20139,
10000,
false,
},
{
// Calculated difficulty is exactly 0x20000 without min cap.
EVMC_BYZANTIUM,
"min_difficulty_byzantium2",
3'000'001,
0x20000,
10060,
0x20140,
10000,
false,
},
};

TEST(state_difficulty, tests)
Expand Down
Loading