github.com/Finschia/finschia-sdk@v0.48.1/x/staking/spec/01_state.md (about)

     1  <!--
     2  order: 1
     3  -->
     4  
     5  # State
     6  
     7  ## Pool
     8  
     9  Pool is used for tracking bonded and not-bonded token supply of the bond denomination.
    10  
    11  ## LastTotalPower
    12  
    13  LastTotalPower tracks the total amounts of bonded tokens recorded during the previous end block.
    14  Store entries prefixed with "Last" must remain unchanged until EndBlock.
    15  
    16  - LastTotalPower: `0x12 -> ProtocolBuffer(sdk.Int)`
    17  
    18  ## Params
    19  
    20  Params is a module-wide configuration structure that stores system parameters
    21  and defines overall functioning of the staking module.
    22  
    23  - Params: `Paramsspace("staking") -> legacy_amino(params)`
    24  
    25  +++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.1/proto/cosmos/staking/v1beta1/staking.proto#L230-L241
    26  
    27  ## Validator
    28  
    29  Validators can have one of three statuses
    30  
    31  - `Unbonded`: The validator is not in the active set. They cannot sign blocks and do not earn
    32    rewards. They can receive delegations.
    33  - `Bonded`: Once the validator receives sufficient bonded tokens they automtically join the
    34    active set during [`EndBlock`](./05_end_block.md#validator-set-changes) and their status is updated to `Bonded`.
    35    They are signing blocks and receiving rewards. They can receive further delegations.
    36    They can be slashed for misbehavior. Delegators to this validator who unbond their delegation
    37    must wait the duration of the UnbondingTime, a chain-specific param, during which time
    38    they are still slashable for offences of the source validator if those offences were committed
    39    during the period of time that the tokens were bonded.
    40  - `Unbonding`: When a validator leaves the active set, either by choice or due to slashing, jailing or
    41    tombstoning, an unbonding of all their delegations begins. All delegations must then wait the UnbondingTime
    42    before their tokens are moved to their accounts from the `BondedPool`.
    43  
    44  `OperatorAddr`, an SDK validator address for the operator of the validator. Two
    45  additional indices are maintained per validator object in order to fulfill
    46  required lookups for slashing and validator-set updates. A third special index
    47  (`LastValidatorPower`) is also maintained which however remains constant
    48  throughout each block, unlike the first two indices which mirror the validator
    49  records within a block.
    50  
    51  - Validators: `0x21 | OperatorAddrLen (1 byte) | OperatorAddr -> ProtocolBuffer(validator)`
    52  - ValidatorsByConsAddr: `0x22 | ConsAddrLen (1 byte) | ConsAddr -> OperatorAddr`
    53  - ValidatorsByPower: `0x23 | BigEndian(ConsensusPower) | OperatorAddrLen (1 byte) | OperatorAddr -> OperatorAddr`
    54  - LastValidatorsPower: `0x11 | OperatorAddrLen (1 byte) | OperatorAddr -> ProtocolBuffer(ConsensusPower)`
    55  
    56  `Validators` is the primary index - it ensures that each operator can have only one
    57  associated validator, where the public key of that validator can change in the
    58  future. Delegators can refer to the immutable operator of the validator, without
    59  concern for the changing public key.
    60  
    61  `ValidatorByConsAddr` is an additional index that enables lookups for slashing.
    62  When Tendermint reports evidence, it provides the validator address, so this
    63  map is needed to find the operator. Note that the `ConsAddr` corresponds to the
    64  address which can be derived from the validator's `ConsPubKey`.
    65  
    66  `ValidatorsByPower` is an additional index that provides a sorted list of
    67  potential validators to quickly determine the current active set. Here
    68  ConsensusPower is validator.Tokens/10^6 by default. Note that all validators
    69  where `Jailed` is true are not stored within this index.
    70  
    71  `LastValidatorsPower` is a special index that provides a historical list of the
    72  last-block's bonded validators. This index remains constant during a block but
    73  is updated during the validator set update process which takes place in [`EndBlock`](./05_end_block.md).
    74  Each validator's state is stored in a `Validator` struct:
    75  
    76  +++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L65-L99
    77  
    78  +++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L24-L63
    79  
    80  ## Delegation
    81  
    82  Delegations are identified by combining `DelegatorAddr` (the address of the delegator)
    83  with the `ValidatorAddr` Delegators are indexed in the store as follows:
    84  
    85  - Delegation: `0x31 | DelegatorAddrLen (1 byte) | DelegatorAddr | ValidatorAddrLen (1 byte) | ValidatorAddr -> ProtocolBuffer(delegation)`
    86  
    87  Stake holders may delegate coins to validators; under this circumstance their
    88  funds are held in a `Delegation` data structure. It is owned by one
    89  delegator, and is associated with the shares for one validator. The sender of
    90  the transaction is the owner of the bond.
    91  
    92  +++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L159-L170
    93  
    94  ### Delegator Shares
    95  
    96  When one Delegates tokens to a Validator they are issued a number of delegator shares based on a
    97  dynamic exchange rate, calculated as follows from the total number of tokens delegated to the
    98  validator and the number of shares issued so far:
    99  
   100  `Shares per Token = validator.TotalShares() / validator.Tokens()`
   101  
   102  Only the number of shares received is stored on the DelegationEntry. When a delegator then
   103  Undelegates, the token amount they receive is calculated from the number of shares they currently
   104  hold and the inverse exchange rate:
   105  
   106  `Tokens per Share = validator.Tokens() / validatorShares()`
   107  
   108  These `Shares` are simply an accounting mechanism. They are not a fungible asset. The reason for
   109  this mechanism is to simplify the accounting around slashing. Rather than iteratively slashing the
   110  tokens of every delegation entry, instead the Validators total bonded tokens can be slashed,
   111  effectively reducing the value of each issued delegator share.
   112  
   113  ## UnbondingDelegation
   114  
   115  Shares in a `Delegation` can be unbonded, but they must for some time exist as
   116  an `UnbondingDelegation`, where shares can be reduced if Byzantine behavior is
   117  detected.
   118  
   119  `UnbondingDelegation` are indexed in the store as:
   120  
   121  - UnbondingDelegation: `0x32 | DelegatorAddrLen (1 byte) | DelegatorAddr | ValidatorAddrLen (1 byte) | ValidatorAddr -> ProtocolBuffer(unbondingDelegation)`
   122  - UnbondingDelegationsFromValidator: `0x33 | ValidatorAddrLen (1 byte) | ValidatorAddr | DelegatorAddrLen (1 byte) | DelegatorAddr -> nil`
   123  
   124  The first map here is used in queries, to lookup all unbonding delegations for
   125  a given delegator, while the second map is used in slashing, to lookup all
   126  unbonding delegations associated with a given validator that need to be
   127  slashed.
   128  
   129  A UnbondingDelegation object is created every time an unbonding is initiated.
   130  
   131  +++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L172-L198
   132  
   133  ## Redelegation
   134  
   135  The bonded tokens worth of a `Delegation` may be instantly redelegated from a
   136  source validator to a different validator (destination validator). However when
   137  this occurs they must be tracked in a `Redelegation` object, whereby their
   138  shares can be slashed if their tokens have contributed to a Byzantine fault
   139  committed by the source validator.
   140  
   141  `Redelegation` are indexed in the store as:
   142  
   143  - Redelegations: `0x34 | DelegatorAddrLen (1 byte) | DelegatorAddr | ValidatorAddrLen (1 byte) | ValidatorSrcAddr | ValidatorDstAddr -> ProtocolBuffer(redelegation)`
   144  - RedelegationsBySrc: `0x35 | ValidatorSrcAddrLen (1 byte) | ValidatorSrcAddr | ValidatorDstAddrLen (1 byte) | ValidatorDstAddr | DelegatorAddrLen (1 byte) | DelegatorAddr -> nil`
   145  - RedelegationsByDst: `0x36 | ValidatorDstAddrLen (1 byte) | ValidatorDstAddr | ValidatorSrcAddrLen (1 byte) | ValidatorSrcAddr | DelegatorAddrLen (1 byte) | DelegatorAddr -> nil`
   146  
   147  The first map here is used for queries, to lookup all redelegations for a given
   148  delegator. The second map is used for slashing based on the `ValidatorSrcAddr`,
   149  while the third map is for slashing based on the `ValidatorDstAddr`.
   150  
   151  A redelegation object is created every time a redelegation occurs. To prevent
   152  "redelegation hopping" redelegations may not occur under the situation that:
   153  
   154  - the (re)delegator already has another immature redelegation in progress
   155    with a destination to a validator (let's call it `Validator X`)
   156  - and, the (re)delegator is attempting to create a _new_ redelegation
   157    where the source validator for this new redelegation is `Validator X`.
   158  
   159  +++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L200-L228
   160  
   161  ## Queues
   162  
   163  All queues objects are sorted by timestamp. The time used within any queue is
   164  first rounded to the nearest nanosecond then sorted. The sortable time format
   165  used is a slight modification of the RFC3339Nano and uses the the format string
   166  `"2006-01-02T15:04:05.000000000"`. Notably this format:
   167  
   168  - right pads all zeros
   169  - drops the time zone info (uses UTC)
   170  
   171  In all cases, the stored timestamp represents the maturation time of the queue
   172  element.
   173  
   174  ### UnbondingDelegationEntry
   175  
   176  For the purpose of tracking progress of unbonding delegations the unbonding
   177  delegations queue is kept.
   178  
   179  - UnbondingDelegation: `0x41 | format(time) -> []DVPair`
   180  
   181  +++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L123-L133
   182  
   183  ### RedelegationQueue
   184  
   185  For the purpose of tracking progress of redelegations the redelegation queue is
   186  kept.
   187  
   188  - RedelegationQueue: `0x42 | format(time) -> []DVVTriplet`
   189  
   190  +++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L140-L152
   191  
   192  ### ValidatorQueue
   193  
   194  For the purpose of tracking progress of unbonding validators the validator
   195  queue is kept.
   196  
   197  - ValidatorQueueTime: `0x43 | format(time) -> []sdk.ValAddress`
   198  
   199  The stored object as each key is an array of validator operator addresses from
   200  which the validator object can be accessed. Typically it is expected that only
   201  a single validator record will be associated with a given timestamp however it is possible
   202  that multiple validators exist in the queue at the same location.
   203  
   204  ## HistoricalInfo
   205  
   206  HistoricalInfo objects are stored and pruned at each block such that the staking keeper persists
   207  the `n` most recent historical info defined by staking module parameter: `HistoricalEntries`.
   208  
   209  +++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0/proto/cosmos/staking/v1beta1/staking.proto#L15-L22
   210  
   211  At each BeginBlock, the staking keeper will persist the current Header and the Validators that committed
   212  the current block in a `HistoricalInfo` object. The Validators are sorted on their address to ensure that
   213  they are in a determisnistic order.
   214  The oldest HistoricalEntries will be pruned to ensure that there only exist the parameter-defined number of
   215  historical entries.