-
Notifications
You must be signed in to change notification settings - Fork 331
Blockchain tests support: Introduce difficulty calculation in t8n #682
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // evmone: Fast Ethereum Virtual Machine implementation | ||
| // Copyright 2023 The evmone Authors. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include "ethash_difficulty.hpp" | ||
| #include <algorithm> | ||
| #include <cassert> | ||
|
|
||
| namespace evmone::state | ||
| { | ||
| namespace | ||
| { | ||
| int64_t get_bomb_delay(evmc_revision rev) noexcept | ||
| { | ||
| switch (rev) | ||
| { | ||
| default: | ||
| return 0; | ||
| case EVMC_BYZANTIUM: | ||
| return 3'000'000; | ||
| case EVMC_CONSTANTINOPLE: | ||
| case EVMC_PETERSBURG: | ||
| case EVMC_ISTANBUL: | ||
| return 5'000'000; | ||
| case EVMC_BERLIN: | ||
| return 9'000'000; | ||
| case EVMC_LONDON: | ||
| return 9'700'000; | ||
| } | ||
| } | ||
| } // namespace | ||
|
|
||
| int64_t calculate_difficulty(int64_t parent_difficulty, bool parent_has_ommers, | ||
| 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. | ||
|
|
||
| // TODO: Implement for older revisions | ||
| if (rev < EVMC_BYZANTIUM) | ||
| return 0x020000; | ||
|
|
||
| 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; | ||
| assert(p < 63); | ||
| const auto epsilon = p < 0 ? 0 : int64_t{1} << p; | ||
| const auto y = parent_has_ommers ? 2 : 1; | ||
|
|
||
| const auto timestamp_diff = current_timestamp - parent_timestamp; | ||
| 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); | ||
| } | ||
| } // namespace evmone::state |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // evmone: Fast Ethereum Virtual Machine implementation | ||
| // Copyright 2023 The evmone Authors. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| #pragma once | ||
|
|
||
| #include <evmc/evmc.h> | ||
|
|
||
| namespace evmone::state | ||
| { | ||
| int64_t calculate_difficulty(int64_t parent_difficulty, bool parent_has_ommers, | ||
| int64_t parent_timestamp, int64_t current_timestamp, int64_t block_number, | ||
| evmc_revision rev) noexcept; | ||
| } // namespace evmone::state |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 // NOLINT(clang-analyzer-optin.performance.Padding) | ||
| { | ||
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.