Skip to content

Commit 82626f8

Browse files
committed
eth,params,forks: add forks package with forks enum and LatestActive method on config
1 parent 78eafeb commit 82626f8

File tree

3 files changed

+64
-27
lines changed

3 files changed

+64
-27
lines changed

eth/catalyst/api.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"github.com/ethereum/go-ethereum/log"
3434
"github.com/ethereum/go-ethereum/miner"
3535
"github.com/ethereum/go-ethereum/node"
36-
"github.com/ethereum/go-ethereum/params"
36+
"github.com/ethereum/go-ethereum/params/forks"
3737
"github.com/ethereum/go-ethereum/rpc"
3838
)
3939

@@ -192,7 +192,7 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV2(update engine.ForkchoiceStateV1, pa
192192
if params.BeaconRoot != nil {
193193
return engine.STATUS_INVALID, engine.InvalidParams.With(errors.New("unexpected beacon root"))
194194
}
195-
if !latestActive(api.eth.BlockChain().Config(), "shanghai", params.Timestamp) {
195+
if api.eth.BlockChain().Config().LatestFork(params.Timestamp) != forks.Shanghai {
196196
return engine.STATUS_INVALID, engine.UnsupportedFork.With(errors.New("forkchoiceUpdatedV2 must only be called for shanghai payloads"))
197197
}
198198
}
@@ -208,8 +208,8 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV3(update engine.ForkchoiceStateV1, pa
208208
if params.BeaconRoot == nil {
209209
return engine.STATUS_INVALID, engine.InvalidParams.With(errors.New("missing beacon root"))
210210
}
211-
if !latestActive(api.eth.BlockChain().Config(), "cancun", params.Timestamp) {
212-
return engine.STATUS_INVALID, engine.UnsupportedFork.With(errors.New("forkchoiceUpdatedV3 must only be called for shanghai payloads"))
211+
if api.eth.BlockChain().Config().LatestFork(params.Timestamp) != forks.Cancun {
212+
return engine.STATUS_INVALID, engine.UnsupportedFork.With(errors.New("forkchoiceUpdatedV3 must only be called for cancun payloads"))
213213
}
214214
}
215215
return api.forkchoiceUpdated(update, params)
@@ -445,7 +445,7 @@ func (api *ConsensusAPI) NewPayloadV1(params engine.ExecutableData) (engine.Payl
445445

446446
// NewPayloadV2 creates an Eth1 block, inserts it in the chain, and returns the status of the chain.
447447
func (api *ConsensusAPI) NewPayloadV2(params engine.ExecutableData) (engine.PayloadStatusV1, error) {
448-
if latestActive(api.eth.BlockChain().Config(), "shanghai", params.Timestamp) {
448+
if api.eth.BlockChain().Config().LatestFork(params.Timestamp) == forks.Shanghai {
449449
if params.Withdrawals == nil {
450450
return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("nil withdrawals post-shanghai"))
451451
}
@@ -482,33 +482,12 @@ func (api *ConsensusAPI) NewPayloadV3(params engine.ExecutableData, versionedHas
482482
return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("nil parentBeaconBlockRoot post-cancun"))
483483
}
484484

485-
if !latestActive(api.eth.BlockChain().Config(), "cancun", params.Timestamp) {
485+
if api.eth.BlockChain().Config().LatestFork(params.Timestamp) != forks.Cancun {
486486
return engine.PayloadStatusV1{Status: engine.INVALID}, engine.UnsupportedFork.With(errors.New("newPayloadV3 must only be called for cancun payloads"))
487487
}
488488
return api.newPayload(params, versionedHashes, beaconRoot)
489489
}
490490

491-
// latestActive returns true only if fork is the latest latestActive fork. It returns false otherwise.
492-
func latestActive(config *params.ChainConfig, fork string, time uint64) bool {
493-
switch fork {
494-
case "shanghai":
495-
if !config.IsShanghai(config.LondonBlock, time) {
496-
return false
497-
}
498-
if config.IsCancun(config.LondonBlock, time) {
499-
return false
500-
}
501-
case "cancun":
502-
if !config.IsCancun(config.LondonBlock, time) {
503-
return false
504-
}
505-
if config.IsPrague(config.LondonBlock, time) {
506-
return false
507-
}
508-
}
509-
return true
510-
}
511-
512491
func (api *ConsensusAPI) newPayload(params engine.ExecutableData, versionedHashes []common.Hash, beaconRoot *common.Hash) (engine.PayloadStatusV1, error) {
513492
// The locking here is, strictly, not required. Without these locks, this can happen:
514493
//

params/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"math/big"
2222

2323
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/params/forks"
2425
)
2526

2627
// Genesis hashes to enforce below configs on.
@@ -750,6 +751,21 @@ func (c *ChainConfig) ElasticityMultiplier() uint64 {
750751
return DefaultElasticityMultiplier
751752
}
752753

754+
// LatestFork returns the latest time-based fork that would be active for the given time.
755+
func (c *ChainConfig) LatestFork(time uint64) forks.Fork {
756+
// Assume last non-time-based fork has passed.
757+
london := c.LondonBlock
758+
759+
switch {
760+
case c.IsCancun(london, time):
761+
return forks.Cancun
762+
case c.IsShanghai(london, time):
763+
return forks.Shanghai
764+
default:
765+
return forks.Paris
766+
}
767+
}
768+
753769
// isForkBlockIncompatible returns true if a fork scheduled at block s1 cannot be
754770
// rescheduled to block s2 because head is already past the fork.
755771
func isForkBlockIncompatible(s1, s2, head *big.Int) bool {

params/forks/forks.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2023 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package forks
18+
19+
// Fork is a numerical identifier of specific network upgrades (forks).
20+
type Fork int
21+
22+
const (
23+
Frontier = iota
24+
FrontierThawing
25+
Homestead
26+
DAO
27+
TangerineWhistle
28+
SpuriousDragon
29+
Byzantium
30+
Constantinople
31+
Petersburg
32+
Istanbul
33+
MuirGlacier
34+
Berlin
35+
London
36+
ArrowGlacier
37+
GrayGlacier
38+
Paris
39+
Shanghai
40+
Cancun
41+
Prague
42+
)

0 commit comments

Comments
 (0)