/
accounts_exit.go
199 lines (177 loc) · 6.49 KB
/
accounts_exit.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
package accounts
import (
"bytes"
"context"
"encoding/json"
"fmt"
"path"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/blocks"
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
"github.com/prysmaticlabs/prysm/v4/config/params"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v4/io/file"
eth "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v4/validator/client"
beacon_api "github.com/prysmaticlabs/prysm/v4/validator/client/beacon-api"
"github.com/prysmaticlabs/prysm/v4/validator/client/iface"
"github.com/prysmaticlabs/prysm/v4/validator/keymanager"
"google.golang.org/protobuf/types/known/emptypb"
)
// PerformExitCfg for account voluntary exits.
type PerformExitCfg struct {
ValidatorClient iface.ValidatorClient
NodeClient iface.NodeClient
Keymanager keymanager.IKeymanager
RawPubKeys [][]byte
FormattedPubKeys []string
OutputDirectory string
}
// Exit performs a voluntary exit on one or more accounts.
func (acm *CLIManager) Exit(ctx context.Context) error {
// User decided to cancel the voluntary exit.
if acm.rawPubKeys == nil && acm.formattedPubKeys == nil {
return nil
}
validatorClient, nodeClient, err := acm.prepareBeaconClients(ctx)
if err != nil {
return err
}
if nodeClient == nil {
return errors.New("could not prepare beacon node client")
}
syncStatus, err := (*nodeClient).GetSyncStatus(ctx, &emptypb.Empty{})
if err != nil {
return err
}
if syncStatus == nil {
return errors.New("could not get sync status")
}
if syncStatus.Syncing {
return errors.New("could not perform exit: beacon node is syncing.")
}
cfg := PerformExitCfg{
*validatorClient,
*nodeClient,
acm.keymanager,
acm.rawPubKeys,
acm.formattedPubKeys,
acm.exitJSONOutputPath,
}
rawExitedKeys, trimmedExitedKeys, err := PerformVoluntaryExit(ctx, cfg)
if err != nil {
return err
}
displayExitInfo(rawExitedKeys, trimmedExitedKeys)
return nil
}
// PerformVoluntaryExit uses gRPC clients to submit a voluntary exit message to a beacon node.
func PerformVoluntaryExit(
ctx context.Context, cfg PerformExitCfg,
) (rawExitedKeys [][]byte, formattedExitedKeys []string, err error) {
var rawNotExitedKeys [][]byte
genesisResponse, err := cfg.NodeClient.GetGenesis(ctx, &emptypb.Empty{})
if err != nil {
log.WithError(err).Errorf("voluntary exit failed: %v", err)
}
for i, key := range cfg.RawPubKeys {
// When output directory is present, only create the signed exit, but do not propose it.
// Otherwise, propose the exit immediately.
epoch, err := client.CurrentEpoch(genesisResponse.GenesisTime)
if err != nil {
log.WithError(err).Errorf("voluntary exit failed: %v", err)
}
if len(cfg.OutputDirectory) > 0 {
sve, err := client.CreateSignedVoluntaryExit(ctx, cfg.ValidatorClient, cfg.Keymanager.Sign, key, epoch)
if err != nil {
rawNotExitedKeys = append(rawNotExitedKeys, key)
msg := err.Error()
if strings.Contains(msg, blocks.ValidatorAlreadyExitedMsg) ||
strings.Contains(msg, blocks.ValidatorCannotExitYetMsg) {
log.Warningf("Could not create voluntary exit for account %s: %s", cfg.FormattedPubKeys[i], msg)
} else {
log.WithError(err).Errorf("voluntary exit failed for account %s", cfg.FormattedPubKeys[i])
}
} else if err := writeSignedVoluntaryExitJSON(sve, cfg.OutputDirectory); err != nil {
log.WithError(err).Error("failed to write voluntary exit")
}
} else if err := client.ProposeExit(ctx, cfg.ValidatorClient, cfg.Keymanager.Sign, key, epoch); err != nil {
rawNotExitedKeys = append(rawNotExitedKeys, key)
msg := err.Error()
if strings.Contains(msg, blocks.ValidatorAlreadyExitedMsg) ||
strings.Contains(msg, blocks.ValidatorCannotExitYetMsg) {
log.Warningf("Could not perform voluntary exit for account %s: %s", cfg.FormattedPubKeys[i], msg)
} else {
log.WithError(err).Errorf("voluntary exit failed for account %s", cfg.FormattedPubKeys[i])
}
}
}
rawExitedKeys = make([][]byte, 0)
formattedExitedKeys = make([]string, 0)
for i, key := range cfg.RawPubKeys {
found := false
for _, notExited := range rawNotExitedKeys {
if bytes.Equal(notExited, key) {
found = true
break
}
}
if !found {
rawExitedKeys = append(rawExitedKeys, key)
formattedExitedKeys = append(formattedExitedKeys, cfg.FormattedPubKeys[i])
}
}
return rawExitedKeys, formattedExitedKeys, nil
}
func prepareAllKeys(validatingKeys [][fieldparams.BLSPubkeyLength]byte) (raw [][]byte, formatted []string) {
raw = make([][]byte, len(validatingKeys))
formatted = make([]string, len(validatingKeys))
for i, pk := range validatingKeys {
raw[i] = make([]byte, len(pk))
copy(raw[i], pk[:])
formatted[i] = fmt.Sprintf("%#x", bytesutil.Trunc(pk[:]))
}
return
}
func displayExitInfo(rawExitedKeys [][]byte, trimmedExitedKeys []string) {
if len(rawExitedKeys) > 0 {
urlFormattedPubKeys := make([]string, len(rawExitedKeys))
for i, key := range rawExitedKeys {
var baseUrl string
if params.BeaconConfig().ConfigName == params.PraterName || params.BeaconConfig().ConfigName == params.GoerliName {
baseUrl = "https://goerli.beaconcha.in/validator/"
} else {
baseUrl = "https://beaconcha.in/validator/"
}
// Remove '0x' prefix
urlFormattedPubKeys[i] = baseUrl + hexutil.Encode(key)[2:]
}
ifaceKeys := make([]interface{}, len(urlFormattedPubKeys))
for i, k := range urlFormattedPubKeys {
ifaceKeys[i] = k
}
info := fmt.Sprintf("Voluntary exit was successful for the accounts listed. "+
"URLs where you can track each validator's exit:\n"+strings.Repeat("%s\n", len(ifaceKeys)), ifaceKeys...)
log.WithField("publicKeys", strings.Join(trimmedExitedKeys, ", ")).Info(info)
} else {
log.Info("No successful voluntary exits")
}
}
func writeSignedVoluntaryExitJSON(sve *eth.SignedVoluntaryExit, outputDirectory string) error {
if err := file.MkdirAll(outputDirectory); err != nil {
return err
}
jsve := beacon_api.JsonifySignedVoluntaryExits([]*eth.SignedVoluntaryExit{sve})[0]
b, err := json.Marshal(jsve)
if err != nil {
return errors.Wrap(err, "failed to marshal JSON signed voluntary exit")
}
filepath := path.Join(outputDirectory, fmt.Sprintf("validator-exit-%s.json", jsve.Exit.ValidatorIndex))
if err := file.WriteFile(filepath, b); err != nil {
return errors.Wrap(err, "failed to write validator exist json")
}
log.Infof("Wrote signed validator exit JSON to %s", filepath)
return nil
}