## Description Upgrades the math library in the repo root and find/replaces many usages of sdk.Dec, sdk.ZeroDec, sdk.OneDec, sdk.NewDec with their legacy-ified-math-lib replacements. Note for review: I assume that I did not find 100% of usages --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
109 lines
4.2 KiB
Go
109 lines
4.2 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"cosmossdk.io/math"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
)
|
|
|
|
// initialize rewards for a new validator
|
|
func (k Keeper) initializeValidator(ctx sdk.Context, val stakingtypes.ValidatorI) {
|
|
// set initial historical rewards (period 0) with reference count of 1
|
|
k.SetValidatorHistoricalRewards(ctx, val.GetOperator(), 0, types.NewValidatorHistoricalRewards(sdk.DecCoins{}, 1))
|
|
|
|
// set current rewards (starting at period 1)
|
|
k.SetValidatorCurrentRewards(ctx, val.GetOperator(), types.NewValidatorCurrentRewards(sdk.DecCoins{}, 1))
|
|
|
|
// set accumulated commission
|
|
k.SetValidatorAccumulatedCommission(ctx, val.GetOperator(), types.InitialValidatorAccumulatedCommission())
|
|
|
|
// set outstanding rewards
|
|
k.SetValidatorOutstandingRewards(ctx, val.GetOperator(), types.ValidatorOutstandingRewards{Rewards: sdk.DecCoins{}})
|
|
}
|
|
|
|
// increment validator period, returning the period just ended
|
|
func (k Keeper) IncrementValidatorPeriod(ctx sdk.Context, val stakingtypes.ValidatorI) 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.GetValidatorOutstandingRewards(ctx, val.GetOperator())
|
|
feePool.CommunityPool = feePool.CommunityPool.Add(rewards.Rewards...)
|
|
outstanding.Rewards = outstanding.GetRewards().Sub(rewards.Rewards)
|
|
k.SetFeePool(ctx, feePool)
|
|
k.SetValidatorOutstandingRewards(ctx, val.GetOperator(), outstanding)
|
|
|
|
current = sdk.DecCoins{}
|
|
} else {
|
|
// note: necessary to truncate so we don't allow withdrawing more rewards than owed
|
|
current = rewards.Rewards.QuoDecTruncate(sdk.NewDecFromInt(val.GetTokens()))
|
|
}
|
|
|
|
// fetch historical rewards for last period
|
|
historical := k.GetValidatorHistoricalRewards(ctx, val.GetOperator(), rewards.Period-1).CumulativeRewardRatio
|
|
|
|
// decrement reference count
|
|
k.decrementReferenceCount(ctx, val.GetOperator(), rewards.Period-1)
|
|
|
|
// set new historical rewards with reference count of 1
|
|
k.SetValidatorHistoricalRewards(ctx, val.GetOperator(), rewards.Period, types.NewValidatorHistoricalRewards(historical.Add(current...), 1))
|
|
|
|
// set current rewards, incrementing period by 1
|
|
k.SetValidatorCurrentRewards(ctx, val.GetOperator(), types.NewValidatorCurrentRewards(sdk.DecCoins{}, rewards.Period+1))
|
|
|
|
return rewards.Period
|
|
}
|
|
|
|
// 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)
|
|
if historical.ReferenceCount > 2 {
|
|
panic("reference count should never exceed 2")
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
|
|
func (k Keeper) updateValidatorSlashFraction(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) {
|
|
if fraction.GT(math.LegacyOneDec()) || fraction.IsNegative() {
|
|
panic(fmt.Sprintf("fraction must be >=0 and <=1, current fraction: %v", fraction))
|
|
}
|
|
|
|
val := k.stakingKeeper.Validator(ctx, valAddr)
|
|
|
|
// increment current period
|
|
newPeriod := k.IncrementValidatorPeriod(ctx, val)
|
|
|
|
// 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)
|
|
}
|