/
validator.go
374 lines (334 loc) · 12.5 KB
/
validator.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package components
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"syscall"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
cmdshared "github.com/prysmaticlabs/prysm/v4/cmd"
"github.com/prysmaticlabs/prysm/v4/cmd/validator/flags"
"github.com/prysmaticlabs/prysm/v4/config/features"
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
"github.com/prysmaticlabs/prysm/v4/config/params"
"github.com/prysmaticlabs/prysm/v4/io/file"
validatorpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1/validator-client"
"github.com/prysmaticlabs/prysm/v4/runtime/interop"
"github.com/prysmaticlabs/prysm/v4/testing/endtoend/helpers"
e2e "github.com/prysmaticlabs/prysm/v4/testing/endtoend/params"
e2etypes "github.com/prysmaticlabs/prysm/v4/testing/endtoend/types"
)
const DefaultFeeRecipientAddress = "0x099FB65722e7b2455043bfebF6177f1D2E9738d9"
var _ e2etypes.ComponentRunner = (*ValidatorNode)(nil)
var _ e2etypes.ComponentRunner = (*ValidatorNodeSet)(nil)
var _ e2etypes.MultipleComponentRunners = (*ValidatorNodeSet)(nil)
// ValidatorNodeSet represents set of validator nodes.
type ValidatorNodeSet struct {
e2etypes.ComponentRunner
config *e2etypes.E2EConfig
started chan struct{}
nodes []e2etypes.ComponentRunner
}
// NewValidatorNodeSet creates and returns a set of validator nodes.
func NewValidatorNodeSet(config *e2etypes.E2EConfig) *ValidatorNodeSet {
return &ValidatorNodeSet{
config: config,
started: make(chan struct{}, 1),
}
}
// Start starts the configured amount of validators, also sending and mining their deposits.
func (s *ValidatorNodeSet) Start(ctx context.Context) error {
// Always using genesis count since using anything else would be difficult to test for.
validatorNum := int(params.BeaconConfig().MinGenesisActiveValidatorCount)
prysmBeaconNodeNum := e2e.TestParams.BeaconNodeCount
beaconNodeNum := prysmBeaconNodeNum + e2e.TestParams.LighthouseBeaconNodeCount
if validatorNum%beaconNodeNum != 0 {
return errors.New("validator count is not easily divisible by beacon node count")
}
validatorsPerNode := validatorNum / beaconNodeNum
// Create validator nodes.
nodes := make([]e2etypes.ComponentRunner, prysmBeaconNodeNum)
for i := 0; i < prysmBeaconNodeNum; i++ {
nodes[i] = NewValidatorNode(s.config, validatorsPerNode, i, validatorsPerNode*i)
}
s.nodes = nodes
// Wait for all nodes to finish their job (blocking).
// Once nodes are ready passed in handler function will be called.
return helpers.WaitOnNodes(ctx, nodes, func() {
// All nodes started, close channel, so that all services waiting on a set, can proceed.
close(s.started)
})
}
// Started checks whether validator node set is started and all nodes are ready to be queried.
func (s *ValidatorNodeSet) Started() <-chan struct{} {
return s.started
}
// Pause pauses the component and its underlying process.
func (s *ValidatorNodeSet) Pause() error {
for _, n := range s.nodes {
if err := n.Pause(); err != nil {
return err
}
}
return nil
}
// Resume resumes the component and its underlying process.
func (s *ValidatorNodeSet) Resume() error {
for _, n := range s.nodes {
if err := n.Resume(); err != nil {
return err
}
}
return nil
}
// Stop stops the component and its underlying process.
func (s *ValidatorNodeSet) Stop() error {
for _, n := range s.nodes {
if err := n.Stop(); err != nil {
return err
}
}
return nil
}
// PauseAtIndex pauses the component and its underlying process at the desired index.
func (s *ValidatorNodeSet) PauseAtIndex(i int) error {
if i >= len(s.nodes) {
return errors.Errorf("provided index exceeds slice size: %d >= %d", i, len(s.nodes))
}
return s.nodes[i].Pause()
}
// ResumeAtIndex resumes the component and its underlying process at the desired index.
func (s *ValidatorNodeSet) ResumeAtIndex(i int) error {
if i >= len(s.nodes) {
return errors.Errorf("provided index exceeds slice size: %d >= %d", i, len(s.nodes))
}
return s.nodes[i].Resume()
}
// StopAtIndex stops the component and its underlying process at the desired index.
func (s *ValidatorNodeSet) StopAtIndex(i int) error {
if i >= len(s.nodes) {
return errors.Errorf("provided index exceeds slice size: %d >= %d", i, len(s.nodes))
}
return s.nodes[i].Stop()
}
// ComponentAtIndex returns the component at the provided index.
func (s *ValidatorNodeSet) ComponentAtIndex(i int) (e2etypes.ComponentRunner, error) {
if i >= len(s.nodes) {
return nil, errors.Errorf("provided index exceeds slice size: %d >= %d", i, len(s.nodes))
}
return s.nodes[i], nil
}
// ValidatorNode represents a validator node.
type ValidatorNode struct {
e2etypes.ComponentRunner
config *e2etypes.E2EConfig
started chan struct{}
validatorNum int
index int
offset int
cmd *exec.Cmd
}
// NewValidatorNode creates and returns a validator node.
func NewValidatorNode(config *e2etypes.E2EConfig, validatorNum, index, offset int) *ValidatorNode {
return &ValidatorNode{
config: config,
validatorNum: validatorNum,
index: index,
offset: offset,
started: make(chan struct{}, 1),
}
}
func (node *ValidatorNode) saveConfig() (string, error) {
cfg := params.BeaconConfig().Copy()
cfgBytes := params.ConfigToYaml(cfg)
cfgDir := path.Join(e2e.TestParams.TestPath, fmt.Sprintf("config/%d", node.index))
if err := file.MkdirAll(cfgDir); err != nil {
return "", err
}
cfgPath := path.Join(cfgDir, "validator-config.yaml")
return cfgPath, file.WriteFile(cfgPath, cfgBytes)
}
// Start starts a validator client.
func (v *ValidatorNode) Start(ctx context.Context) error {
validatorHexPubKeys := make([]string, 0)
var pkg, target string
if v.config.UsePrysmShValidator {
pkg = ""
target = "prysm_sh"
} else {
pkg = "cmd/validator"
target = "validator"
}
binaryPath, found := bazel.FindBinary(pkg, target)
if !found {
return errors.New("validator binary not found")
}
config, validatorNum, index, offset := v.config, v.validatorNum, v.index, v.offset
beaconRPCPort := e2e.TestParams.Ports.PrysmBeaconNodeRPCPort + index
if beaconRPCPort >= e2e.TestParams.Ports.PrysmBeaconNodeRPCPort+e2e.TestParams.BeaconNodeCount {
// Point any extra validator clients to a node we know is running.
beaconRPCPort = e2e.TestParams.Ports.PrysmBeaconNodeRPCPort
}
file, err := helpers.DeleteAndCreateFile(e2e.TestParams.LogPath, fmt.Sprintf(e2e.ValidatorLogFileName, index))
if err != nil {
return err
}
gFile, err := helpers.GraffitiYamlFile(e2e.TestParams.TestPath)
if err != nil {
return err
}
_, pubs, err := interop.DeterministicallyGenerateKeys(uint64(offset), uint64(validatorNum))
if err != nil {
return err
}
for _, pub := range pubs {
validatorHexPubKeys = append(validatorHexPubKeys, hexutil.Encode(pub.Marshal()))
}
proposerSettingsPathPath, err := createProposerSettingsPath(validatorHexPubKeys, index)
if err != nil {
return err
}
cfgPath, err := v.saveConfig()
if err != nil {
return err
}
args := []string{
fmt.Sprintf("--%s=%s/eth2-val-%d", cmdshared.DataDirFlag.Name, e2e.TestParams.TestPath, index),
fmt.Sprintf("--%s=%s", cmdshared.LogFileName.Name, file.Name()),
fmt.Sprintf("--%s=%s", flags.GraffitiFileFlag.Name, gFile),
fmt.Sprintf("--%s=%d", flags.MonitoringPortFlag.Name, e2e.TestParams.Ports.ValidatorMetricsPort+index),
fmt.Sprintf("--%s=%d", flags.GRPCGatewayPort.Name, e2e.TestParams.Ports.ValidatorGatewayPort+index),
fmt.Sprintf("--%s=localhost:%d", flags.BeaconRPCProviderFlag.Name, beaconRPCPort),
fmt.Sprintf("--%s=%s", flags.GrpcHeadersFlag.Name, "dummy=value,foo=bar"), // Sending random headers shouldn't break anything.
fmt.Sprintf("--%s=%s", cmdshared.VerbosityFlag.Name, "debug"),
fmt.Sprintf("--%s=%s", flags.ProposerSettingsFlag.Name, proposerSettingsPathPath),
fmt.Sprintf("--%s=%s", cmdshared.ChainConfigFileFlag.Name, cfgPath),
"--" + cmdshared.ForceClearDB.Name,
"--" + cmdshared.AcceptTosFlag.Name,
}
if v.config.UseBeaconRestApi {
beaconRestApiPort := e2e.TestParams.Ports.PrysmBeaconNodeGatewayPort + index
if beaconRestApiPort >= e2e.TestParams.Ports.PrysmBeaconNodeGatewayPort+e2e.TestParams.BeaconNodeCount {
// Point any extra validator clients to a node we know is running.
beaconRestApiPort = e2e.TestParams.Ports.PrysmBeaconNodeGatewayPort
}
args = append(args,
fmt.Sprintf("--%s=http://localhost:%d", flags.BeaconRESTApiProviderFlag.Name, beaconRestApiPort),
fmt.Sprintf("--%s", features.EnableBeaconRESTApi.Name))
}
// Only apply e2e flags to the current branch. New flags may not exist in previous release.
if !v.config.UsePrysmShValidator {
args = append(args, features.E2EValidatorFlags...)
}
if v.config.UseWeb3RemoteSigner {
// Write the pubkeys as comma separated hex strings with 0x prefix.
// See: https://docs.teku.consensys.net/en/latest/HowTo/External-Signer/Use-External-Signer/
args = append(args,
fmt.Sprintf("--%s=http://localhost:%d", flags.Web3SignerURLFlag.Name, Web3RemoteSignerPort),
fmt.Sprintf("--%s=%s", flags.Web3SignerPublicValidatorKeysFlag.Name, strings.Join(validatorHexPubKeys, ",")))
} else {
// When not using remote key signer, use interop keys.
args = append(args,
fmt.Sprintf("--%s=%d", flags.InteropNumValidators.Name, validatorNum),
fmt.Sprintf("--%s=%d", flags.InteropStartIndex.Name, offset),
)
}
if v.config.UseBuilder {
args = append(args, fmt.Sprintf("--%s", flags.EnableBuilderFlag.Name))
}
args = append(args, config.ValidatorFlags...)
if v.config.UsePrysmShValidator {
args = append([]string{"validator"}, args...)
log.Warning("Using latest release validator via prysm.sh")
}
cmd := exec.CommandContext(ctx, binaryPath, args...) // #nosec G204 -- Safe
// Write stdout and stderr to log files.
stdout, err := os.Create(path.Join(e2e.TestParams.LogPath, fmt.Sprintf("validator_%d_stdout.log", index)))
if err != nil {
return err
}
stderr, err := os.Create(path.Join(e2e.TestParams.LogPath, fmt.Sprintf("validator_%d_stderr.log", index)))
if err != nil {
return err
}
defer func() {
if err := stdout.Close(); err != nil {
log.WithError(err).Error("Failed to close stdout file")
}
if err := stderr.Close(); err != nil {
log.WithError(err).Error("Failed to close stderr file")
}
}()
cmd.Stdout = stdout
cmd.Stderr = stderr
log.Infof("Starting validator client %d with flags: %s %s", index, binaryPath, strings.Join(args, " "))
if err = cmd.Start(); err != nil {
return err
}
// Mark node as ready.
close(v.started)
v.cmd = cmd
return cmd.Wait()
}
// Started checks whether validator node is started and ready to be queried.
func (v *ValidatorNode) Started() <-chan struct{} {
return v.started
}
// Pause pauses the component and its underlying process.
func (v *ValidatorNode) Pause() error {
return v.cmd.Process.Signal(syscall.SIGSTOP)
}
// Resume resumes the component and its underlying process.
func (v *ValidatorNode) Resume() error {
return v.cmd.Process.Signal(syscall.SIGCONT)
}
// Stop stops the component and its underlying process.
func (v *ValidatorNode) Stop() error {
return v.cmd.Process.Kill()
}
func (v *ValidatorNode) UnderlyingProcess() *os.Process {
return v.cmd.Process
}
func createProposerSettingsPath(pubkeys []string, nodeIdx int) (string, error) {
testNetDir := e2e.TestParams.TestPath + fmt.Sprintf("/proposer-settings/validator_%d", nodeIdx)
configPath := filepath.Join(testNetDir, "config.json")
if len(pubkeys) == 0 {
return "", errors.New("number of validators must be greater than 0")
}
config := make(map[string]*validatorpb.ProposerOptionPayload)
for i, pubkey := range pubkeys {
config[pubkeys[i]] = &validatorpb.ProposerOptionPayload{
FeeRecipient: FeeRecipientFromPubkey(pubkey),
}
}
proposerSettingsPayload := &validatorpb.ProposerSettingsPayload{
ProposerConfig: config,
DefaultConfig: &validatorpb.ProposerOptionPayload{
FeeRecipient: DefaultFeeRecipientAddress,
},
}
jsonBytes, err := json.Marshal(proposerSettingsPayload)
if err != nil {
return "", err
}
if err := file.MkdirAll(testNetDir); err != nil {
return "", err
}
if err := file.WriteFile(configPath, jsonBytes); err != nil {
return "", err
}
return configPath, nil
}
// FeeRecipientFromPubkey slices, from the beginning of the hex-encoded pubkey string, the 2 character 0x preamble
// plus enough hex chars to fill out the fee_recipient byte value.
func FeeRecipientFromPubkey(key string) string {
// pubkey[:(2+fieldparams.FeeRecipientLength*2)] slicing 2 (for the 0x preamble) + 2 hex chars for each byte
return common.HexToAddress(key[:(2 + fieldparams.FeeRecipientLength*2)]).Hex()
}