cosmos-sdk/docs/spec/staking/state.md

202 lines
7.9 KiB
Markdown
Raw Normal View History

2018-05-08 14:35:24 -07:00
## State
2018-05-23 12:42:37 -07:00
### Pool
2018-06-15 02:53:00 -07:00
- key: `01`
- value: `amino(pool)`
2018-05-23 12:42:37 -07:00
The pool is a space for all dynamic global state of the Cosmos Hub. It tracks
information about the total amounts of Atoms in all states, representative
validator shares for stake in the global pools, moving Atom inflation
information, etc.
```golang
type Pool struct {
Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
2018-06-26 19:00:12 -07:00
LooseTokens int64 // tokens not associated with any validator
2018-06-15 02:53:00 -07:00
UnbondedTokens int64 // reserve of unbonded tokens held with validators
UnbondingTokens int64 // tokens moving from bonded to unbonded pool
BondedTokens int64 // reserve of bonded tokens
UnbondedShares sdk.Rat // sum of all shares distributed for the Unbonded Pool
UnbondingShares sdk.Rat // shares moving from Bonded to Unbonded Pool
BondedShares sdk.Rat // sum of all shares distributed for the Bonded Pool
InflationLastTime int64 // block which the last inflation was processed // TODO make time
Inflation sdk.Rat // current annual inflation rate
DateLastCommissionReset int64 // unix timestamp for last commission accounting reset (daily)
2018-05-08 14:35:24 -07:00
}
2018-05-23 12:42:37 -07:00
type PoolShares struct {
2018-06-15 02:53:00 -07:00
Status sdk.BondStatus // either: unbonded, unbonding, or bonded
Amount sdk.Rat // total shares of type ShareKind
2018-05-23 12:42:37 -07:00
}
```
### Params
2018-06-15 02:53:00 -07:00
- key: `00`
- value: `amino(params)`
2018-05-23 12:42:37 -07:00
Params is global data structure that stores system parameters and defines
overall functioning of the stake module.
```golang
2018-05-08 14:35:24 -07:00
type Params struct {
InflationRateChange sdk.Rat // maximum annual change in inflation rate
InflationMax sdk.Rat // maximum inflation rate
InflationMin sdk.Rat // minimum inflation rate
GoalBonded sdk.Rat // Goal of percent bonded atoms
2018-05-23 12:42:37 -07:00
MaxValidators uint16 // maximum number of validators
BondDenom string // bondable coin denomination
2018-05-08 14:35:24 -07:00
}
```
### Validator
2018-05-29 11:50:35 -07:00
2018-06-15 02:53:00 -07:00
Validators are identified according to the `ValOwnerAddr`,
an SDK account address for the owner of the validator.
Validators also have a `ValTendermintAddr`, the address
of the public key of the validator.
Validators are indexed in the store using the following maps:
- Validators: `0x02 | ValOwnerAddr -> amino(validator)`
- ValidatorsByPubKey: `0x03 | ValTendermintAddr -> ValOwnerAddr`
- ValidatorsByPower: `0x05 | power | blockHeight | blockTx -> ValOwnerAddr`
2018-06-14 17:48:33 -07:00
2018-06-15 02:53:00 -07:00
`Validators` is the primary index - it ensures that each owner can have only one
associated validator, where the public key of that validator can change in the
future. Delegators can refer to the immutable owner of the validator, without
concern for the changing public key.
2018-06-14 17:48:33 -07:00
2018-06-15 02:53:00 -07:00
`ValidatorsByPubKey` is a secondary index that enables lookups for slashing.
When Tendermint reports evidence, it provides the validator address, so this
map is needed to find the owner.
2018-06-14 17:48:33 -07:00
2018-06-15 02:53:00 -07:00
`ValidatorsByPower` is a secondary index that provides a sorted list of
potential validators to quickly determine the current active set. For instance,
the first 100 validators in this list can be returned with every EndBlock.
2018-05-08 14:35:24 -07:00
2018-05-23 12:42:37 -07:00
The `Validator` holds the current state and some historical actions of the
validator.
```golang
type Validator struct {
2018-06-15 02:53:00 -07:00
ConsensusPubKey crypto.PubKey // Tendermint consensus pubkey of validator
Revoked bool // has the validator been revoked?
PoolShares PoolShares // total shares for tokens held in the pool
DelegatorShares sdk.Rat // total shares issued to a validator's delegators
SlashRatio sdk.Rat // increases each time the validator is slashed
Description Description // description terms for the validator
2018-06-14 17:48:33 -07:00
// Needed for ordering vals in the bypower key
2018-06-15 02:53:00 -07:00
BondHeight int64 // earliest height as a bonded validator
BondIntraTxCounter int16 // block-local tx index of validator change
2018-06-14 17:48:33 -07:00
CommissionInfo CommissionInfo // info about the validator's commission
2018-06-15 02:53:00 -07:00
ProposerRewardPool sdk.Coins // reward pool collected from being the proposer
2018-06-14 17:48:33 -07:00
// TODO: maybe this belongs in distribution module ?
2018-06-15 02:53:00 -07:00
PrevPoolShares PoolShares // total shares of a global hold pools
2018-06-14 17:48:33 -07:00
}
2018-05-09 06:55:19 -07:00
2018-06-14 17:48:33 -07:00
type CommissionInfo struct {
2018-06-15 02:53:00 -07:00
Rate sdk.Rat // the commission rate of fees charged to any delegators
Max sdk.Rat // maximum commission rate which this validator can ever charge
ChangeRate sdk.Rat // maximum daily increase of the validator commission
ChangeToday sdk.Rat // commission rate change today, reset each day (UTC time)
2018-06-14 17:48:33 -07:00
LastChange int64 // unix timestamp of last commission change
2018-05-08 14:35:24 -07:00
}
type Description struct {
Moniker string // name
Identity string // optional identity signature (ex. UPort or Keybase)
Website string // optional website link
Details string // optional details
2018-05-08 14:35:24 -07:00
}
```
2018-05-23 12:42:37 -07:00
### Delegation
2018-06-14 17:48:33 -07:00
2018-06-15 02:53:00 -07:00
Delegations are identified by combining `DelegatorAddr` (the address of the delegator) with the ValOwnerAddr
Delegators are indexed in the store as follows:
- Delegation: ` 0x0A | DelegatorAddr | ValOwnerAddr -> amino(delegation)`
2018-05-08 14:35:24 -07:00
Atom holders may delegate coins to validators; under this circumstance their
2018-05-23 12:42:37 -07:00
funds are held in a `Delegation` data structure. It is owned by one
delegator, and is associated with the shares for one validator. The sender of
2018-05-08 14:35:24 -07:00
the transaction is the owner of the bond.
```golang
2018-05-23 12:42:37 -07:00
type Delegation struct {
2018-06-14 17:48:33 -07:00
Shares sdk.Rat // delegation shares recieved
Height int64 // last height bond updated
2018-05-08 14:35:24 -07:00
}
```
2018-05-23 12:42:37 -07:00
### UnbondingDelegation
2018-06-14 17:48:33 -07:00
2018-06-15 02:53:00 -07:00
Shares in a `Delegation` can be unbonded, but they must for some time exist as an `UnbondingDelegation`,
where shares can be reduced if Byzantine behaviour is detected.
`UnbondingDelegation` are indexed in the store as:
- UnbondingDelegationByDelegator: ` 0x0B | DelegatorAddr | ValOwnerAddr ->
amino(unbondingDelegation)`
- UnbondingDelegationByValOwner: ` 0x0C | ValOwnerAddr | DelegatorAddr | ValOwnerAddr ->
nil`
2018-06-14 17:48:33 -07:00
2018-06-15 02:53:00 -07:00
The first map here is used in queries, to lookup all unbonding delegations for
a given delegator, while the second map is used in slashing, to lookup all
unbonding delegations associated with a given validator that need to be
slashed.
2018-05-08 14:35:24 -07:00
2018-05-23 12:42:37 -07:00
A UnbondingDelegation object is created every time an unbonding is initiated.
The unbond must be completed with a second transaction provided by the
delegation owner after the unbonding period has passed.
2018-05-08 14:35:24 -07:00
```golang
2018-05-23 12:42:37 -07:00
type UnbondingDelegation struct {
2018-06-14 17:48:33 -07:00
Tokens sdk.Coins // the value in Atoms of the amount of shares which are unbonding
CompleteTime int64 // unix time to complete redelegation
2018-05-08 14:35:24 -07:00
}
```
2018-05-23 12:42:37 -07:00
### Redelegation
2018-06-14 17:48:33 -07:00
2018-06-15 02:53:00 -07:00
Shares in a `Delegation` can be rebonded to a different validator, but they must for some time exist as a `Redelegation`,
where shares can be reduced if Byzantine behaviour is detected. This is tracked
as moving a delegation from a `FromValOwnerAddr` to a `ToValOwnerAddr`.
`Redelegation` are indexed in the store as:
- Redelegations: `0x0D | DelegatorAddr | FromValOwnerAddr | ToValOwnerAddr ->
amino(redelegation)`
- RedelegationsBySrc: `0x0E | FromValOwnerAddr | ToValOwnerAddr |
DelegatorAddr -> nil`
- RedelegationsByDst: `0x0F | ToValOwnerAddr | FromValOwnerAddr | DelegatorAddr
-> nil`
2018-06-14 17:48:33 -07:00
2018-06-15 02:53:00 -07:00
The first map here is used for queries, to lookup all redelegations for a given
delegator. The second map is used for slashing based on the FromValOwnerAddr,
while the third map is for slashing based on the ToValOwnerAddr.
2018-05-08 14:35:24 -07:00
2018-05-25 15:52:34 -07:00
A redelegation object is created every time a redelegation occurs. The
redelegation must be completed with a second transaction provided by the
delegation owner after the unbonding period has passed. The destination
delegation of a redelegation may not itself undergo a new redelegation until
the original redelegation has been completed.
2018-05-08 14:35:24 -07:00
```golang
2018-05-23 12:42:37 -07:00
type Redelegation struct {
SourceShares sdk.Rat // amount of source shares redelegating
DestinationShares sdk.Rat // amount of destination shares created at redelegation
CompleteTime int64 // unix time to complete redelegation
2018-05-08 14:35:24 -07:00
}
```