-
Notifications
You must be signed in to change notification settings - Fork 86
/
storage.go
222 lines (185 loc) · 5.87 KB
/
storage.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
package validator
import (
"encoding/hex"
"strings"
"sync"
beaconprotocol "github.com/bloxapp/ssv/protocol/v1/blockchain/beacon"
"github.com/bloxapp/ssv/protocol/v1/blockchain/eth1"
"github.com/bloxapp/ssv/protocol/v1/validator"
"github.com/bloxapp/ssv/storage/basedb"
"github.com/pkg/errors"
"go.uber.org/zap"
)
// ICollection interface for validator storage
type ICollection interface {
eth1.RegistryStore
SaveValidatorShare(share *beaconprotocol.Share) error
GetValidatorShare(key []byte) (*beaconprotocol.Share, bool, error)
GetAllValidatorShares() ([]*beaconprotocol.Share, error)
GetOperatorValidatorShares(operatorPubKey string, enabled bool) ([]*beaconprotocol.Share, error)
GetOperatorIDValidatorShares(operatorID uint32, enabled bool) ([]*beaconprotocol.Share, error)
GetValidatorSharesByOwnerAddress(ownerAddress string) ([]*beaconprotocol.Share, error)
DeleteValidatorShare(key []byte) error
}
func collectionPrefix() []byte {
return []byte("share-")
}
// CollectionOptions struct
type CollectionOptions struct {
DB basedb.IDb
Logger *zap.Logger
}
// Collection struct
type Collection struct {
db basedb.IDb
logger *zap.Logger
lock sync.RWMutex
}
// NewCollection creates new share storage
func NewCollection(options CollectionOptions) validator.ICollection {
collection := Collection{
db: options.DB,
logger: options.Logger,
lock: sync.RWMutex{},
}
return &collection
}
// SaveValidatorShare save validator share to db
func (s *Collection) SaveValidatorShare(share *beaconprotocol.Share) error {
s.lock.Lock()
defer s.lock.Unlock()
err := s.saveUnsafe(share)
if err != nil {
return err
}
return nil
}
// SaveValidatorShare save validator share to db
func (s *Collection) saveUnsafe(share *beaconprotocol.Share) error {
value, err := share.Serialize()
if err != nil {
s.logger.Error("failed serialized validator", zap.Error(err))
return err
}
return s.db.Set(collectionPrefix(), share.PublicKey.Serialize(), value)
}
// GetValidatorShare by key
func (s *Collection) GetValidatorShare(key []byte) (*beaconprotocol.Share, bool, error) {
s.lock.RLock()
defer s.lock.RUnlock()
return s.getUnsafe(key)
}
// GetValidatorShare by key
func (s *Collection) getUnsafe(key []byte) (*beaconprotocol.Share, bool, error) {
obj, found, err := s.db.Get(collectionPrefix(), key)
if !found {
return nil, false, nil
}
if err != nil {
return nil, found, err
}
share, err := (&beaconprotocol.Share{}).Deserialize(obj.Key, obj.Value)
return share, found, err
}
// CleanRegistryData clears all registry data
func (s *Collection) CleanRegistryData() error {
return s.cleanAllShares()
}
func (s *Collection) cleanAllShares() error {
return s.db.RemoveAllByCollection(collectionPrefix())
}
// GetAllValidatorShares returns all shares
func (s *Collection) GetAllValidatorShares() ([]*beaconprotocol.Share, error) {
s.lock.RLock()
defer s.lock.RUnlock()
var res []*beaconprotocol.Share
err := s.db.GetAll(collectionPrefix(), func(i int, obj basedb.Obj) error {
val, err := (&beaconprotocol.Share{}).Deserialize(obj.Key, obj.Value)
if err != nil {
return errors.Wrap(err, "failed to deserialize validator")
}
res = append(res, val)
return nil
})
return res, err
}
// GetOperatorValidatorShares returns all not liquidated validator shares belongs to operator
func (s *Collection) GetOperatorValidatorShares(operatorPubKey string, enabled bool) ([]*beaconprotocol.Share, error) {
s.lock.RLock()
defer s.lock.RUnlock()
var res []*beaconprotocol.Share
err := s.db.GetAll(collectionPrefix(), func(i int, obj basedb.Obj) error {
val, err := (&beaconprotocol.Share{}).Deserialize(obj.Key, obj.Value)
if err != nil {
return errors.Wrap(err, "failed to deserialize validator")
}
if !val.Liquidated || !enabled {
if ok := val.IsOperatorShare(operatorPubKey); ok {
res = append(res, val)
}
}
return nil
})
return res, err
}
// GetOperatorIDValidatorShares returns all not liquidated validator shares belongs to operator ID.
// TODO: check regards returning a slice of public keys instead of share objects
func (s *Collection) GetOperatorIDValidatorShares(operatorID uint32, enabled bool) ([]*beaconprotocol.Share, error) {
s.lock.RLock()
defer s.lock.RUnlock()
var res []*beaconprotocol.Share
err := s.db.GetAll(collectionPrefix(), func(i int, obj basedb.Obj) error {
val, err := (&beaconprotocol.Share{}).Deserialize(obj.Key, obj.Value)
if err != nil {
return errors.Wrap(err, "failed to deserialize validator")
}
if !val.Liquidated || !enabled {
if ok := val.IsOperatorIDShare(uint64(operatorID)); ok {
res = append(res, val)
}
}
return nil
})
return res, err
}
// GetValidatorSharesByOwnerAddress returns all validator shares belongs to owner address
func (s *Collection) GetValidatorSharesByOwnerAddress(ownerAddress string) ([]*beaconprotocol.Share, error) {
s.lock.RLock()
defer s.lock.RUnlock()
var res []*beaconprotocol.Share
err := s.db.GetAll(collectionPrefix(), func(i int, obj basedb.Obj) error {
val, err := (&beaconprotocol.Share{}).Deserialize(obj.Key, obj.Value)
if err != nil {
return errors.Wrap(err, "failed to deserialize validator")
}
if strings.EqualFold(val.OwnerAddress, ownerAddress) {
res = append(res, val)
}
return nil
})
return res, err
}
// DeleteValidatorShare removes validator share by key
func (s *Collection) DeleteValidatorShare(key []byte) error {
s.lock.Lock()
defer s.lock.Unlock()
return s.db.Delete(collectionPrefix(), key)
}
// UpdateValidatorMetadata updates the metadata of the given validator
func (s *Collection) UpdateValidatorMetadata(pk string, metadata *beaconprotocol.ValidatorMetadata) error {
s.lock.Lock()
defer s.lock.Unlock()
key, err := hex.DecodeString(pk)
if err != nil {
return err
}
share, found, err := s.getUnsafe(key)
if err != nil {
return err
}
if !found {
return nil
}
share.Metadata = metadata
return s.saveUnsafe(share)
}