From ed9f36b54f0ec2a302a819a996cd1abb07296c07 Mon Sep 17 00:00:00 2001 From: Delweng Date: Tue, 18 Jun 2024 01:38:54 +0800 Subject: [PATCH] feat: calc block base reward use block number only (#8876) Signed-off-by: jsvisa Co-authored-by: Matthias Seitz --- crates/chainspec/src/spec.rs | 7 +++++++ crates/consensus/common/src/calc.rs | 24 +++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/crates/chainspec/src/spec.rs b/crates/chainspec/src/spec.rs index 2462da93a..aa9e90f8c 100644 --- a/crates/chainspec/src/spec.rs +++ b/crates/chainspec/src/spec.rs @@ -853,6 +853,13 @@ impl ChainSpec { self.fork(Hardfork::Homestead).active_at_block(block_number) } + /// The Paris hardfork (merge) is activated via ttd. If we have knowledge of the block, this + /// function will return true if the block number is greater than or equal to the Paris + /// (merge) block. + pub fn is_paris_active_at_block(&self, block_number: u64) -> Option { + self.paris_block_and_final_difficulty.map(|(paris_block, _)| block_number >= paris_block) + } + /// Convenience method to check if [`Hardfork::Bedrock`] is active at a given block number. #[cfg(feature = "optimism")] #[inline] diff --git a/crates/consensus/common/src/calc.rs b/crates/consensus/common/src/calc.rs index 52206bbfd..27320700b 100644 --- a/crates/consensus/common/src/calc.rs +++ b/crates/consensus/common/src/calc.rs @@ -1,5 +1,6 @@ use reth_chainspec::{Chain, ChainSpec, Hardfork}; use reth_primitives::{constants::ETH_TO_WEI, BlockNumber, U256}; + /// Calculates the base block reward. /// /// The base block reward is defined as: @@ -25,16 +26,25 @@ pub fn base_block_reward( block_difficulty: U256, total_difficulty: U256, ) -> Option { - if chain_spec.chain == Chain::goerli() || - chain_spec.fork(Hardfork::Paris).active_at_ttd(total_difficulty, block_difficulty) + if chain_spec.fork(Hardfork::Paris).active_at_ttd(total_difficulty, block_difficulty) || + chain_spec.chain == Chain::goerli() { None - } else if chain_spec.fork(Hardfork::Constantinople).active_at_block(block_number) { - Some(ETH_TO_WEI * 2) - } else if chain_spec.fork(Hardfork::Byzantium).active_at_block(block_number) { - Some(ETH_TO_WEI * 3) } else { - Some(ETH_TO_WEI * 5) + Some(base_block_reward_pre_merge(chain_spec, block_number)) + } +} + +/// 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: &ChainSpec, block_number: BlockNumber) -> u128 { + if chain_spec.fork(Hardfork::Constantinople).active_at_block(block_number) { + ETH_TO_WEI * 2 + } else if chain_spec.fork(Hardfork::Byzantium).active_at_block(block_number) { + ETH_TO_WEI * 3 + } else { + ETH_TO_WEI * 5 } }