beacon/types: enforce fork order based on known forks list (#29380)
Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
parent
15ff066a24
commit
35fcf9c52b
@ -19,7 +19,9 @@ package types
|
|||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -27,6 +29,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/beacon/merkle"
|
"github.com/ethereum/go-ethereum/beacon/merkle"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,6 +37,8 @@ import (
|
|||||||
// across signing different data structures.
|
// across signing different data structures.
|
||||||
const syncCommitteeDomain = 7
|
const syncCommitteeDomain = 7
|
||||||
|
|
||||||
|
var knownForks = []string{"GENESIS", "ALTAIR", "BELLATRIX", "CAPELLA", "DENEB"}
|
||||||
|
|
||||||
// Fork describes a single beacon chain fork and also stores the calculated
|
// Fork describes a single beacon chain fork and also stores the calculated
|
||||||
// signature domain used after this fork.
|
// signature domain used after this fork.
|
||||||
type Fork struct {
|
type Fork struct {
|
||||||
@ -46,6 +51,9 @@ type Fork struct {
|
|||||||
// Fork version, see https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#custom-types
|
// Fork version, see https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#custom-types
|
||||||
Version []byte
|
Version []byte
|
||||||
|
|
||||||
|
// index in list of known forks or MaxInt if unknown
|
||||||
|
knownIndex int
|
||||||
|
|
||||||
// calculated by computeDomain, based on fork version and genesis validators root
|
// calculated by computeDomain, based on fork version and genesis validators root
|
||||||
domain merkle.Value
|
domain merkle.Value
|
||||||
}
|
}
|
||||||
@ -101,7 +109,12 @@ func (f Forks) SigningRoot(header Header) (common.Hash, error) {
|
|||||||
|
|
||||||
func (f Forks) Len() int { return len(f) }
|
func (f Forks) Len() int { return len(f) }
|
||||||
func (f Forks) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
|
func (f Forks) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
|
||||||
func (f Forks) Less(i, j int) bool { return f[i].Epoch < f[j].Epoch }
|
func (f Forks) Less(i, j int) bool {
|
||||||
|
if f[i].Epoch != f[j].Epoch {
|
||||||
|
return f[i].Epoch < f[j].Epoch
|
||||||
|
}
|
||||||
|
return f[i].knownIndex < f[j].knownIndex
|
||||||
|
}
|
||||||
|
|
||||||
// ChainConfig contains the beacon chain configuration.
|
// ChainConfig contains the beacon chain configuration.
|
||||||
type ChainConfig struct {
|
type ChainConfig struct {
|
||||||
@ -122,16 +135,22 @@ func (c *ChainConfig) ForkAtEpoch(epoch uint64) Fork {
|
|||||||
|
|
||||||
// AddFork adds a new item to the list of forks.
|
// AddFork adds a new item to the list of forks.
|
||||||
func (c *ChainConfig) AddFork(name string, epoch uint64, version []byte) *ChainConfig {
|
func (c *ChainConfig) AddFork(name string, epoch uint64, version []byte) *ChainConfig {
|
||||||
|
knownIndex := slices.Index(knownForks, name)
|
||||||
|
if knownIndex == -1 {
|
||||||
|
knownIndex = math.MaxInt // assume that the unknown fork happens after the known ones
|
||||||
|
if epoch != math.MaxUint64 {
|
||||||
|
log.Warn("Unknown fork in config.yaml", "fork name", name, "known forks", knownForks)
|
||||||
|
}
|
||||||
|
}
|
||||||
fork := &Fork{
|
fork := &Fork{
|
||||||
Name: name,
|
Name: name,
|
||||||
Epoch: epoch,
|
Epoch: epoch,
|
||||||
Version: version,
|
Version: version,
|
||||||
|
knownIndex: knownIndex,
|
||||||
}
|
}
|
||||||
fork.computeDomain(c.GenesisValidatorsRoot)
|
fork.computeDomain(c.GenesisValidatorsRoot)
|
||||||
|
|
||||||
c.Forks = append(c.Forks, fork)
|
c.Forks = append(c.Forks, fork)
|
||||||
sort.Sort(c.Forks)
|
sort.Sort(c.Forks)
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,6 +200,5 @@ func (c *ChainConfig) LoadForks(path string) error {
|
|||||||
for name := range versions {
|
for name := range versions {
|
||||||
return fmt.Errorf("epoch number missing for fork %q in beacon chain config file", name)
|
return fmt.Errorf("epoch number missing for fork %q in beacon chain config file", name)
|
||||||
}
|
}
|
||||||
sort.Sort(c.Forks)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user