mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-10 07:58:22 -05:00
* Problem: nondeterministic default fork value when generate genesis add sort versions * add doc * Apply suggestions from code review * lint --------- Co-authored-by: Bastin <43618253+Inspector-Butters@users.noreply.github.com>
70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package version
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
const (
|
|
Phase0 = iota
|
|
Altair
|
|
Bellatrix
|
|
Capella
|
|
Deneb
|
|
Electra
|
|
Fulu
|
|
)
|
|
|
|
var versionToString = map[int]string{
|
|
Phase0: "phase0",
|
|
Altair: "altair",
|
|
Bellatrix: "bellatrix",
|
|
Capella: "capella",
|
|
Deneb: "deneb",
|
|
Electra: "electra",
|
|
Fulu: "fulu",
|
|
}
|
|
|
|
// stringToVersion and allVersions are populated in init()
|
|
var stringToVersion = map[string]int{}
|
|
var allVersions []int
|
|
|
|
// ErrUnrecognizedVersionName means a string does not match the list of canonical version names.
|
|
var ErrUnrecognizedVersionName = errors.New("version name doesn't map to a known value in the enum")
|
|
|
|
// FromString translates a canonical version name to the version number.
|
|
func FromString(name string) (int, error) {
|
|
v, ok := stringToVersion[name]
|
|
if !ok {
|
|
return 0, errors.Wrap(ErrUnrecognizedVersionName, name)
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// String returns the canonical string form of a version.
|
|
// Unrecognized versions won't generate an error and are represented by the string "unknown version".
|
|
func String(version int) string {
|
|
name, ok := versionToString[version]
|
|
if !ok {
|
|
return "unknown version"
|
|
}
|
|
return name
|
|
}
|
|
|
|
// All returns a list of all known fork versions.
|
|
func All() []int {
|
|
return allVersions
|
|
}
|
|
|
|
func init() {
|
|
allVersions = make([]int, len(versionToString))
|
|
i := 0
|
|
for v, s := range versionToString {
|
|
allVersions[i] = v
|
|
stringToVersion[s] = v
|
|
i++
|
|
}
|
|
sort.Ints(allVersions)
|
|
}
|