From 78eafeb518c7a0769c951b86ad6f78ca1e0b332d Mon Sep 17 00:00:00 2001 From: lightclient Date: Mon, 16 Oct 2023 15:08:11 -0600 Subject: [PATCH] eth/catalyst: different idea for checking if fork is latest active --- eth/catalyst/api.go | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index 46ce967b31..a7cd024816 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -20,7 +20,6 @@ package catalyst import ( "errors" "fmt" - "math/big" "sync" "time" @@ -34,6 +33,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/node" + "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" ) @@ -192,8 +192,7 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV2(update engine.ForkchoiceStateV1, pa if params.BeaconRoot != nil { return engine.STATUS_INVALID, engine.InvalidParams.With(errors.New("unexpected beacon root")) } - c := api.eth.BlockChain().Config() - if !active(c.IsShanghai, c.IsCancun, c.LondonBlock, params.Timestamp) { + if !latestActive(api.eth.BlockChain().Config(), "shanghai", params.Timestamp) { return engine.STATUS_INVALID, engine.UnsupportedFork.With(errors.New("forkchoiceUpdatedV2 must only be called for shanghai payloads")) } } @@ -209,8 +208,7 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV3(update engine.ForkchoiceStateV1, pa if params.BeaconRoot == nil { return engine.STATUS_INVALID, engine.InvalidParams.With(errors.New("missing beacon root")) } - c := api.eth.BlockChain().Config() - if !active(c.IsCancun, c.IsPrague, c.LondonBlock, params.Timestamp) { + if !latestActive(api.eth.BlockChain().Config(), "cancun", params.Timestamp) { return engine.STATUS_INVALID, engine.UnsupportedFork.With(errors.New("forkchoiceUpdatedV3 must only be called for shanghai payloads")) } } @@ -447,8 +445,7 @@ func (api *ConsensusAPI) NewPayloadV1(params engine.ExecutableData) (engine.Payl // NewPayloadV2 creates an Eth1 block, inserts it in the chain, and returns the status of the chain. func (api *ConsensusAPI) NewPayloadV2(params engine.ExecutableData) (engine.PayloadStatusV1, error) { - c := api.eth.BlockChain().Config() - if active(c.IsShanghai, c.IsCancun, c.LondonBlock, params.Timestamp) { + if latestActive(api.eth.BlockChain().Config(), "shanghai", params.Timestamp) { if params.Withdrawals == nil { return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("nil withdrawals post-shanghai")) } @@ -485,21 +482,29 @@ func (api *ConsensusAPI) NewPayloadV3(params engine.ExecutableData, versionedHas return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("nil parentBeaconBlockRoot post-cancun")) } - c := api.eth.BlockChain().Config() - if !active(c.IsCancun, c.IsPrague, c.LondonBlock, params.Timestamp) { + if !latestActive(api.eth.BlockChain().Config(), "cancun", params.Timestamp) { return engine.PayloadStatusV1{Status: engine.INVALID}, engine.UnsupportedFork.With(errors.New("newPayloadV3 must only be called for cancun payloads")) } - return api.newPayload(params, versionedHashes, beaconRoot) } -// active returns true only if current returns true and next returns false. It returns false otherwise. -func active(current, next func(*big.Int, uint64) bool, london *big.Int, time uint64) bool { - if !current(london, time) { - return false - } - if next(london, time) { - return false +// latestActive returns true only if fork is the latest latestActive fork. It returns false otherwise. +func latestActive(config *params.ChainConfig, fork string, time uint64) bool { + switch fork { + case "shanghai": + if !config.IsShanghai(config.LondonBlock, time) { + return false + } + if config.IsCancun(config.LondonBlock, time) { + return false + } + case "cancun": + if !config.IsCancun(config.LondonBlock, time) { + return false + } + if config.IsPrague(config.LondonBlock, time) { + return false + } } return true }