-
Notifications
You must be signed in to change notification settings - Fork 26
/
validators.go
275 lines (242 loc) · 8.02 KB
/
validators.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"github.com/tyler-smith/go-bip39"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v3"
blshd "github.com/protolambda/bls12-381-hd"
blsu "github.com/protolambda/bls12-381-util"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/zrnt/eth2/beacon/phase0"
)
func loadValidatorKeys(spec *common.Spec, mnemonicsConfigPath string, validatorsListPath string, tranchesDir string, ethWithdrawalAddress common.Eth1Address) ([]phase0.KickstartValidatorData, error) {
validators := []phase0.KickstartValidatorData{}
if mnemonicsConfigPath != "" {
val, err := generateValidatorKeysByMnemonic(spec, mnemonicsConfigPath, tranchesDir, ethWithdrawalAddress)
if err != nil {
fmt.Printf("error loading validators from mnemonic yaml (%s): %s\n", mnemonicsConfigPath, err)
} else {
fmt.Printf("generated %d validators from mnemonic yaml (%s)\n", len(val), mnemonicsConfigPath)
validators = append(validators, val...)
}
}
if validatorsListPath != "" {
val, err := loadValidatorsFromFile(spec, validatorsListPath)
if err != nil {
fmt.Printf("error loading validators from validators list (%s): %s\n", validatorsListPath, err)
} else {
fmt.Printf("loaded %d validators from validators list (%s)\n", len(val), validatorsListPath)
validators = append(validators, val...)
}
}
return validators, nil
}
func generateValidatorKeysByMnemonic(spec *common.Spec, mnemonicsConfigPath string, tranchesDir string, ethWithdrawalAddress common.Eth1Address) ([]phase0.KickstartValidatorData, error) {
mnemonics, err := loadMnemonics(mnemonicsConfigPath)
if err != nil {
return nil, err
}
//var validators []phase0.KickstartValidatorData
var valCount uint64 = 0
for _, mnemonicSrc := range mnemonics {
valCount += mnemonicSrc.Count
}
validators := make([]phase0.KickstartValidatorData, valCount)
offset := uint64(0)
for m, mnemonicSrc := range mnemonics {
var g errgroup.Group
g.SetLimit(10_000) // when generating large states, do squeeze processing, but do not go out of memory
var prog int32
fmt.Printf("processing mnemonic %d, for %d validators\n", m, mnemonicSrc.Count)
seed, err := seedFromMnemonic(mnemonicSrc.Mnemonic)
if err != nil {
return nil, fmt.Errorf("mnemonic %d is bad", m)
}
pubs := make([]string, mnemonicSrc.Count)
for i := uint64(0); i < mnemonicSrc.Count; i++ {
valIndex := offset + i
idx := i
g.Go(func() error {
signingSK, err := blshd.SecretKeyFromHD(seed, validatorKeyName(idx))
if err != nil {
return err
}
var blsSK blsu.SecretKey
if err := blsSK.Deserialize(signingSK); err != nil {
return fmt.Errorf("failed to decode derived secret key: %w", err)
}
pub, err := blsu.SkToPk(&blsSK)
if err != nil {
return fmt.Errorf("failed to compute pubkey: %w", err)
}
// BLS signing key
var data phase0.KickstartValidatorData
data.Pubkey = pub.Serialize()
pubs[idx] = data.Pubkey.String()
if ethWithdrawalAddress == (common.Eth1Address{}) {
// BLS withdrawal credentials
withdrawalSK, err := blshd.SecretKeyFromHD(seed, withdrawalKeyName(idx))
if err != nil {
return err
}
var withdrawalBlsSK blsu.SecretKey
if err := withdrawalBlsSK.Deserialize(withdrawalSK); err != nil {
return fmt.Errorf("failed to decode derived secret key: %w", err)
}
withdrawalPub, err := blsu.SkToPk(&withdrawalBlsSK)
if err != nil {
return fmt.Errorf("failed to compute pubkey: %w", err)
}
withdrawalPubBytes := withdrawalPub.Serialize()
h := sha256.New()
h.Write(withdrawalPubBytes[:])
copy(data.WithdrawalCredentials[:], h.Sum(nil))
data.WithdrawalCredentials[0] = common.BLS_WITHDRAWAL_PREFIX
} else {
// spec:
// The withdrawal_credentials field must be such that:
// withdrawal_credentials[:1] == ETH1_ADDRESS_WITHDRAWAL_PREFIX
// withdrawal_credentials[1:12] == b'\x00' * 11
// withdrawal_credentials[12:] == eth1_withdrawal_address
data.WithdrawalCredentials[0] = common.ETH1_ADDRESS_WITHDRAWAL_PREFIX
copy(data.WithdrawalCredentials[12:], ethWithdrawalAddress[:])
}
// Max effective balance by default for activation
data.Balance = spec.MAX_EFFECTIVE_BALANCE
validators[valIndex] = data
count := atomic.AddInt32(&prog, 1)
if count%100 == 0 {
fmt.Printf("...validator %d/%d\n", prog, mnemonicSrc.Count)
}
return nil
})
}
offset += mnemonicSrc.Count
if err := g.Wait(); err != nil {
return nil, err
}
fmt.Println("Writing pubkeys list file...")
if err := outputPubkeys(filepath.Join(tranchesDir, fmt.Sprintf("tranche_%04d.txt", m)), pubs); err != nil {
return nil, err
}
}
return validators, nil
}
func validatorKeyName(i uint64) string {
return fmt.Sprintf("m/12381/3600/%d/0/0", i)
}
func withdrawalKeyName(i uint64) string {
return fmt.Sprintf("m/12381/3600/%d/0", i)
}
func seedFromMnemonic(mnemonic string) (seed []byte, err error) {
mnemonic = strings.TrimSpace(mnemonic)
if !bip39.IsMnemonicValid(mnemonic) {
return nil, errors.New("mnemonic is not valid")
}
return bip39.NewSeed(mnemonic, ""), nil
}
func outputPubkeys(outPath string, data []string) error {
f, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY, 0777)
if err != nil {
return err
}
defer f.Close()
for _, p := range data {
if _, err := f.WriteString(p + "\n"); err != nil {
return err
}
}
return nil
}
type MnemonicSrc struct {
Mnemonic string `yaml:"mnemonic"`
Count uint64 `yaml:"count"`
}
func loadMnemonics(srcPath string) ([]MnemonicSrc, error) {
f, err := os.Open(srcPath)
if err != nil {
return nil, err
}
defer f.Close()
var data []MnemonicSrc
dec := yaml.NewDecoder(f)
if err := dec.Decode(&data); err != nil {
return nil, err
}
return data, nil
}
func loadValidatorsFromFile(spec *common.Spec, validatorsConfigPath string) ([]phase0.KickstartValidatorData, error) {
validatorsFile, err := os.Open(validatorsConfigPath)
if err != nil {
return nil, err
}
defer validatorsFile.Close()
validators := make([]phase0.KickstartValidatorData, 0)
pubkeyMap := map[string]int{}
scanner := bufio.NewScanner(validatorsFile)
lineNum := 0
for scanner.Scan() {
line := scanner.Text()
lineNum++
if line == "" || strings.HasPrefix(line, "#") {
continue
}
lineParts := strings.Split(line, ":")
validatorEntry := phase0.KickstartValidatorData{}
// Public key
pubKey, err := hex.DecodeString(strings.Replace(lineParts[0], "0x", "", -1))
if err != nil {
return nil, err
}
if len(pubKey) != 48 {
return nil, errors.New(fmt.Sprintf("invalid pubkey (invalid length) on line %v", lineNum))
}
if pubkeyMap[string(pubKey)] != 0 {
return nil, errors.New(fmt.Sprintf("duplicate pubkey on line %v and %v", pubkeyMap[string(pubKey)], lineNum))
}
pubkeyMap[string(pubKey)] = lineNum
copy(validatorEntry.Pubkey[:], pubKey)
// Withdrawal credentials
withdrawalCred, err := hex.DecodeString(strings.Replace(lineParts[1], "0x", "", -1))
if err != nil {
return nil, err
}
if len(withdrawalCred) != 32 {
return nil, errors.New(fmt.Sprintf("invalid withdrawal credentials (invalid length) on line %v", lineNum))
}
switch withdrawalCred[0] {
case 0x00:
break
case 0x01:
if !bytes.Equal(withdrawalCred[1:12], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) {
return nil, errors.New(fmt.Sprintf("invalid withdrawal credentials (invalid 0x01 cred) on line %v", lineNum))
}
break
default:
return nil, errors.New(fmt.Sprintf("invalid withdrawal credentials (invalid type) on line %v", lineNum))
}
copy(validatorEntry.WithdrawalCredentials[:], withdrawalCred)
// Validator balance
if len(lineParts) > 2 {
balance, err := strconv.ParseUint(string(lineParts[2]), 10, 64)
if err != nil {
return nil, err
}
validatorEntry.Balance = common.Gwei(balance)
} else {
validatorEntry.Balance = spec.MAX_EFFECTIVE_BALANCE
}
validators = append(validators, validatorEntry)
}
return validators, nil
}