Skip to content

Commit 35fcf9c

Browse files
zsfelfoldifjl
andauthored
beacon/types: enforce fork order based on known forks list (#29380)
Co-authored-by: Felix Lange <[email protected]>
1 parent 15ff066 commit 35fcf9c

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

beacon/types/config.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,26 @@ package types
1919
import (
2020
"crypto/sha256"
2121
"fmt"
22+
"math"
2223
"os"
24+
"slices"
2325
"sort"
2426
"strconv"
2527
"strings"
2628

2729
"github.com/ethereum/go-ethereum/beacon/merkle"
2830
"github.com/ethereum/go-ethereum/common"
2931
"github.com/ethereum/go-ethereum/common/hexutil"
32+
"github.com/ethereum/go-ethereum/log"
3033
"gopkg.in/yaml.v3"
3134
)
3235

3336
// syncCommitteeDomain specifies the signatures specific use to avoid clashes
3437
// across signing different data structures.
3538
const syncCommitteeDomain = 7
3639

40+
var knownForks = []string{"GENESIS", "ALTAIR", "BELLATRIX", "CAPELLA", "DENEB"}
41+
3742
// Fork describes a single beacon chain fork and also stores the calculated
3843
// signature domain used after this fork.
3944
type Fork struct {
@@ -46,6 +51,9 @@ type Fork struct {
4651
// Fork version, see https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#custom-types
4752
Version []byte
4853

54+
// index in list of known forks or MaxInt if unknown
55+
knownIndex int
56+
4957
// calculated by computeDomain, based on fork version and genesis validators root
5058
domain merkle.Value
5159
}
@@ -99,9 +107,14 @@ func (f Forks) SigningRoot(header Header) (common.Hash, error) {
99107
return signingRoot, nil
100108
}
101109

102-
func (f Forks) Len() int { return len(f) }
103-
func (f Forks) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
104-
func (f Forks) Less(i, j int) bool { return f[i].Epoch < f[j].Epoch }
110+
func (f Forks) Len() int { return len(f) }
111+
func (f Forks) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
112+
func (f Forks) Less(i, j int) bool {
113+
if f[i].Epoch != f[j].Epoch {
114+
return f[i].Epoch < f[j].Epoch
115+
}
116+
return f[i].knownIndex < f[j].knownIndex
117+
}
105118

106119
// ChainConfig contains the beacon chain configuration.
107120
type ChainConfig struct {
@@ -122,16 +135,22 @@ func (c *ChainConfig) ForkAtEpoch(epoch uint64) Fork {
122135

123136
// AddFork adds a new item to the list of forks.
124137
func (c *ChainConfig) AddFork(name string, epoch uint64, version []byte) *ChainConfig {
138+
knownIndex := slices.Index(knownForks, name)
139+
if knownIndex == -1 {
140+
knownIndex = math.MaxInt // assume that the unknown fork happens after the known ones
141+
if epoch != math.MaxUint64 {
142+
log.Warn("Unknown fork in config.yaml", "fork name", name, "known forks", knownForks)
143+
}
144+
}
125145
fork := &Fork{
126-
Name: name,
127-
Epoch: epoch,
128-
Version: version,
146+
Name: name,
147+
Epoch: epoch,
148+
Version: version,
149+
knownIndex: knownIndex,
129150
}
130151
fork.computeDomain(c.GenesisValidatorsRoot)
131-
132152
c.Forks = append(c.Forks, fork)
133153
sort.Sort(c.Forks)
134-
135154
return c
136155
}
137156

@@ -181,6 +200,5 @@ func (c *ChainConfig) LoadForks(path string) error {
181200
for name := range versions {
182201
return fmt.Errorf("epoch number missing for fork %q in beacon chain config file", name)
183202
}
184-
sort.Sort(c.Forks)
185203
return nil
186204
}

0 commit comments

Comments
 (0)