github.com/prysmaticlabs/prysm@v1.4.4/shared/params/loader.go (about)

     1  package params
     2  
     3  import (
     4  	"encoding/hex"
     5  	"io/ioutil"
     6  	"strings"
     7  
     8  	log "github.com/sirupsen/logrus"
     9  	"gopkg.in/yaml.v2"
    10  )
    11  
    12  // LoadChainConfigFile load, convert hex values into valid param yaml format,
    13  // unmarshal , and apply beacon chain config file.
    14  func LoadChainConfigFile(chainConfigFileName string) {
    15  	yamlFile, err := ioutil.ReadFile(chainConfigFileName)
    16  	if err != nil {
    17  		log.WithError(err).Fatal("Failed to read chain config file.")
    18  	}
    19  	// Convert 0x hex inputs to fixed bytes arrays
    20  	lines := strings.Split(string(yamlFile), "\n")
    21  	for i, line := range lines {
    22  		// No need to convert the deposit contract address to byte array (as config expects a string).
    23  		if strings.HasPrefix(line, "DEPOSIT_CONTRACT_ADDRESS") {
    24  			continue
    25  		}
    26  		if !strings.HasPrefix(line, "#") && strings.Contains(line, "0x") {
    27  			parts := replaceHexStringWithYAMLFormat(line)
    28  			lines[i] = strings.Join(parts, "\n")
    29  		}
    30  	}
    31  	yamlFile = []byte(strings.Join(lines, "\n"))
    32  	conf := MainnetConfig()
    33  	if err := yaml.Unmarshal(yamlFile, conf); err != nil {
    34  		log.WithError(err).Fatal("Failed to parse chain config yaml file.")
    35  	}
    36  	log.Debugf("Config file values: %+v", conf)
    37  	OverrideBeaconConfig(conf)
    38  }
    39  
    40  func replaceHexStringWithYAMLFormat(line string) []string {
    41  	parts := strings.Split(line, "0x")
    42  	decoded, err := hex.DecodeString(parts[1])
    43  	if err != nil {
    44  		log.WithError(err).Error("Failed to decode hex string.")
    45  	}
    46  	switch l := len(decoded); {
    47  	case l == 1:
    48  		var b byte
    49  		b = decoded[0]
    50  		fixedByte, err := yaml.Marshal(b)
    51  		if err != nil {
    52  			log.WithError(err).Error("Failed to marshal config file.")
    53  		}
    54  		parts[0] += string(fixedByte)
    55  		parts = parts[:1]
    56  	case l > 1 && l <= 4:
    57  		var arr [4]byte
    58  		copy(arr[:], decoded)
    59  		fixedByte, err := yaml.Marshal(arr)
    60  		if err != nil {
    61  			log.WithError(err).Error("Failed to marshal config file.")
    62  		}
    63  		parts[1] = string(fixedByte)
    64  	case l > 4 && l <= 8:
    65  		var arr [8]byte
    66  		copy(arr[:], decoded)
    67  		fixedByte, err := yaml.Marshal(arr)
    68  		if err != nil {
    69  			log.WithError(err).Error("Failed to marshal config file.")
    70  		}
    71  		parts[1] = string(fixedByte)
    72  	case l > 8 && l <= 16:
    73  		var arr [16]byte
    74  		copy(arr[:], decoded)
    75  		fixedByte, err := yaml.Marshal(arr)
    76  		if err != nil {
    77  			log.WithError(err).Error("Failed to marshal config file.")
    78  		}
    79  		parts[1] = string(fixedByte)
    80  	case l > 16 && l <= 20:
    81  		var arr [20]byte
    82  		copy(arr[:], decoded)
    83  		fixedByte, err := yaml.Marshal(arr)
    84  		if err != nil {
    85  			log.WithError(err).Error("Failed to marshal config file.")
    86  		}
    87  		parts[1] = string(fixedByte)
    88  	case l > 20 && l <= 32:
    89  		var arr [32]byte
    90  		copy(arr[:], decoded)
    91  		fixedByte, err := yaml.Marshal(arr)
    92  		if err != nil {
    93  			log.WithError(err).Error("Failed to marshal config file.")
    94  		}
    95  		parts[1] = string(fixedByte)
    96  	case l > 32 && l <= 48:
    97  		var arr [48]byte
    98  		copy(arr[:], decoded)
    99  		fixedByte, err := yaml.Marshal(arr)
   100  		if err != nil {
   101  			log.WithError(err).Error("Failed to marshal config file.")
   102  		}
   103  		parts[1] = string(fixedByte)
   104  	case l > 48 && l <= 64:
   105  		var arr [64]byte
   106  		copy(arr[:], decoded)
   107  		fixedByte, err := yaml.Marshal(arr)
   108  		if err != nil {
   109  			log.WithError(err).Error("Failed to marshal config file.")
   110  		}
   111  		parts[1] = string(fixedByte)
   112  	case l > 64 && l <= 96:
   113  		var arr [96]byte
   114  		copy(arr[:], decoded)
   115  		fixedByte, err := yaml.Marshal(arr)
   116  		if err != nil {
   117  			log.WithError(err).Error("Failed to marshal config file.")
   118  		}
   119  		parts[1] = string(fixedByte)
   120  	}
   121  	return parts
   122  }