prysm-pulse/shared/params/loader.go
Shay Zluf 1a27c21d9c
Load chain config from file (#5694)
* load chain config from file

* revert flag name change

* add dependencies to image

* Update shared/cmd/flags.go

Co-authored-by: Victor Farazdagi <simple.square@gmail.com>

* Update beacon-chain/main.go

Co-authored-by: Victor Farazdagi <simple.square@gmail.com>

* added test to load config file

* Merge branch 'yaml_chain_config' of github.com:prysmaticlabs/prysm into yaml_chain_config

# Conflicts:
#	beacon-chain/main.go

* replace hex with yaml format of fixed byte array

* fix test and check if comment

* move to node package

* gaz

* added contract address case

* fix key name issue

* add tests

* gaz

* add 1 byte handling

* change to fatal

* add config printout

* revert main changes

* revert line removal

* fix one byte handling

* fix test and one byte handling

* remove log

* Apply suggestions from code review

* change to debug

* Update beacon-chain/node/node.go

* move helper methods

Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: terence tsao <terence@prysmaticlabs.com>
2020-05-05 20:58:07 +03:00

119 lines
3.1 KiB
Go

package params
import (
"encoding/hex"
"io/ioutil"
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// LoadChainConfigFile load, convert hex values into valid param yaml format,
// unmarshal , and apply beacon chain config file.
func LoadChainConfigFile(chainConfigFileName string) {
yamlFile, err := ioutil.ReadFile(chainConfigFileName)
if err != nil {
log.WithError(err).Fatal("Failed to read chain config file.")
}
// Convert 0x hex inputs to fixed bytes arrays
lines := strings.Split(string(yamlFile), "\n")
for i, line := range lines {
if !strings.HasPrefix(line, "#") && strings.Contains(line, "0x") {
parts := replaceHexStringWithYAMLFormat(line)
lines[i] = strings.Join(parts, "\n")
}
}
yamlFile = []byte(strings.Join(lines, "\n"))
conf := BeaconConfig()
if err := yaml.Unmarshal(yamlFile, conf); err != nil {
log.WithError(err).Fatal("Failed to parse chain config yaml file.")
}
log.Debugf("Config file values: %+v", conf)
OverrideBeaconConfig(conf)
}
func replaceHexStringWithYAMLFormat(line string) []string {
parts := strings.Split(line, "0x")
b, err := hex.DecodeString(parts[1])
if err != nil {
log.WithError(err).Error("Failed to decode hex string.")
}
switch l := len(b); {
case l == 1:
var byte byte
byte = b[0]
fixedByte, err := yaml.Marshal(byte)
if err != nil {
log.WithError(err).Error("Failed to marshal config file.")
}
parts[0] = parts[0] + string(fixedByte)
parts = parts[:1]
case l > 1 && l <= 4:
var arr [4]byte
copy(arr[:], b)
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
copy(arr[:], b)
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
copy(arr[:], b)
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
copy(arr[:], b)
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
copy(arr[:], b)
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
copy(arr[:], b)
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
copy(arr[:], b)
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
copy(arr[:], b)
fixedByte, err := yaml.Marshal(arr)
if err != nil {
log.WithError(err).Error("Failed to marshal config file.")
}
parts[1] = string(fixedByte)
}
return parts
}