/
keeper.go
438 lines (361 loc) · 15.2 KB
/
keeper.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package keeper
import (
"bytes"
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
mapset "github.com/deckarep/golang-set"
"github.com/ethereum/go-ethereum/common"
"github.com/peggyjv/sommelier/x/allocation/types"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/libs/log"
)
// Keeper of the oracle store
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
paramSpace paramtypes.Subspace
stakingKeeper types.StakingKeeper
gravityKeeper types.GravityKeeper
}
// NewKeeper creates a new distribution Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
stakingKeeper types.StakingKeeper, gravityKeeper types.GravityKeeper,
) Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
storeKey: key,
cdc: cdc,
paramSpace: paramSpace,
stakingKeeper: stakingKeeper,
gravityKeeper: gravityKeeper,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
////////////////////////
// MsgDelegateAddress //
////////////////////////
// SetValidatorDelegateAddress sets a new address that will have the power to send data on behalf of the validator
func (k Keeper) SetValidatorDelegateAddress(ctx sdk.Context, del sdk.AccAddress, val sdk.ValAddress) {
ctx.KVStore(k.storeKey).Set(types.GetAllocationDelegateKey(del), val.Bytes())
}
// GetValidatorAddressFromDelegate returns the delegate address for a given validator
func (k Keeper) GetValidatorAddressFromDelegate(ctx sdk.Context, del sdk.AccAddress) sdk.ValAddress {
return ctx.KVStore(k.storeKey).Get(types.GetAllocationDelegateKey(del))
}
// GetDelegateAddressFromValidator returns the validator address for a given delegate
func (k Keeper) GetDelegateAddressFromValidator(ctx sdk.Context, val sdk.ValAddress) sdk.AccAddress {
var address sdk.AccAddress
// TODO: create secondary index
k.IterateDelegateAddresses(ctx, func(delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress) bool {
if !val.Equals(validatorAddr) {
return false
}
address = delegatorAddr
return true
})
return address
}
// IsDelegateAddress returns true if the validator has delegated their feed to an address
func (k Keeper) IsDelegateAddress(ctx sdk.Context, del sdk.AccAddress) bool {
return ctx.KVStore(k.storeKey).Has(types.GetAllocationDelegateKey(del))
}
// IterateDelegateAddresses iterates over all delegate address pairs in the store
func (k Keeper) IterateDelegateAddresses(ctx sdk.Context, handler func(del sdk.AccAddress, val sdk.ValAddress) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte{types.AllocationDelegateKeyPrefix})
defer iter.Close()
for ; iter.Valid(); iter.Next() {
del := sdk.AccAddress(bytes.TrimPrefix(iter.Key(), []byte{types.AllocationDelegateKeyPrefix}))
val := sdk.ValAddress(iter.Value())
if handler(del, val) {
break
}
}
}
// GetAllAllocationDelegations returns all the delegations for allocations
func (k Keeper) GetAllAllocationDelegations(ctx sdk.Context) []types.MsgDelegateAllocations {
allocationDelegations := make([]types.MsgDelegateAllocations, 0)
k.IterateDelegateAddresses(ctx, func(del sdk.AccAddress, val sdk.ValAddress) bool {
allocationDelegations = append(allocationDelegations, types.MsgDelegateAllocations{
Delegate: del.String(),
Validator: val.String(),
})
return false
})
return allocationDelegations
}
/////////////
// Cellars //
/////////////
func (k Keeper) SetCellar(ctx sdk.Context, c types.Cellar) {
bz := k.cdc.MustMarshal(&c)
ctx.KVStore(k.storeKey).Set(types.GetCellarKey(c.Address()), bz)
}
func (k Keeper) GetCellarByID(ctx sdk.Context, id common.Address) (cellar types.Cellar, found bool) {
bz := ctx.KVStore(k.storeKey).Get(types.GetCellarKey(id))
if len(bz) == 0 {
return types.Cellar{}, false
}
k.cdc.MustUnmarshal(bz, &cellar)
return cellar, true
}
func (k Keeper) IterateCellars(ctx sdk.Context, cb func(cellar types.Cellar) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte{types.CellarKeyPrefix})
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var cellar types.Cellar
k.cdc.MustUnmarshal(iter.Value(), &cellar)
if cb(cellar) {
break
}
}
}
func (k Keeper) GetCellars(ctx sdk.Context) (cellars []types.Cellar) {
k.IterateCellars(ctx, func(cellar types.Cellar) (stop bool) {
cellars = append(cellars, cellar)
return false
})
return
}
//////////////////////////
// Allocation Precommit //
//////////////////////////
// SetAllocationPrecommit sets the precommit for a given validator
// CONTRACT: must provide the validator address here not the delegate address
func (k Keeper) SetAllocationPrecommit(ctx sdk.Context, validatorAddr sdk.ValAddress, cellarAddr common.Address, precommit types.AllocationPrecommit) {
bz := k.cdc.MustMarshal(&precommit)
ctx.KVStore(k.storeKey).Set(types.GetAllocationPrecommitKey(validatorAddr, cellarAddr), bz)
}
// GetAllocationPrecommit gets the prevote for a given validator
// CONTRACT: must provide the validator address here not the delegate address
func (k Keeper) GetAllocationPrecommit(ctx sdk.Context, val sdk.ValAddress, cel common.Address) (types.AllocationPrecommit, bool) {
bz := ctx.KVStore(k.storeKey).Get(types.GetAllocationPrecommitKey(val, cel))
if len(bz) == 0 {
return types.AllocationPrecommit{}, false
}
var precommit types.AllocationPrecommit
k.cdc.MustUnmarshal(bz, &precommit)
return precommit, true
}
// DeleteAllPrecommits removes all the prevotes for the current block iteration
func (k Keeper) DeleteAllPrecommits(ctx sdk.Context) {
k.IterateAllocationPrecommits(ctx, func(val sdk.ValAddress, cel common.Address, _ types.AllocationPrecommit) bool {
k.DeleteAllocationPrecommit(ctx, val, cel)
return false
})
}
// DeleteAllocationPrecommit deletes the prevote for a given validator
// CONTRACT: must provide the validator address here not the delegate address
func (k Keeper) DeleteAllocationPrecommit(ctx sdk.Context, val sdk.ValAddress, cel common.Address) {
ctx.KVStore(k.storeKey).Delete(types.GetAllocationPrecommitKey(val, cel))
}
// HasAllocationPrecommit gets the precommit for a given validator
// CONTRACT: must provide the validator address here not the delegate address
func (k Keeper) HasAllocationPrecommit(ctx sdk.Context, val sdk.ValAddress, cel common.Address) bool {
return ctx.KVStore(k.storeKey).Has(types.GetAllocationPrecommitKey(val, cel))
}
// IterateAllocationPrecommits iterates over all prevotes in the store
func (k Keeper) IterateAllocationPrecommits(ctx sdk.Context, cb func(val sdk.ValAddress, cel common.Address, precommit types.AllocationPrecommit) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte{types.AllocationPrecommitKeyPrefix})
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var precommit types.AllocationPrecommit
keyPair := bytes.NewBuffer(bytes.TrimPrefix(iter.Key(), []byte{types.AllocationPrecommitKeyPrefix}))
val := sdk.ValAddress(keyPair.Next(20))
cel := common.BytesToAddress(keyPair.Bytes())
k.cdc.MustUnmarshal(iter.Value(), &precommit)
if cb(val, cel, precommit) {
break
}
}
}
///////////////////////
// MsgAllocationCommit //
///////////////////////
// SetAllocationCommit sets the prevote for a given validator
// CONTRACT: must provide the validator address here not the delegate address
func (k Keeper) SetAllocationCommit(ctx sdk.Context, val sdk.ValAddress, cel common.Address, allocationCommit types.Allocation) {
bz := k.cdc.MustMarshal(&allocationCommit)
ctx.KVStore(k.storeKey).Set(types.GetAllocationCommitForCellarKey(val, cel), bz)
}
// GetAllocationCommit gets the prevote for a given validator
// CONTRACT: must provide the validator address here not the delegate address
func (k Keeper) GetAllocationCommit(ctx sdk.Context, val sdk.ValAddress, cel common.Address) (types.Allocation, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetAllocationCommitForCellarKey(val, cel))
if len(bz) == 0 {
return types.Allocation{}, false
}
var allocationCommit types.Allocation
k.cdc.MustUnmarshal(bz, &allocationCommit)
return allocationCommit, true
}
// DeleteAllocationCommit deletes the prevote for a given validator
// CONTRACT: must provide the validator address here not the delegate address
func (k Keeper) DeleteAllocationCommit(ctx sdk.Context, val sdk.ValAddress, cel common.Address) {
ctx.KVStore(k.storeKey).Delete(types.GetAllocationCommitForCellarKey(val, cel))
}
// HasAllocationCommitForCellar gets the prevote for a given validator
// CONTRACT: must provide the validator address here not the delegate address
func (k Keeper) HasAllocationCommitForCellar(ctx sdk.Context, val sdk.ValAddress, cel common.Address) bool {
return ctx.KVStore(k.storeKey).Has(types.GetAllocationCommitForCellarKey(val, cel))
}
// HasAllocationCommit gets the existence of any commit for a given validator
// CONTRACT: must provide the validator address here not the delegate address
func (k Keeper) HasAllocationCommit(ctx sdk.Context, val sdk.ValAddress) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.GetAllocationCommitKeyPrefix(val))
iter := store.Iterator(nil, nil)
defer iter.Close()
return iter.Valid()
}
// IterateAllocationCommits iterates over all votes in the store
func (k Keeper) IterateAllocationCommits(ctx sdk.Context, handler func(val sdk.ValAddress, cel common.Address, commit types.Allocation) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte{types.AllocationCommitForCellarKeyPrefix})
defer iter.Close()
for ; iter.Valid(); iter.Next() {
keyPair := bytes.NewBuffer(bytes.TrimPrefix(iter.Key(), []byte{types.AllocationCommitForCellarKeyPrefix}))
val := sdk.ValAddress(keyPair.Next(20))
cel := common.BytesToAddress(keyPair.Bytes())
var commit types.Allocation
k.cdc.MustUnmarshal(iter.Value(), &commit)
if handler(val, cel, commit) {
break
}
}
}
// IterateAllocationCommitValidators iterates over all validators who have committed allocations
func (k Keeper) IterateAllocationCommitValidators(ctx sdk.Context, handler func(val sdk.ValAddress) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte{types.AllocationCommitForCellarKeyPrefix})
defer iter.Close()
seenValidators := mapset.NewThreadUnsafeSet()
for ; iter.Valid(); iter.Next() {
keyPair := bytes.NewBuffer(bytes.TrimPrefix(iter.Key(), []byte{types.AllocationCommitForCellarKeyPrefix}))
val := sdk.ValAddress(keyPair.Next(20))
// add seen validator to set. if already in set, don't return to consumer
if !seenValidators.Add(val) {
continue
}
if handler(val) {
break
}
}
}
// IterateValidatorAllocationCommits Iterates all of the commits for a provided validator
func (k Keeper) IterateValidatorAllocationCommits(ctx sdk.Context, val sdk.ValAddress, handler func(cellar common.Address, commit types.Allocation) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, append([]byte{types.AllocationCommitForCellarKeyPrefix}, val.Bytes()...))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
keyPair := bytes.NewBuffer(bytes.TrimPrefix(iter.Key(), []byte{types.AllocationCommitForCellarKeyPrefix}))
keyPair.Next(20)
cel := common.BytesToAddress(keyPair.Bytes())
var commit types.Allocation
k.cdc.MustUnmarshal(iter.Value(), &commit)
if handler(cel, commit) {
break
}
}
}
//////////////////
// CommitPeriod //
//////////////////
// SetCommitPeriodStart sets the current vote period start height
func (k Keeper) SetCommitPeriodStart(ctx sdk.Context, height int64) {
store := ctx.KVStore(k.storeKey)
store.Set([]byte{types.CommitPeriodStartKey}, sdk.Uint64ToBigEndian(uint64(height)))
}
// GetCommitPeriodStart returns the vote period start height
func (k Keeper) GetCommitPeriodStart(ctx sdk.Context) (int64, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte{types.CommitPeriodStartKey})
if len(bz) == 0 {
return 0, false
}
return int64(sdk.BigEndianToUint64(bz)), true
}
// HasCommitPeriodStart returns true if the vote period start has been set
func (k Keeper) HasCommitPeriodStart(ctx sdk.Context) bool {
return ctx.KVStore(k.storeKey).Has([]byte{types.CommitPeriodStartKey})
}
////////////
// Params //
////////////
// GetParamSet returns the vote period from the parameters
func (k Keeper) GetParamSet(ctx sdk.Context) types.Params {
var p types.Params
k.paramSpace.GetParamSet(ctx, &p)
return p
}
// SetParams sets the parameters in the store
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, ¶ms)
}
/////////////////////////
// Invalidation Nonces //
/////////////////////////
func (k Keeper) GetLatestInvalidationNonce(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte{types.LatestInvalidationNonceKey})
return sdk.BigEndianToUint64(bz)
}
func (k Keeper) SetLatestInvalidationNonce(ctx sdk.Context, invalidationNonce uint64) {
store := ctx.KVStore(k.storeKey)
store.Set([]byte{types.LatestInvalidationNonceKey}, sdk.Uint64ToBigEndian(invalidationNonce))
}
func (k Keeper) IncrementInvalidationNonce(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
nextNonce := k.GetLatestInvalidationNonce(ctx) + 1
store.Set([]byte{types.LatestInvalidationNonceKey}, sdk.Uint64ToBigEndian(nextNonce))
return nextNonce
}
////////////////////
// Contract Calls //
////////////////////
func (k Keeper) GetPendingCellarUpdate(ctx sdk.Context, invalidationNonce uint64) (types.CellarUpdate, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetCellarUpdateKey(invalidationNonce))
if len(bz) == 0 {
return types.CellarUpdate{}, false
}
var cellarUpdate types.CellarUpdate
k.cdc.MustUnmarshal(bz, &cellarUpdate)
return cellarUpdate, true
}
func (k Keeper) HasPendingCellarUpdate(ctx sdk.Context, invalidationNonce uint64) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetCellarUpdateKey(invalidationNonce))
}
func (k Keeper) SetPendingCellarUpdate(ctx sdk.Context, cellarUpdate types.CellarUpdate) {
bz := k.cdc.MustMarshal(&cellarUpdate)
ctx.KVStore(k.storeKey).Set(types.GetCellarUpdateKey(cellarUpdate.InvalidationNonce), bz)
}
func (k Keeper) CommitCellarUpdate(ctx sdk.Context, invalidationNonce uint64, invalidationScope tmbytes.HexBytes) {
cellarUpdate, ok := k.GetPendingCellarUpdate(ctx, invalidationNonce)
if !ok {
panic(fmt.Sprintf("cellar update with invalidation nonce %v not found", invalidationNonce))
}
if !bytes.Equal(cellarUpdate.Cellar.InvalidationScope(), invalidationScope) {
panic(fmt.Sprintf("stored invalidation scope did not match event invalidation scope: %v != %v", cellarUpdate.Cellar.InvalidationScope(), invalidationNonce))
}
k.SetCellar(ctx, *cellarUpdate.Cellar)
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetCellarUpdateKey(cellarUpdate.InvalidationNonce))
}
func (k Keeper) DeleteCellar(ctx sdk.Context, cellarAddr common.Address) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetCellarKey(cellarAddr))
}