This commit is contained in:
rigelrozanski 2018-09-19 22:13:12 -04:00
parent 7002438d5f
commit 18fdccf95b
8 changed files with 53 additions and 43 deletions

View File

@ -44,6 +44,7 @@ type Validator interface {
GetPubKey() crypto.PubKey // validation pubkey GetPubKey() crypto.PubKey // validation pubkey
GetPower() Dec // validation power GetPower() Dec // validation power
GetTokens() Dec // validation tokens GetTokens() Dec // validation tokens
GetCommission() Dec // validator commission rate
GetDelegatorShares() Dec // Total out standing delegator shares GetDelegatorShares() Dec // Total out standing delegator shares
GetBondHeight() int64 // height in which the validator became active GetBondHeight() int64 // height in which the validator became active
} }

View File

@ -2,6 +2,7 @@ package keeper
import ( import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
) )
// Allocate fees handles distribution of the collected fees // Allocate fees handles distribution of the collected fees
@ -10,22 +11,22 @@ func (k Keeper) AllocateFees(ctx sdk.Context) {
// get the proposer of this block // get the proposer of this block
proposerConsAddr := k.GetProposerConsAddr(ctx) proposerConsAddr := k.GetProposerConsAddr(ctx)
proserValidator := k.stakeKeeper.GetValidatorFromConsAddr(ctx, proposerConsAddr) proserValidator := k.stakeKeeper.GetValidatorFromConsAddr(ctx, proposerConsAddr)
proposerDist := k.GetFeeDistribution(ctx, proserValidator.OperatorAddr) proposerDist := k.GetValidatorDistInfo(ctx, proserValidator.GetOperator())
// get the fees which have been getting collected through all the // get the fees which have been getting collected through all the
// transactions in the block // transactions in the block
feesCollected := k.FeeCollectionKeeper.GetCollectedFees(ctx) feesCollected := k.feeCollectionKeeper.GetCollectedFees(ctx)
feesCollectedDec := NewDecCoins(feesCollected) feesCollectedDec := types.NewDecCoins(feesCollected)
// allocated rewards to proposer // allocated rewards to proposer
stakePool := k.stakeKeeper.GetPool(ctx) bondedTokens := k.stakeKeeper.TotalPower(ctx)
sumPowerPrecommitValidators := sdk.NewDec(1) // XXX TODO actually calculate this sumPowerPrecommitValidators := sdk.NewDec(1) // XXX TODO actually calculate this
proposerMultiplier := sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(4, 2).Mul( proposerMultiplier := sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(4, 2).Mul(
sumPowerPrecommitValidators).Div(stakePool.BondedTokens)) sumPowerPrecommitValidators).Quo(bondedTokens))
proposerReward := feesCollectedDec.Mul(proposerMultiplier) proposerReward := feesCollectedDec.Mul(proposerMultiplier)
// apply commission // apply commission
commission := proposerReward.Mul(proserValidator.Commission) commission := proposerReward.Mul(proserValidator.GetCommission())
proposerDist.PoolCommission = proposerDist.PoolCommission.Add(commission) proposerDist.PoolCommission = proposerDist.PoolCommission.Add(commission)
proposerDist.Pool = proposerDist.Pool.Add(proposerReward.Sub(commission)) proposerDist.Pool = proposerDist.Pool.Add(proposerReward.Sub(commission))
@ -33,15 +34,15 @@ func (k Keeper) AllocateFees(ctx sdk.Context) {
communityTax := k.GetCommunityTax(ctx) communityTax := k.GetCommunityTax(ctx)
communityFunding := feesCollectedDec.Mul(communityTax) communityFunding := feesCollectedDec.Mul(communityTax)
feePool := k.GetFeePool(ctx) feePool := k.GetFeePool(ctx)
feePool.CommunityFund = feePool.CommunityFund.Add(communityFunding) feePool.CommunityPool = feePool.CommunityPool.Add(communityFunding)
// set the global pool within the distribution module // set the global pool within the distribution module
poolReceived := feesCollectedDec.Sub(proposerReward).Sub(communityFunding) poolReceived := feesCollectedDec.Mul(sdk.OneDec().Sub(proposerMultiplier).Sub(communityTax))
feePool.Pool = feePool.Pool.Add(poolReceived) feePool.Pool = feePool.Pool.Plus(poolReceived)
SetValidatorDistribution(proposerDist) k.SetValidatorDistInfo(ctx, proposerDist)
SetFeePool(feePool) k.SetFeePool(ctx, feePool)
// clear the now distributed fees // clear the now distributed fees
k.FeeCollectionKeeper.ClearCollectedFees(ctx) k.feeCollectionKeeper.ClearCollectedFees(ctx)
} }

View File

@ -32,7 +32,7 @@ func (k Keeper) RemoveDelegatorDistInfo(ctx sdk.Context, delAddr sdk.AccAddress,
valOperatorAddr sdk.ValAddress) { valOperatorAddr sdk.ValAddress) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
store.Delete(GetDelegationDistInfoKey(DelegatorAddr, ValOperatorAddr)) store.Delete(GetDelegationDistInfoKey(delAddr, valOperatorAddr))
} }
//___________________________________________________________________________________________ //___________________________________________________________________________________________
@ -45,13 +45,13 @@ func (k Keeper) GetDelegatorWithdrawAddr(ctx sdk.Context, delAddr sdk.AccAddress
if b == nil { if b == nil {
return delAddr return delAddr
} }
return sdk.AccAddress{b} return sdk.AccAddress(b)
} }
// set the delegator withdraw address // set the delegator withdraw address
func (k Keeper) SetDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) { func (k Keeper) SetDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
store.Set(GetDelegatorWithdrawAddrKey(ddi.DelegatorAddr), withdrawAddr.Bytes()) store.Set(GetDelegatorWithdrawAddrKey(delAddr), withdrawAddr.Bytes())
} }
// remove a delegator withdraw info // remove a delegator withdraw info
@ -67,18 +67,18 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccA
validatorAddr sdk.ValAddress) { validatorAddr sdk.ValAddress) {
height := ctx.BlockHeight() height := ctx.BlockHeight()
pool := k.sk.GetPool(ctx) bondedTokens := k.stakeKeeper.TotalPower(ctx)
feePool := k.GetFeePool(ctx) feePool := k.GetFeePool(ctx)
delInfo := k.GetDelegationDistInfo(ctx, delegatorAddr, validatorAddr) delInfo := k.GetDelegatorDistInfo(ctx, delegatorAddr, validatorAddr)
valInfo := k.GetValidatorDistInfo(ctx, validatorAddr) valInfo := k.GetValidatorDistInfo(ctx, validatorAddr)
validator := k.GetValidator(ctx, validatorAddr) validator := k.stakeKeeper.GetValidator(ctx, validatorAddr)
feePool, withdraw := delInfo.WithdrawRewards(feePool, valInfo, height, pool.BondedTokens, delInfo, feePool, withdraw := delInfo.WithdrawRewards(ctx, feePool, valInfo, height, bondedTokens,
validator.Tokens, validator.DelegatorShares, validator.Commission) validator.Tokens, validator.DelegatorShares, validator.Commission)
k.SetFeePool(ctx, feePool) k.SetFeePool(ctx, feePool)
withdrawAddr := k.GetDelegatorWithdrawAddr(delegatorAddr) withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr)
k.ck.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal()) k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal())
} }
//___________________________________________________________________________________________ //___________________________________________________________________________________________
@ -92,7 +92,7 @@ func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk.
} }
// return all rewards for all delegations of a delegator // return all rewards for all delegations of a delegator
func (k Keeper) GetDelegatorRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress, height int64) DecCoins { func (k Keeper) GetDelegatorRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress, height int64) types.DecCoins {
withdraw := sdk.NewDec(0) withdraw := sdk.NewDec(0)
pool := k.sk.GetPool(ctx) pool := k.sk.GetPool(ctx)

View File

@ -14,26 +14,29 @@ const (
// keeper of the stake store // keeper of the stake store
type Keeper struct { type Keeper struct {
storeKey sdk.StoreKey storeKey sdk.StoreKey
storeTKey sdk.StoreKey storeTKey sdk.StoreKey
cdc *codec.Codec cdc *codec.Codec
ps params.Setter ps params.Setter
coinKeeper types.CoinKeeper bankKeeper types.BankKeeper
stakeKeeper types.StakeKeeper stakeKeeper types.StakeKeeper
feeCollectionKeeper types.FeeCollectionKeeper
// codespace // codespace
codespace sdk.CodespaceType codespace sdk.CodespaceType
} }
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ps params.Setter, ck types.CoinKeeper, func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ps params.Setter, ck types.BankKeeper,
sk types.StakeKeeper, codespace sdk.CodespaceType) Keeper { sk types.StakeKeeper, fck types.FeeCollectionKeeper, codespace sdk.CodespaceType) Keeper {
keeper := Keeper{ keeper := Keeper{
storeKey: key, storeKey: key,
storeTKey: tkey, storeTKey: tkey,
cdc: cdc, cdc: cdc,
coinKeeper: ck, bankKeeper: ck,
codespace: codespace, stakeKeeper: sk,
feeCollectionKeeper: fck,
codespace: codespace,
} }
return keeper return keeper
} }

View File

@ -21,8 +21,9 @@ func (di DelegatorDistInfo) WithdrawRewards(fp FeePool, vi ValidatorDistInfo,
di.WithdrawalHeight = height di.WithdrawalHeight = height
accum := delegatorShares.Mul(sdk.NewDec(blocks)) accum := delegatorShares.Mul(sdk.NewDec(blocks))
withdrawalTokens := vi.Pool.Mul(accum.Quo(vi.DelAccum.Accum)) withdrawalTokens := vi.Pool.Mul(accum.Quo(vi.DelAccum.Accum))
remainingTokens := vi.Pool.Mul(sdk.OneDec().Sub(accum.Quo(vi.DelAccum.Accum)))
vi.Pool = vi.Pool.Sub(withdrawalTokens) vi.Pool = remainingTokens
vi.DelAccum.Accum = vi.DelAccum.Accum.Sub(accum) vi.DelAccum.Accum = vi.DelAccum.Accum.Sub(accum)
return di, fp, withdrawalTokens return di, fp, withdrawalTokens

View File

@ -6,9 +6,10 @@ import sdk "github.com/cosmos/cosmos-sdk/types"
type StakeKeeper interface { type StakeKeeper interface {
IterateDelegations(ctx sdk.Context, delegator sdk.AccAddress, IterateDelegations(ctx sdk.Context, delegator sdk.AccAddress,
fn func(index int64, delegation sdk.Delegation) (stop bool)) fn func(index int64, delegation sdk.Delegation) (stop bool))
GetDelegation(ctx sdk.Context, delAddr sdk.AccAddress) GetDelegation(ctx sdk.Context, delAddr sdk.AccAddress) sdk.Delegation
GetValidator(ctx sdk.Context, valAddr sdk.AccAddress) GetValidator(ctx sdk.Context, valAddr sdk.AccAddress) sdk.Validator
GetPool(ctx sdk.Context) GetValidatorFromConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) sdk.Validator
TotalPower(ctx sdk.Context) sdk.Dec
} }
// expected coin keeper // expected coin keeper

View File

@ -31,13 +31,15 @@ func (vi ValidatorDistInfo) TakeFeePoolRewards(fp FeePool, height int64, totalBo
vi.FeePoolWithdrawalHeight = height vi.FeePoolWithdrawalHeight = height
accum := sdk.NewDec(blocks).Mul(vdTokens) accum := sdk.NewDec(blocks).Mul(vdTokens)
withdrawalTokens := fp.Pool.Mul(accum.Quo(fp.ValAccum.Accum)) withdrawalTokens := fp.Pool.Mul(accum.Quo(fp.ValAccum.Accum))
remainingTokens := fp.Pool.Mul(sdk.OneDec().Sub(accum.Quo(fp.ValAccum.Accum)))
commission := withdrawalTokens.Mul(commissionRate) commission := withdrawalTokens.Mul(commissionRate)
afterCommission := withdrawalTokens.Sub(commission) afterCommission := withdrawalTokens.Mul(sdk.OneDec().Sub(commissionRate))
fp.ValAccum.Accum = fp.ValAccum.Accum.Sub(accum) fp.ValAccum.Accum = fp.ValAccum.Accum.Sub(accum)
fp.Pool = fp.Pool.Sub(withdrawalTokens) fp.Pool = remainingTokens
vi.PoolCommission = vi.PoolCommission.Add(commission) vi.PoolCommission = vi.PoolCommission.Plus(commission)
vi.Pool = vi.Pool.Add(afterCommission) vi.Pool = vi.Pool.Plus(afterCommission)
return vi, fp return vi, fp
} }

View File

@ -477,5 +477,6 @@ func (v Validator) GetOperator() sdk.ValAddress { return v.OperatorAddr }
func (v Validator) GetPubKey() crypto.PubKey { return v.ConsPubKey } func (v Validator) GetPubKey() crypto.PubKey { return v.ConsPubKey }
func (v Validator) GetPower() sdk.Dec { return v.BondedTokens() } func (v Validator) GetPower() sdk.Dec { return v.BondedTokens() }
func (v Validator) GetTokens() sdk.Dec { return v.Tokens } func (v Validator) GetTokens() sdk.Dec { return v.Tokens }
func (v Validator) GetCommission() sdk.Dec { return v.Commission }
func (v Validator) GetDelegatorShares() sdk.Dec { return v.DelegatorShares } func (v Validator) GetDelegatorShares() sdk.Dec { return v.DelegatorShares }
func (v Validator) GetBondHeight() int64 { return v.BondHeight } func (v Validator) GetBondHeight() int64 { return v.BondHeight }