prysm-pulse/config/params/values.go
kasey cf0bd633f0
Checkpoint Sync 1/5 - fork/version detection and unmarshaling support (#10380)
* fork/version detection and unmarshaling support

* Update config/params/config.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update proto/detect/configfork.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* PR feedback

* move ssz initialization into the detect package

* clarify comment

* VersionForEpoch is much simpler/clearer in reverse

* simpler VersionForEpoch; build AllConfigs in init

* use fieldparams for Version

* Update proto/detect/configfork_test.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* remove custom ForkName type, use runtime/version

* pr cleanup

* random fix from bad gh ui suggestion; privatize

* privatize fieldSpec methods; + unit tests

* Update proto/detect/configfork.go

Co-authored-by: Potuz <potuz@prysmaticlabs.com>

* fix bad github ui suggestion

* ensure unique versions for simpler config match

* fmt & adding unit test for ByState()

* table-driven unit test for ByState

* TestUnmarshalState

* OrderedSchedule -> network/forks per PR feedback

* goimports

* lint fixes

* move proto/detect -> ssz/encoding/detect

* use typeUndefined in String

* backport config tests from e2e PR

* fix config parity test; make debugging it easier

* lint

* fix fork schedule initialization

* cleanup

* fix build

* fix big ole derp

* anything for you, deep source

* goimportsss

* InitializeForkSchedule in LoadChainConfigFile

* PR feedback

Co-authored-by: kasey <kasey@users.noreply.github.com>
Co-authored-by: Radosław Kapka <rkapka@wp.pl>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-03-21 19:43:41 +00:00

102 lines
2.9 KiB
Go

package params
import (
"fmt"
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
)
const (
Mainnet ConfigName = iota
Minimal
EndToEnd
Pyrmont
Prater
EndToEndMainnet
)
// ConfigName enum describes the type of known network in use.
type ConfigName int
func (n ConfigName) String() string {
s, ok := ConfigNames[n]
if !ok {
return "undefined"
}
return s
}
// ConfigNames provides network configuration names.
var ConfigNames = map[ConfigName]string{
Mainnet: "mainnet",
Minimal: "minimal",
EndToEnd: "end-to-end",
Pyrmont: "pyrmont",
Prater: "prater",
EndToEndMainnet: "end-to-end-mainnet",
}
// KnownConfigs provides an index of all known BeaconChainConfig values.
var KnownConfigs = map[ConfigName]func() *BeaconChainConfig{
Mainnet: MainnetConfig,
Prater: PraterConfig,
Pyrmont: PyrmontConfig,
Minimal: MinimalSpecConfig,
EndToEnd: E2ETestConfig,
EndToEndMainnet: E2EMainnetTestConfig,
}
var knownForkVersions map[[fieldparams.VersionLength]byte]ConfigName
var errUnknownForkVersion = errors.New("version not found in fork version schedule for any known config")
// ConfigForVersion find the BeaconChainConfig corresponding to the version bytes.
// Version bytes for BeaconChainConfig values in KnownConfigs are proven to be unique during package initialization.
func ConfigForVersion(version [fieldparams.VersionLength]byte) (*BeaconChainConfig, error) {
cfg, ok := knownForkVersions[version]
if !ok {
return nil, errors.Wrapf(errUnknownForkVersion, "version=%#x", version)
}
return KnownConfigs[cfg](), nil
}
func init() {
knownForkVersions = make(map[[fieldparams.VersionLength]byte]ConfigName)
for n, cfunc := range KnownConfigs {
cfg := cfunc()
// ensure that fork schedule is consistent w/ struct fields for all known configurations
if err := equalForkSchedules(configForkSchedule(cfg), cfg.ForkVersionSchedule); err != nil {
panic(errors.Wrapf(err, "improperly initialized for schedule for config %s", n.String()))
}
// ensure that all fork versions are unique
for v := range cfg.ForkVersionSchedule {
pn, exists := knownForkVersions[v]
if exists {
previous := KnownConfigs[pn]()
msg := fmt.Sprintf("version %#x is duplicated in 2 configs, %s at epoch %d, %s at epoch %d",
v, pn, previous.ForkVersionSchedule[v], n, cfg.ForkVersionSchedule[v])
panic(msg)
}
knownForkVersions[v] = n
}
}
}
func equalForkSchedules(a, b map[[fieldparams.VersionLength]byte]types.Epoch) error {
if len(a) != len(b) {
return fmt.Errorf("different lengths, a=%d, b=%d", len(a), len(b))
}
for k, v := range a {
bv, ok := b[k]
if !ok {
return fmt.Errorf("fork version %#x from 'a', not present in 'b'", k)
}
if v != bv {
return fmt.Errorf("fork version mismatch, epoch in a=%d, b=%d", v, bv)
}
}
return nil
}