Skip to content

Commit 20185f2

Browse files
committed
Use isEuclid, add json tag to Requested, make go idiomatic
1 parent ec2e3d0 commit 20185f2

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

consensus/consensus_wrapper/consensus.go renamed to consensus/wrapper/consensus.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package consensus_wrapper
1+
package wrapper
22

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

1919
// clique is the original Clique consensus engine.
2020
clique consensus.Engine
@@ -24,22 +24,17 @@ type UpgradableEngine struct {
2424
}
2525

2626
// NewUpgradableEngine constructs a new upgradable consensus middleware.
27-
func NewUpgradableEngine(forkBlock *big.Int, clique consensus.Engine, system consensus.Engine) *UpgradableEngine {
27+
func NewUpgradableEngine(isUpgraded func(uint64) bool, clique consensus.Engine, system consensus.Engine) *UpgradableEngine {
2828
return &UpgradableEngine{
29-
forkBlock: forkBlock,
30-
clique: clique,
31-
system: system,
29+
isUpgraded: isUpgraded,
30+
clique: clique,
31+
system: system,
3232
}
3333
}
3434

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

179174
// Choose engine based on whether the chain head is before or after the fork block.
180-
if head.Number.Cmp(ue.forkBlock) >= 0 {
175+
if ue.isUpgraded(head.Time) {
181176
return ue.system.APIs(chain)
182177
}
183178
return ue.clique.APIs(chain)

core/types/block.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type Header struct {
105105
//Hacky: used internally to mark the header as requested by the downloader at the deliver queue
106106
// The tag "rlp:\"-\"" ensures it's excluded from the RLP encoding (and thus, from the hash computation).
107107

108-
Requested bool `rlp:"-"`
108+
Requested bool `json:"-" rlp:"-"`
109109
}
110110

111111
// field type overrides for gencodec

eth/ethconfig/config.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package ethconfig
1919

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

243-
// Determine the fork block at which the switch occurs.
244-
var forkBlock *big.Int
245-
if chainConfig.EuclidBlock != nil {
246-
forkBlock = chainConfig.EuclidBlock
247-
}
248-
return consensus_wrapper.NewUpgradableEngine(forkBlock, cliqueEngine, sysEngine)
243+
return wrapper.NewUpgradableEngine(chainConfig.IsEuclid, cliqueEngine, sysEngine)
249244
}
250245

251-
// Case 2: Only the Clique engine is defined.
246+
//// Case 2: Only the Clique engine is defined.
252247
if chainConfig.Clique != nil {
253248
return clique.New(chainConfig.Clique, db)
254249
}
255-
256-
// Case 3: Only the SystemContract engine is defined.
250+
//
251+
//// Case 3: Only the SystemContract engine is defined.
257252
if chainConfig.SystemContract != nil {
258253
return system_contract.New(context.Background(), chainConfig.SystemContract, l1Client)
259254
}

params/config.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ type ChainConfig struct {
633633
CurieBlock *big.Int `json:"curieBlock,omitempty"` // Curie switch block (nil = no fork, 0 = already on curie)
634634
DarwinTime *uint64 `json:"darwinTime,omitempty"` // Darwin switch time (nil = no fork, 0 = already on darwin)
635635
DarwinV2Time *uint64 `json:"darwinv2Time,omitempty"` // DarwinV2 switch time (nil = no fork, 0 = already on darwinv2)
636-
EuclidBlock *big.Int `json:"euclidBlock,omitempty"` // Euclid switch block (nil = no fork, 0 = already on euclid)
636+
EuclidTime *uint64 `json:"euclidTime,omitempty"` // Euclid switch time (nil = no fork, 0 = already on euclid)
637637

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

908+
// IsEuclid returns whether num is either equal to the Darwin fork block or greater.
909+
func (c *ChainConfig) IsEuclid(now uint64) bool {
910+
return isForkedTime(now, c.EuclidTime)
911+
}
912+
908913
// IsTerminalPoWBlock returns whether the given block is the last block of PoW stage.
909914
func (c *ChainConfig) IsTerminalPoWBlock(parentTotalDiff *big.Int, totalDiff *big.Int) bool {
910915
if c.TerminalTotalDifficulty == nil {

0 commit comments

Comments
 (0)