From 3ee21f1ff7ffc87cf294fc9d7deabf925b14d91d Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 12 Oct 2022 16:03:45 -0400 Subject: [PATCH 1/3] refactor: Improve AllocateTokens #13524 (cherry picked from commit 7781cdb3d20bc7ebac017452897ce1e6ab3903ef) # Conflicts: # x/distribution/keeper/allocation.go --- x/distribution/keeper/allocation.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/x/distribution/keeper/allocation.go b/x/distribution/keeper/allocation.go index 273db1314eb8..814a3bfdeddf 100644 --- a/x/distribution/keeper/allocation.go +++ b/x/distribution/keeper/allocation.go @@ -80,17 +80,34 @@ func (k Keeper) AllocateTokens( // calculate fraction allocated to validators communityTax := k.GetCommunityTax(ctx) +<<<<<<< HEAD voteMultiplier := sdk.OneDec().Sub(proposerMultiplier).Sub(communityTax) +======= + voteMultiplier := math.LegacyOneDec().Sub(communityTax) + feeMultiplier := feesCollected.MulDecTruncate(voteMultiplier) +>>>>>>> 7781cdb3d (refactor: Improve AllocateTokens #13524) // 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) +<<<<<<< HEAD // TODO consider microslashing 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) +======= + // TODO: Consider micro-slashing for missing votes. + // + // Ref: https://github.com/cosmos/cosmos-sdk/issues/2525#issuecomment-430838701 + powerFraction := math.LegacyNewDec(vote.Validator.Power).QuoTruncate(math.LegacyNewDec(totalPreviousPower)) + reward := feeMultiplier.MulDecTruncate(powerFraction) + +>>>>>>> 7781cdb3d (refactor: Improve AllocateTokens #13524) k.AllocateTokensToValidator(ctx, validator, reward) remaining = remaining.Sub(reward) } @@ -100,7 +117,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 +149,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) From c18424ce977a738d1130642973474c7cff3d7435 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 13 Oct 2022 08:27:55 -0400 Subject: [PATCH 2/3] bez: updates --- x/distribution/keeper/allocation.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/x/distribution/keeper/allocation.go b/x/distribution/keeper/allocation.go index 814a3bfdeddf..a9e2d6e78789 100644 --- a/x/distribution/keeper/allocation.go +++ b/x/distribution/keeper/allocation.go @@ -80,12 +80,8 @@ func (k Keeper) AllocateTokens( // calculate fraction allocated to validators communityTax := k.GetCommunityTax(ctx) -<<<<<<< HEAD - voteMultiplier := sdk.OneDec().Sub(proposerMultiplier).Sub(communityTax) -======= - voteMultiplier := math.LegacyOneDec().Sub(communityTax) + voteMultiplier := sdk.OneDec().Sub(communityTax) feeMultiplier := feesCollected.MulDecTruncate(voteMultiplier) ->>>>>>> 7781cdb3d (refactor: Improve AllocateTokens #13524) // allocate tokens proportionally to voting power // @@ -95,19 +91,12 @@ func (k Keeper) AllocateTokens( for _, vote := range bondedVotes { validator := k.stakingKeeper.ValidatorByConsAddr(ctx, vote.Validator.Address) -<<<<<<< HEAD - // TODO consider microslashing 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) -======= // TODO: Consider micro-slashing for missing votes. // // Ref: https://github.com/cosmos/cosmos-sdk/issues/2525#issuecomment-430838701 - powerFraction := math.LegacyNewDec(vote.Validator.Power).QuoTruncate(math.LegacyNewDec(totalPreviousPower)) + powerFraction := sdk.NewDec(vote.Validator.Power).QuoTruncate(sdk.NewDec(totalPreviousPower)) reward := feeMultiplier.MulDecTruncate(powerFraction) ->>>>>>> 7781cdb3d (refactor: Improve AllocateTokens #13524) k.AllocateTokensToValidator(ctx, validator, reward) remaining = remaining.Sub(reward) } From 6d4cdcf34766b49eb8376379cc20b5b3c3991a20 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 13 Oct 2022 08:29:07 -0400 Subject: [PATCH 3/3] bez: updates --- x/distribution/keeper/allocation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/distribution/keeper/allocation.go b/x/distribution/keeper/allocation.go index a9e2d6e78789..02be99db4e63 100644 --- a/x/distribution/keeper/allocation.go +++ b/x/distribution/keeper/allocation.go @@ -80,7 +80,7 @@ func (k Keeper) AllocateTokens( // calculate fraction allocated to validators communityTax := k.GetCommunityTax(ctx) - voteMultiplier := sdk.OneDec().Sub(communityTax) + voteMultiplier := sdk.OneDec().Sub(proposerMultiplier).Sub(communityTax) feeMultiplier := feesCollected.MulDecTruncate(voteMultiplier) // allocate tokens proportionally to voting power