mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
beacon/types: enforce fork order based on known forks list
This commit is contained in:
parent
767b00b0b5
commit
80bf2c0143
1 changed files with 29 additions and 8 deletions
|
|
@ -19,6 +19,7 @@ package types
|
|||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
|
@ -27,6 +28,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/beacon/merkle"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
|
|
@ -34,6 +36,8 @@ import (
|
|||
// across signing different data structures.
|
||||
const syncCommitteeDomain = 7
|
||||
|
||||
var knownForks = []string{"GENESIS", "ALTAIR", "BELLATRIX", "CAPELLA", "DENEB"}
|
||||
|
||||
// Fork describes a single beacon chain fork and also stores the calculated
|
||||
// signature domain used after this fork.
|
||||
type Fork struct {
|
||||
|
|
@ -46,6 +50,9 @@ type Fork struct {
|
|||
// Fork version, see https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#custom-types
|
||||
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
|
||||
domain merkle.Value
|
||||
}
|
||||
|
|
@ -99,9 +106,14 @@ func (f Forks) SigningRoot(header Header) (common.Hash, error) {
|
|||
return signingRoot, nil
|
||||
}
|
||||
|
||||
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) Less(i, j int) bool { return f[i].Epoch < f[j].Epoch }
|
||||
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) Less(i, j int) bool {
|
||||
if f[i].knownIndex != f[j].knownIndex {
|
||||
return f[i].knownIndex < f[j].knownIndex
|
||||
}
|
||||
return f[i].Epoch < f[j].Epoch
|
||||
}
|
||||
|
||||
// ChainConfig contains the beacon chain configuration.
|
||||
type ChainConfig struct {
|
||||
|
|
@ -122,16 +134,26 @@ func (c *ChainConfig) ForkAtEpoch(epoch uint64) Fork {
|
|||
|
||||
// AddFork adds a new item to the list of forks.
|
||||
func (c *ChainConfig) AddFork(name string, epoch uint64, version []byte) *ChainConfig {
|
||||
knownIndex := math.MaxInt
|
||||
for i, fname := range knownForks {
|
||||
if name == fname {
|
||||
knownIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if knownIndex == math.MaxInt && epoch != math.MaxUint64 {
|
||||
log.Warn("Unknown fork in config.yaml", "fork name", name, "known forks", knownForks)
|
||||
}
|
||||
fork := &Fork{
|
||||
Name: name,
|
||||
Epoch: epoch,
|
||||
Version: version,
|
||||
Name: name,
|
||||
Epoch: epoch,
|
||||
Version: version,
|
||||
knownIndex: knownIndex,
|
||||
}
|
||||
fork.computeDomain(c.GenesisValidatorsRoot)
|
||||
|
||||
c.Forks = append(c.Forks, fork)
|
||||
sort.Sort(c.Forks)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
|
|
@ -181,6 +203,5 @@ func (c *ChainConfig) LoadForks(path string) error {
|
|||
for name := range versions {
|
||||
return fmt.Errorf("epoch number missing for fork %q in beacon chain config file", name)
|
||||
}
|
||||
sort.Sort(c.Forks)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue