cosmos-sdk/x/distribution/types/validator_info.go

55 lines
2.0 KiB
Go
Raw Normal View History

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-17 20:02:15 -07:00
GlobalWithdrawalHeight 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-17 20:02:15 -07:00
DelAccum TotalAccum `json:"del_accum"` // total proposer pool accumulation factor held by delegators
}
// update total delegator accumululation
func (vi ValidatorDistInfo) UpdateTotalDelAccum(height int64, totalDelShares Dec) ValidatorDistInfo {
2018-09-04 23:41:17 -07:00
vi.DelAccum = vi.DelAccum.Update(height, totalDelShares)
return vi
}
// move any available accumulated fees in the Global to the validator's pool
func (vi ValidatorDistInfo) TakeGlobalRewards(g Global, height int64, totalBonded, vdTokens, commissionRate Dec) (
vi ValidatorDistInfo, g Global) {
g.UpdateTotalValAccum(height, totalBondedShares)
// update the validators pool
blocks = height - vi.GlobalWithdrawalHeight
vi.GlobalWithdrawalHeight = height
accum = blocks * vdTokens
withdrawalTokens := g.Pool * accum / g.TotalValAccum
commission := withdrawalTokens * commissionRate
g.TotalValAccum -= accumm
g.Pool -= withdrawalTokens
vi.PoolCommission += commission
vi.PoolCommissionFree += withdrawalTokens - commission
return vi, g
}
// withdraw commission rewards
func (vi ValidatorDistInfo) WithdrawCommission(g Global, height int64,
totalBonded, vdTokens, commissionRate Dec) (
vi ValidatorDistInfo, g Global, withdrawn DecCoins) {
g = vi.TakeGlobalRewards(g, height, totalBonded, vdTokens, commissionRate)
withdrawalTokens := vi.PoolCommission
vi.PoolCommission = 0
return vi, g, withdrawalTokens
}