-
Notifications
You must be signed in to change notification settings - Fork 291
/
keeper.go
306 lines (261 loc) · 10.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
package keeper
import (
"fmt"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
core "github.com/terra-project/core/types"
"github.com/terra-project/core/x/oracle/internal/types"
)
// Keeper of the oracle store
type Keeper struct {
cdc *codec.Codec
storeKey sdk.StoreKey
paramSpace params.Subspace
distrKeeper types.DistributionKeeper
StakingKeeper types.StakingKeeper
supplyKeeper types.SupplyKeeper
distrName string
// codespace
codespace sdk.CodespaceType
}
// NewKeeper constructs a new keeper for oracle
func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey,
paramspace params.Subspace, distrKeeper types.DistributionKeeper,
stakingKeeper types.StakingKeeper, supplyKeeper types.SupplyKeeper,
distrName string, codespace sdk.CodespaceType) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
paramSpace: paramspace.WithKeyTable(ParamKeyTable()),
distrKeeper: distrKeeper,
StakingKeeper: stakingKeeper,
supplyKeeper: supplyKeeper,
distrName: distrName,
codespace: codespace,
}
}
// 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))
}
// Codespace returns a codespace of keeper
func (k Keeper) Codespace() sdk.CodespaceType {
return k.codespace
}
//-----------------------------------
// ExchangeRatePrevote logic
// GetExchangeRatePrevote retrieves an oracle prevote from the store
func (k Keeper) GetExchangeRatePrevote(ctx sdk.Context, denom string, voter sdk.ValAddress) (prevote types.ExchangeRatePrevote, err sdk.Error) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetExchangeRatePrevoteKey(denom, voter))
if b == nil {
err = types.ErrNoPrevote(k.codespace, voter, denom)
return
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &prevote)
return
}
// AddExchangeRatePrevote adds an oracle prevote to the store
func (k Keeper) AddExchangeRatePrevote(ctx sdk.Context, prevote types.ExchangeRatePrevote) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(prevote)
store.Set(types.GetExchangeRatePrevoteKey(prevote.Denom, prevote.Voter), bz)
}
// DeleteExchangeRatePrevote deletes an oracle prevote from the store
func (k Keeper) DeleteExchangeRatePrevote(ctx sdk.Context, prevote types.ExchangeRatePrevote) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetExchangeRatePrevoteKey(prevote.Denom, prevote.Voter))
}
// IterateExchangeRatePrevotes iterates rate over prevotes in the store
func (k Keeper) IterateExchangeRatePrevotes(ctx sdk.Context, handler func(prevote types.ExchangeRatePrevote) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.PrevoteKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var prevote types.ExchangeRatePrevote
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &prevote)
if handler(prevote) {
break
}
}
}
// iterateExchangeRatePrevotesWithPrefix iterates over prevotes in the store with given prefix
func (k Keeper) iterateExchangeRatePrevotesWithPrefix(ctx sdk.Context, prefix []byte, handler func(vote types.ExchangeRatePrevote) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, prefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var prevote types.ExchangeRatePrevote
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &prevote)
if handler(prevote) {
break
}
}
}
//-----------------------------------
// ExchangeRateVotes logic
// IterateExchangeRateVotes iterates over votes in the store
func (k Keeper) IterateExchangeRateVotes(ctx sdk.Context, handler func(vote types.ExchangeRateVote) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.VoteKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var vote types.ExchangeRateVote
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &vote)
if handler(vote) {
break
}
}
}
// Iterate over oracle votes in the store
func (k Keeper) iterateExchangeRateVotesWithPrefix(ctx sdk.Context, prefix []byte, handler func(vote types.ExchangeRateVote) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, prefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var vote types.ExchangeRateVote
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &vote)
if handler(vote) {
break
}
}
}
// Retrieves an oracle vote from the store
func (k Keeper) getExchangeRateVote(ctx sdk.Context, denom string, voter sdk.ValAddress) (vote types.ExchangeRateVote, err sdk.Error) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetVoteKey(denom, voter))
if b == nil {
err = types.ErrNoVote(k.codespace, voter, denom)
return
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &vote)
return
}
// AddExchangeRateVote adds an oracle vote to the store
func (k Keeper) AddExchangeRateVote(ctx sdk.Context, vote types.ExchangeRateVote) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(vote)
store.Set(types.GetVoteKey(vote.Denom, vote.Voter), bz)
}
// DeleteExchangeRateVote deletes an oracle vote from the store
func (k Keeper) DeleteExchangeRateVote(ctx sdk.Context, vote types.ExchangeRateVote) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetVoteKey(vote.Denom, vote.Voter))
}
//-----------------------------------
// 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) (exchangeRate sdk.Dec, err sdk.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(), types.ErrUnknownDenomination(k.codespace, denom)
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &exchangeRate)
return
}
// 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.MustMarshalBinaryLengthPrefixed(exchangeRate)
store.Set(types.GetExchangeRateKey(denom), bz)
}
// 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):])
var exchangeRate sdk.Dec
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &exchangeRate)
if handler(denom, exchangeRate) {
break
}
}
}
//-----------------------------------
// Oracle delegation logic
// GetOracleDelegate gets the account address that the validator operator delegated oracle vote rights to
func (k Keeper) GetOracleDelegate(ctx sdk.Context, operator sdk.ValAddress) (delegate sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetFeederDelegationKey(operator))
if b == nil {
// By default the right is delegated to the validator itself
return sdk.AccAddress(operator)
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &delegate)
return
}
// SetOracleDelegate sets the account address that the validator operator delegated oracle vote rights to
func (k Keeper) SetOracleDelegate(ctx sdk.Context, operator sdk.ValAddress, delegatedFeeder sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(delegatedFeeder)
store.Set(types.GetFeederDelegationKey(operator), bz)
}
// IterateOracleDelegates iterates over the feed delegates and performs a callback function.
func (k Keeper) IterateOracleDelegates(ctx sdk.Context,
handler func(delegator sdk.ValAddress, delegatee 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()[len(types.FeederDelegationKey):])
var delegatee sdk.AccAddress
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &delegatee)
if handler(delegator, delegatee) {
break
}
}
}
//-----------------------------------
// Reward pool logic
// getRewardPool retrieves the balance of the oracle module account
func (k Keeper) getRewardPool(ctx sdk.Context) sdk.Coins {
acc := k.supplyKeeper.GetModuleAccount(ctx, types.ModuleName)
return acc.GetCoins()
}
//-----------------------------------
// Miss counter logic
// GetMissCounter retrives the # of vote periods missed in this oracle slash window
func (k Keeper) GetMissCounter(ctx sdk.Context, operator sdk.ValAddress) (missCounter int64) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetMissCounterKey(operator))
if b == nil {
// By default the counter is zero
return 0
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &missCounter)
return
}
// SetMissCounter updates the # of vote periods missed in this oracle slash window
func (k Keeper) SetMissCounter(ctx sdk.Context, operator sdk.ValAddress, missCounter int64) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(missCounter)
store.Set(types.GetMissCounterKey(operator), bz)
}
// IterateMissCounters iterates over the miss counters and performs a callback function.
func (k Keeper) IterateMissCounters(ctx sdk.Context,
handler func(operator sdk.ValAddress, missCounter int64) (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()[len(types.MissCounterKey):])
var missCounter int64
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &missCounter)
if handler(operator, missCounter) {
break
}
}
}