Skip to content

Commit 6bc72b0

Browse files
committed
Add comment and move isPostMerge
1 parent de11e85 commit 6bc72b0

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

consensus/beacon/consensus.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,6 @@ func New(ethone consensus.Engine) *Beacon {
7171
return &Beacon{ethone: ethone}
7272
}
7373

74-
// isPostMerge reports whether the given block number is assumed to be post-merge.
75-
// Here we check the MergeNetsplitBlock to allow configuring networks with a PoW or
76-
// PoA chain for unit testing purposes.
77-
func isPostMerge(config *params.ChainConfig, blockNum uint64, timestamp uint64) bool {
78-
mergedAtGenesis := config.TerminalTotalDifficulty != nil && config.TerminalTotalDifficulty.Sign() == 0
79-
return mergedAtGenesis ||
80-
config.MergeNetsplitBlock != nil && blockNum >= config.MergeNetsplitBlock.Uint64() ||
81-
config.ShanghaiTime != nil && timestamp >= *config.ShanghaiTime
82-
}
83-
8474
// Author implements consensus.Engine, returning the verified author of the block.
8575
func (beacon *Beacon) Author(header *types.Header) (common.Address, error) {
8676
if !beacon.IsPoSHeader(header) {
@@ -328,7 +318,7 @@ func (beacon *Beacon) verifyHeaders(chain consensus.ChainHeaderReader, headers [
328318
// Prepare implements consensus.Engine, initializing the difficulty field of a
329319
// header to conform to the beacon protocol. The changes are done inline.
330320
func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
331-
if !isPostMerge(chain.Config(), header.Number.Uint64(), header.Time) {
321+
if !chain.Config().IsPostMerge(header.Number.Uint64(), header.Time) {
332322
return beacon.ethone.Prepare(chain, header)
333323
}
334324
header.Difficulty = beaconDifficulty
@@ -442,7 +432,7 @@ func (beacon *Beacon) SealHash(header *types.Header) common.Hash {
442432
// the difficulty that a new block should have when created at time
443433
// given the parent block's time and difficulty.
444434
func (beacon *Beacon) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
445-
if !isPostMerge(chain.Config(), parent.Number.Uint64()+1, time) {
435+
if !chain.Config().IsPostMerge(parent.Number.Uint64()+1, time) {
446436
return beacon.ethone.CalcDifficulty(chain, time, parent)
447437
}
448438
return beaconDifficulty

internal/ethapi/simulate.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,6 @@ func (sim *simulator) sanitizeChain(blocks []simBlock) ([]simBlock, error) {
458458
return res, nil
459459
}
460460

461-
func isPostMerge(config *params.ChainConfig, blockNum uint64, timestamp uint64) bool {
462-
mergedAtGenesis := config.TerminalTotalDifficulty != nil && config.TerminalTotalDifficulty.Sign() == 0
463-
return mergedAtGenesis ||
464-
config.MergeNetsplitBlock != nil && blockNum >= config.MergeNetsplitBlock.Uint64() ||
465-
config.ShanghaiTime != nil && timestamp >= *config.ShanghaiTime
466-
}
467-
468461
// makeHeaders makes header object with preliminary fields based on a simulated block.
469462
// Some fields have to be filled post-execution.
470463
// It assumes blocks are in order and numbers have been validated.
@@ -493,8 +486,10 @@ func (sim *simulator) makeHeaders(blocks []simBlock) ([]*types.Header, error) {
493486
parentBeaconRoot = overrides.BeaconRoot
494487
}
495488
}
489+
// Set difficulty to zero if the given block is post-merge. Without this, all post-merge hardforks would remain inactive.
490+
// 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.
496491
difficulty := header.Difficulty
497-
if isPostMerge(sim.chainConfig, number.Uint64(), timestamp) {
492+
if sim.chainConfig.IsPostMerge(number.Uint64(), timestamp) {
498493
difficulty = big.NewInt(0)
499494
}
500495
header = overrides.MakeHeader(&types.Header{

params/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,16 @@ func (c *ChainConfig) IsTerminalPoWBlock(parentTotalDiff *big.Int, totalDiff *bi
654654
return parentTotalDiff.Cmp(c.TerminalTotalDifficulty) < 0 && totalDiff.Cmp(c.TerminalTotalDifficulty) >= 0
655655
}
656656

657+
// IsPostMerge reports whether the given block number is assumed to be post-merge.
658+
// Here we check the MergeNetsplitBlock to allow configuring networks with a PoW or
659+
// PoA chain for unit testing purposes.
660+
func (c *ChainConfig) IsPostMerge(blockNum uint64, timestamp uint64) bool {
661+
mergedAtGenesis := c.TerminalTotalDifficulty != nil && c.TerminalTotalDifficulty.Sign() == 0
662+
return mergedAtGenesis ||
663+
c.MergeNetsplitBlock != nil && blockNum >= c.MergeNetsplitBlock.Uint64() ||
664+
c.ShanghaiTime != nil && timestamp >= *c.ShanghaiTime
665+
}
666+
657667
// IsShanghai returns whether time is either equal to the Shanghai fork time or greater.
658668
func (c *ChainConfig) IsShanghai(num *big.Int, time uint64) bool {
659669
return c.IsLondon(num) && isTimestampForked(c.ShanghaiTime, time)

0 commit comments

Comments
 (0)