-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
244 lines (211 loc) · 6.98 KB
/
main.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
package main
import (
"context"
"encoding/hex"
"fmt"
"os"
"strings"
"time"
eth2client "github.com/attestantio/go-eth2-client"
api "github.com/attestantio/go-eth2-client/api/v1"
hbls "github.com/herumi/bls-eth-go-binary/bls"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/tyler-smith/go-bip39"
util "github.com/wealdtech/ethdo/util"
goEthUtil "github.com/wealdtech/go-eth2-util"
"github.com/wealdtech/go-string2eth"
)
func init() {
hbls.Init(hbls.BLS12_381)
hbls.SetETHmode(hbls.EthModeLatest)
}
const (
_exitSuccess = 0
_exitFailure = 1
)
func makeCheckErr(cmd *cobra.Command) func(err error, msg string) {
return func(err error, msg string) {
if err != nil {
if msg != "" {
err = fmt.Errorf("%s: %v", msg, err)
}
cmd.PrintErr(err)
os.Exit(1)
}
}
}
// errCheck checks for an error and quits if it is present
func errCheck(err error, msg string) {
if err != nil {
if msg == "" {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
} else {
fmt.Fprintf(os.Stderr, "%s: %s\n", msg, err.Error())
}
os.Exit(1)
}
}
// Narrow pubkeys: we don't want 0xAb... to be different from ab...
func narrowedPubkey(pub string) string {
return strings.TrimPrefix(strings.ToLower(pub), "0x")
}
func mnemonicToSeed(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 validatorsFromMnemonic(mnemonic string, accountMin uint64, accountMax uint64) map[string]string {
seed, err := mnemonicToSeed(mnemonic)
errCheck(err, "failed to generate seed from mnemonic")
var validators map[string]string
validators = make(map[string]string)
for i := accountMin; i < accountMax; i++ {
idx := i
path := fmt.Sprintf("m/12381/3600/%d/0/0", idx)
validatorPrivkey, err := goEthUtil.PrivateKeyFromSeedAndPath(seed, path)
errCheck(err, "failed to derive validator private key")
pubkey := narrowedPubkey(hex.EncodeToString(validatorPrivkey.PublicKey().Marshal()))
pubkey = fmt.Sprintf("0x%s", pubkey)
validators[pubkey] = hex.EncodeToString(validatorPrivkey.Marshal())
}
return validators
}
func printValidatorStatus(validator *api.Validator) {
if validator.Status.IsPending() || validator.Status.HasActivated() {
fmt.Printf("Index: %d\n", validator.Index)
}
if validator.Status.IsPending() {
fmt.Printf("Activation eligibility epoch: %d\n", validator.Validator.ActivationEligibilityEpoch)
}
if validator.Status.HasActivated() {
fmt.Printf("Activation epoch: %d\n", validator.Validator.ActivationEpoch)
}
fmt.Printf("Public key: %#x\n", validator.Validator.PublicKey)
fmt.Printf("Status: %v\n", validator.Status)
switch validator.Status {
case api.ValidatorStateActiveExiting, api.ValidatorStateActiveSlashed:
fmt.Printf("Exit epoch: %d\n", validator.Validator.ExitEpoch)
case api.ValidatorStateExitedUnslashed, api.ValidatorStateExitedSlashed:
fmt.Printf("Withdrawable epoch: %d\n", validator.Validator.WithdrawableEpoch)
}
fmt.Printf("Balance: %s\n", string2eth.GWeiToString(uint64(validator.Balance), true))
if validator.Status.IsActive() {
fmt.Printf("Effective balance: %s\n", string2eth.GWeiToString(uint64(validator.Validator.EffectiveBalance), true))
}
fmt.Printf("Withdrawal credentials: %#x\n", validator.Validator.WithdrawalCredentials)
}
func printValidatorsStatus(vals []*api.Validator) {
for _, validator := range vals {
printValidatorStatus(validator)
}
}
func checkValidatorsStatus(vals map[string]string) []*api.Validator {
ctx := context.Background()
eth2Client, err := util.ConnectToBeaconNode(ctx,
"",
10*time.Second,
false)
errCheck(err, "Failed to connect to Ethereum 2 beacon node")
validatorPubKeys := []string{}
for valPubKey, _ := range vals {
validatorPubKeys = append(validatorPubKeys, valPubKey)
}
validators, err := util.ParseValidators(ctx, eth2Client.(eth2client.ValidatorsProvider), validatorPubKeys, "head")
errCheck(err, "Failed to obtain validator")
network, err := util.Network(ctx, eth2Client)
errCheck(err, "Failed to obtain network")
fmt.Sprintf("Network is %s", network)
return validators
}
func runStatus(sourceMnemonic string, accountMin uint64, accountMax uint64) {
fmt.Println("Checking validators' status")
vals := validatorsFromMnemonic(sourceMnemonic, accountMin, accountMax)
validators := checkValidatorsStatus(vals)
printValidatorsStatus(validators)
}
func delayOneSlot() {
time.Sleep(time.Duration(12) * time.Second)
}
func Status() *cobra.Command {
var sourceMnemonic string
var accountMin uint64
var accountMax uint64
var onSlot bool
cmd := &cobra.Command{
Use: "status",
Short: "check the status of all validators",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if onSlot {
fmt.Println("Checking validator status on each slot update")
for {
runStatus(sourceMnemonic, accountMin, accountMax)
delayOneSlot()
}
} else {
runStatus(sourceMnemonic, accountMin, accountMax)
}
},
}
cmd.Flags().StringVar(&sourceMnemonic, "source-mnemonic", "", "The validators mnemonic to source account keys from.")
cmd.Flags().Uint64Var(&accountMin, "source-min", 0, "Minimum validator index in HD path range (incl.)")
cmd.Flags().Uint64Var(&accountMax, "source-max", 0, "Maximum validator index in HD path range (excl.)")
cmd.Flags().BoolVar(&onSlot, "on-slot", false, "if set the status will be checked on each slot update")
return cmd
}
func Deposit() *cobra.Command {
cmd := &cobra.Command{
Use: "deposit",
Short: "build and submit deposit messages for all validators...",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Building and submitting deposit messages for all validators...")
},
}
return cmd
}
func BlsExecutionChange() *cobra.Command {
cmd := &cobra.Command{
Use: "bls-change",
Short: "build and submit BLS Execution Change messages for all validators...",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Building and submitting BLS Execution Change messages...")
},
}
return cmd
}
func Withdrawal() *cobra.Command {
cmd := &cobra.Command{
Use: "withdrawal",
Short: "build and submit withdrawal messages for all validators...",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Building and submitting withdrawal messages for all validators...")
},
}
return cmd
}
func main() {
rootCmd := &cobra.Command{
Use: "shapella-fuzz",
Short: "Create Deposits, BlsExecutionChange, and Withdrawal messages",
Long: "Create Deposits, BlsExecutionChange, and Withdrawal messages." +
"standing on the shoulders of ethdo and eth2-val-tools",
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
}
rootCmd.AddCommand(Status())
rootCmd.AddCommand(Deposit())
rootCmd.AddCommand(BlsExecutionChange())
rootCmd.AddCommand(Withdrawal())
rootCmd.SetOut(os.Stdout)
rootCmd.SetErr(os.Stderr)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}