cosmos-sdk/x/distribution/keeper/allocation.go

64 lines
2.3 KiB
Go
Raw Normal View History

2018-09-04 23:41:17 -07:00
package keeper
2018-09-11 10:35:47 -07:00
import (
2018-09-28 00:29:52 -07:00
"fmt"
2018-09-11 10:35:47 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-09-19 19:13:12 -07:00
"github.com/cosmos/cosmos-sdk/x/distribution/types"
2018-09-11 10:35:47 -07:00
)
2018-09-04 23:41:17 -07:00
2018-09-05 16:28:18 -07:00
// Allocate fees handles distribution of the collected fees
2018-09-13 23:35:02 -07:00
func (k Keeper) AllocateFees(ctx sdk.Context) {
2018-09-28 01:02:07 -07:00
ctx.Logger().With("module", "x/distribution").Error(fmt.Sprintf("allocation height: %v", ctx.BlockHeight()))
2018-09-04 23:41:17 -07:00
// if there is no power in the system nothing should be allocated
bondedTokens := k.stakeKeeper.TotalPower(ctx).TruncateInt()
if bondedTokens.IsZero() {
return
}
2018-09-11 10:35:47 -07:00
// get the proposer of this block
2018-09-13 23:35:02 -07:00
proposerConsAddr := k.GetProposerConsAddr(ctx)
2018-09-27 20:04:37 -07:00
proposerValidator := k.stakeKeeper.ValidatorByConsAddr(ctx, proposerConsAddr)
proposerDist := k.GetValidatorDistInfo(ctx, proposerValidator.GetOperator())
2018-09-05 15:29:20 -07:00
2018-09-11 10:35:47 -07:00
// get the fees which have been getting collected through all the
// transactions in the block
2018-09-19 19:13:12 -07:00
feesCollected := k.feeCollectionKeeper.GetCollectedFees(ctx)
feesCollectedDec := types.NewDecCoins(feesCollected)
2018-09-11 10:35:47 -07:00
// allocated rewards to proposer
sumPowerPrecommitValidators := k.GetSumPrecommitPower(ctx)
percentVoting := sdk.NewDec(sumPowerPrecommitValidators).QuoInt(bondedTokens)
// rare edge case for rounding tendermint power vs bonded decimal power
if percentVoting.GT(sdk.OneDec()) {
percentVoting = sdk.OneDec()
}
proposerMultiplier := sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(4, 2).Mul(percentVoting))
2018-10-04 00:00:24 -07:00
proposerReward := feesCollectedDec.MulDec(proposerMultiplier)
2018-09-04 23:41:17 -07:00
2018-09-11 10:35:47 -07:00
// apply commission
2018-10-04 00:00:24 -07:00
commission := proposerReward.MulDec(proposerValidator.GetCommission())
remaining := proposerReward.MulDec(sdk.OneDec().Sub(proposerValidator.GetCommission()))
proposerDist.PoolCommission = proposerDist.PoolCommission.Plus(commission)
proposerDist.Pool = proposerDist.Pool.Plus(remaining)
2018-09-04 23:41:17 -07:00
2018-09-11 10:35:47 -07:00
// allocate community funding
2018-09-18 21:42:05 -07:00
communityTax := k.GetCommunityTax(ctx)
2018-10-04 00:00:24 -07:00
communityFunding := feesCollectedDec.MulDec(communityTax)
2018-09-11 10:35:47 -07:00
feePool := k.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Plus(communityFunding)
2018-09-04 23:41:17 -07:00
2018-09-11 10:35:47 -07:00
// set the global pool within the distribution module
poolReceived := feesCollectedDec.Minus(proposerReward).Minus(communityFunding)
2018-09-19 19:13:12 -07:00
feePool.Pool = feePool.Pool.Plus(poolReceived)
2018-09-04 23:41:17 -07:00
2018-09-19 19:13:12 -07:00
k.SetValidatorDistInfo(ctx, proposerDist)
k.SetFeePool(ctx, feePool)
2018-09-11 10:35:47 -07:00
// clear the now distributed fees
2018-09-19 19:13:12 -07:00
k.feeCollectionKeeper.ClearCollectedFees(ctx)
2018-09-04 23:41:17 -07:00
}