-
Notifications
You must be signed in to change notification settings - Fork 48
/
keeper.go
372 lines (315 loc) · 12.5 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
package keeper
import (
"fmt"
"github.com/tendermint/tendermint/libs/log"
gogotypes "github.com/gogo/protobuf/types"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
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"
core "github.com/classic-terra/core/v2/types"
"github.com/classic-terra/core/v2/x/oracle/types"
)
// Keeper of the oracle store
type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.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 storetypes.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))
}
//-----------------------------------
// ExchangeRate logic
// GetLunaExchangeRate gets the consensus exchange rate of Luna denominated in the denom asset from the store.
func (k Keeper) GetLunaExchangeRate(ctx sdk.Context, denom string) (sdk.Dec, error) {
if denom == core.MicroLunaDenom {
return sdk.OneDec(), nil
}
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetExchangeRateKey(denom))
if b == nil {
return sdk.ZeroDec(), sdkerrors.Wrap(types.ErrUnknownDenom, denom)
}
dp := sdk.DecProto{}
k.cdc.MustUnmarshal(b, &dp)
return dp.Dec, nil
}
// SetLunaExchangeRate sets the consensus exchange rate of Luna denominated in the denom asset to the store.
func (k Keeper) SetLunaExchangeRate(ctx sdk.Context, denom string, exchangeRate sdk.Dec) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.DecProto{Dec: exchangeRate})
store.Set(types.GetExchangeRateKey(denom), bz)
}
// SetLunaExchangeRateWithEvent sets the consensus exchange rate of Luna
// denominated in the denom asset to the store with ABCI event
func (k Keeper) SetLunaExchangeRateWithEvent(ctx sdk.Context, denom string, exchangeRate sdk.Dec) {
k.SetLunaExchangeRate(ctx, denom, exchangeRate)
ctx.EventManager().EmitEvent(
sdk.NewEvent(types.EventTypeExchangeRateUpdate,
sdk.NewAttribute(types.AttributeKeyDenom, denom),
sdk.NewAttribute(types.AttributeKeyExchangeRate, exchangeRate.String()),
),
)
}
// DeleteLunaExchangeRate deletes the consensus exchange rate of Luna denominated in the denom asset from the store.
func (k Keeper) DeleteLunaExchangeRate(ctx sdk.Context, denom string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetExchangeRateKey(denom))
}
// IterateLunaExchangeRates iterates over luna rates in the store
func (k Keeper) IterateLunaExchangeRates(ctx sdk.Context, handler func(denom string, exchangeRate sdk.Dec) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.ExchangeRateKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
denom := string(iter.Key()[len(types.ExchangeRateKey):])
dp := sdk.DecProto{}
k.cdc.MustUnmarshal(iter.Value(), &dp)
if handler(denom, dp.Dec) {
break
}
}
}
//-----------------------------------
// Oracle delegation logic
// GetFeederDelegation gets the account address that the validator operator delegated oracle vote rights to
func (k Keeper) GetFeederDelegation(ctx sdk.Context, operator sdk.ValAddress) sdk.AccAddress {
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)
}
return sdk.AccAddress(bz)
}
// SetFeederDelegation sets the account address that the validator operator delegated oracle vote rights to
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())
}
// IterateFeederDelegations iterates over the feed delegates and performs a callback function.
func (k Keeper) IterateFeederDelegations(ctx sdk.Context,
handler func(delegator sdk.ValAddress, delegate sdk.AccAddress) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.FeederDelegationKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
delegator := sdk.ValAddress(iter.Key()[2:])
delegate := sdk.AccAddress(iter.Value())
if handler(delegator, delegate) {
break
}
}
}
//-----------------------------------
// Miss counter logic
// 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(operator sdk.ValAddress, missCounter uint64) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.MissCounterKey)
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
}
}
}
//-----------------------------------
// AggregateExchangeRatePrevote logic
// GetAggregateExchangeRatePrevote retrieves an oracle prevote from the store
func (k Keeper) GetAggregateExchangeRatePrevote(ctx sdk.Context, voter sdk.ValAddress) (aggregatePrevote types.AggregateExchangeRatePrevote, err error) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetAggregateExchangeRatePrevoteKey(voter))
if b == nil {
err = sdkerrors.Wrap(types.ErrNoAggregatePrevote, voter.String())
return
}
k.cdc.MustUnmarshal(b, &aggregatePrevote)
return
}
// 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(voterAddr sdk.ValAddress, aggregatePrevote types.AggregateExchangeRatePrevote) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.AggregateExchangeRatePrevoteKey)
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
}
}
}
//-----------------------------------
// AggregateExchangeRateVote logic
// GetAggregateExchangeRateVote retrieves an oracle prevote from the store
func (k Keeper) GetAggregateExchangeRateVote(ctx sdk.Context, voter sdk.ValAddress) (aggregateVote types.AggregateExchangeRateVote, err error) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetAggregateExchangeRateVoteKey(voter))
if b == nil {
err = sdkerrors.Wrap(types.ErrNoAggregateVote, voter.String())
return
}
k.cdc.MustUnmarshal(b, &aggregateVote)
return
}
// 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))
}
// IterateAggregateExchangeRateVotes iterates rate over prevotes in the store
func (k Keeper) IterateAggregateExchangeRateVotes(ctx sdk.Context, handler func(voterAddr sdk.ValAddress, aggregateVote types.AggregateExchangeRateVote) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.AggregateExchangeRateVoteKey)
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
}
}
}
// GetTobinTax return tobin tax for the denom
func (k Keeper) GetTobinTax(ctx sdk.Context, denom string) (sdk.Dec, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetTobinTaxKey(denom))
if bz == nil {
err := sdkerrors.Wrap(types.ErrNoTobinTax, denom)
return sdk.Dec{}, err
}
tobinTax := sdk.DecProto{}
k.cdc.MustUnmarshal(bz, &tobinTax)
return tobinTax.Dec, nil
}
// SetTobinTax updates tobin tax for the denom
func (k Keeper) SetTobinTax(ctx sdk.Context, denom string, tobinTax sdk.Dec) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.DecProto{Dec: tobinTax})
store.Set(types.GetTobinTaxKey(denom), bz)
}
// IterateTobinTaxes iterates rate over tobin taxes in the store
func (k Keeper) IterateTobinTaxes(ctx sdk.Context, handler func(denom string, tobinTax sdk.Dec) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.TobinTaxKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
denom := types.ExtractDenomFromTobinTaxKey(iter.Key())
var tobinTax sdk.DecProto
k.cdc.MustUnmarshal(iter.Value(), &tobinTax)
if handler(denom, tobinTax.Dec) {
break
}
}
}
// ClearTobinTaxes clears tobin taxes
func (k Keeper) ClearTobinTaxes(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.TobinTaxKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
store.Delete(iter.Key())
}
}
// ValidateFeeder return the given feeder is allowed to feed the message or not
func (k Keeper) ValidateFeeder(ctx sdk.Context, feederAddr sdk.AccAddress, validatorAddr sdk.ValAddress) error {
if !feederAddr.Equals(validatorAddr) {
delegate := k.GetFeederDelegation(ctx, validatorAddr)
if !delegate.Equals(feederAddr) {
return sdkerrors.Wrap(types.ErrNoVotingPermission, feederAddr.String())
}
}
// Check that the given validator exists
if val := k.StakingKeeper.Validator(ctx, validatorAddr); val == nil || !val.IsBonded() {
return sdkerrors.Wrapf(stakingtypes.ErrNoValidatorFound, "validator %s is not active set", validatorAddr.String())
}
return nil
}