eth/catalyst: different idea for checking if fork is latest active

This commit is contained in:
lightclient 2023-10-16 15:08:11 -06:00
parent eb2da1ae66
commit 78eafeb518
No known key found for this signature in database
GPG key ID: 75C916AFEE20183E

View file

@ -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,22 +482,30 @@ 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) {
// 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 next(london, time) {
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
}