Skip to content

Commit

Permalink
Use isEuclid, add json tag to Requested, make go idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
ranchalp committed Jan 23, 2025
1 parent ec2e3d0 commit 20185f2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package consensus_wrapper
package wrapper

import (
"github.com/scroll-tech/go-ethereum/common"
Expand All @@ -14,7 +14,7 @@ import (
// calls to either Clique or SystemContract consensus based on block height.
type UpgradableEngine struct {
// forkBlock is the block number at which the switchover to SystemContract occurs.
forkBlock *big.Int
isUpgraded func(uint64) bool

// clique is the original Clique consensus engine.
clique consensus.Engine
Expand All @@ -24,22 +24,17 @@ type UpgradableEngine struct {
}

// NewUpgradableEngine constructs a new upgradable consensus middleware.
func NewUpgradableEngine(forkBlock *big.Int, clique consensus.Engine, system consensus.Engine) *UpgradableEngine {
func NewUpgradableEngine(isUpgraded func(uint64) bool, clique consensus.Engine, system consensus.Engine) *UpgradableEngine {
return &UpgradableEngine{
forkBlock: forkBlock,
clique: clique,
system: system,
isUpgraded: isUpgraded,
clique: clique,
system: system,
}
}

// chooseEngine returns the appropriate consensus engine based on the header's number.
func (ue *UpgradableEngine) chooseEngine(header *types.Header) consensus.Engine {
if header.Number == nil {
// Fallback: treat as pre-fork if header number is unknown.
return ue.clique
}
// If block number is >= forkBlock, use the new SystemContract consensus, else use Clique.
if header.Number.Cmp(ue.forkBlock) >= 0 {
if ue.isUpgraded(header.Time) {
return ue.system
}
return ue.clique
Expand Down Expand Up @@ -177,7 +172,7 @@ func (ue *UpgradableEngine) APIs(chain consensus.ChainHeaderReader) []rpc.API {
}

// Choose engine based on whether the chain head is before or after the fork block.
if head.Number.Cmp(ue.forkBlock) >= 0 {
if ue.isUpgraded(head.Time) {
return ue.system.APIs(chain)
}
return ue.clique.APIs(chain)
Expand Down
2 changes: 1 addition & 1 deletion core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type Header struct {
//Hacky: used internally to mark the header as requested by the downloader at the deliver queue
// The tag "rlp:\"-\"" ensures it's excluded from the RLP encoding (and thus, from the hash computation).

Requested bool `rlp:"-"`
Requested bool `json:"-" rlp:"-"`
}

// field type overrides for gencodec
Expand Down
15 changes: 5 additions & 10 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package ethconfig

import (
"context"
"github.com/scroll-tech/go-ethereum/consensus/consensus_wrapper"
"github.com/scroll-tech/go-ethereum/consensus/wrapper"
"math/big"
"os"
"os/user"
Expand Down Expand Up @@ -240,20 +240,15 @@ func CreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, co
// Create the SystemContract engine.
sysEngine := system_contract.New(context.Background(), chainConfig.SystemContract, l1Client)

// Determine the fork block at which the switch occurs.
var forkBlock *big.Int
if chainConfig.EuclidBlock != nil {
forkBlock = chainConfig.EuclidBlock
}
return consensus_wrapper.NewUpgradableEngine(forkBlock, cliqueEngine, sysEngine)
return wrapper.NewUpgradableEngine(chainConfig.IsEuclid, cliqueEngine, sysEngine)
}

// Case 2: Only the Clique engine is defined.
//// Case 2: Only the Clique engine is defined.
if chainConfig.Clique != nil {
return clique.New(chainConfig.Clique, db)
}

// Case 3: Only the SystemContract engine is defined.
//
//// Case 3: Only the SystemContract engine is defined.
if chainConfig.SystemContract != nil {
return system_contract.New(context.Background(), chainConfig.SystemContract, l1Client)
}
Expand Down
7 changes: 6 additions & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ type ChainConfig struct {
CurieBlock *big.Int `json:"curieBlock,omitempty"` // Curie switch block (nil = no fork, 0 = already on curie)
DarwinTime *uint64 `json:"darwinTime,omitempty"` // Darwin switch time (nil = no fork, 0 = already on darwin)
DarwinV2Time *uint64 `json:"darwinv2Time,omitempty"` // DarwinV2 switch time (nil = no fork, 0 = already on darwinv2)
EuclidBlock *big.Int `json:"euclidBlock,omitempty"` // Euclid switch block (nil = no fork, 0 = already on euclid)
EuclidTime *uint64 `json:"euclidTime,omitempty"` // Euclid switch time (nil = no fork, 0 = already on euclid)

// TerminalTotalDifficulty is the amount of total difficulty reached by
// the network that triggers the consensus upgrade.
Expand Down Expand Up @@ -905,6 +905,11 @@ func (c *ChainConfig) IsDarwinV2(now uint64) bool {
return isForkedTime(now, c.DarwinV2Time)
}

// IsEuclid returns whether num is either equal to the Darwin fork block or greater.
func (c *ChainConfig) IsEuclid(now uint64) bool {
return isForkedTime(now, c.EuclidTime)
}

// IsTerminalPoWBlock returns whether the given block is the last block of PoW stage.
func (c *ChainConfig) IsTerminalPoWBlock(parentTotalDiff *big.Int, totalDiff *big.Int) bool {
if c.TerminalTotalDifficulty == nil {
Expand Down

0 comments on commit 20185f2

Please sign in to comment.