2020-05-05 17:58:07 +00:00
|
|
|
package params
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2022-02-02 06:56:07 +00:00
|
|
|
"fmt"
|
2022-04-18 20:42:07 +00:00
|
|
|
"os"
|
2020-05-05 17:58:07 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-05-20 07:16:53 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-08-16 12:20:13 +00:00
|
|
|
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/math"
|
2020-05-05 17:58:07 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2022-03-21 19:43:41 +00:00
|
|
|
func isMinimal(lines []string) bool {
|
|
|
|
for _, l := range lines {
|
|
|
|
if strings.HasPrefix(l, "PRESET_BASE: 'minimal'") ||
|
|
|
|
strings.HasPrefix(l, `PRESET_BASE: "minimal"`) ||
|
|
|
|
strings.HasPrefix(l, "PRESET_BASE: minimal") ||
|
|
|
|
strings.HasPrefix(l, "# Minimal preset") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-20 07:16:53 +00:00
|
|
|
func UnmarshalConfigFile(path string, conf *BeaconChainConfig) (*BeaconChainConfig, error) {
|
|
|
|
yamlFile, err := os.ReadFile(path) // #nosec G304
|
2020-05-05 17:58:07 +00:00
|
|
|
if err != nil {
|
2022-05-20 07:16:53 +00:00
|
|
|
return nil, errors.Wrap(err, "Failed to read chain config file.")
|
2020-05-05 17:58:07 +00:00
|
|
|
}
|
2021-11-30 03:27:26 +00:00
|
|
|
// To track if config name is defined inside config file.
|
|
|
|
hasConfigName := false
|
2020-05-05 17:58:07 +00:00
|
|
|
// Convert 0x hex inputs to fixed bytes arrays
|
|
|
|
lines := strings.Split(string(yamlFile), "\n")
|
2022-03-21 19:43:41 +00:00
|
|
|
if conf == nil {
|
|
|
|
if isMinimal(lines) {
|
|
|
|
conf = MinimalSpecConfig().Copy()
|
|
|
|
} else {
|
|
|
|
// Default to using mainnet.
|
|
|
|
conf = MainnetConfig().Copy()
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 17:58:07 +00:00
|
|
|
for i, line := range lines {
|
2020-12-18 22:22:48 +00:00
|
|
|
// No need to convert the deposit contract address to byte array (as config expects a string).
|
|
|
|
if strings.HasPrefix(line, "DEPOSIT_CONTRACT_ADDRESS") {
|
|
|
|
continue
|
|
|
|
}
|
2021-11-30 03:27:26 +00:00
|
|
|
if strings.HasPrefix(line, "CONFIG_NAME") {
|
|
|
|
hasConfigName = true
|
|
|
|
}
|
2020-05-05 17:58:07 +00:00
|
|
|
if !strings.HasPrefix(line, "#") && strings.Contains(line, "0x") {
|
2021-09-21 15:02:48 +00:00
|
|
|
parts := ReplaceHexStringWithYAMLFormat(line)
|
2020-05-05 17:58:07 +00:00
|
|
|
lines[i] = strings.Join(parts, "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yamlFile = []byte(strings.Join(lines, "\n"))
|
2021-08-01 03:26:24 +00:00
|
|
|
if err := yaml.UnmarshalStrict(yamlFile, conf); err != nil {
|
|
|
|
if _, ok := err.(*yaml.TypeError); !ok {
|
2022-05-20 07:16:53 +00:00
|
|
|
return nil, errors.Wrap(err, "Failed to parse chain config yaml file.")
|
2021-08-01 03:26:24 +00:00
|
|
|
} else {
|
|
|
|
log.WithError(err).Error("There were some issues parsing the config from a yaml file")
|
|
|
|
}
|
2020-05-05 17:58:07 +00:00
|
|
|
}
|
2021-11-30 03:27:26 +00:00
|
|
|
if !hasConfigName {
|
2022-05-20 07:16:53 +00:00
|
|
|
conf.ConfigName = DevnetName
|
2021-11-30 03:27:26 +00:00
|
|
|
}
|
2021-09-09 17:03:00 +00:00
|
|
|
// recompute SqrRootSlotsPerEpoch constant to handle non-standard values of SlotsPerEpoch
|
2021-09-17 21:55:24 +00:00
|
|
|
conf.SqrRootSlotsPerEpoch = types.Slot(math.IntegerSquareRoot(uint64(conf.SlotsPerEpoch)))
|
2020-05-05 17:58:07 +00:00
|
|
|
log.Debugf("Config file values: %+v", conf)
|
2022-05-20 07:16:53 +00:00
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadChainConfigFile load, convert hex values into valid param yaml format,
|
|
|
|
// unmarshal , and apply beacon chain config file.
|
|
|
|
func LoadChainConfigFile(path string, conf *BeaconChainConfig) error {
|
|
|
|
c, err := UnmarshalConfigFile(path, conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return SetActive(c)
|
2020-05-05 17:58:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 15:02:48 +00:00
|
|
|
// ReplaceHexStringWithYAMLFormat will replace hex strings that the yaml parser will understand.
|
|
|
|
func ReplaceHexStringWithYAMLFormat(line string) []string {
|
2020-05-05 17:58:07 +00:00
|
|
|
parts := strings.Split(line, "0x")
|
2020-09-14 10:49:15 +00:00
|
|
|
decoded, err := hex.DecodeString(parts[1])
|
2020-05-05 17:58:07 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to decode hex string.")
|
|
|
|
}
|
2020-09-14 10:49:15 +00:00
|
|
|
switch l := len(decoded); {
|
2020-05-05 17:58:07 +00:00
|
|
|
case l == 1:
|
2020-09-14 10:49:15 +00:00
|
|
|
var b byte
|
|
|
|
b = decoded[0]
|
|
|
|
fixedByte, err := yaml.Marshal(b)
|
2020-05-05 17:58:07 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to marshal config file.")
|
|
|
|
}
|
2021-02-01 15:14:36 +00:00
|
|
|
parts[0] += string(fixedByte)
|
2020-05-05 17:58:07 +00:00
|
|
|
parts = parts[:1]
|
|
|
|
case l > 1 && l <= 4:
|
|
|
|
var arr [4]byte
|
2020-09-14 10:49:15 +00:00
|
|
|
copy(arr[:], decoded)
|
2020-05-05 17:58:07 +00:00
|
|
|
fixedByte, err := yaml.Marshal(arr)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to marshal config file.")
|
|
|
|
}
|
|
|
|
parts[1] = string(fixedByte)
|
|
|
|
case l > 4 && l <= 8:
|
|
|
|
var arr [8]byte
|
2020-09-14 10:49:15 +00:00
|
|
|
copy(arr[:], decoded)
|
2020-05-05 17:58:07 +00:00
|
|
|
fixedByte, err := yaml.Marshal(arr)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to marshal config file.")
|
|
|
|
}
|
|
|
|
parts[1] = string(fixedByte)
|
|
|
|
case l > 8 && l <= 16:
|
|
|
|
var arr [16]byte
|
2020-09-14 10:49:15 +00:00
|
|
|
copy(arr[:], decoded)
|
2020-05-05 17:58:07 +00:00
|
|
|
fixedByte, err := yaml.Marshal(arr)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to marshal config file.")
|
|
|
|
}
|
|
|
|
parts[1] = string(fixedByte)
|
|
|
|
case l > 16 && l <= 20:
|
|
|
|
var arr [20]byte
|
2020-09-14 10:49:15 +00:00
|
|
|
copy(arr[:], decoded)
|
2020-05-05 17:58:07 +00:00
|
|
|
fixedByte, err := yaml.Marshal(arr)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to marshal config file.")
|
|
|
|
}
|
|
|
|
parts[1] = string(fixedByte)
|
|
|
|
case l > 20 && l <= 32:
|
|
|
|
var arr [32]byte
|
2020-09-14 10:49:15 +00:00
|
|
|
copy(arr[:], decoded)
|
2020-05-05 17:58:07 +00:00
|
|
|
fixedByte, err := yaml.Marshal(arr)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to marshal config file.")
|
|
|
|
}
|
|
|
|
parts[1] = string(fixedByte)
|
|
|
|
case l > 32 && l <= 48:
|
|
|
|
var arr [48]byte
|
2020-09-14 10:49:15 +00:00
|
|
|
copy(arr[:], decoded)
|
2020-05-05 17:58:07 +00:00
|
|
|
fixedByte, err := yaml.Marshal(arr)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to marshal config file.")
|
|
|
|
}
|
|
|
|
parts[1] = string(fixedByte)
|
|
|
|
case l > 48 && l <= 64:
|
|
|
|
var arr [64]byte
|
2020-09-14 10:49:15 +00:00
|
|
|
copy(arr[:], decoded)
|
2020-05-05 17:58:07 +00:00
|
|
|
fixedByte, err := yaml.Marshal(arr)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to marshal config file.")
|
|
|
|
}
|
|
|
|
parts[1] = string(fixedByte)
|
|
|
|
case l > 64 && l <= 96:
|
|
|
|
var arr [96]byte
|
2020-09-14 10:49:15 +00:00
|
|
|
copy(arr[:], decoded)
|
2020-05-05 17:58:07 +00:00
|
|
|
fixedByte, err := yaml.Marshal(arr)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to marshal config file.")
|
|
|
|
}
|
|
|
|
parts[1] = string(fixedByte)
|
|
|
|
}
|
|
|
|
return parts
|
|
|
|
}
|
2022-02-02 06:56:07 +00:00
|
|
|
|
|
|
|
// ConfigToYaml takes a provided config and outputs its contents
|
|
|
|
// in yaml. This allows prysm's custom configs to be read by other clients.
|
|
|
|
func ConfigToYaml(cfg *BeaconChainConfig) []byte {
|
2022-03-21 19:43:41 +00:00
|
|
|
lines := []string{
|
|
|
|
fmt.Sprintf("PRESET_BASE: '%s'", cfg.PresetBase),
|
|
|
|
fmt.Sprintf("CONFIG_NAME: '%s'", cfg.ConfigName),
|
|
|
|
fmt.Sprintf("MIN_GENESIS_ACTIVE_VALIDATOR_COUNT: %d", cfg.MinGenesisActiveValidatorCount),
|
|
|
|
fmt.Sprintf("GENESIS_DELAY: %d", cfg.GenesisDelay),
|
|
|
|
fmt.Sprintf("MIN_GENESIS_TIME: %d", cfg.MinGenesisTime),
|
|
|
|
fmt.Sprintf("GENESIS_FORK_VERSION: %#x", cfg.GenesisForkVersion),
|
|
|
|
fmt.Sprintf("CHURN_LIMIT_QUOTIENT: %d", cfg.ChurnLimitQuotient),
|
|
|
|
fmt.Sprintf("SECONDS_PER_SLOT: %d", cfg.SecondsPerSlot),
|
|
|
|
fmt.Sprintf("SLOTS_PER_EPOCH: %d", cfg.SlotsPerEpoch),
|
|
|
|
fmt.Sprintf("SECONDS_PER_ETH1_BLOCK: %d", cfg.SecondsPerETH1Block),
|
|
|
|
fmt.Sprintf("ETH1_FOLLOW_DISTANCE: %d", cfg.Eth1FollowDistance),
|
|
|
|
fmt.Sprintf("EPOCHS_PER_ETH1_VOTING_PERIOD: %d", cfg.EpochsPerEth1VotingPeriod),
|
|
|
|
fmt.Sprintf("SHARD_COMMITTEE_PERIOD: %d", cfg.ShardCommitteePeriod),
|
|
|
|
fmt.Sprintf("MIN_VALIDATOR_WITHDRAWABILITY_DELAY: %d", cfg.MinValidatorWithdrawabilityDelay),
|
|
|
|
fmt.Sprintf("MAX_SEED_LOOKAHEAD: %d", cfg.MaxSeedLookahead),
|
|
|
|
fmt.Sprintf("EJECTION_BALANCE: %d", cfg.EjectionBalance),
|
|
|
|
fmt.Sprintf("MIN_PER_EPOCH_CHURN_LIMIT: %d", cfg.MinPerEpochChurnLimit),
|
|
|
|
fmt.Sprintf("DEPOSIT_CHAIN_ID: %d", cfg.DepositChainID),
|
|
|
|
fmt.Sprintf("DEPOSIT_NETWORK_ID: %d", cfg.DepositNetworkID),
|
|
|
|
fmt.Sprintf("ALTAIR_FORK_EPOCH: %d", cfg.AltairForkEpoch),
|
|
|
|
fmt.Sprintf("ALTAIR_FORK_VERSION: %#x", cfg.AltairForkVersion),
|
|
|
|
fmt.Sprintf("BELLATRIX_FORK_EPOCH: %d", cfg.BellatrixForkEpoch),
|
|
|
|
fmt.Sprintf("BELLATRIX_FORK_VERSION: %#x", cfg.BellatrixForkVersion),
|
|
|
|
fmt.Sprintf("SHARDING_FORK_EPOCH: %d", cfg.ShardingForkEpoch),
|
|
|
|
fmt.Sprintf("SHARDING_FORK_VERSION: %#x", cfg.ShardingForkVersion),
|
|
|
|
fmt.Sprintf("INACTIVITY_SCORE_BIAS: %d", cfg.InactivityScoreBias),
|
|
|
|
fmt.Sprintf("INACTIVITY_SCORE_RECOVERY_RATE: %d", cfg.InactivityScoreRecoveryRate),
|
|
|
|
fmt.Sprintf("TERMINAL_TOTAL_DIFFICULTY: %s", cfg.TerminalTotalDifficulty),
|
|
|
|
fmt.Sprintf("TERMINAL_BLOCK_HASH: %#x", cfg.TerminalBlockHash),
|
|
|
|
fmt.Sprintf("TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH: %d", cfg.TerminalBlockHashActivationEpoch),
|
|
|
|
}
|
2022-02-02 06:56:07 +00:00
|
|
|
|
|
|
|
yamlFile := []byte(strings.Join(lines, "\n"))
|
|
|
|
return yamlFile
|
|
|
|
}
|