2018-09-04 23:41:17 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2019-07-01 09:03:34 -07:00
|
|
|
"fmt"
|
|
|
|
|
2018-09-04 23:41:17 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-01-16 13:38:05 -08:00
|
|
|
|
2018-09-04 23:41:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
2019-06-05 10:42:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/exported"
|
2018-09-04 23:41:17 -07:00
|
|
|
)
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// initialize rewards for a new validator
|
2019-06-05 10:42:25 -07:00
|
|
|
func (k Keeper) initializeValidator(ctx sdk.Context, val exported.ValidatorI) {
|
2019-01-23 03:37:03 -08:00
|
|
|
// set initial historical rewards (period 0) with reference count of 1
|
|
|
|
k.SetValidatorHistoricalRewards(ctx, val.GetOperator(), 0, types.NewValidatorHistoricalRewards(sdk.DecCoins{}, 1))
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// set current rewards (starting at period 1)
|
|
|
|
k.SetValidatorCurrentRewards(ctx, val.GetOperator(), types.NewValidatorCurrentRewards(sdk.DecCoins{}, 1))
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// set accumulated commission
|
|
|
|
k.SetValidatorAccumulatedCommission(ctx, val.GetOperator(), types.InitialValidatorAccumulatedCommission())
|
2019-03-06 10:54:12 -08:00
|
|
|
|
|
|
|
// set outstanding rewards
|
2020-02-11 06:58:37 -08:00
|
|
|
k.SetValidatorOutstandingRewards(ctx, val.GetOperator(), types.ValidatorOutstandingRewards{Rewards: sdk.DecCoins{}})
|
2018-09-04 23:41:17 -07:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// increment validator period, returning the period just ended
|
2020-03-05 06:00:45 -08:00
|
|
|
func (k Keeper) IncrementValidatorPeriod(ctx sdk.Context, val exported.ValidatorI) uint64 {
|
2019-01-16 13:38:05 -08:00
|
|
|
// fetch current rewards
|
|
|
|
rewards := k.GetValidatorCurrentRewards(ctx, val.GetOperator())
|
|
|
|
|
|
|
|
// calculate current ratio
|
|
|
|
var current sdk.DecCoins
|
|
|
|
if val.GetTokens().IsZero() {
|
|
|
|
|
|
|
|
// can't calculate ratio for zero-token validators
|
|
|
|
// ergo we instead add to the community pool
|
|
|
|
feePool := k.GetFeePool(ctx)
|
2019-03-06 10:54:12 -08:00
|
|
|
outstanding := k.GetValidatorOutstandingRewards(ctx, val.GetOperator())
|
2020-01-03 12:44:53 -08:00
|
|
|
feePool.CommunityPool = feePool.CommunityPool.Add(rewards.Rewards...)
|
2020-02-11 06:58:37 -08:00
|
|
|
outstanding.Rewards = outstanding.GetRewards().Sub(rewards.Rewards)
|
2019-01-16 13:38:05 -08:00
|
|
|
k.SetFeePool(ctx, feePool)
|
2019-03-06 10:54:12 -08:00
|
|
|
k.SetValidatorOutstandingRewards(ctx, val.GetOperator(), outstanding)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
current = sdk.DecCoins{}
|
|
|
|
} else {
|
2019-01-24 13:01:32 -08:00
|
|
|
// note: necessary to truncate so we don't allow withdrawing more rewards than owed
|
2019-02-21 00:05:31 -08:00
|
|
|
current = rewards.Rewards.QuoDecTruncate(val.GetTokens().ToDec())
|
2018-12-19 08:05:33 -08:00
|
|
|
}
|
2018-11-26 04:13:47 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// fetch historical rewards for last period
|
2019-01-23 03:37:03 -08:00
|
|
|
historical := k.GetValidatorHistoricalRewards(ctx, val.GetOperator(), rewards.Period-1).CumulativeRewardRatio
|
2018-09-17 20:02:15 -07:00
|
|
|
|
2019-01-24 13:01:32 -08:00
|
|
|
// decrement reference count
|
|
|
|
k.decrementReferenceCount(ctx, val.GetOperator(), rewards.Period-1)
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// set new historical rewards with reference count of 1
|
2020-01-03 12:44:53 -08:00
|
|
|
k.SetValidatorHistoricalRewards(ctx, val.GetOperator(), rewards.Period, types.NewValidatorHistoricalRewards(historical.Add(current...), 1))
|
2018-11-26 04:21:23 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// set current rewards, incrementing period by 1
|
|
|
|
k.SetValidatorCurrentRewards(ctx, val.GetOperator(), types.NewValidatorCurrentRewards(sdk.DecCoins{}, rewards.Period+1))
|
2018-11-26 04:21:23 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
return rewards.Period
|
2018-11-26 04:21:23 -08:00
|
|
|
}
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// increment the reference count for a historical rewards value
|
|
|
|
func (k Keeper) incrementReferenceCount(ctx sdk.Context, valAddr sdk.ValAddress, period uint64) {
|
|
|
|
historical := k.GetValidatorHistoricalRewards(ctx, valAddr, period)
|
2019-01-24 13:01:32 -08:00
|
|
|
if historical.ReferenceCount > 2 {
|
|
|
|
panic("reference count should never exceed 2")
|
2019-01-23 03:37:03 -08:00
|
|
|
}
|
|
|
|
historical.ReferenceCount++
|
|
|
|
k.SetValidatorHistoricalRewards(ctx, valAddr, period, historical)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrement the reference count for a historical rewards value, and delete if zero references remain
|
|
|
|
func (k Keeper) decrementReferenceCount(ctx sdk.Context, valAddr sdk.ValAddress, period uint64) {
|
|
|
|
historical := k.GetValidatorHistoricalRewards(ctx, valAddr, period)
|
|
|
|
if historical.ReferenceCount == 0 {
|
|
|
|
panic("cannot set negative reference count")
|
|
|
|
}
|
|
|
|
historical.ReferenceCount--
|
|
|
|
if historical.ReferenceCount == 0 {
|
|
|
|
k.DeleteValidatorHistoricalReward(ctx, valAddr, period)
|
|
|
|
} else {
|
|
|
|
k.SetValidatorHistoricalRewards(ctx, valAddr, period, historical)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
func (k Keeper) updateValidatorSlashFraction(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) {
|
2019-10-14 13:05:42 -07:00
|
|
|
if fraction.GT(sdk.OneDec()) || fraction.IsNegative() {
|
2019-07-01 09:03:34 -07:00
|
|
|
panic(fmt.Sprintf("fraction must be >=0 and <=1, current fraction: %v", fraction))
|
2019-01-24 13:01:32 -08:00
|
|
|
}
|
2019-04-10 15:53:42 -07:00
|
|
|
|
2019-07-01 09:03:34 -07:00
|
|
|
val := k.stakingKeeper.Validator(ctx, valAddr)
|
2019-04-10 15:53:42 -07:00
|
|
|
|
2019-07-01 09:03:34 -07:00
|
|
|
// increment current period
|
2020-03-05 06:00:45 -08:00
|
|
|
newPeriod := k.IncrementValidatorPeriod(ctx, val)
|
2019-07-01 09:03:34 -07:00
|
|
|
|
|
|
|
// increment reference count on period we need to track
|
|
|
|
k.incrementReferenceCount(ctx, valAddr, newPeriod)
|
|
|
|
|
|
|
|
slashEvent := types.NewValidatorSlashEvent(newPeriod, fraction)
|
|
|
|
height := uint64(ctx.BlockHeight())
|
|
|
|
|
|
|
|
k.SetValidatorSlashEvent(ctx, valAddr, height, newPeriod, slashEvent)
|
2018-09-04 23:41:17 -07:00
|
|
|
}
|