-
Notifications
You must be signed in to change notification settings - Fork 61
/
keeper.go
706 lines (608 loc) · 24.6 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
package keeper
import (
"bytes"
"errors"
"fmt"
"time"
"github.com/tendermint/tendermint/libs/log"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/types/tx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
icacontrollerkeeper "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/keeper"
ibctransferkeeper "github.com/cosmos/ibc-go/v5/modules/apps/transfer/keeper"
ibckeeper "github.com/cosmos/ibc-go/v5/modules/core/keeper"
ibctmtypes "github.com/cosmos/ibc-go/v5/modules/light-clients/07-tendermint/types"
"github.com/quicksilver-zone/quicksilver/utils"
"github.com/quicksilver-zone/quicksilver/utils/addressutils"
epochskeeper "github.com/quicksilver-zone/quicksilver/x/epochs/keeper"
interchainquerykeeper "github.com/quicksilver-zone/quicksilver/x/interchainquery/keeper"
icqtypes "github.com/quicksilver-zone/quicksilver/x/interchainquery/types"
"github.com/quicksilver-zone/quicksilver/x/interchainstaking/types"
)
// Keeper of this module maintains collections of registered zones.
type Keeper struct {
cdc codec.Codec
storeKey storetypes.StoreKey
scopedKeeper *capabilitykeeper.ScopedKeeper
ICAControllerKeeper icacontrollerkeeper.Keeper
ICQKeeper interchainquerykeeper.Keeper
AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
IBCKeeper *ibckeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
ClaimsManagerKeeper types.ClaimsManagerKeeper
EpochsKeeper types.EpochsKeeper
Ir codectypes.InterfaceRegistry
hooks types.IcsHooks
paramStore paramtypes.Subspace
}
// NewKeeper returns a new instance of zones Keeper.
// This function will panic on failure.
func NewKeeper(
cdc codec.Codec,
storeKey storetypes.StoreKey,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
icaControllerKeeper icacontrollerkeeper.Keeper,
scopedKeeper *capabilitykeeper.ScopedKeeper,
icqKeeper interchainquerykeeper.Keeper,
ibcKeeper *ibckeeper.Keeper,
transferKeeper ibctransferkeeper.Keeper,
claimsManagerKeeper types.ClaimsManagerKeeper,
ps paramtypes.Subspace,
) *Keeper {
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
if addr := accountKeeper.GetModuleAddress(types.EscrowModuleAccount); addr == nil {
panic(fmt.Sprintf("%s escrow account has not been set", types.EscrowModuleAccount))
}
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}
if ibcKeeper == nil {
panic("ibcKeeper is nil")
}
return &Keeper{
cdc: cdc,
storeKey: storeKey,
scopedKeeper: scopedKeeper,
ICAControllerKeeper: icaControllerKeeper,
ICQKeeper: icqKeeper,
BankKeeper: bankKeeper,
AccountKeeper: accountKeeper,
IBCKeeper: ibcKeeper,
TransferKeeper: transferKeeper,
ClaimsManagerKeeper: claimsManagerKeeper,
hooks: nil,
paramStore: ps,
}
}
// SetHooks set the ics hooks.
func (k *Keeper) SetHooks(icsh types.IcsHooks) *Keeper {
if k.hooks != nil {
panic("cannot set epochs hooks twice")
}
k.hooks = icsh
return k
}
func (k *Keeper) GetGovAuthority(_ sdk.Context) string {
return sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), k.AccountKeeper.GetModuleAddress(govtypes.ModuleName))
}
func (k *Keeper) SetEpochsKeeper(epochsKeeper epochskeeper.Keeper) {
k.EpochsKeeper = &epochsKeeper
}
// Logger returns a module-specific logger.
func (*Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
func (k *Keeper) GetCodec() codec.Codec {
return k.cdc
}
func (k *Keeper) ScopedKeeper() *capabilitykeeper.ScopedKeeper {
return k.scopedKeeper
}
// ClaimCapability claims the channel capability passed via the OnOpenChanInit callback.
func (k *Keeper) ClaimCapability(ctx sdk.Context, capability *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, capability, name)
}
func (k *Keeper) SetConnectionForPort(ctx sdk.Context, connectionID, port string) {
mapping := types.PortConnectionTuple{ConnectionId: connectionID, PortId: port}
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixPortMapping)
bz := k.cdc.MustMarshal(&mapping)
store.Set([]byte(port), bz)
}
func (k *Keeper) GetConnectionForPort(ctx sdk.Context, port string) (string, error) {
mapping := types.PortConnectionTuple{}
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixPortMapping)
bz := store.Get([]byte(port))
if len(bz) == 0 {
return "", fmt.Errorf("unable to find mapping for port %s", port)
}
k.cdc.MustUnmarshal(bz, &mapping)
return mapping.ConnectionId, nil
}
// IteratePortConnections iterates through all of the delegations.
func (k *Keeper) IteratePortConnections(ctx sdk.Context, cb func(pc types.PortConnectionTuple) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixPortMapping)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
pc := types.PortConnectionTuple{}
k.cdc.MustUnmarshal(iterator.Value(), &pc)
if cb(pc) {
break
}
}
}
// AllPortConnections returns all delegations used during genesis dump.
func (k *Keeper) AllPortConnections(ctx sdk.Context) (pcs []types.PortConnectionTuple) {
k.IteratePortConnections(ctx, func(pc types.PortConnectionTuple) bool {
pcs = append(pcs, pc)
return false
})
return pcs
}
// ### Interval functions >>>
// * some of these functions (or portions thereof) may be changed to single
// query type functions, dependent upon callback features / capabilities;
func (k *Keeper) SetValidatorsForZone(ctx sdk.Context, data []byte, icqQuery icqtypes.Query) error {
validatorsRes, err := k.UnmarshalValidatorsResponse(data)
if err != nil {
k.Logger(ctx).Error("unable to unmarshal validators info for zone", "zone", icqQuery.ChainId, "err", err)
return err
}
if validatorsRes.Pagination != nil && !bytes.Equal(validatorsRes.Pagination.NextKey, []byte{}) {
validatorsReq, err := k.UnmarshalValidatorsRequest(icqQuery.Request)
if err != nil {
k.Logger(ctx).Error("unable to unmarshal request info for zone", "zone", icqQuery.ChainId, "err", err)
return err
}
if validatorsReq.Pagination == nil {
validatorsReq.Pagination = new(query.PageRequest)
}
validatorsReq.Pagination.Key = validatorsRes.Pagination.NextKey
k.Logger(ctx).Debug("Found pagination nextKey in valset; resubmitting...")
err = k.EmitValSetQuery(ctx, icqQuery.ConnectionId, icqQuery.ChainId, validatorsReq, sdkmath.NewInt(-1))
if err != nil {
return nil
}
}
for _, validator := range validatorsRes.Validators {
addr, err := addressutils.ValAddressFromBech32(validator.OperatorAddress, "")
if err != nil {
return err
}
val, found := k.GetValidator(ctx, icqQuery.ChainId, addr)
toQuery := false
switch {
case !found:
k.Logger(ctx).Debug("Unable to find validator - fetching proof...", "valoper", validator.OperatorAddress)
toQuery = true
case !val.CommissionRate.Equal(validator.GetCommission()):
k.Logger(ctx).Debug("Validator commission change; fetching proof", "valoper", validator.OperatorAddress, "from", val.CommissionRate, "to", validator.GetCommission())
toQuery = true
case !val.VotingPower.Equal(validator.Tokens):
k.Logger(ctx).Debug("Validator voting power change; fetching proof", "valoper", validator.OperatorAddress, "from", val.VotingPower, "to", validator.Tokens)
toQuery = true
case !val.DelegatorShares.Equal(validator.DelegatorShares):
k.Logger(ctx).Debug("Validator shares amount change; fetching proof", "valoper", validator.OperatorAddress, "from", val.DelegatorShares, "to", validator.DelegatorShares)
toQuery = true
}
if toQuery {
k.EmitValidatorQuery(ctx, icqQuery.ConnectionId, icqQuery.ChainId, validator)
}
}
return nil
}
func (k *Keeper) SetValidatorForZone(ctx sdk.Context, zone *types.Zone, data []byte) error {
if data == nil {
k.Logger(ctx).Error("expected validator state, got nil")
// return nil here, as if we receive nil we fail to unmarshal (as nil validators are invalid),
// so we can never hope to resolve this query. Possibly received a valset update from a
// different chain.
return nil
}
validator, err := k.UnmarshalValidator(data)
if err != nil {
k.Logger(ctx).Error("unable to unmarshal validator info for zone", "zone", zone.ChainId, "err", err)
return err
}
valAddrBytes, err := addressutils.ValAddressFromBech32(validator.OperatorAddress, zone.GetValoperPrefix())
if err != nil {
return err
}
val, found := k.GetValidator(ctx, zone.ChainId, valAddrBytes)
if !found {
k.Logger(ctx).Info("Unable to find validator - adding...", "valoper", validator.OperatorAddress)
jailTime := time.Time{}
if validator.IsJailed() {
jailTime = ctx.BlockTime()
}
if err := k.SetValidator(ctx, zone.ChainId, types.Validator{
ValoperAddress: validator.OperatorAddress,
CommissionRate: validator.GetCommission(),
VotingPower: validator.Tokens,
DelegatorShares: validator.DelegatorShares,
Score: sdk.ZeroDec(),
Status: validator.Status.String(),
Jailed: validator.IsJailed(),
JailedSince: jailTime,
}); err != nil {
return err
}
if err := k.MakePerformanceDelegation(ctx, zone, validator.OperatorAddress); err != nil {
return err
}
} else {
if !val.Jailed && validator.IsJailed() {
k.Logger(ctx).Info("Transitioning validator to jailed state", "valoper", validator.OperatorAddress, "old_vp", val.VotingPower, "new_vp", validator.Tokens, "new_shares", validator.DelegatorShares, "old_shares", val.DelegatorShares)
val.Jailed = true
val.JailedSince = ctx.BlockTime()
if !val.VotingPower.IsPositive() {
return fmt.Errorf("existing voting power must be greater than zero, received %s", val.VotingPower)
}
if !validator.Tokens.IsPositive() {
return fmt.Errorf("incoming voting power must be greater than zero, received %s", validator.Tokens)
}
// determine difference between previous vp/shares ratio and new ratio.
prevRatio := val.DelegatorShares.Quo(sdk.NewDecFromInt(val.VotingPower))
newRatio := validator.DelegatorShares.Quo(sdk.NewDecFromInt(validator.Tokens))
delta := newRatio.Quo(prevRatio)
err = k.UpdateWithdrawalRecordsForSlash(ctx, zone, val.ValoperAddress, delta)
if err != nil {
return err
}
} else if val.Jailed && !validator.IsJailed() {
k.Logger(ctx).Info("Transitioning validator to unjailed state", "valoper", validator.OperatorAddress)
val.Jailed = false
val.JailedSince = time.Time{}
}
if !val.CommissionRate.Equal(validator.GetCommission()) {
k.Logger(ctx).Debug("Validator commission rate change; updating...", "valoper", validator.OperatorAddress, "oldRate", val.CommissionRate, "newRate", validator.GetCommission())
val.CommissionRate = validator.GetCommission()
}
if !val.VotingPower.Equal(validator.Tokens) {
k.Logger(ctx).Debug("Validator voting power change; updating", "valoper", validator.OperatorAddress, "oldPower", val.VotingPower, "newPower", validator.Tokens)
val.VotingPower = validator.Tokens
}
if !val.DelegatorShares.Equal(validator.DelegatorShares) {
k.Logger(ctx).Debug("Validator delegator shares change; updating", "valoper", validator.OperatorAddress, "oldShares", val.DelegatorShares, "newShares", validator.DelegatorShares)
val.DelegatorShares = validator.DelegatorShares
}
if val.Status != validator.Status.String() {
k.Logger(ctx).Debug("Transitioning validator status", "valoper", validator.OperatorAddress, "previous", val.Status, "current", validator.Status.String())
val.Status = validator.Status.String()
}
if err := k.SetValidator(ctx, zone.ChainId, val); err != nil {
return err
}
if _, found := k.GetPerformanceDelegation(ctx, zone, validator.OperatorAddress); !found {
if err := k.MakePerformanceDelegation(ctx, zone, validator.OperatorAddress); err != nil {
return err
}
}
}
return nil
}
func (k *Keeper) UpdateWithdrawalRecordsForSlash(ctx sdk.Context, zone *types.Zone, valoper string, delta sdk.Dec) error {
var err error
k.IterateZoneStatusWithdrawalRecords(ctx, zone.ChainId, types.WithdrawStatusUnbond, func(_ int64, record types.WithdrawalRecord) bool {
recordSubAmount := sdkmath.ZeroInt()
distr := record.Distribution
for _, d := range distr {
if d.Valoper != valoper {
continue
}
newAmount := sdk.NewDec(int64(d.Amount)).Quo(delta).TruncateInt()
thisSubAmount := sdkmath.NewInt(int64(d.Amount)).Sub(newAmount)
recordSubAmount = recordSubAmount.Add(thisSubAmount)
d.Amount = newAmount.Uint64()
k.Logger(ctx).Info("Updated withdrawal record due to slashing", "valoper", valoper, "old_amount", d.Amount, "new_amount", newAmount.Int64(), "sub_amount", thisSubAmount.Int64())
}
record.Distribution = distr
record.Amount = record.Amount.Sub(sdk.NewCoin(zone.BaseDenom, recordSubAmount))
k.SetWithdrawalRecord(ctx, record)
return false
})
return err
}
func (k *Keeper) depositInterval(ctx sdk.Context) zoneItrFn {
return func(index int64, zone *types.Zone) (stop bool) {
if zone.DepositAddress != nil {
if !zone.DepositAddress.Balance.Empty() {
k.Logger(ctx).Debug("balance is non zero", "balance", zone.DepositAddress.Balance)
k.EmitDepositIntervalQuery(ctx, zone)
}
} else {
k.Logger(ctx).Error("deposit account is nil")
}
return false
}
}
func (k *Keeper) GetParam(ctx sdk.Context, key []byte) uint64 {
var out uint64
k.paramStore.Get(ctx, key, &out)
return out
}
func (k *Keeper) GetUnbondingEnabled(ctx sdk.Context) bool {
var out bool
k.paramStore.Get(ctx, types.KeyUnbondingEnabled, &out)
return out
}
func (k *Keeper) GetCommissionRate(ctx sdk.Context) sdk.Dec {
var out sdk.Dec
k.paramStore.Get(ctx, types.KeyCommissionRate, &out)
return out
}
// MigrateParams fetches params, adds ClaimsEnabled field and re-sets params.
func (k *Keeper) MigrateParams(ctx sdk.Context) {
params := types.Params{}
params.DepositInterval = k.GetParam(ctx, types.KeyDepositInterval)
params.CommissionRate = k.GetCommissionRate(ctx)
params.ValidatorsetInterval = k.GetParam(ctx, types.KeyValidatorSetInterval)
params.UnbondingEnabled = false
k.paramStore.SetParamSet(ctx, ¶ms)
}
func (k *Keeper) GetParams(clientCtx sdk.Context) (params types.Params) {
k.paramStore.GetParamSet(clientCtx, ¶ms)
return params
}
// SetParams sets the distribution parameters to the param space.
func (k *Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramStore.SetParamSet(ctx, ¶ms)
}
func (k *Keeper) GetChainID(ctx sdk.Context, connectionID string) (string, error) {
conn, found := k.IBCKeeper.ConnectionKeeper.GetConnection(ctx, connectionID)
if !found {
return "", fmt.Errorf("invalid connection id, \"%s\" not found", connectionID)
}
clientState, found := k.IBCKeeper.ClientKeeper.GetClientState(ctx, conn.ClientId)
if !found {
return "", fmt.Errorf("client id \"%s\" not found for connection \"%s\"", conn.ClientId, connectionID)
}
client, ok := clientState.(*ibctmtypes.ClientState)
if !ok {
return "", fmt.Errorf("invalid client state for client \"%s\" on connection \"%s\"", conn.ClientId, connectionID)
}
return client.ChainId, nil
}
func (k *Keeper) GetChainIDFromContext(ctx sdk.Context) (string, error) {
connectionID := ctx.Context().Value(utils.ContextKey("connectionID"))
if connectionID == nil {
return "", errors.New("connectionID not in context")
}
return k.GetChainID(ctx, connectionID.(string))
}
func (k *Keeper) EmitPerformanceBalanceQuery(ctx sdk.Context, zone *types.Zone) error {
_, addr, err := bech32.DecodeAndConvert(zone.PerformanceAddress.Address)
if err != nil {
return err
}
data := banktypes.CreateAccountBalancesPrefix(addr)
// query performance account for baseDenom balance every 100 blocks.
k.ICQKeeper.MakeRequest(
ctx,
zone.ConnectionId,
zone.ChainId,
types.BankStoreKey,
append(data, []byte(zone.BaseDenom)...),
sdk.NewInt(-1),
types.ModuleName,
"perfbalance",
100,
)
return nil
}
func (k *Keeper) EmitValSetQuery(ctx sdk.Context, connectionID, chainID string, validatorsReq stakingtypes.QueryValidatorsRequest, period sdkmath.Int) error {
bz, err := k.cdc.Marshal(&validatorsReq)
if err != nil {
return errors.New("failed to marshal valset pagination request")
}
k.ICQKeeper.MakeRequest(
ctx,
connectionID,
chainID,
"cosmos.staking.v1beta1.Query/Validators",
bz,
period,
types.ModuleName,
"valset",
0,
)
return nil
}
func (k *Keeper) EmitValidatorQuery(ctx sdk.Context, connectionID, chainID string, validator stakingtypes.Validator) {
_, addr, _ := bech32.DecodeAndConvert(validator.OperatorAddress)
data := stakingtypes.GetValidatorKey(addr)
k.ICQKeeper.MakeRequest(
ctx,
connectionID,
chainID,
"store/staking/key",
data,
sdk.NewInt(-1),
types.ModuleName,
"validator",
0,
)
}
func (k *Keeper) EmitDepositIntervalQuery(ctx sdk.Context, zone *types.Zone) {
req := tx.GetTxsEventRequest{
Events: []string{
"transfer.recipient='" + zone.DepositAddress.GetAddress() + "'",
},
OrderBy: tx.OrderBy_ORDER_BY_DESC,
Pagination: &query.PageRequest{
Limit: types.TxRetrieveCount,
},
}
k.ICQKeeper.MakeRequest(
ctx,
zone.ConnectionId,
zone.ChainId,
"cosmos.tx.v1beta1.Service/GetTxsEvent",
k.cdc.MustMarshal(&req),
sdk.NewInt(-1),
types.ModuleName,
"depositinterval",
0,
)
}
func (k *Keeper) GetDelegationsInProcess(ctx sdk.Context, zone *types.Zone) sdkmath.Int {
delegationsInProcess := sdkmath.ZeroInt()
k.IterateZoneReceipts(ctx, zone, func(_ int64, receipt types.Receipt) (stop bool) {
if receipt.Completed == nil {
for _, coin := range receipt.Amount {
delegationsInProcess = delegationsInProcess.Add(coin.Amount) // we cannot simply choose
}
}
return false
})
return delegationsInProcess
}
// redemption rate
func (k *Keeper) UpdateRedemptionRate(ctx sdk.Context, zone *types.Zone, epochRewards sdkmath.Int) {
delegationsInProcess := k.GetDelegationsInProcess(ctx, zone)
ratio, isZero := k.GetRatio(ctx, zone, epochRewards.Add(delegationsInProcess))
k.Logger(ctx).Info("Epochly rewards", "coins", epochRewards)
k.Logger(ctx).Info("Last redemption rate", "rate", zone.LastRedemptionRate)
k.Logger(ctx).Info("Current redemption rate", "rate", zone.RedemptionRate)
k.Logger(ctx).Info("New redemption rate", "rate", ratio, "supply", k.BankKeeper.GetSupply(ctx, zone.LocalDenom).Amount, "lv", k.GetDelegatedAmount(ctx, zone).Amount.Add(epochRewards).Add(delegationsInProcess))
// soft cap redemption rate, instead of panicking.
delta := ratio.Quo(zone.RedemptionRate)
if delta.GT(sdk.NewDecWithPrec(102, 2)) {
k.Logger(ctx).Error("ratio diverged by more than 2% upwards in the last epoch; capping at 1.02...")
ratio = zone.RedemptionRate.Mul(sdk.NewDecWithPrec(102, 2))
} else if delta.LT(sdk.NewDecWithPrec(95, 2)) && !isZero { // we allow a bigger downshift if all assets were withdrawn and we revert to zero.
k.Logger(ctx).Error("ratio diverged by more than 5% downwards in the last epoch; 5% is the theoretical max if _all_ controlled tokens were tombstoned. capping at 0.95...")
ratio = zone.RedemptionRate.Mul(sdk.NewDecWithPrec(95, 2))
}
zone.LastRedemptionRate = zone.RedemptionRate
zone.RedemptionRate = ratio
k.SetZone(ctx, zone)
}
func (k *Keeper) OverrideRedemptionRateNoCap(ctx sdk.Context, zone *types.Zone) {
ratio, _ := k.GetRatio(ctx, zone, sdk.ZeroInt())
k.Logger(ctx).Info("Last redemption rate", "rate", zone.LastRedemptionRate)
k.Logger(ctx).Info("Current redemption rate", "rate", zone.RedemptionRate)
k.Logger(ctx).Info("New redemption rate", "rate", ratio, "supply", k.BankKeeper.GetSupply(ctx, zone.LocalDenom).Amount, "lv", k.GetDelegatedAmount(ctx, zone).Amount)
zone.RedemptionRate = ratio
k.SetZone(ctx, zone)
}
func (k *Keeper) GetRatio(ctx sdk.Context, zone *types.Zone, epochRewards sdkmath.Int) (sdk.Dec, bool) {
// native asset amount
nativeAssetAmount := k.GetDelegatedAmount(ctx, zone).Amount
nativeAssetUnbondingAmount := k.GetUnbondingAmount(ctx, zone).Amount
// qAsset amount
qAssetAmount := k.BankKeeper.GetSupply(ctx, zone.LocalDenom).Amount
// check if zone is fully withdrawn (no qAssets remain)
if qAssetAmount.IsZero() {
// ratio 1.0 (default 1:1 ratio between nativeAssets and qAssets)
// native assets should not reach zero before qAssets (discount rate asymptote)
return sdk.OneDec(), true
}
return sdk.NewDecFromInt(nativeAssetAmount.Add(epochRewards).Add(nativeAssetUnbondingAmount)).Quo(sdk.NewDecFromInt(qAssetAmount)), false
}
func (k *Keeper) GetAggregateIntentOrDefault(ctx sdk.Context, z *types.Zone) (types.ValidatorIntents, error) {
var intents types.ValidatorIntents
var filteredIntents types.ValidatorIntents
if len(z.AggregateIntent) == 0 {
intents = k.DefaultAggregateIntents(ctx, z.ChainId)
} else {
intents = z.AggregateIntent
}
// filter intents here...
// check validators for tombstoned
for _, v := range intents {
valAddrBytes, err := addressutils.ValAddressFromBech32(v.ValoperAddress, z.GetValoperPrefix())
if err != nil {
return nil, err
}
val, found := k.GetValidator(ctx, z.ChainId, valAddrBytes)
// this case should not happen as we check the validity of a validator entry when intent is set.
if !found {
continue
}
// we should never let tombstoned validators into the list, even if they are explicitly selected
if val.Tombstoned {
continue
}
// we should never let denylist validators into the list, even if they are explicitly selected
// if in deny list {
// continue
// }
filteredIntents = append(filteredIntents, v)
}
return filteredIntents, nil
}
func (k *Keeper) Rebalance(ctx sdk.Context, zone *types.Zone, epochNumber int64) error {
currentAllocations, currentSum, currentLocked, lockedSum := k.GetDelegationMap(ctx, zone)
targetAllocations, err := k.GetAggregateIntentOrDefault(ctx, zone)
if err != nil {
return err
}
rebalances := types.DetermineAllocationsForRebalancing(currentAllocations, currentLocked, currentSum, lockedSum, targetAllocations, k.Logger(ctx))
msgs := make([]sdk.Msg, 0)
for _, rebalance := range rebalances {
msgs = append(msgs, &stakingtypes.MsgBeginRedelegate{DelegatorAddress: zone.DelegationAddress.Address, ValidatorSrcAddress: rebalance.Source, ValidatorDstAddress: rebalance.Target, Amount: sdk.NewCoin(zone.BaseDenom, rebalance.Amount)})
k.SetRedelegationRecord(ctx, types.RedelegationRecord{
ChainId: zone.ChainId,
EpochNumber: epochNumber,
Source: rebalance.Source,
Destination: rebalance.Target,
Amount: rebalance.Amount.Int64(),
})
}
if len(msgs) == 0 {
k.Logger(ctx).Info("No rebalancing required")
return nil
}
k.Logger(ctx).Info("Send rebalancing messages", "msgs", msgs)
return k.SubmitTx(ctx, msgs, zone.DelegationAddress, types.EpochRebalanceMemo(epochNumber), zone.MessagesPerTx)
}
// UnmarshalValidatorsResponse attempts to umarshal a byte slice into a QueryValidatorsResponse.
func (k *Keeper) UnmarshalValidatorsResponse(data []byte) (stakingtypes.QueryValidatorsResponse, error) {
validatorsRes := stakingtypes.QueryValidatorsResponse{}
if len(data) == 0 {
return validatorsRes, errors.New("attempted to unmarshal zero length byte slice (8)")
}
err := k.cdc.Unmarshal(data, &validatorsRes)
if err != nil {
return validatorsRes, err
}
return validatorsRes, nil
}
// UnmarshalValidatorsRequest attempts to umarshal a byte slice into a QueryValidatorsRequest.
func (k *Keeper) UnmarshalValidatorsRequest(data []byte) (stakingtypes.QueryValidatorsRequest, error) {
validatorsReq := stakingtypes.QueryValidatorsRequest{}
err := k.cdc.Unmarshal(data, &validatorsReq)
if err != nil {
return validatorsReq, err
}
return validatorsReq, nil
}
// UnmarshalValidator attempts to umarshal a byte slice into a Validator.
func (k *Keeper) UnmarshalValidator(data []byte) (stakingtypes.Validator, error) {
validator := stakingtypes.Validator{}
if len(data) == 0 {
return validator, errors.New("attempted to unmarshal zero length byte slice (9)")
}
err := k.cdc.Unmarshal(data, &validator)
if err != nil {
return validator, err
}
return validator, nil
}