refactor: reduce Hardforks trait usage (#13728)

This commit is contained in:
Arsenii Kulikov
2025-01-08 17:02:49 +03:00
committed by GitHub
parent 9d51260fbc
commit 8f2ecc44e8
17 changed files with 126 additions and 76 deletions

View File

@ -1,6 +1,6 @@
use alloy_consensus::constants::ETH_TO_WEI;
use alloy_primitives::BlockNumber;
use reth_chainspec::{EthereumHardfork, EthereumHardforks, Hardforks};
use reth_chainspec::EthereumHardforks;
/// Calculates the base block reward.
///
@ -35,10 +35,13 @@ pub fn base_block_reward<ChainSpec: EthereumHardforks>(
/// Calculates the base block reward __before__ the merge (Paris hardfork).
///
/// Caution: The caller must ensure that the block number is before the merge.
pub fn base_block_reward_pre_merge(chain_spec: impl Hardforks, block_number: BlockNumber) -> u128 {
if chain_spec.fork(EthereumHardfork::Constantinople).active_at_block(block_number) {
pub fn base_block_reward_pre_merge(
chain_spec: impl EthereumHardforks,
block_number: BlockNumber,
) -> u128 {
if chain_spec.is_constantinople_active_at_block(block_number) {
ETH_TO_WEI * 2
} else if chain_spec.fork(EthereumHardfork::Byzantium).active_at_block(block_number) {
} else if chain_spec.is_byzantium_active_at_block(block_number) {
ETH_TO_WEI * 3
} else {
ETH_TO_WEI * 5

View File

@ -25,8 +25,7 @@ pub fn validate_header_base_fee<H: BlockHeader, ChainSpec: EthereumHardforks>(
header: &H,
chain_spec: &ChainSpec,
) -> Result<(), ConsensusError> {
if chain_spec.is_fork_active_at_block(EthereumHardfork::London, header.number()) &&
header.base_fee_per_gas().is_none()
if chain_spec.is_london_active_at_block(header.number()) && header.base_fee_per_gas().is_none()
{
return Err(ConsensusError::BaseFeeMissing)
}
@ -253,23 +252,25 @@ pub fn validate_against_parent_eip1559_base_fee<
parent: &H,
chain_spec: &ChainSpec,
) -> Result<(), ConsensusError> {
if chain_spec.fork(EthereumHardfork::London).active_at_block(header.number()) {
if chain_spec.is_london_active_at_block(header.number()) {
let base_fee = header.base_fee_per_gas().ok_or(ConsensusError::BaseFeeMissing)?;
let expected_base_fee =
if chain_spec.fork(EthereumHardfork::London).transitions_at_block(header.number()) {
alloy_eips::eip1559::INITIAL_BASE_FEE
} else {
// This BaseFeeMissing will not happen as previous blocks are checked to have
// them.
let base_fee = parent.base_fee_per_gas().ok_or(ConsensusError::BaseFeeMissing)?;
calc_next_block_base_fee(
parent.gas_used(),
parent.gas_limit(),
base_fee,
chain_spec.base_fee_params_at_timestamp(header.timestamp()),
)
};
let expected_base_fee = if chain_spec
.ethereum_fork_activation(EthereumHardfork::London)
.transitions_at_block(header.number())
{
alloy_eips::eip1559::INITIAL_BASE_FEE
} else {
// This BaseFeeMissing will not happen as previous blocks are checked to have
// them.
let base_fee = parent.base_fee_per_gas().ok_or(ConsensusError::BaseFeeMissing)?;
calc_next_block_base_fee(
parent.gas_used(),
parent.gas_limit(),
base_fee,
chain_spec.base_fee_params_at_timestamp(header.timestamp()),
)
};
if expected_base_fee != base_fee {
return Err(ConsensusError::BaseFeeDiff(GotExpected {
expected: expected_base_fee,