Add comment and move isPostMerge

This commit is contained in:
Nikita Meshcheriakov 2025-09-16 15:58:13 +03:00
parent de11e85160
commit 6bc72b0dc5
3 changed files with 15 additions and 20 deletions

View file

@ -71,16 +71,6 @@ func New(ethone consensus.Engine) *Beacon {
return &Beacon{ethone: ethone} return &Beacon{ethone: ethone}
} }
// isPostMerge reports whether the given block number is assumed to be post-merge.
// Here we check the MergeNetsplitBlock to allow configuring networks with a PoW or
// PoA chain for unit testing purposes.
func isPostMerge(config *params.ChainConfig, blockNum uint64, timestamp uint64) bool {
mergedAtGenesis := config.TerminalTotalDifficulty != nil && config.TerminalTotalDifficulty.Sign() == 0
return mergedAtGenesis ||
config.MergeNetsplitBlock != nil && blockNum >= config.MergeNetsplitBlock.Uint64() ||
config.ShanghaiTime != nil && timestamp >= *config.ShanghaiTime
}
// Author implements consensus.Engine, returning the verified author of the block. // Author implements consensus.Engine, returning the verified author of the block.
func (beacon *Beacon) Author(header *types.Header) (common.Address, error) { func (beacon *Beacon) Author(header *types.Header) (common.Address, error) {
if !beacon.IsPoSHeader(header) { if !beacon.IsPoSHeader(header) {
@ -328,7 +318,7 @@ func (beacon *Beacon) verifyHeaders(chain consensus.ChainHeaderReader, headers [
// Prepare implements consensus.Engine, initializing the difficulty field of a // Prepare implements consensus.Engine, initializing the difficulty field of a
// header to conform to the beacon protocol. The changes are done inline. // header to conform to the beacon protocol. The changes are done inline.
func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error { func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
if !isPostMerge(chain.Config(), header.Number.Uint64(), header.Time) { if !chain.Config().IsPostMerge(header.Number.Uint64(), header.Time) {
return beacon.ethone.Prepare(chain, header) return beacon.ethone.Prepare(chain, header)
} }
header.Difficulty = beaconDifficulty header.Difficulty = beaconDifficulty
@ -442,7 +432,7 @@ func (beacon *Beacon) SealHash(header *types.Header) common.Hash {
// the difficulty that a new block should have when created at time // the difficulty that a new block should have when created at time
// given the parent block's time and difficulty. // given the parent block's time and difficulty.
func (beacon *Beacon) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int { func (beacon *Beacon) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
if !isPostMerge(chain.Config(), parent.Number.Uint64()+1, time) { if !chain.Config().IsPostMerge(parent.Number.Uint64()+1, time) {
return beacon.ethone.CalcDifficulty(chain, time, parent) return beacon.ethone.CalcDifficulty(chain, time, parent)
} }
return beaconDifficulty return beaconDifficulty

View file

@ -458,13 +458,6 @@ func (sim *simulator) sanitizeChain(blocks []simBlock) ([]simBlock, error) {
return res, nil return res, nil
} }
func isPostMerge(config *params.ChainConfig, blockNum uint64, timestamp uint64) bool {
mergedAtGenesis := config.TerminalTotalDifficulty != nil && config.TerminalTotalDifficulty.Sign() == 0
return mergedAtGenesis ||
config.MergeNetsplitBlock != nil && blockNum >= config.MergeNetsplitBlock.Uint64() ||
config.ShanghaiTime != nil && timestamp >= *config.ShanghaiTime
}
// makeHeaders makes header object with preliminary fields based on a simulated block. // makeHeaders makes header object with preliminary fields based on a simulated block.
// Some fields have to be filled post-execution. // Some fields have to be filled post-execution.
// It assumes blocks are in order and numbers have been validated. // It assumes blocks are in order and numbers have been validated.
@ -493,8 +486,10 @@ func (sim *simulator) makeHeaders(blocks []simBlock) ([]*types.Header, error) {
parentBeaconRoot = overrides.BeaconRoot parentBeaconRoot = overrides.BeaconRoot
} }
} }
// Set difficulty to zero if the given block is post-merge. Without this, all post-merge hardforks would remain inactive.
// For example, calling eth_simulateV1(..., blockParameter: 0x0) on hoodi network will cause all blocks to have a difficulty of 1 and be treated as pre-merge.
difficulty := header.Difficulty difficulty := header.Difficulty
if isPostMerge(sim.chainConfig, number.Uint64(), timestamp) { if sim.chainConfig.IsPostMerge(number.Uint64(), timestamp) {
difficulty = big.NewInt(0) difficulty = big.NewInt(0)
} }
header = overrides.MakeHeader(&types.Header{ header = overrides.MakeHeader(&types.Header{

View file

@ -654,6 +654,16 @@ func (c *ChainConfig) IsTerminalPoWBlock(parentTotalDiff *big.Int, totalDiff *bi
return parentTotalDiff.Cmp(c.TerminalTotalDifficulty) < 0 && totalDiff.Cmp(c.TerminalTotalDifficulty) >= 0 return parentTotalDiff.Cmp(c.TerminalTotalDifficulty) < 0 && totalDiff.Cmp(c.TerminalTotalDifficulty) >= 0
} }
// IsPostMerge reports whether the given block number is assumed to be post-merge.
// Here we check the MergeNetsplitBlock to allow configuring networks with a PoW or
// PoA chain for unit testing purposes.
func (c *ChainConfig) IsPostMerge(blockNum uint64, timestamp uint64) bool {
mergedAtGenesis := c.TerminalTotalDifficulty != nil && c.TerminalTotalDifficulty.Sign() == 0
return mergedAtGenesis ||
c.MergeNetsplitBlock != nil && blockNum >= c.MergeNetsplitBlock.Uint64() ||
c.ShanghaiTime != nil && timestamp >= *c.ShanghaiTime
}
// IsShanghai returns whether time is either equal to the Shanghai fork time or greater. // IsShanghai returns whether time is either equal to the Shanghai fork time or greater.
func (c *ChainConfig) IsShanghai(num *big.Int, time uint64) bool { func (c *ChainConfig) IsShanghai(num *big.Int, time uint64) bool {
return c.IsLondon(num) && isTimestampForked(c.ShanghaiTime, time) return c.IsLondon(num) && isTimestampForked(c.ShanghaiTime, time)