Skip to content

Commit

Permalink
feat: calc block base reward use block number only (paradigmxyz#8876)
Browse files Browse the repository at this point in the history
Signed-off-by: jsvisa <[email protected]>
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
jsvisa and mattsse authored Jun 17, 2024
1 parent 20102ee commit ed9f36b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
7 changes: 7 additions & 0 deletions crates/chainspec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
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]
Expand Down
22 changes: 16 additions & 6 deletions crates/consensus/common/src/calc.rs
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -25,16 +26,25 @@ pub fn base_block_reward(
block_difficulty: U256,
total_difficulty: U256,
) -> Option<u128> {
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 {
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) {
Some(ETH_TO_WEI * 3)
ETH_TO_WEI * 3
} else {
Some(ETH_TO_WEI * 5)
ETH_TO_WEI * 5
}
}

Expand Down

0 comments on commit ed9f36b

Please sign in to comment.