2018-09-04 23:41:17 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
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-01-16 13:38:05 -08:00
|
|
|
// initialize rewards for a new validator
|
|
|
|
func (k Keeper) initializeValidator(ctx sdk.Context, val sdk.Validator) {
|
|
|
|
// set initial historical rewards (period 0)
|
|
|
|
k.SetValidatorHistoricalRewards(ctx, val.GetOperator(), 0, types.ValidatorHistoricalRewards{})
|
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())
|
2018-09-04 23:41:17 -07:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// increment validator period, returning the period just ended
|
|
|
|
func (k Keeper) incrementValidatorPeriod(ctx sdk.Context, val sdk.Validator) uint64 {
|
|
|
|
// 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)
|
|
|
|
outstanding := k.GetOutstandingRewards(ctx)
|
|
|
|
feePool.CommunityPool = feePool.CommunityPool.Plus(rewards.Rewards)
|
|
|
|
outstanding = outstanding.Minus(rewards.Rewards)
|
|
|
|
k.SetFeePool(ctx, feePool)
|
|
|
|
k.SetOutstandingRewards(ctx, outstanding)
|
|
|
|
|
|
|
|
current = sdk.DecCoins{}
|
|
|
|
} else {
|
|
|
|
current = rewards.Rewards.QuoDec(sdk.NewDecFromInt(val.GetTokens()))
|
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
|
|
|
|
historical := k.GetValidatorHistoricalRewards(ctx, val.GetOperator(), rewards.Period-1)
|
2018-09-17 20:02:15 -07:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// fet new historical rewards
|
|
|
|
k.SetValidatorHistoricalRewards(ctx, val.GetOperator(), rewards.Period, historical.Plus(current))
|
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-16 13:38:05 -08:00
|
|
|
func (k Keeper) updateValidatorSlashFraction(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) {
|
|
|
|
height := uint64(ctx.BlockHeight())
|
|
|
|
currentFraction := sdk.ZeroDec()
|
|
|
|
currentPeriod := k.GetValidatorCurrentRewards(ctx, valAddr).Period
|
|
|
|
current, found := k.GetValidatorSlashEvent(ctx, valAddr, height)
|
|
|
|
if found {
|
|
|
|
// there has already been a slash event this height,
|
|
|
|
// and we don't need to store more than one,
|
|
|
|
// so just update the current slash fraction
|
|
|
|
currentFraction = current.Fraction
|
2018-10-26 04:42:53 -07:00
|
|
|
}
|
2019-01-16 13:38:05 -08:00
|
|
|
currentMultiplicand := sdk.OneDec().Sub(currentFraction)
|
|
|
|
newMultiplicand := sdk.OneDec().Sub(fraction)
|
|
|
|
updatedFraction := sdk.OneDec().Sub(currentMultiplicand.Mul(newMultiplicand))
|
|
|
|
k.SetValidatorSlashEvent(ctx, valAddr, height, types.NewValidatorSlashEvent(currentPeriod, updatedFraction))
|
2018-09-04 23:41:17 -07:00
|
|
|
}
|