-
Notifications
You must be signed in to change notification settings - Fork 84
/
controller_shares.go
68 lines (62 loc) · 2.24 KB
/
controller_shares.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
package validator
import (
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/pkg/errors"
"go.uber.org/zap"
)
// initShares initializes shares, should be called upon creation of controller
func (c *controller) initShares(options ControllerOptions) error {
if options.CleanRegistryData {
if err := c.collection.CleanRegistryData(); err != nil {
return errors.Wrap(err, "failed to clean validator storage registry data")
}
c.logger.Debug("all shares were removed")
}
if len(options.Shares) > 0 {
c.loadSharesFromConfig(options.Shares)
}
return nil
}
func (c *controller) loadSharesFromConfig(items []ShareOptions) {
var addedValidators []string
if len(items) > 0 {
c.logger.Info("loading validators share from config", zap.Int("count", len(items)))
for _, opts := range items {
pubkey, err := c.loadShare(opts)
if err != nil {
c.logger.Error("failed to load validator share data from config", zap.Error(err))
continue
}
addedValidators = append(addedValidators, pubkey)
}
c.logger.Info("successfully loaded validators from config", zap.Strings("pubkeys", addedValidators))
}
}
func (c *controller) loadShare(options ShareOptions) (string, error) {
if len(options.PublicKey) == 0 || len(options.ShareKey) == 0 || len(options.Committee) == 0 {
return "", errors.New("one or more fields are missing (PublicKey, ShareKey, Committee)")
}
share, err := options.ToShare()
if err != nil {
return "", errors.WithMessage(err, "failed to create share object")
}
shareKey := &bls.SecretKey{}
if err = shareKey.SetHexString(options.ShareKey); err != nil {
return "", errors.Wrap(err, "failed to set hex private key")
}
if share != nil {
if updated, err := UpdateShareMetadata(share, c.beacon); err != nil {
return "", errors.Wrap(err, "could not update validator metadata")
} else if !updated {
return "", errors.New("could not find validator metadata")
}
if err := c.keyManager.AddShare(shareKey); err != nil {
return "", errors.Wrap(err, "could not save share key from share options")
}
if err := c.collection.SaveValidatorShare(share); err != nil {
return "", errors.Wrap(err, "could not save share from share options")
}
return options.PublicKey, err
}
return "", errors.New("returned nil share")
}