Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improve AllocateTokens #13524

Merged
merged 2 commits into from Oct 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions x/distribution/keeper/allocation.go
Expand Up @@ -38,16 +38,22 @@ func (k Keeper) AllocateTokens(ctx sdk.Context, totalPreviousPower int64, bonded
remaining := feesCollected
communityTax := k.GetCommunityTax(ctx)
voteMultiplier := math.LegacyOneDec().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 := math.LegacyNewDec(vote.Validator.Power).QuoTruncate(math.LegacyNewDec(totalPreviousPower))
reward := feesCollected.MulDecTruncate(voteMultiplier).MulDecTruncate(powerFraction)
reward := feeMultiplier.MulDecTruncate(powerFraction)

k.AllocateTokensToValidator(ctx, validator, reward)
remaining = remaining.Sub(reward)
}
Expand All @@ -57,7 +63,8 @@ func (k Keeper) AllocateTokens(ctx sdk.Context, totalPreviousPower int64, bonded
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())
Expand Down Expand Up @@ -88,6 +95,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)
Expand Down