/
keeper.go
406 lines (330 loc) · 12.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
package keeper
import (
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
gogotypes "github.com/gogo/protobuf/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/umee-network/umee/x/oracle/types"
)
var ten = sdk.MustNewDecFromStr("10")
// Keeper of the oracle store
type Keeper struct {
cdc codec.BinaryCodec
storeKey sdk.StoreKey
paramSpace paramstypes.Subspace
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistributionKeeper
StakingKeeper types.StakingKeeper
distrName string
}
// NewKeeper constructs a new keeper for oracle
func NewKeeper(
cdc codec.BinaryCodec,
storeKey sdk.StoreKey,
paramspace paramstypes.Subspace,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
distrKeeper types.DistributionKeeper,
stakingKeeper types.StakingKeeper,
distrName string,
) Keeper {
// ensure oracle module account is set
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
// set KeyTable if it has not already been set
if !paramspace.HasKeyTable() {
paramspace = paramspace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
cdc: cdc,
storeKey: storeKey,
paramSpace: paramspace,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
distrKeeper: distrKeeper,
StakingKeeper: stakingKeeper,
distrName: distrName,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// GetExchangeRate gets the consensus exchange rate of USD denominated in the
// denom asset from the store.
func (k Keeper) GetExchangeRate(ctx sdk.Context, symbol string) (sdk.Dec, error) {
store := ctx.KVStore(k.storeKey)
symbol = strings.ToUpper(symbol)
b := store.Get(types.GetExchangeRateKey(symbol))
if b == nil {
return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrUnknownDenom, symbol)
}
decProto := sdk.DecProto{}
k.cdc.MustUnmarshal(b, &decProto)
return decProto.Dec, nil
}
// GetExchangeRateBase gets the consensus exchange rate of an asset
// in the base denom (e.g. ATOM -> uatom)
func (k Keeper) GetExchangeRateBase(ctx sdk.Context, denom string) (sdk.Dec, error) {
var symbol string
// Translate the base denom -> symbol
params := k.GetParams(ctx)
for _, listDenom := range params.AcceptList {
if listDenom.BaseDenom == denom {
symbol = listDenom.SymbolDenom
break
}
}
if len(symbol) == 0 {
return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrUnknownDenom, denom)
}
exchangeRate, err := k.GetExchangeRate(ctx, symbol)
if err != nil {
return sdk.ZeroDec(), err
}
for _, acceptedDenom := range params.AcceptList {
if denom == acceptedDenom.BaseDenom {
powerReduction := ten.Power(uint64(acceptedDenom.Exponent))
return exchangeRate.Quo(powerReduction), nil
}
}
return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrUnknownDenom, denom)
}
// SetExchangeRate sets the consensus exchange rate of USD denominated in the
// denom asset to the store.
func (k Keeper) SetExchangeRate(ctx sdk.Context, denom string, exchangeRate sdk.Dec) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.DecProto{Dec: exchangeRate})
denom = strings.ToUpper(denom)
store.Set(types.GetExchangeRateKey(denom), bz)
}
// SetExchangeRateWithEvent sets an consensus
// exchange rate to the store with ABCI event
func (k Keeper) SetExchangeRateWithEvent(ctx sdk.Context, denom string, exchangeRate sdk.Dec) {
k.SetExchangeRate(ctx, denom, exchangeRate)
ctx.EventManager().EmitEvent(
sdk.NewEvent(types.EventTypeExchangeRateUpdate,
sdk.NewAttribute(types.EventAttrKeyDenom, denom),
sdk.NewAttribute(types.EventAttrKeyExchangeRate, exchangeRate.String()),
),
)
}
// DeleteExchangeRate deletes the consensus exchange rate of USD denominated in
// the denom asset from the store.
func (k Keeper) DeleteExchangeRate(ctx sdk.Context, denom string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetExchangeRateKey(denom))
}
// IterateExchangeRates iterates over USD rates in the store.
func (k Keeper) IterateExchangeRates(ctx sdk.Context, handler func(string, sdk.Dec) bool) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixExchangeRate)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
key := iter.Key()
denom := string(key[len(types.KeyPrefixExchangeRate) : len(key)-1])
dp := sdk.DecProto{}
k.cdc.MustUnmarshal(iter.Value(), &dp)
if handler(denom, dp.Dec) {
break
}
}
}
// GetFeederDelegation gets the account address to which the validator operator
// delegated oracle vote rights.
func (k Keeper) GetFeederDelegation(ctx sdk.Context, operator sdk.ValAddress) (sdk.AccAddress, error) {
// check that the given validator exists
if val := k.StakingKeeper.Validator(ctx, operator); val == nil || !val.IsBonded() {
return nil, sdkerrors.Wrapf(stakingtypes.ErrNoValidatorFound, "validator %s is not active set", operator.String())
}
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetFeederDelegationKey(operator))
if bz == nil {
// by default the right is delegated to the validator itself
return sdk.AccAddress(operator), nil
}
return sdk.AccAddress(bz), nil
}
// SetFeederDelegation sets the account address to which the validator operator
// delegated oracle vote rights.
func (k Keeper) SetFeederDelegation(ctx sdk.Context, operator sdk.ValAddress, delegatedFeeder sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetFeederDelegationKey(operator), delegatedFeeder.Bytes())
}
type IterateFeederDelegationHandler func(delegator sdk.ValAddress, delegate sdk.AccAddress) (stop bool)
// IterateFeederDelegations iterates over the feed delegates and performs a
// callback function.
func (k Keeper) IterateFeederDelegations(ctx sdk.Context, handler IterateFeederDelegationHandler) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixFeederDelegation)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
delegator := sdk.ValAddress(iter.Key()[2:])
delegate := sdk.AccAddress(iter.Value())
if handler(delegator, delegate) {
break
}
}
}
// GetMissCounter retrieves the # of vote periods missed in this oracle slash
// window.
func (k Keeper) GetMissCounter(ctx sdk.Context, operator sdk.ValAddress) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMissCounterKey(operator))
if bz == nil {
// by default the counter is zero
return 0
}
var missCounter gogotypes.UInt64Value
k.cdc.MustUnmarshal(bz, &missCounter)
return missCounter.Value
}
// SetMissCounter updates the # of vote periods missed in this oracle slash
// window.
func (k Keeper) SetMissCounter(ctx sdk.Context, operator sdk.ValAddress, missCounter uint64) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.UInt64Value{Value: missCounter})
store.Set(types.GetMissCounterKey(operator), bz)
}
// DeleteMissCounter removes miss counter for the validator.
func (k Keeper) DeleteMissCounter(ctx sdk.Context, operator sdk.ValAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetMissCounterKey(operator))
}
// IterateMissCounters iterates over the miss counters and performs a callback
// function.
func (k Keeper) IterateMissCounters(ctx sdk.Context, handler func(sdk.ValAddress, uint64) bool) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixMissCounter)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
operator := sdk.ValAddress(iter.Key()[2:])
var missCounter gogotypes.UInt64Value
k.cdc.MustUnmarshal(iter.Value(), &missCounter)
if handler(operator, missCounter.Value) {
break
}
}
}
// GetAggregateExchangeRatePrevote retrieves an oracle prevote from the store.
func (k Keeper) GetAggregateExchangeRatePrevote(
ctx sdk.Context,
voter sdk.ValAddress,
) (types.AggregateExchangeRatePrevote, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetAggregateExchangeRatePrevoteKey(voter))
if bz == nil {
return types.AggregateExchangeRatePrevote{}, sdkerrors.Wrap(types.ErrNoAggregatePrevote, voter.String())
}
var aggregatePrevote types.AggregateExchangeRatePrevote
k.cdc.MustUnmarshal(bz, &aggregatePrevote)
return aggregatePrevote, nil
}
// HasAggregateExchangeRatePrevote checks if a validator has an existing prevote.
func (k Keeper) HasAggregateExchangeRatePrevote(
ctx sdk.Context,
voter sdk.ValAddress,
) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetAggregateExchangeRatePrevoteKey(voter))
}
// SetAggregateExchangeRatePrevote set an oracle aggregate prevote to the store.
func (k Keeper) SetAggregateExchangeRatePrevote(
ctx sdk.Context,
voter sdk.ValAddress,
prevote types.AggregateExchangeRatePrevote,
) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&prevote)
store.Set(types.GetAggregateExchangeRatePrevoteKey(voter), bz)
}
// DeleteAggregateExchangeRatePrevote deletes an oracle prevote from the store.
func (k Keeper) DeleteAggregateExchangeRatePrevote(ctx sdk.Context, voter sdk.ValAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetAggregateExchangeRatePrevoteKey(voter))
}
// IterateAggregateExchangeRatePrevotes iterates rate over prevotes in the store
func (k Keeper) IterateAggregateExchangeRatePrevotes(
ctx sdk.Context,
handler func(sdk.ValAddress, types.AggregateExchangeRatePrevote) bool,
) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixAggregateExchangeRatePrevote)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
voterAddr := sdk.ValAddress(iter.Key()[2:])
var aggregatePrevote types.AggregateExchangeRatePrevote
k.cdc.MustUnmarshal(iter.Value(), &aggregatePrevote)
if handler(voterAddr, aggregatePrevote) {
break
}
}
}
// GetAggregateExchangeRateVote retrieves an oracle prevote from the store.
func (k Keeper) GetAggregateExchangeRateVote(
ctx sdk.Context,
voter sdk.ValAddress,
) (types.AggregateExchangeRateVote, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetAggregateExchangeRateVoteKey(voter))
if bz == nil {
return types.AggregateExchangeRateVote{}, sdkerrors.Wrap(types.ErrNoAggregateVote, voter.String())
}
var aggregateVote types.AggregateExchangeRateVote
k.cdc.MustUnmarshal(bz, &aggregateVote)
return aggregateVote, nil
}
// SetAggregateExchangeRateVote adds an oracle aggregate prevote to the store.
func (k Keeper) SetAggregateExchangeRateVote(
ctx sdk.Context,
voter sdk.ValAddress,
vote types.AggregateExchangeRateVote,
) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&vote)
store.Set(types.GetAggregateExchangeRateVoteKey(voter), bz)
}
// DeleteAggregateExchangeRateVote deletes an oracle prevote from the store.
func (k Keeper) DeleteAggregateExchangeRateVote(ctx sdk.Context, voter sdk.ValAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetAggregateExchangeRateVoteKey(voter))
}
type IterateExchangeRateVote = func(
voterAddr sdk.ValAddress,
aggregateVote types.AggregateExchangeRateVote,
) (stop bool)
// IterateAggregateExchangeRateVotes iterates rate over prevotes in the store.
func (k Keeper) IterateAggregateExchangeRateVotes(
ctx sdk.Context,
handler IterateExchangeRateVote,
) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixAggregateExchangeRateVote)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
voterAddr := sdk.ValAddress(iter.Key()[2:])
var aggregateVote types.AggregateExchangeRateVote
k.cdc.MustUnmarshal(iter.Value(), &aggregateVote)
if handler(voterAddr, aggregateVote) {
break
}
}
}
// ValidateFeeder returns the given feeder is allowed to feed the message or not.
func (k Keeper) ValidateFeeder(ctx sdk.Context, feederAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
delegate, err := k.GetFeederDelegation(ctx, valAddr)
if err != nil {
return err
}
if !delegate.Equals(feederAddr) {
return sdkerrors.Wrap(types.ErrNoVotingPermission, feederAddr.String())
}
return nil
}