-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
state: Introduce difficulty calculation in t8n
- Loading branch information
Showing
8 changed files
with
146 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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,64 @@ | ||
#include "ethash_difficulty.hpp" | ||
#include "hash_utils.hpp" | ||
#include "mpt_hash.hpp" | ||
#include <evmc/evmc.h> | ||
#include <intx/intx.hpp> | ||
#include <stdexcept> | ||
|
||
using namespace evmone; | ||
|
||
namespace evmone::state | ||
{ | ||
namespace | ||
{ | ||
int64_t get_bomb_delay(evmc_revision rev) | ||
{ | ||
switch (rev) | ||
{ | ||
case EVMC_BYZANTIUM: | ||
return 3000000; | ||
case EVMC_CONSTANTINOPLE: | ||
case EVMC_PETERSBURG: | ||
case EVMC_ISTANBUL: | ||
return 5000000; | ||
case EVMC_BERLIN: | ||
return 9000000; | ||
case EVMC_LONDON: | ||
return 9700000; | ||
default: | ||
throw std::runtime_error("get_bomb_delay: Wrong rev"); | ||
} | ||
} | ||
} // namespace | ||
|
||
int64_t calc_difficulty(const int64_t& parent_difficulty, const hash256& parent_uncle_hash, | ||
const int64_t& parent_timestamp, const int64_t& current_timestamp, const int64_t& block_num, | ||
evmc_revision rev) | ||
{ | ||
if (rev >= EVMC_PARIS) | ||
return 0; | ||
|
||
// TODO: Implement for older revisions | ||
if (rev < EVMC_BYZANTIUM) | ||
return 0x020000; | ||
|
||
static constexpr auto min_difficulty = int64_t{1} << 17; | ||
|
||
const auto kappa = get_bomb_delay(rev); | ||
|
||
const auto H_i_prime = kappa >= block_num ? 0 : block_num - kappa; | ||
|
||
const auto p = (H_i_prime / 100000) - 2; | ||
assert(p < 63); | ||
|
||
const auto epsilon = p < 0 ? 0 : int64_t{1} << p; | ||
|
||
const auto y = parent_uncle_hash != state::EmptyListHash ? 2 : 1; | ||
|
||
const auto sigma_2 = std::max(y - (current_timestamp - parent_timestamp) / 9, int64_t{-99}); | ||
|
||
const int64_t x = parent_difficulty / 2048; | ||
|
||
return std::max(min_difficulty, (int64_t)parent_difficulty + x * sigma_2 + epsilon); | ||
} | ||
} // namespace evmone::state |
This file contains 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,14 @@ | ||
// evmone: Fast Ethereum Virtual Machine implementation | ||
// Copyright 2023 The evmone Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#pragma once | ||
|
||
#include "hash_utils.hpp" | ||
#include <evmc/evmc.h> | ||
|
||
namespace evmone::state | ||
{ | ||
int64_t calc_difficulty(const int64_t& parent_difficulty, const hash256& parent_uncle_hash, | ||
const int64_t& parent_timestamp, const int64_t& current_timestamp, const int64_t& block_num, | ||
evmc_revision rev); | ||
} // namespace evmone::state |
This file contains 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 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 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 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 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