-
Notifications
You must be signed in to change notification settings - Fork 1k
/
loader.go
257 lines (246 loc) · 8.82 KB
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package params
import (
"encoding/hex"
"fmt"
"os"
"strings"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v4/math"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
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
}
func UnmarshalConfig(yamlFile []byte, conf *BeaconChainConfig) (*BeaconChainConfig, error) {
// To track if config name is defined inside config file.
hasConfigName := false
// Convert 0x hex inputs to fixed bytes arrays
lines := strings.Split(string(yamlFile), "\n")
if conf == nil {
if isMinimal(lines) {
conf = MinimalSpecConfig().Copy()
} else {
// Default to using mainnet.
conf = MainnetConfig().Copy()
}
}
for i, line := range lines {
// No need to convert the deposit contract address to byte array (as config expects a string).
if strings.HasPrefix(line, "DEPOSIT_CONTRACT_ADDRESS") {
continue
}
if strings.HasPrefix(line, "CONFIG_NAME") {
hasConfigName = true
}
if !strings.HasPrefix(line, "#") && strings.Contains(line, "0x") {
parts := ReplaceHexStringWithYAMLFormat(line)
lines[i] = strings.Join(parts, "\n")
}
}
yamlFile = []byte(strings.Join(lines, "\n"))
if err := yaml.UnmarshalStrict(yamlFile, conf); err != nil {
if _, ok := err.(*yaml.TypeError); !ok {
return nil, errors.Wrap(err, "Failed to parse chain config yaml file.")
} else {
log.WithError(err).Error("There were some issues parsing the config from a yaml file")
}
}
if !hasConfigName {
conf.ConfigName = DevnetName
}
// recompute SqrRootSlotsPerEpoch constant to handle non-standard values of SlotsPerEpoch
conf.SqrRootSlotsPerEpoch = primitives.Slot(math.IntegerSquareRoot(uint64(conf.SlotsPerEpoch)))
log.Debugf("Config file values: %+v", conf)
return conf, nil
}
func UnmarshalNetConfig(yamlFile []byte, conf *NetworkConfig) (*NetworkConfig, error) {
// Convert 0x hex inputs to fixed bytes arrays
lines := strings.Split(string(yamlFile), "\n")
if conf == nil {
if isMinimal(lines) {
conf = MinimalNetSpecConfig().Copy()
} else {
// Default to using mainnet.
conf = BeaconNetworkConfig().Copy()
}
}
for i, line := range lines {
// No need to convert the deposit contract address to byte array (as config expects a string).
if strings.HasPrefix(line, "DEPOSIT_CONTRACT_ADDRESS") {
continue
}
if !strings.HasPrefix(line, "#") && strings.Contains(line, "0x") {
parts := ReplaceHexStringWithYAMLFormat(line)
lines[i] = strings.Join(parts, "\n")
}
}
yamlFile = []byte(strings.Join(lines, "\n"))
if err := yaml.UnmarshalStrict(yamlFile, conf); err != nil {
if _, ok := err.(*yaml.TypeError); !ok {
return nil, errors.Wrap(err, "Failed to parse chain config yaml file.")
} else {
log.WithError(err).Error("There were some issues parsing the config from a yaml file")
}
}
log.Debugf("Config file values: %+v", conf)
return conf, nil
}
func UnmarshalConfigFile(path string, conf *BeaconChainConfig) (*BeaconChainConfig, error) {
yamlFile, err := os.ReadFile(path) // #nosec G304
if err != nil {
return nil, errors.Wrap(err, "Failed to read chain config file.")
}
return UnmarshalConfig(yamlFile, conf)
}
func UnmarshalNetConfigFile(path string, conf *NetworkConfig) (*NetworkConfig, error) {
yamlFile, err := os.ReadFile(path) // #nosec G304
if err != nil {
return nil, errors.Wrap(err, "Failed to read network config file.")
}
return UnmarshalNetConfig(yamlFile, conf)
}
// 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)
}
// ReplaceHexStringWithYAMLFormat will replace hex strings that the yaml parser will understand.
func ReplaceHexStringWithYAMLFormat(line string) []string {
parts := strings.Split(line, "0x")
decoded, err := hex.DecodeString(parts[1])
if err != nil {
log.WithError(err).Error("Failed to decode hex string.")
}
switch l := len(decoded); {
case l == 1:
var b byte
b = decoded[0]
fixedByte, err := yaml.Marshal(b)
if err != nil {
log.WithError(err).Error("Failed to marshal config file.")
}
parts[0] += string(fixedByte)
parts = parts[:1]
case l > 1 && l <= 4:
var arr [4]byte
copy(arr[:], decoded)
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[:], decoded)
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[:], decoded)
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[:], decoded)
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[:], decoded)
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[:], decoded)
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[:], decoded)
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[:], decoded)
fixedByte, err := yaml.Marshal(arr)
if err != nil {
log.WithError(err).Error("Failed to marshal config file.")
}
parts[1] = string(fixedByte)
}
return parts
}
// 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 {
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_VALIDATORS_PER_WITHDRAWALS_SWEEP: %d", cfg.MaxValidatorsPerWithdrawalsSweep),
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("CAPELLA_FORK_EPOCH: %d", cfg.CapellaForkEpoch),
fmt.Sprintf("CAPELLA_FORK_VERSION: %#x", cfg.CapellaForkVersion),
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),
fmt.Sprintf("DEPOSIT_CONTRACT_ADDRESS: %s", cfg.DepositContractAddress),
fmt.Sprintf("DENEB_FORK_EPOCH: %d", cfg.DenebForkEpoch),
fmt.Sprintf("DENEB_FORK_VERSION: %#x", cfg.DenebForkVersion),
}
yamlFile := []byte(strings.Join(lines, "\n"))
return yamlFile
}