From 86e8bcb539218e6b2693780fc46d8d12ada8183f Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 13 Oct 2022 13:35:40 +0000 Subject: [PATCH] refactor: Improve AllocateTokens (backport #13524) (#13526) --- x/distribution/keeper/allocation.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/x/distribution/keeper/allocation.go b/x/distribution/keeper/allocation.go index 273db1314eb8..02be99db4e63 100644 --- a/x/distribution/keeper/allocation.go +++ b/x/distribution/keeper/allocation.go @@ -81,16 +81,22 @@ func (k Keeper) AllocateTokens( // calculate fraction allocated to validators communityTax := k.GetCommunityTax(ctx) voteMultiplier := sdk.OneDec().Sub(proposerMultiplier).Sub(communityTax) + feeMultiplier := feesCollected.MulDecTruncate(voteMultiplier) // allocate tokens proportionally to voting power - // TODO consider parallelizing later, ref https://github.com/cosmos/cosmos-sdk/pull/3099#discussion_r246276376 + // + // TODO: Consider parallelizing later + // + // Ref: https://github.com/cosmos/cosmos-sdk/pull/3099#discussion_r246276376 for _, vote := range bondedVotes { validator := k.stakingKeeper.ValidatorByConsAddr(ctx, vote.Validator.Address) - // TODO consider microslashing for missing votes. - // ref https://github.com/cosmos/cosmos-sdk/issues/2525#issuecomment-430838701 + // TODO: Consider micro-slashing for missing votes. + // + // Ref: https://github.com/cosmos/cosmos-sdk/issues/2525#issuecomment-430838701 powerFraction := sdk.NewDec(vote.Validator.Power).QuoTruncate(sdk.NewDec(totalPreviousPower)) - reward := feesCollected.MulDecTruncate(voteMultiplier).MulDecTruncate(powerFraction) + reward := feeMultiplier.MulDecTruncate(powerFraction) + k.AllocateTokensToValidator(ctx, validator, reward) remaining = remaining.Sub(reward) } @@ -100,7 +106,8 @@ func (k Keeper) AllocateTokens( k.SetFeePool(ctx, feePool) } -// AllocateTokensToValidator allocate tokens to a particular validator, splitting according to commission +// AllocateTokensToValidator allocate tokens to a particular validator, +// splitting according to commission. func (k Keeper) AllocateTokensToValidator(ctx sdk.Context, val stakingtypes.ValidatorI, tokens sdk.DecCoins) { // split tokens between validator and delegators according to commission commission := tokens.MulDec(val.GetCommission()) @@ -131,6 +138,7 @@ func (k Keeper) AllocateTokensToValidator(ctx sdk.Context, val stakingtypes.Vali sdk.NewAttribute(types.AttributeKeyValidator, val.GetOperator().String()), ), ) + outstanding := k.GetValidatorOutstandingRewards(ctx, val.GetOperator()) outstanding.Rewards = outstanding.Rewards.Add(tokens...) k.SetValidatorOutstandingRewards(ctx, val.GetOperator(), outstanding)