2019-12-17 03:44:44 -08:00
|
|
|
<!--
|
2019-12-11 09:35:27 -08:00
|
|
|
order: 1
|
2019-12-17 03:44:44 -08:00
|
|
|
-->
|
2019-12-11 09:35:27 -08:00
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
# State
|
2018-05-08 14:35:24 -07:00
|
|
|
|
2021-05-10 05:41:24 -07:00
|
|
|
## Pool
|
|
|
|
|
|
|
|
Pool is used for tracking bonded and not-bonded token supply of the bond denomination.
|
|
|
|
|
2019-02-08 15:57:33 -08:00
|
|
|
## LastTotalPower
|
|
|
|
|
2019-07-01 09:47:55 -07:00
|
|
|
LastTotalPower tracks the total amounts of bonded tokens recorded during the previous end block.
|
2021-03-19 05:06:53 -07:00
|
|
|
Store entries prefixed with "Last" must remain unchanged until EndBlock.
|
2019-02-08 15:57:33 -08:00
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* LastTotalPower: `0x12 -> ProtocolBuffer(sdk.Int)`
|
2019-02-08 15:57:33 -08:00
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
## Params
|
2018-05-23 12:42:37 -07:00
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
Params is a module-wide configuration structure that stores system parameters
|
|
|
|
and defines overall functioning of the staking module.
|
2018-05-23 12:42:37 -07:00
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* Params: `Paramsspace("staking") -> legacy_amino(params)`
|
2018-08-08 03:10:21 -07:00
|
|
|
|
2022-02-14 14:39:35 -08:00
|
|
|
+++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.1/proto/cosmos/staking/v1beta1/staking.proto#L230-L241
|
2018-05-08 14:35:24 -07:00
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
## Validator
|
2018-06-15 02:53:00 -07:00
|
|
|
|
2019-09-12 08:24:31 -07:00
|
|
|
Validators can have one of three statuses
|
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* `Unbonded`: The validator is not in the active set. They cannot sign blocks and do not earn
|
2019-09-12 08:24:31 -07:00
|
|
|
rewards. They can receive delegations.
|
2022-02-10 04:07:01 -08:00
|
|
|
* `Bonded`": Once the validator receives sufficient bonded tokens they automtically join the
|
2020-01-02 07:25:09 -08:00
|
|
|
active set during [`EndBlock`](./05_end_block.md#validator-set-changes) and their status is updated to `Bonded`.
|
2019-09-12 08:24:31 -07:00
|
|
|
They are signing blocks and receiving rewards. They can receive further delegations.
|
|
|
|
They can be slashed for misbehavior. Delegators to this validator who unbond their delegation
|
2021-03-19 05:06:53 -07:00
|
|
|
must wait the duration of the UnbondingTime, a chain-specific param, during which time
|
2021-02-01 04:35:18 -08:00
|
|
|
they are still slashable for offences of the source validator if those offences were committed
|
|
|
|
during the period of time that the tokens were bonded.
|
2022-02-10 04:07:01 -08:00
|
|
|
* `Unbonding`: When a validator leaves the active set, either by choice or due to slashing, jailing or
|
2019-09-12 08:24:31 -07:00
|
|
|
tombstoning, an unbonding of all their delegations begins. All delegations must then wait the UnbondingTime
|
2021-03-19 05:06:53 -07:00
|
|
|
before their tokens are moved to their accounts from the `BondedPool`.
|
2019-09-12 08:24:31 -07:00
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
Validators objects should be primarily stored and accessed by the
|
|
|
|
`OperatorAddr`, an SDK validator address for the operator of the validator. Two
|
2019-02-08 15:57:33 -08:00
|
|
|
additional indices are maintained per validator object in order to fulfill
|
|
|
|
required lookups for slashing and validator-set updates. A third special index
|
|
|
|
(`LastValidatorPower`) is also maintained which however remains constant
|
|
|
|
throughout each block, unlike the first two indices which mirror the validator
|
2019-07-01 09:47:55 -07:00
|
|
|
records within a block.
|
2018-06-15 02:53:00 -07:00
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* Validators: `0x21 | OperatorAddrLen (1 byte) | OperatorAddr -> ProtocolBuffer(validator)`
|
|
|
|
* ValidatorsByConsAddr: `0x22 | ConsAddrLen (1 byte) | ConsAddr -> OperatorAddr`
|
|
|
|
* ValidatorsByPower: `0x23 | BigEndian(ConsensusPower) | OperatorAddrLen (1 byte) | OperatorAddr -> OperatorAddr`
|
|
|
|
* LastValidatorsPower: `0x11 | OperatorAddrLen (1 byte) | OperatorAddr -> ProtocolBuffer(ConsensusPower)`
|
2018-06-14 17:48:33 -07:00
|
|
|
|
2018-09-08 01:44:58 -07:00
|
|
|
`Validators` is the primary index - it ensures that each operator can have only one
|
|
|
|
associated validator, where the public key of that validator can change in the
|
|
|
|
future. Delegators can refer to the immutable operator of the validator, without
|
|
|
|
concern for the changing public key.
|
2018-06-14 17:48:33 -07:00
|
|
|
|
2019-02-08 15:57:33 -08:00
|
|
|
`ValidatorByConsAddr` is an additional index that enables lookups for slashing.
|
2018-09-08 01:44:58 -07:00
|
|
|
When Tendermint reports evidence, it provides the validator address, so this
|
2019-01-21 16:52:03 -08:00
|
|
|
map is needed to find the operator. Note that the `ConsAddr` corresponds to the
|
2019-07-01 09:47:55 -07:00
|
|
|
address which can be derived from the validator's `ConsPubKey`.
|
2018-06-14 17:48:33 -07:00
|
|
|
|
2021-03-19 05:06:53 -07:00
|
|
|
`ValidatorsByPower` is an additional index that provides a sorted list of
|
2019-06-12 08:57:47 -07:00
|
|
|
potential validators to quickly determine the current active set. Here
|
2021-03-19 05:06:53 -07:00
|
|
|
ConsensusPower is validator.Tokens/10^6 by default. Note that all validators
|
|
|
|
where `Jailed` is true are not stored within this index.
|
2018-05-08 14:35:24 -07:00
|
|
|
|
2019-02-08 15:57:33 -08:00
|
|
|
`LastValidatorsPower` is a special index that provides a historical list of the
|
|
|
|
last-block's bonded validators. This index remains constant during a block but
|
2020-01-02 07:25:09 -08:00
|
|
|
is updated during the validator set update process which takes place in [`EndBlock`](./05_end_block.md).
|
2019-02-08 15:57:33 -08:00
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
Each validator's state is stored in a `Validator` struct:
|
2018-05-23 12:42:37 -07:00
|
|
|
|
2022-02-14 14:39:35 -08:00
|
|
|
+++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L65-L99
|
2021-02-01 04:35:18 -08:00
|
|
|
|
2022-02-14 14:39:35 -08:00
|
|
|
+++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L24-L63
|
2018-05-08 14:35:24 -07:00
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
## Delegation
|
2018-06-14 17:48:33 -07:00
|
|
|
|
2018-09-08 01:44:58 -07:00
|
|
|
Delegations are identified by combining `DelegatorAddr` (the address of the delegator)
|
2019-01-16 02:35:18 -08:00
|
|
|
with the `ValidatorAddr` Delegators are indexed in the store as follows:
|
2018-06-15 02:53:00 -07:00
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* Delegation: `0x31 | DelegatorAddrLen (1 byte) | DelegatorAddr | ValidatorAddrLen (1 byte) | ValidatorAddr -> ProtocolBuffer(delegation)`
|
2018-05-08 14:35:24 -07:00
|
|
|
|
2019-02-20 10:21:57 -08:00
|
|
|
Stake holders may delegate coins to validators; under this circumstance their
|
2018-09-08 01:44:58 -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.
|
|
|
|
|
2022-02-14 14:39:35 -08:00
|
|
|
+++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L159-L170
|
2018-05-08 14:35:24 -07:00
|
|
|
|
2019-09-12 08:24:31 -07:00
|
|
|
### Delegator Shares
|
|
|
|
|
|
|
|
When one Delegates tokens to a Validator they are issued a number of delegator shares based on a
|
|
|
|
dynamic exchange rate, calculated as follows from the total number of tokens delegated to the
|
|
|
|
validator and the number of shares issued so far:
|
|
|
|
|
|
|
|
`Shares per Token = validator.TotalShares() / validator.Tokens()`
|
|
|
|
|
|
|
|
Only the number of shares received is stored on the DelegationEntry. When a delegator then
|
|
|
|
Undelegates, the token amount they receive is calculated from the number of shares they currently
|
|
|
|
hold and the inverse exchange rate:
|
|
|
|
|
|
|
|
`Tokens per Share = validator.Tokens() / validatorShares()`
|
|
|
|
|
|
|
|
These `Shares` are simply an accounting mechanism. They are not a fungible asset. The reason for
|
|
|
|
this mechanism is to simplify the accounting around slashing. Rather than iteratively slashing the
|
|
|
|
tokens of every delegation entry, instead the Validators total bonded tokens can be slashed,
|
|
|
|
effectively reducing the value of each issued delegator share.
|
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
## UnbondingDelegation
|
2018-06-14 17:48:33 -07:00
|
|
|
|
2019-01-16 02:35:18 -08: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 behavior is
|
|
|
|
detected.
|
2018-06-15 02:53:00 -07:00
|
|
|
|
|
|
|
`UnbondingDelegation` are indexed in the store as:
|
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* UnbondingDelegation: `0x32 | DelegatorAddrLen (1 byte) | DelegatorAddr | ValidatorAddrLen (1 byte) | ValidatorAddr -> ProtocolBuffer(unbondingDelegation)`
|
|
|
|
* UnbondingDelegationsFromValidator: `0x33 | ValidatorAddrLen (1 byte) | ValidatorAddr | DelegatorAddrLen (1 byte) | DelegatorAddr -> nil`
|
2018-06-14 17:48:33 -07:00
|
|
|
|
2019-01-16 02:35:18 -08: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.
|
2018-05-08 14:35:24 -07:00
|
|
|
|
2022-02-14 14:39:35 -08:00
|
|
|
+++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L172-L198
|
2018-05-08 14:35:24 -07:00
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
## Redelegation
|
2018-06-14 17:48:33 -07:00
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
The bonded tokens worth of a `Delegation` may be instantly redelegated from a
|
|
|
|
source validator to a different validator (destination validator). However when
|
|
|
|
this occurs they must be tracked in a `Redelegation` object, whereby their
|
|
|
|
shares can be slashed if their tokens have contributed to a Byzantine fault
|
2019-07-01 09:47:55 -07:00
|
|
|
committed by the source validator.
|
2018-06-15 02:53:00 -07:00
|
|
|
|
|
|
|
`Redelegation` are indexed in the store as:
|
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* Redelegations: `0x34 | DelegatorAddrLen (1 byte) | DelegatorAddr | ValidatorAddrLen (1 byte) | ValidatorSrcAddr | ValidatorDstAddr -> ProtocolBuffer(redelegation)`
|
|
|
|
* RedelegationsBySrc: `0x35 | ValidatorSrcAddrLen (1 byte) | ValidatorSrcAddr | ValidatorDstAddrLen (1 byte) | ValidatorDstAddr | DelegatorAddrLen (1 byte) | DelegatorAddr -> nil`
|
|
|
|
* RedelegationsByDst: `0x36 | ValidatorDstAddrLen (1 byte) | ValidatorDstAddr | ValidatorSrcAddrLen (1 byte) | ValidatorSrcAddr | DelegatorAddrLen (1 byte) | 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
|
2019-01-16 02:35:18 -08:00
|
|
|
delegator. The second map is used for slashing based on the `ValidatorSrcAddr`,
|
2019-01-21 16:52:03 -08:00
|
|
|
while the third map is for slashing based on the `ValidatorDstAddr`.
|
|
|
|
|
2021-02-01 04:35:18 -08:00
|
|
|
A redelegation object is created every time a redelegation occurs. To prevent
|
2019-09-12 08:24:31 -07:00
|
|
|
"redelegation hopping" redelegations may not occur under the situation that:
|
2018-05-25 15:52:34 -07:00
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* the (re)delegator already has another immature redelegation in progress
|
2021-02-01 04:35:18 -08:00
|
|
|
with a destination to a validator (let's call it `Validator X`)
|
2022-02-10 04:07:01 -08:00
|
|
|
* and, the (re)delegator is attempting to create a _new_ redelegation
|
2021-03-19 05:06:53 -07:00
|
|
|
where the source validator for this new redelegation is `Validator X`.
|
2021-02-01 04:35:18 -08:00
|
|
|
|
2022-02-14 14:39:35 -08:00
|
|
|
+++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L200-L228
|
2019-02-08 15:57:33 -08:00
|
|
|
|
|
|
|
## Queues
|
|
|
|
|
|
|
|
All queues objects are sorted by timestamp. The time used within any queue is
|
|
|
|
first rounded to the nearest nanosecond then sorted. The sortable time format
|
|
|
|
used is a slight modification of the RFC3339Nano and uses the the format string
|
2019-07-01 09:47:55 -07:00
|
|
|
`"2006-01-02T15:04:05.000000000"`. Notably this format:
|
2019-02-08 15:57:33 -08:00
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* right pads all zeros
|
|
|
|
* drops the time zone info (uses UTC)
|
2019-02-08 15:57:33 -08:00
|
|
|
|
|
|
|
In all cases, the stored timestamp represents the maturation time of the queue
|
2019-07-01 09:47:55 -07:00
|
|
|
element.
|
2019-02-08 15:57:33 -08:00
|
|
|
|
|
|
|
### UnbondingDelegationQueue
|
|
|
|
|
|
|
|
For the purpose of tracking progress of unbonding delegations the unbonding
|
2019-07-01 09:47:55 -07:00
|
|
|
delegations queue is kept.
|
2019-02-08 15:57:33 -08:00
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* UnbondingDelegation: `0x41 | format(time) -> []DVPair`
|
2019-02-08 15:57:33 -08:00
|
|
|
|
2022-02-14 14:39:35 -08:00
|
|
|
+++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L123-L133
|
2019-02-08 15:57:33 -08:00
|
|
|
|
|
|
|
### RedelegationQueue
|
|
|
|
|
|
|
|
For the purpose of tracking progress of redelegations the redelegation queue is
|
2019-07-01 09:47:55 -07:00
|
|
|
kept.
|
2019-02-08 15:57:33 -08:00
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* RedelegationQueue: `0x42 | format(time) -> []DVVTriplet`
|
2019-02-08 15:57:33 -08:00
|
|
|
|
2022-02-14 14:39:35 -08:00
|
|
|
+++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L140-L152
|
2019-02-08 15:57:33 -08:00
|
|
|
|
|
|
|
### ValidatorQueue
|
|
|
|
|
|
|
|
For the purpose of tracking progress of unbonding validators the validator
|
2019-07-01 09:47:55 -07:00
|
|
|
queue is kept.
|
2019-02-08 15:57:33 -08:00
|
|
|
|
2022-02-10 04:07:01 -08:00
|
|
|
* ValidatorQueueTime: `0x43 | format(time) -> []sdk.ValAddress`
|
2019-02-08 15:57:33 -08:00
|
|
|
|
|
|
|
The stored object as each key is an array of validator operator addresses from
|
2021-02-01 04:35:18 -08:00
|
|
|
which the validator object can be accessed. Typically it is expected that only
|
2019-02-08 15:57:33 -08:00
|
|
|
a single validator record will be associated with a given timestamp however it is possible
|
|
|
|
that multiple validators exist in the queue at the same location.
|
2019-12-18 05:20:02 -08:00
|
|
|
|
|
|
|
## HistoricalInfo
|
|
|
|
|
|
|
|
HistoricalInfo objects are stored and pruned at each block such that the staking keeper persists
|
|
|
|
the `n` most recent historical info defined by staking module parameter: `HistoricalEntries`.
|
|
|
|
|
2022-02-14 14:39:35 -08:00
|
|
|
+++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L15-L22
|
2019-12-18 05:20:02 -08:00
|
|
|
|
|
|
|
At each BeginBlock, the staking keeper will persist the current Header and the Validators that committed
|
2021-02-01 04:35:18 -08:00
|
|
|
the current block in a `HistoricalInfo` object. The Validators are sorted on their address to ensure that
|
2019-12-18 05:20:02 -08:00
|
|
|
they are in a determisnistic order.
|
2021-02-01 04:35:18 -08:00
|
|
|
The oldest HistoricalEntries will be pruned to ensure that there only exist the parameter-defined number of
|
2019-12-18 05:20:02 -08:00
|
|
|
historical entries.
|