Skip to content
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

feat(ethereum-forks): remove total difficulty for hardfork check #13362

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,15 @@ where
.header_td(&block.parent_hash)?
.ok_or_else(|| BlockchainTreeError::CanonicalChain { block_hash: block.parent_hash })?;

let chain = self.externals.provider_factory.chain_spec().chain();

// Pass the parent total difficulty to short-circuit unnecessary calculations.
if !self
.externals
.provider_factory
.chain_spec()
.fork(EthereumHardfork::Paris)
.active_at_ttd(parent_td, U256::ZERO)
.active_at_ttd(chain)
{
return Err(BlockExecutionError::Validation(BlockValidationError::BlockPreMerge {
hash: block.hash(),
Expand Down Expand Up @@ -1032,21 +1034,12 @@ where
durations_recorder.record_relative(MakeCanonicalAction::FindCanonicalHeader);
if let Some(header) = canonical_header {
info!(target: "blockchain_tree", %block_hash, "Block is already canonical, ignoring.");
// TODO: this could be fetched from the chainspec first
let td =
self.externals.provider_factory.provider()?.header_td(&block_hash)?.ok_or_else(
|| {
CanonicalError::from(BlockValidationError::MissingTotalDifficulty {
hash: block_hash,
})
},
)?;
if !self
.externals
.provider_factory
.chain_spec()
.fork(EthereumHardfork::Paris)
.active_at_ttd(td, U256::ZERO)
.active_at_ttd(chain)
{
return Err(CanonicalError::from(BlockValidationError::BlockPreMerge {
hash: block_hash,
Expand Down
7 changes: 4 additions & 3 deletions crates/chainspec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,19 +1667,20 @@ Post-merge hard forks (timestamp based):
fn check_terminal_ttd() {
let chainspec = ChainSpecBuilder::mainnet().build();

// TODO: fix
// Check that Paris is not active on terminal PoW block #15537393.
let terminal_block_ttd = U256::from(58750003716598352816469_u128);
let terminal_block_difficulty = U256::from(11055787484078698_u128);
assert!(!chainspec
.fork(EthereumHardfork::Paris)
.active_at_ttd(terminal_block_ttd, terminal_block_difficulty));
.active_at_ttd(chain));

// Check that Paris is active on first PoS block #15537394.
let first_pos_block_ttd = U256::from(58750003716598352816469_u128);
let first_pos_difficulty = U256::ZERO;
assert!(chainspec
.fork(EthereumHardfork::Paris)
.active_at_ttd(first_pos_block_ttd, first_pos_difficulty));
.active_at_ttd(chain));
}

#[test]
Expand Down Expand Up @@ -2162,7 +2163,7 @@ Post-merge hard forks (timestamp based):
fn holesky_paris_activated_at_genesis() {
assert!(HOLESKY
.fork(EthereumHardfork::Paris)
.active_at_ttd(HOLESKY.genesis.difficulty, HOLESKY.genesis.difficulty));
.active_at_ttd(HOLESKY));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/common/src/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn base_block_reward(
block_difficulty: U256,
total_difficulty: U256,
) -> Option<u128> {
if chain_spec.fork(EthereumHardfork::Paris).active_at_ttd(total_difficulty, block_difficulty) {
if chain_spec.fork(EthereumHardfork::Paris).active_at_ttd(chain_spec.chain()) {
None
} else {
Some(base_block_reward_pre_merge(chain_spec, block_number))
Expand Down
27 changes: 6 additions & 21 deletions crates/ethereum-forks/src/forkcondition.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::Head;
use crate::{EthereumHardfork, Head};
use alloy_chains::Chain;
use alloy_primitives::{BlockNumber, U256};

/// The condition at which a fork is activated.
Expand All @@ -7,18 +8,6 @@ use alloy_primitives::{BlockNumber, U256};
pub enum ForkCondition {
/// The fork is activated after a certain block.
Block(BlockNumber),
/// The fork is activated after a total difficulty has been reached.
TTD {
/// The block number at which TTD is reached, if it is known.
///
/// This should **NOT** be set unless you want this block advertised as [EIP-2124][eip2124]
/// `FORK_NEXT`. This is currently only the case for Sepolia and Holesky.
///
/// [eip2124]: https://eips.ethereum.org/EIPS/eip-2124
fork_block: Option<BlockNumber>,
/// The total difficulty after which the fork is activated.
total_difficulty: U256,
},
/// The fork is activated after a specific timestamp.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, this has implications on the forkid.

I think the way we should go about this is keeping this variant but requiring that it knows the block number

@Rjected @joshieDo I can't remember what the implications of the fork_block field are because we set this.
I think we can't just set the fork_block field because this messes with the forkid, for example for mainnet we must set this to none,

so to enforce a block number for ttd we should add a new field

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we need to keep one eip-2124 fork block at least. We would want another blocknum field, that is used for checking if the merge is active, but not for the forkid.

Timestamp(u64),
/// The fork is never activated
Expand All @@ -38,8 +27,7 @@ impl ForkCondition {
///
/// For timestamp conditions, this will always return false.
pub const fn active_at_block(&self, current_block: BlockNumber) -> bool {
matches!(self, Self::Block(block)
| Self::TTD { fork_block: Some(block), .. } if current_block >= *block)
matches!(self, Self::Block(block) if current_block >= *block)
}

/// Checks if the given block is the first block that satisfies the fork condition.
Expand All @@ -58,9 +46,8 @@ impl ForkCondition {
/// `58_750_000_000_000_000_000_000`)
///
/// This will return false for any condition that is not TTD-based.
pub fn active_at_ttd(&self, ttd: U256, difficulty: U256) -> bool {
matches!(self, Self::TTD { total_difficulty, .. }
if ttd.saturating_sub(difficulty) >= *total_difficulty)
pub fn active_at_ttd(&self, chain: Chain) -> bool {
matches!(self, Self::Block(block) if block >= EthereumHardfork::Paris.activation_block(chain))
}

/// Checks whether the fork condition is satisfied at the given timestamp.
Expand All @@ -85,9 +72,7 @@ impl ForkCondition {
/// - The condition is satisfied by the timestamp;
/// - or the condition is satisfied by the total difficulty
pub fn active_at_head(&self, head: &Head) -> bool {
self.active_at_block(head.number) ||
self.active_at_timestamp(head.timestamp) ||
self.active_at_ttd(head.total_difficulty, head.difficulty)
self.active_at_block(head.number) || self.active_at_timestamp(head.timestamp)
}

/// Get the total terminal difficulty for this fork condition.
Expand Down
18 changes: 3 additions & 15 deletions crates/ethereum-forks/src/hardfork/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,7 @@ impl EthereumHardfork {
(Self::London, ForkCondition::Block(12965000)),
(Self::ArrowGlacier, ForkCondition::Block(13773000)),
(Self::GrayGlacier, ForkCondition::Block(15050000)),
(
Self::Paris,
ForkCondition::TTD {
fork_block: None,
total_difficulty: uint!(58_750_000_000_000_000_000_000_U256),
},
),
(Self::Paris, ForkCondition::Block(15537394)),
(Self::Shanghai, ForkCondition::Timestamp(1681338455)),
(Self::Cancun, ForkCondition::Timestamp(1710338135)),
]
Expand All @@ -376,13 +370,7 @@ impl EthereumHardfork {
(Self::MuirGlacier, ForkCondition::Block(0)),
(Self::Berlin, ForkCondition::Block(0)),
(Self::London, ForkCondition::Block(0)),
(
Self::Paris,
ForkCondition::TTD {
fork_block: Some(1735371),
total_difficulty: uint!(17_000_000_000_000_000_U256),
},
),
(Self::Paris, ForkCondition::Block(1735371)),
(Self::Shanghai, ForkCondition::Timestamp(1677557088)),
(Self::Cancun, ForkCondition::Timestamp(1706655072)),
]
Expand All @@ -403,7 +391,7 @@ impl EthereumHardfork {
(Self::MuirGlacier, ForkCondition::Block(0)),
(Self::Berlin, ForkCondition::Block(0)),
(Self::London, ForkCondition::Block(0)),
(Self::Paris, ForkCondition::TTD { fork_block: Some(0), total_difficulty: U256::ZERO }),
(Self::Paris, ForkCondition::Block(0)),
(Self::Shanghai, ForkCondition::Timestamp(1696000704)),
(Self::Cancun, ForkCondition::Timestamp(1707305664)),
]
Expand Down
3 changes: 0 additions & 3 deletions crates/ethereum-forks/src/hardforks/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ pub trait EthereumHardforks: Hardforks {
fn is_paris_active_at_block(&self, block_number: u64) -> Option<bool> {
match self.fork(EthereumHardfork::Paris) {
ForkCondition::Block(paris_block) => Some(block_number >= paris_block),
ForkCondition::TTD { fork_block, .. } => {
fork_block.map(|paris_block| block_number >= paris_block)
}
_ => None,
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/ethereum-forks/src/hardforks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ impl ChainHardforks {
pub fn fork_block<H: Hardfork>(&self, fork: H) -> Option<u64> {
match self.fork(fork) {
ForkCondition::Block(block) => Some(block),
ForkCondition::TTD { fork_block, .. } => fork_block,
ForkCondition::Timestamp(ts) => Some(ts),
ForkCondition::Never => None,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ethereum/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ where
let is_post_merge = self
.chain_spec
.fork(EthereumHardfork::Paris)
.active_at_ttd(total_difficulty, header.difficulty());
.active_at_ttd(self.chain_spec.chain());

if is_post_merge {
// TODO: add `is_zero_difficulty` to `alloy_consensus::BlockHeader` trait
Expand Down
Loading