cosmos-sdk/docs/spec/distribution/04_messages.md

211 lines
7.1 KiB
Markdown
Raw Normal View History

Merge PR #3281: Staking Spec Upgrade * remove kv seperation for marshalling * pending * cleanup * cleanup x2 * pending * working * minor refactors * entry structs defined * uncompiled mechanism written * add many compile fixes * code compiles * fix test compile errors * test cover passes * ... * multiple entries fix * ... * more design fix * working * fix test cover bug * Update PENDING.md * update comment around queue completion for redelegations/ubds * basic spec updates * spec folder cleanup * cleanup docs folder cont. * ... * find-replace and folder rename * supplimentary find/replace * pending * supplimentary * pending * few undos, stakingd -> staked * to staking -> to stake * undos * most staking -> most stake * ... * undos * simplestake->simplestaking * ... * pending update * capital letter replacements * ... * working * staking doc updates from rigel/delegation-index branch * spec-spec * spec-spec * LooseTokens -> NotBondedTokens * staking state.md updates * updates to hook and endblock spec * Update docs/gaia/gaiacli.md Co-Authored-By: rigelrozanski <rigel.rozanski@gmail.com> * Update docs/gaia/validators/validator-setup.md Co-Authored-By: rigelrozanski <rigel.rozanski@gmail.com> * Update docs/gaia/validators/validator-setup.md Co-Authored-By: rigelrozanski <rigel.rozanski@gmail.com> * comment undo * remove ErrConflictingRedelegation * @cwgoes comments are resolved * further updates to endblock and state * msg json update * working transaction updates * working * complete transaction rewrite * PENDING.md * typo * add todo * address @jackzampolin @cwgoes comments * couple leftover comments, rename * Update x/staking/types/pool.go Co-Authored-By: rigelrozanski <rigel.rozanski@gmail.com> * cwgoes additions * cwgoes suggestions x2
2019-01-21 16:52:03 -08:00
# Messages
2018-09-18 20:30:00 -07:00
## MsgWithdrawDelegationRewardsAll
2018-08-15 20:33:26 -07:00
When a delegator wishes to withdraw their rewards it must send
2018-09-18 20:30:00 -07:00
`MsgWithdrawDelegationRewardsAll`. Note that parts of this transaction logic are also
triggered each with any change in individual delegations, such as an unbond,
redelegation, or delegation of additional tokens to a specific validator.
```golang
2018-09-18 20:30:00 -07:00
type MsgWithdrawDelegationRewardsAll struct {
DelegatorAddr sdk.AccAddress
}
2018-09-03 12:49:36 -07:00
func WithdrawDelegationRewardsAll(delegatorAddr, withdrawAddr sdk.AccAddress)
2018-08-15 20:33:26 -07:00
height = GetHeight()
2018-09-03 12:49:36 -07:00
withdraw = GetDelegatorRewardsAll(delegatorAddr, height)
AddCoins(withdrawAddr, withdraw.TruncateDecimal())
2018-09-03 12:49:36 -07:00
func GetDelegatorRewardsAll(delegatorAddr sdk.AccAddress, height int64) DecCoins
2018-08-15 20:33:26 -07:00
// get all distribution scenarios
delegations = GetDelegations(delegatorAddr)
2018-08-15 20:33:26 -07:00
// collect all entitled rewards
withdraw = 0
2019-01-11 12:08:01 -08:00
pool = staking.GetPool()
2018-09-05 16:15:15 -07:00
feePool = GetFeePool()
for delegation = range delegations
delInfo = GetDelegationDistInfo(delegation.DelegatorAddr,
delegation.ValidatorAddr)
valInfo = GetValidatorDistInfo(delegation.ValidatorAddr)
validator = GetValidator(delegation.ValidatorAddr)
2018-09-05 16:15:15 -07:00
feePool, diWithdraw = delInfo.WithdrawRewards(feePool, valInfo, height, pool.BondedTokens,
2018-08-15 20:33:26 -07:00
validator.Tokens, validator.DelegatorShares, validator.Commission)
withdraw += diWithdraw
2018-09-05 16:15:15 -07:00
SetFeePool(feePool)
2018-08-15 20:33:26 -07:00
return withdraw
```
2018-09-18 20:30:00 -07:00
## MsgWithdrawDelegationReward
under special circumstances a delegator may wish to withdraw rewards from only
a single validator.
```golang
2018-09-18 20:30:00 -07:00
type MsgWithdrawDelegationReward struct {
DelegatorAddr sdk.AccAddress
ValidatorAddr sdk.ValAddress
}
func WithdrawDelegationReward(delegatorAddr, validatorAddr, withdrawAddr sdk.AccAddress)
height = GetHeight()
// get all distribution scenarios
2019-01-11 12:08:01 -08:00
pool = staking.GetPool()
2018-09-05 16:15:15 -07:00
feePool = GetFeePool()
delInfo = GetDelegationDistInfo(delegatorAddr,
validatorAddr)
valInfo = GetValidatorDistInfo(validatorAddr)
validator = GetValidator(validatorAddr)
2018-09-05 16:15:15 -07:00
feePool, withdraw = delInfo.WithdrawRewards(feePool, valInfo, height, pool.BondedTokens,
validator.Tokens, validator.DelegatorShares, validator.Commission)
2018-09-05 16:15:15 -07:00
SetFeePool(feePool)
AddCoins(withdrawAddr, withdraw.TruncateDecimal())
```
2018-09-18 20:30:00 -07:00
## MsgWithdrawValidatorRewardsAll
2018-08-15 20:33:26 -07:00
When a validator wishes to withdraw their rewards it must send
2018-09-18 20:30:00 -07:00
`MsgWithdrawValidatorRewardsAll`. Note that parts of this transaction logic are also
triggered each with any change in individual delegations, such as an unbond,
redelegation, or delegation of additional tokens to a specific validator. This
2018-08-15 20:33:26 -07:00
transaction withdraws the validators commission fee, as well as any rewards
earning on their self-delegation.
2018-08-08 21:34:19 -07:00
```
2018-09-18 20:30:00 -07:00
type MsgWithdrawValidatorRewardsAll struct {
OperatorAddr sdk.ValAddress // validator address to withdraw from
}
2018-09-03 12:49:36 -07:00
func WithdrawValidatorRewardsAll(operatorAddr, withdrawAddr sdk.AccAddress)
2018-08-15 20:33:26 -07:00
height = GetHeight()
2018-09-05 16:15:15 -07:00
feePool = GetFeePool()
pool = GetPool()
ValInfo = GetValidatorDistInfo(delegation.ValidatorAddr)
validator = GetValidator(delegation.ValidatorAddr)
2018-08-15 20:33:26 -07:00
// withdraw self-delegation
2018-09-03 12:49:36 -07:00
withdraw = GetDelegatorRewardsAll(validator.OperatorAddr, height)
2018-08-15 20:33:26 -07:00
// withdrawal validator commission rewards
2018-09-05 16:15:15 -07:00
feePool, commission = valInfo.WithdrawCommission(feePool, valInfo, height, pool.BondedTokens,
validator.Tokens, validator.Commission)
2018-08-15 20:33:26 -07:00
withdraw += commission
2018-09-05 16:15:15 -07:00
SetFeePool(feePool)
AddCoins(withdrawAddr, withdraw.TruncateDecimal())
```
2018-08-15 20:33:26 -07:00
2018-08-14 20:33:40 -07:00
## Common calculations
2018-08-08 21:34:19 -07:00
### Update total validator accum
2018-08-08 21:34:19 -07:00
The total amount of validator accum must be calculated in order to determine
2018-08-14 10:45:13 -07:00
the amount of pool tokens which a validator is entitled to at a particular
block. The accum is always additive to the existing accum. This term is to be
2018-08-20 08:50:13 -07:00
updated each time rewards are withdrawn from the system.
2018-08-08 21:34:19 -07:00
```
2018-09-05 16:15:15 -07:00
func (g FeePool) UpdateTotalValAccum(height int64, totalBondedTokens Dec) FeePool
2018-08-14 10:45:13 -07:00
blocks = height - g.TotalValAccumUpdateHeight
g.TotalValAccum += totalDelShares * blocks
2018-08-15 17:03:39 -07:00
g.TotalValAccumUpdateHeight = height
2018-09-03 12:49:36 -07:00
return g
2018-08-14 10:45:13 -07:00
```
2018-08-14 20:33:40 -07:00
### Update validator's accums
2018-08-08 21:34:19 -07:00
2018-08-14 10:45:13 -07:00
The total amount of delegator accum must be updated in order to determine the
amount of pool tokens which each delegator is entitled to, relative to the
other delegators for that validator. The accum is always additive to
the existing accum. This term is to be updated each time a
withdrawal is made from a validator.
2018-08-08 21:34:19 -07:00
2018-08-14 10:45:13 -07:00
```
2018-09-03 12:49:36 -07:00
func (vi ValidatorDistInfo) UpdateTotalDelAccum(height int64, totalDelShares Dec) ValidatorDistInfo
blocks = height - vi.TotalDelAccumUpdateHeight
vi.TotalDelAccum += totalDelShares * blocks
vi.TotalDelAccumUpdateHeight = height
2018-09-03 12:49:36 -07:00
return vi
2018-08-08 21:34:19 -07:00
```
2018-09-05 16:15:15 -07:00
### FeePool pool to validator pool
2018-08-08 21:34:19 -07:00
Every time a validator or delegator executes a withdrawal or the validator is
the proposer and receives new tokens, the relevant validator must move tokens
from the passive global pool to their own pool. It is at this point that the
commission is withdrawn
2018-08-08 21:34:19 -07:00
```
2018-09-05 16:15:15 -07:00
func (vi ValidatorDistInfo) TakeFeePoolRewards(g FeePool, height int64, totalBonded, vdTokens, commissionRate Dec) (
vi ValidatorDistInfo, g FeePool)
2018-09-03 12:49:36 -07:00
2018-08-14 10:45:13 -07:00
g.UpdateTotalValAccum(height, totalBondedShares)
2018-08-14 20:33:40 -07:00
// update the validators pool
2018-09-05 16:15:15 -07:00
blocks = height - vi.FeePoolWithdrawalHeight
vi.FeePoolWithdrawalHeight = height
2018-08-14 20:33:40 -07:00
accum = blocks * vdTokens
withdrawalTokens := g.Pool * accum / g.TotalValAccum
commission := withdrawalTokens * commissionRate
2018-08-14 20:33:40 -07:00
2018-08-15 01:12:44 -07:00
g.TotalValAccum -= accumm
vi.PoolCommission += commission
vi.PoolCommissionFree += withdrawalTokens - commission
2018-08-14 20:33:40 -07:00
g.Pool -= withdrawalTokens
2018-09-03 12:49:36 -07:00
return vi, g
2018-08-08 21:34:19 -07:00
```
2018-08-20 08:50:13 -07:00
### Delegation reward withdrawal
For delegations (including validator's self-delegation) all rewards from reward
pool have already had the validator's commission taken away.
2018-08-08 21:34:19 -07:00
```
2018-10-05 17:32:06 -07:00
func (di DelegationDistInfo) WithdrawRewards(g FeePool, vi ValidatorDistInfo,
2018-09-03 12:49:36 -07:00
height int64, totalBonded, vdTokens, totalDelShares, commissionRate Dec) (
2018-10-05 17:32:06 -07:00
di DelegationDistInfo, g FeePool, withdrawn DecCoins)
2018-08-14 20:33:40 -07:00
vi.UpdateTotalDelAccum(height, totalDelShares)
2018-09-05 16:15:15 -07:00
g = vi.TakeFeePoolRewards(g, height, totalBonded, vdTokens, commissionRate)
2018-08-14 20:33:40 -07:00
blocks = height - di.WithdrawalHeight
di.WithdrawalHeight = height
accum = delegatorShares * blocks
2018-08-15 01:12:44 -07:00
withdrawalTokens := vi.Pool * accum / vi.TotalDelAccum
vi.TotalDelAccum -= accum
2018-08-15 17:03:39 -07:00
vi.Pool -= withdrawalTokens
vi.TotalDelAccum -= accum
2018-09-03 12:49:36 -07:00
return di, g, withdrawalTokens
2018-08-14 20:33:40 -07:00
```
2018-08-20 08:50:13 -07:00
### Validator commission withdrawal
Commission is calculated each time rewards enter into the validator.
```
2018-09-05 16:15:15 -07:00
func (vi ValidatorDistInfo) WithdrawCommission(g FeePool, height int64,
2018-09-03 12:49:36 -07:00
totalBonded, vdTokens, commissionRate Dec) (
2018-09-05 16:15:15 -07:00
vi ValidatorDistInfo, g FeePool, withdrawn DecCoins)
2018-08-15 01:12:44 -07:00
2018-09-05 16:15:15 -07:00
g = vi.TakeFeePoolRewards(g, height, totalBonded, vdTokens, commissionRate)
2018-08-15 01:12:44 -07:00
withdrawalTokens := vi.PoolCommission
vi.PoolCommission = 0
2018-08-15 01:12:44 -07:00
2018-09-03 12:49:36 -07:00
return vi, g, withdrawalTokens
```