Skip to content

Commit 38da10d

Browse files
committed
feat: Update fork epochs
1 parent 5cc03d2 commit 38da10d

File tree

3 files changed

+77
-30
lines changed

3 files changed

+77
-30
lines changed

Diff for: pkg/beacon/metrics_fork.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,22 @@ func (f *ForkMetrics) calculateCurrent(ctx context.Context) error {
112112
f.Epochs.Reset()
113113

114114
for _, fork := range spec.ForkEpochs {
115-
f.Epochs.WithLabelValues(fork.Name).Set(float64(fork.Epoch))
115+
f.Epochs.WithLabelValues(string(fork.Name)).Set(float64(fork.Epoch))
116116

117-
if fork.Active(phase0.Slot(slot.Number()), slotsPerEpoch) {
118-
f.Activated.WithLabelValues(fork.Name).Set(1)
117+
if fork.Active(phase0.Epoch(phase0.Slot(slot.Number()) / slotsPerEpoch)) {
118+
f.Activated.WithLabelValues(string(fork.Name)).Set(1)
119119
} else {
120-
f.Activated.WithLabelValues(fork.Name).Set(0)
120+
f.Activated.WithLabelValues(string(fork.Name)).Set(0)
121121
}
122122
}
123123

124-
current, err := spec.ForkEpochs.CurrentFork(phase0.Slot(slot.Number()), slotsPerEpoch)
124+
current, err := spec.ForkEpochs.CurrentFork(phase0.Epoch(phase0.Slot(slot.Number()) / slotsPerEpoch))
125125
if err != nil {
126126
f.log.WithError(err).Error("Failed to set current fork")
127127
} else {
128128
f.Current.Reset()
129129

130-
f.Current.WithLabelValues(current.Name).Set(1)
130+
f.Current.WithLabelValues(string(current.Name)).Set(1)
131131
}
132132

133133
return nil

Diff for: pkg/beacon/state/fork_epoch.go

+53-23
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,81 @@ package state
22

33
import (
44
"errors"
5+
"sort"
56

7+
"github.com/attestantio/go-eth2-client/spec"
68
"github.com/attestantio/go-eth2-client/spec/phase0"
79
)
810

11+
var (
12+
// ForkOrder is the canonical order of the forks.
13+
ForkOrder = []spec.DataVersion{
14+
spec.DataVersionPhase0,
15+
spec.DataVersionAltair,
16+
spec.DataVersionBellatrix,
17+
spec.DataVersionCapella,
18+
spec.DataVersionDeneb,
19+
}
20+
)
21+
922
// ForkEpoch is a beacon fork that activates at a specific epoch.
1023
type ForkEpoch struct {
11-
Epoch phase0.Epoch `yaml:"epoch"`
12-
Version string `json:"version"`
13-
Name string `json:"name"`
24+
Epoch phase0.Epoch `yaml:"epoch"`
25+
Version string `json:"version"`
26+
Name spec.DataVersion `json:"name"`
1427
}
1528

16-
// Active returns true if the fork is active at the given slot.
17-
func (f *ForkEpoch) Active(slot, slotsPerEpoch phase0.Slot) bool {
18-
return phase0.Epoch(int(slot)/int(slotsPerEpoch)) >= f.Epoch
29+
// Active returns true if the fork is active at the given epoch.
30+
func (f *ForkEpoch) Active(epoch phase0.Epoch) bool {
31+
return epoch >= f.Epoch
1932
}
2033

2134
// ForkEpochs is a list of forks that activate at specific epochs.
2235
type ForkEpochs []*ForkEpoch
2336

24-
// Active returns a list of forks that are active at the given slot.
25-
func (f *ForkEpochs) Active(slot, slotsPerEpoch phase0.Slot) []*ForkEpoch {
37+
// Active returns a list of forks that are active at the given epoch.
38+
func (f *ForkEpochs) Active(epoch phase0.Epoch) []*ForkEpoch {
2639
activated := []*ForkEpoch{}
2740

2841
for _, fork := range *f {
29-
if fork.Active(slot, slotsPerEpoch) {
42+
if fork.Active(epoch) {
3043
activated = append(activated, fork)
3144
}
3245
}
3346

47+
// Sort them by our fork order since multiple forks can be activated on the same epoch.
48+
// For example, a non-phase0 genesis.
49+
sort.Slice(activated, func(i, j int) bool {
50+
return f.IndexOf(activated[i].Name) < f.IndexOf(activated[j].Name)
51+
})
52+
3453
return activated
3554
}
3655

37-
// CurrentFork returns the current fork at the given slot.
38-
func (f *ForkEpochs) Scheduled(slot, slotsPerEpoch phase0.Slot) []*ForkEpoch {
56+
// Scheduled returns the scheduled forks at the given epoch.
57+
func (f *ForkEpochs) Scheduled(epoch phase0.Epoch) []*ForkEpoch {
3958
scheduled := []*ForkEpoch{}
4059

4160
for _, fork := range *f {
42-
if !fork.Active(slot, slotsPerEpoch) {
61+
if !fork.Active(epoch) {
4362
scheduled = append(scheduled, fork)
4463
}
4564
}
4665

4766
return scheduled
4867
}
4968

50-
// CurrentFork returns the current fork at the given slot.
51-
func (f *ForkEpochs) CurrentFork(slot, slotsPerEpoch phase0.Slot) (*ForkEpoch, error) {
69+
// CurrentFork returns the current fork at the given epoch.
70+
func (f *ForkEpochs) CurrentFork(epoch phase0.Epoch) (*ForkEpoch, error) {
5271
found := false
5372

5473
largest := &ForkEpoch{
5574
Epoch: 0,
5675
}
5776

58-
for _, fork := range f.Active(slot, slotsPerEpoch) {
59-
if fork.Active(slot, slotsPerEpoch) && fork.Epoch >= largest.Epoch {
77+
active := f.Active(epoch)
78+
for _, fork := range active {
79+
if fork.Epoch >= largest.Epoch {
6080
found = true
6181

6282
largest = fork
@@ -70,13 +90,13 @@ func (f *ForkEpochs) CurrentFork(slot, slotsPerEpoch phase0.Slot) (*ForkEpoch, e
7090
return largest, nil
7191
}
7292

73-
// PreviousFork returns the previous fork at the given slot.
74-
func (f *ForkEpochs) PreviousFork(slot, slotsPerEpoch phase0.Slot) (*ForkEpoch, error) {
93+
// PreviousFork returns the previous fork at the given epoch.
94+
func (f *ForkEpochs) PreviousFork(epoch phase0.Epoch) (*ForkEpoch, error) {
7595
if len(*f) == 1 {
76-
return f.CurrentFork(slot, slotsPerEpoch)
96+
return f.CurrentFork(epoch)
7797
}
7898

79-
current, err := f.CurrentFork(slot, slotsPerEpoch)
99+
current, err := f.CurrentFork(epoch)
80100
if err != nil {
81101
return nil, err
82102
}
@@ -87,8 +107,8 @@ func (f *ForkEpochs) PreviousFork(slot, slotsPerEpoch phase0.Slot) (*ForkEpoch,
87107
Epoch: 0,
88108
}
89109

90-
for _, fork := range f.Active(slot, slotsPerEpoch) {
91-
if fork.Active(slot, slotsPerEpoch) && fork.Name != current.Name && fork.Epoch > largest.Epoch {
110+
for _, fork := range f.Active(epoch) {
111+
if fork.Active(epoch) && fork.Name != current.Name && fork.Epoch > largest.Epoch {
92112
found = true
93113

94114
largest = fork
@@ -105,7 +125,7 @@ func (f *ForkEpochs) PreviousFork(slot, slotsPerEpoch phase0.Slot) (*ForkEpoch,
105125
// GetByName returns the fork with the given name.
106126
func (f *ForkEpochs) GetByName(name string) (*ForkEpoch, error) {
107127
for _, fork := range *f {
108-
if fork.Name == name {
128+
if fork.Name.String() == name {
109129
return fork, nil
110130
}
111131
}
@@ -117,3 +137,13 @@ func (f *ForkEpochs) GetByName(name string) (*ForkEpoch, error) {
117137
func (f *ForkEpochs) AsScheduledForks() ([]*ScheduledFork, error) {
118138
return ForkScheduleFromForkEpochs(*f)
119139
}
140+
141+
// IndexOf returns the index of the given data version in the fork order.
142+
func (f *ForkEpochs) IndexOf(name spec.DataVersion) int {
143+
for i, version := range ForkOrder {
144+
if version == name {
145+
return i
146+
}
147+
}
148+
return -1 // Return -1 if the name is not found
149+
}

Diff for: pkg/beacon/state/spec.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package state
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"math/big"
67
"strings"
78

9+
sp "github.com/attestantio/go-eth2-client/spec"
810
"github.com/attestantio/go-eth2-client/spec/phase0"
911
"github.com/spf13/cast"
1012
)
@@ -175,9 +177,15 @@ func NewSpec(data map[string]interface{}) Spec {
175177
version = v
176178
}
177179

180+
// Convert the name to a DataVersion.
181+
dataVersion, err := dataVersionFromString(k)
182+
if err != nil {
183+
continue
184+
}
185+
178186
spec.ForkEpochs = append(spec.ForkEpochs, &ForkEpoch{
179187
Epoch: v,
180-
Name: k,
188+
Name: dataVersion,
181189
Version: version,
182190
})
183191
}
@@ -189,3 +197,12 @@ func NewSpec(data map[string]interface{}) Spec {
189197
func (s *Spec) Validate() error {
190198
return nil
191199
}
200+
201+
func dataVersionFromString(name string) (sp.DataVersion, error) {
202+
var v sp.DataVersion
203+
if err := json.Unmarshal([]byte(fmt.Sprintf("\"%q\"", name)), &v); err != nil {
204+
return sp.DataVersionUnknown, err
205+
}
206+
207+
return v, nil
208+
}

0 commit comments

Comments
 (0)