-
Notifications
You must be signed in to change notification settings - Fork 199
/
reward_db.go
281 lines (232 loc) · 6.48 KB
/
reward_db.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package rewardserver
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/lavanet/lava/utils"
pairingtypes "github.com/lavanet/lava/x/pairing/types"
)
const keySeparator = "."
type DB interface {
Key() string
Save(dbEntry *DBEntry) error
BatchSave(dbEntries []*DBEntry) error
FindOne(key string) ([]byte, error)
FindAll() (map[string][]byte, error)
Delete(key string) error
DeletePrefix(prefix string) error
Close() error
}
type DBEntry struct {
Key string
Data []byte
Ttl time.Duration
}
type RewardDB struct {
lock sync.RWMutex // this lock is used only for adding new db (dbs) fields in parallel
dbs map[string]DB // key is spec id
ttl time.Duration
}
type RewardEntity struct {
Epoch uint64
ConsumerAddr string
ConsumerKey string
SessionId uint64
Proof *pairingtypes.RelaySession
}
func (rs *RewardDB) Save(rewardEntity *RewardEntity) error {
return rs.BatchSave([]*RewardEntity{rewardEntity})
}
func (rs *RewardDB) BatchSave(rewardEntities []*RewardEntity) (err error) {
dbEntriesMap := map[string][]*DBEntry{} // Key is specId
for _, reward := range rewardEntities {
key := rs.assembleKey(reward.Epoch, reward.ConsumerAddr, reward.SessionId, reward.ConsumerKey)
buf, err := json.Marshal(reward)
if err != nil {
return utils.LavaFormatError("failed to encode proof: %s", err)
}
dbEntry := &DBEntry{
Key: key,
Data: buf,
Ttl: rs.ttl,
}
dbEntriesMap[reward.Proof.SpecId] = append(dbEntriesMap[reward.Proof.SpecId], dbEntry)
}
for specId, rewards := range dbEntriesMap {
db, found := rs.dbs[specId]
if !found {
return fmt.Errorf("reward_db: db not found for spec id: %s", specId)
}
err = db.BatchSave(rewards)
if err != nil {
return err
}
}
return nil
}
// currently unused
func (rs *RewardDB) FindOne(
epoch uint64,
consumerAddr string,
consumerKey string,
sessionId uint64,
) (*pairingtypes.RelaySession, error) {
key := rs.assembleKey(epoch, consumerAddr, sessionId, consumerKey)
re, err := rs.findOne(key)
if err != nil {
return nil, err
}
return re.Proof, nil
}
func (rs *RewardDB) findOne(key string) (*RewardEntity, error) {
for _, db := range rs.dbs {
reward, err := db.FindOne(key)
// if not found, continue to next db
if err != nil {
continue
}
if reward != nil {
re := RewardEntity{}
err := json.Unmarshal(reward, &re)
if err != nil {
utils.LavaFormatError("failed to decode proof: %s", err)
return nil, err
}
return &re, nil
}
}
return nil, fmt.Errorf("reward not found for key: %s", key)
}
func (rs *RewardDB) FindAll() (map[uint64]*EpochRewards, error) {
rawRewards := make(map[string]*RewardEntity)
for _, db := range rs.dbs {
err := rs.retrieveAndProcessRewardsFromDB(&db, &rawRewards)
if err != nil {
return nil, err
}
}
return rs.buildEpochRewardsMap(rawRewards), nil
}
// TODO: Add tests for this func
func (rs *RewardDB) FindAllInDB(specId string) (map[uint64]*EpochRewards, error) {
db, found := rs.dbs[specId]
if !found {
return nil, utils.LavaFormatWarning("reward db with given spec id was not found", nil, utils.Attribute{Key: "specId", Value: specId})
}
rawRewards := make(map[string]*RewardEntity)
err := rs.retrieveAndProcessRewardsFromDB(&db, &rawRewards)
if err != nil {
return nil, err
}
return rs.buildEpochRewardsMap(rawRewards), nil
}
func (rs *RewardDB) buildEpochRewardsMap(rawRewards map[string]*RewardEntity) map[uint64]*EpochRewards {
resEpochRewards := map[uint64]*EpochRewards{}
for _, reward := range rawRewards {
epochRewards, ok := resEpochRewards[reward.Epoch]
if !ok {
proofs := map[uint64]*pairingtypes.RelaySession{reward.SessionId: reward.Proof}
consumerRewards := map[string]*ConsumerRewards{reward.ConsumerKey: {epoch: reward.Epoch, consumer: reward.ConsumerAddr, proofs: proofs}}
resEpochRewards[reward.Epoch] = &EpochRewards{epoch: reward.Epoch, consumerRewards: consumerRewards}
continue
}
consumerRewards, ok := epochRewards.consumerRewards[reward.ConsumerKey]
if !ok {
proofs := map[uint64]*pairingtypes.RelaySession{reward.SessionId: reward.Proof}
epochRewards.consumerRewards[reward.ConsumerKey] = &ConsumerRewards{epoch: reward.Epoch, consumer: reward.ConsumerAddr, proofs: proofs}
continue
}
_, ok = consumerRewards.proofs[reward.SessionId]
if !ok {
consumerRewards.proofs[reward.SessionId] = reward.Proof
continue
}
}
return resEpochRewards
}
func (rs *RewardDB) retrieveAndProcessRewardsFromDB(db *DB, rawRewards *map[string]*RewardEntity) (err error) {
raw, err := (*db).FindAll()
if err != nil {
return err
}
for key, reward := range raw {
re := RewardEntity{}
err := json.Unmarshal(reward, &re)
if err != nil {
utils.LavaFormatError("failed to decode proof: %s", err)
continue
}
(*rawRewards)[key] = &re
}
return nil
}
func (rs *RewardDB) DeleteClaimedRewards(epoch uint64, consumerAddr string, sessionId uint64, consumerRewardsKey string) (err error) {
prefix := rs.assembleKey(epoch, consumerAddr, sessionId, consumerRewardsKey)
err = rs.deletePrefix(prefix)
if err != nil {
return utils.LavaFormatError("failed to delete rewards: %s", err)
}
return nil
}
func (rs *RewardDB) DeleteEpochRewards(epoch uint64) error {
prefix := strconv.FormatUint(epoch, 10)
return rs.deletePrefix(prefix)
}
func (rs *RewardDB) deletePrefix(prefix string) error {
for _, db := range rs.dbs {
err := db.DeletePrefix(prefix)
if err != nil {
return err
}
}
return nil
}
func (rs *RewardDB) AddDB(db DB) error {
// reading key before lock to avoid double locking.
dbKey := db.Key()
rs.lock.Lock()
defer rs.lock.Unlock()
_, found := rs.dbs[dbKey]
if found {
return fmt.Errorf("db already exists for key: %s", dbKey)
}
rs.dbs[dbKey] = db
return nil
}
func (rs *RewardDB) DBExists(specId string) bool {
_, found := rs.dbs[specId]
return found
}
func (rs *RewardDB) Close() error {
for _, db := range rs.dbs {
err := db.Close()
if err != nil {
return err
}
}
return nil
}
func NewRewardDB() *RewardDB {
rdb := NewRewardDBWithTTL(DefaultRewardTTL)
return rdb
}
func NewRewardDBWithTTL(ttl time.Duration) *RewardDB {
return &RewardDB{
dbs: map[string]DB{},
ttl: ttl,
}
}
func (rs *RewardDB) assembleKey(epoch uint64, consumerAddr string, sessionId uint64, consumerKey string) string {
keyParts := []string{
strconv.FormatUint(epoch, 10),
consumerAddr,
strconv.FormatUint(sessionId, 10),
}
if consumerKey != "" {
keyParts = append(keyParts, consumerKey)
}
return strings.Join(keyParts, keySeparator)
}