2018-09-04 13:19:37 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
|
|
|
// distribution info for a particular validator
|
|
|
|
type ValidatorDistInfo struct {
|
2018-09-17 20:02:15 -07:00
|
|
|
OperatorAddr sdk.ValAddress `json:"operator_addr"`
|
2018-09-04 13:19:37 -07:00
|
|
|
|
2018-09-19 16:00:21 -07:00
|
|
|
FeePoolWithdrawalHeight int64 `json:"global_withdrawal_height"` // last height this validator withdrew from the global pool
|
|
|
|
Pool DecCoins `json:"pool"` // rewards owed to delegators, commission has already been charged (includes proposer reward)
|
|
|
|
PoolCommission DecCoins `json:"pool_commission"` // commission collected by this validator (pending withdrawal)
|
2018-09-04 13:19:37 -07:00
|
|
|
|
2018-09-17 20:02:15 -07:00
|
|
|
DelAccum TotalAccum `json:"del_accum"` // total proposer pool accumulation factor held by delegators
|
2018-09-04 13:19:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// update total delegator accumululation
|
2018-09-19 11:47:25 -07:00
|
|
|
func (vi ValidatorDistInfo) UpdateTotalDelAccum(height int64, totalDelShares sdk.Dec) ValidatorDistInfo {
|
2018-09-04 23:41:17 -07:00
|
|
|
vi.DelAccum = vi.DelAccum.Update(height, totalDelShares)
|
2018-09-04 13:19:37 -07:00
|
|
|
return vi
|
|
|
|
}
|
|
|
|
|
2018-09-19 11:47:25 -07:00
|
|
|
// XXX TODO Update dec logic
|
2018-09-19 16:00:21 -07:00
|
|
|
// move any available accumulated fees in the FeePool to the validator's pool
|
2018-09-19 11:47:25 -07:00
|
|
|
func (vi ValidatorDistInfo) TakeFeePoolRewards(fp FeePool, height int64, totalBonded, vdTokens,
|
|
|
|
commissionRate sdk.Dec) (ValidatorDistInfo, FeePool) {
|
2018-09-04 13:19:37 -07:00
|
|
|
|
2018-09-19 16:33:12 -07:00
|
|
|
fp.UpdateTotalValAccum(height, totalBonded)
|
2018-09-04 13:19:37 -07:00
|
|
|
|
|
|
|
// update the validators pool
|
2018-09-19 16:33:12 -07:00
|
|
|
blocks := height - vi.FeePoolWithdrawalHeight
|
2018-09-19 16:00:21 -07:00
|
|
|
vi.FeePoolWithdrawalHeight = height
|
2018-09-19 16:33:12 -07:00
|
|
|
accum := sdk.NewDec(blocks).Mul(vdTokens)
|
|
|
|
withdrawalTokens := fp.Pool.Mul(accum.Quo(fp.ValAccum.Accum))
|
2018-09-19 19:13:12 -07:00
|
|
|
remainingTokens := fp.Pool.Mul(sdk.OneDec().Sub(accum.Quo(fp.ValAccum.Accum)))
|
|
|
|
|
2018-09-19 11:47:25 -07:00
|
|
|
commission := withdrawalTokens.Mul(commissionRate)
|
2018-09-19 19:13:12 -07:00
|
|
|
afterCommission := withdrawalTokens.Mul(sdk.OneDec().Sub(commissionRate))
|
2018-09-04 13:19:37 -07:00
|
|
|
|
2018-09-19 16:33:12 -07:00
|
|
|
fp.ValAccum.Accum = fp.ValAccum.Accum.Sub(accum)
|
2018-09-19 19:13:12 -07:00
|
|
|
fp.Pool = remainingTokens
|
|
|
|
vi.PoolCommission = vi.PoolCommission.Plus(commission)
|
|
|
|
vi.Pool = vi.Pool.Plus(afterCommission)
|
2018-09-04 13:19:37 -07:00
|
|
|
|
2018-09-19 11:47:25 -07:00
|
|
|
return vi, fp
|
2018-09-04 13:19:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// withdraw commission rewards
|
2018-09-19 16:00:21 -07:00
|
|
|
func (vi ValidatorDistInfo) WithdrawCommission(fp FeePool, height int64,
|
|
|
|
totalBonded, vdTokens, commissionRate sdk.Dec) (vio ValidatorDistInfo, fpo FeePool, withdrawn DecCoins) {
|
2018-09-04 13:19:37 -07:00
|
|
|
|
2018-09-19 16:33:12 -07:00
|
|
|
vi, fp = vi.TakeFeePoolRewards(fp, height, totalBonded, vdTokens, commissionRate)
|
2018-09-04 13:19:37 -07:00
|
|
|
|
|
|
|
withdrawalTokens := vi.PoolCommission
|
2018-09-19 16:33:12 -07:00
|
|
|
vi.PoolCommission = DecCoins{} // zero
|
2018-09-04 13:19:37 -07:00
|
|
|
|
2018-09-19 11:47:25 -07:00
|
|
|
return vi, fp, withdrawalTokens
|
2018-09-04 13:19:37 -07:00
|
|
|
}
|