-
Notifications
You must be signed in to change notification settings - Fork 83
/
store.go
190 lines (162 loc) · 5.21 KB
/
store.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
package storage
import (
"encoding/binary"
specqbft "github.com/bloxapp/ssv-spec/qbft"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/zap"
"github.com/bloxapp/ssv/protocol/v2/qbft/instance"
qbftstorage "github.com/bloxapp/ssv/protocol/v2/qbft/storage"
"github.com/bloxapp/ssv/storage/basedb"
)
const (
highestInstanceKey = "highest_instance"
instanceKey = "instance"
)
var (
metricsHighestDecided = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "ssv:validator:ibft_highest_decided",
Help: "The highest decided sequence number",
}, []string{"identifier", "pubKey"})
)
func init() {
logger := zap.L()
if err := prometheus.Register(metricsHighestDecided); err != nil {
logger.Debug("could not register prometheus collector")
}
}
// ibftStorage struct
// instanceType is what separates different iBFT eth2 duty types (attestation, proposal and aggregation)
type ibftStorage struct {
prefix []byte
db basedb.Database
}
// New create new ibft storage
func New(db basedb.Database, prefix string) qbftstorage.QBFTStore {
return &ibftStorage{
prefix: []byte(prefix),
db: db,
}
}
// GetHighestInstance returns the StoredInstance for the highest instance.
func (i *ibftStorage) GetHighestInstance(identifier []byte) (*qbftstorage.StoredInstance, error) {
val, found, err := i.get(highestInstanceKey, identifier[:])
if !found {
return nil, nil
}
if err != nil {
return nil, err
}
ret := &qbftstorage.StoredInstance{}
if err := ret.Decode(val); err != nil {
return nil, errors.Wrap(err, "could not decode instance")
}
return ret, nil
}
func (i *ibftStorage) SaveInstance(instance *qbftstorage.StoredInstance) error {
return i.saveInstance(instance, true, false)
}
func (i *ibftStorage) SaveHighestInstance(instance *qbftstorage.StoredInstance) error {
return i.saveInstance(instance, false, true)
}
func (i *ibftStorage) SaveHighestAndHistoricalInstance(instance *qbftstorage.StoredInstance) error {
return i.saveInstance(instance, true, true)
}
func (i *ibftStorage) saveInstance(inst *qbftstorage.StoredInstance, toHistory, asHighest bool) error {
inst.State = instance.CompactCopy(inst.State, inst.DecidedMessage)
value, err := inst.Encode()
if err != nil {
return errors.Wrap(err, "could not encode instance")
}
if asHighest {
err = i.save(value, highestInstanceKey, inst.State.ID)
if err != nil {
return errors.Wrap(err, "could not save highest instance")
}
}
if toHistory {
err = i.save(value, instanceKey, inst.State.ID, uInt64ToByteSlice(uint64(inst.State.Height)))
if err != nil {
return errors.Wrap(err, "could not save historical instance")
}
}
return nil
}
// GetInstance returns historical StoredInstance for the given identifier and height.
func (i *ibftStorage) GetInstance(identifier []byte, height specqbft.Height) (*qbftstorage.StoredInstance, error) {
val, found, err := i.get(instanceKey, identifier[:], uInt64ToByteSlice(uint64(height)))
if !found {
return nil, nil
}
if err != nil {
return nil, err
}
ret := &qbftstorage.StoredInstance{}
if err := ret.Decode(val); err != nil {
return nil, errors.Wrap(err, "could not decode instance")
}
return ret, nil
}
// GetInstancesInRange returns historical StoredInstance's in the given range.
func (i *ibftStorage) GetInstancesInRange(identifier []byte, from specqbft.Height, to specqbft.Height) ([]*qbftstorage.StoredInstance, error) {
instances := make([]*qbftstorage.StoredInstance, 0)
for seq := from; seq <= to; seq++ {
instance, err := i.GetInstance(identifier, seq)
if err != nil {
return nil, errors.Wrap(err, "failed to get instance")
}
if instance != nil {
instances = append(instances, instance)
}
}
return instances, nil
}
// CleanAllInstances removes all StoredInstance's & highest StoredInstance's for msgID.
func (i *ibftStorage) CleanAllInstances(logger *zap.Logger, msgID []byte) error {
prefix := i.prefix
prefix = append(prefix, msgID[:]...)
prefix = append(prefix, []byte(instanceKey)...)
_, err := i.db.DeletePrefix(prefix)
if err != nil {
return errors.Wrap(err, "failed to remove decided")
}
if err := i.delete(highestInstanceKey, msgID[:]); err != nil {
return errors.Wrap(err, "failed to remove last decided")
}
return nil
}
func (i *ibftStorage) save(value []byte, id string, pk []byte, keyParams ...[]byte) error {
prefix := append(i.prefix, pk...)
key := i.key(id, keyParams...)
return i.db.Set(prefix, key, value)
}
func (i *ibftStorage) get(id string, pk []byte, keyParams ...[]byte) ([]byte, bool, error) {
prefix := append(i.prefix, pk...)
key := i.key(id, keyParams...)
obj, found, err := i.db.Get(prefix, key)
if !found {
return nil, found, nil
}
if err != nil {
return nil, found, err
}
return obj.Value, found, nil
}
func (i *ibftStorage) delete(id string, pk []byte, keyParams ...[]byte) error {
prefix := append(i.prefix, pk...)
key := i.key(id, keyParams...)
return i.db.Delete(prefix, key)
}
func (i *ibftStorage) key(id string, params ...[]byte) []byte {
ret := []byte(id)
for _, p := range params {
ret = append(ret, p...)
}
return ret
}
func uInt64ToByteSlice(n uint64) []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, n)
return b
}