-
Notifications
You must be signed in to change notification settings - Fork 28
Staking Notes
- New
Validator
entry is created and written in store. - New
Delegation
entry is added in store. - Tokens are transfered from delegator account to module account (
BondedPool
module account orNotBondedPool
module acount). - Delegation amount is added to
Validator.Tokens
.
x/staking/keeper/msg_server.go CreateValidator()
x/staking/keeper/delegation.go Delegate()
-
Find/Create
Delegation
entry from store, identifier (DelegatorAddr
,ValidatorOperator
) -
subtractAccount == true
-
k.bankKeeper.DelegateCoinsFromAccountToModule()
: Remove tokens from delegator account, add tokens to module account-
Validator.IsBonded
: Tokens from delegator account sent toBondedPool
module account -
Validator.IsUnbonded
ORValidator.IsUnbonding
(Validator Not Bonded): Tokens from delegator account sent toNotBondedPool
module account
-
-
k.AddValidatorTokensAndShares()
: add tokens and shares onValidator
entry in store -
Update
Delegation
entry in store
Pretty much the same as the MsgCreateValidator
. Except not doing Validator initial setup.
- New
Delegation
entry is added in store. - Tokens are transfered from Delegator account to module account (
BondedPool
module account orNotBondedPool
module acount). - Delegation amount is added to
Validator.Tokens
.
- Delete/Update existing
Delegation
entry (relates to source validator) in store. - Create/Update the
Delegation
entry (relates to destination validator) in store. - Tokens are still in module accounts, transfer may happen between these module accounts (
BondedPool
module account orNotBondedPool
module acount). - Redelegation amount is removed from source
Validator.Tokens
. Shares is removed from sourceValidator.DelegatorShares
(total shares). - Redelegation amount is added to destination
Validator.Tokens
. Shares is added to destinationValidator.DelegatorShares
(total shares). - (Optional)
Redelegation
entry is created/updated.
x/staking/keeper/delegation.go BeginRedelegation()
- Get
Delegation
entry from store, identifier (DelegatorAddr
,SrcValidatorOperator
) - Get
SrcValidator
from store. - Delete shares on
Delegation
entry. - Delete/Update
Delegation
in store. -
k.RemoveValidatorTokensAndShares()
: remove tokens and shares on sourceValidator
, update sourceValidator
in store.
-
Find/Create
Delegation
entry from store, identifier (DelegatorAddr
,DstValidatorOperator
) -
subtractAccount == false
:
- Source
Validator
Not Bonded -> destinationValidator
Bonded: sent tokens fromNotBondedPool
toBondedPool
module account - Source
Validator
Bonded -> destinationValidator
Not Bonded: sent tokens fromBondedPool
toNotBondedPool
module account - Other cases, do nothing
- Undefined case, panic
-
k.AddValidatorTokensAndShares()
: add tokens and shares on destinationValidator
in store -
Update
Delegation
in store
Calculate completion time for RedelegationQueueEntry
- Source
Validator.IsBonded
: Wait for A full unbonding time (longest wait) - Source
Validator.IsUnbonded
: Complete right away - Source
Validator.IsUnbonding
: Wait until validator's unbonding is done
- Create/Update
Redelegation
entry in store. - Insert a corresponding entry to
RedelegationQueue
. The queue is for checking and completing matureRedelegation
entries atEndBlocker
.
- Delete/Update existing
Delegation
entry on Validator in store. - Tokens are still in module accounts, transfer may happen between these module accounts (
BondedPool
module account orNotBondedPool
module acount). - Undelegation amount is removed from
Validator.Tokens
. Shares is removed fromValidator.DelegatorShares
(total shares) -
UnbondingDelegation
entry is created/updated.
The tokens are removed from Validator.Tokens
, but is still in module accounts.
Only when it is complete (mature), the tokens will be sent back to delegator.
x/staking/keeper/msg_server.go Undelegate()
-
k.Keeper.Undelegate()
:x/staking/keeper/delegation.go Undelegate()
- Get
Delegation
entry from store, identifier (DelegatorAddr
,ValidatorOperator
) - Get
Validator
in store. - Delete shares on
Delegation
entry. - Delete/Update
Delegation
entry in store. -
k.RemoveValidatorTokensAndShares()
: remove tokens and shares onValidator
, updateValidator
in store.
k.bondedTokensToNotBonded()
: sent tokens from BondedPool
module account to NotBondedPool
module account.
- Create/Update
UnbindingDelegation
entry in store. - Insert a corresponding entry to
UnbondingDelegationQueue
. The queue is for checking and completing matureUnbindingDelegation
entries atEndBlocker
.
x/staking/abci.go EndBlocker()
x/staking/keeper/val_state_change.go BlockValidatorUpdates()
- Loop through
UBDQueue
(kind of like an index for finding matureUnbindingDelegation
entries), find and delete mature entry in it. - Mature
UnbondingDelegation
entry will trigger transfer of tokens, fromNotBondedPool
module account toDelegator
account. - Then the
UnbondingDelegation
entry is removed from store.
x/staking/keeper/delegation.go CompleteUnbonding()
k.bankKeeper.UndelegateCoinsFromModuleToAccount()
- Loop through
RedelegationQueue
(kind of like an index for finding matureRedelegation
entries), find and delete mature entry in it. - Deleting the
Redelegation
entries from store, not moving funds in any account.
x/staking/keeper/delegation.go CompleteRedelegation()
This section list a few important types for staking.
x/staking/types/staking.pb.go
// Validator defines a validator, together with the total amount of the
// Validator's bond shares and their exchange rate to coins. Slashing results in
// a decrease in the exchange rate, allowing correct calculation of future
// undelegations without iterating over delegators. When coins are delegated to
// this validator, the validator is credited with a delegation whose number of
// bond shares is based on the amount of coins delegated divided by the current
// exchange rate. Voting power can be calculated as total bonded shares
// multiplied by exchange rate.
type Validator struct {
// operator_address defines the address of the validator's operator; bech encoded in JSON.
OperatorAddress string
// consensus_pubkey is the consensus public key of the validator, as a Protobuf Any.
ConsensusPubkey *types1.Any
// jailed defined whether the validator has been jailed from bonded status or not.
Jailed bool
// status is the validator status (bonded/unbonding/unbonded).
Status BondStatus
// tokens define the delegated tokens (incl. self-delegation).
Tokens github_com_cosmos_cosmos_sdk_types.Int
// delegator_shares defines total shares issued to a validator's delegators.
DelegatorShares github_com_cosmos_cosmos_sdk_types.Dec
// description defines the description terms for the validator.
Description Description
// unbonding_height defines, if unbonding, the height at which this validator has begun unbonding.
UnbondingHeight int64
// unbonding_time defines, if unbonding, the min time for the validator to complete unbonding.
UnbondingTime time.Time
// commission defines the commission parameters.
Commission Commission
// min_self_delegation is the validator's self declared minimum self delegation.
MinSelfDelegation github_com_cosmos_cosmos_sdk_types.Int
}
One important thing on Delegation
is it does not contain actual tokens amount, it only contains shares
amount.
To calculate the corresponding tokens amount, you will need to find the corresponding Validator
entry.
// Delegation represents the bond with tokens held by an account. It is
// owned by one delegator, and is associated with the voting power of one
// validator.
type Delegation struct {
// delegator_address is the bech32-encoded address of the delegator.
DelegatorAddress string
// validator_address is the bech32-encoded address of the validator.
ValidatorAddress string
// shares define the delegation shares received.
Shares github_com_cosmos_cosmos_sdk_types.Dec
}
One thing to keep in mind is that a delegator could have multiple on-going UnbondingDelegationEntry
on a validator.
Another important thing is the difference between UnbondingDelegationEntry.InitialBalance
and UnbondingDelegationEntry.Balance
.
-
UnbondingDelegationEntry.InitialBalance
: will not change after created. -
UnbondingDelegationEntry.Balance
: this value could change if a slash happen.
// UnbondingDelegation stores all of a single delegator's unbonding bonds
// for a single validator in an time-ordered list.
type UnbondingDelegation struct {
// delegator_address is the bech32-encoded address of the delegator.
DelegatorAddress string
// validator_address is the bech32-encoded address of the validator.
ValidatorAddress string
// entries are the unbonding delegation entries.
Entries []UnbondingDelegationEntry
}
// UnbondingDelegationEntry defines an unbonding object with relevant metadata.
type UnbondingDelegationEntry struct {
// creation_height is the height which the unbonding took place.
CreationHeight int64
// completion_time is the unix time for unbonding completion.
CompletionTime time.Time
// initial_balance defines the tokens initially scheduled to receive at completion.
InitialBalance github_com_cosmos_cosmos_sdk_types.Int
// balance defines the tokens to receive at completion.
Balance github_com_cosmos_cosmos_sdk_types.Int
}
Similar to UnbondingDelegation
, a delegator could have multiple on-going RedelegationEntry
on a validator.
RedelegationEntry.SharesDst
will not be changed when a slash happens.
// Redelegation contains the list of a particular delegator's redelegating bonds
// from a particular source validator to a particular destination validator.
type Redelegation struct {
// delegator_address is the bech32-encoded address of the delegator.
DelegatorAddress string
// validator_src_address is the validator redelegation source operator address.
ValidatorSrcAddress string
// validator_dst_address is the validator redelegation destination operator address.
ValidatorDstAddress string
// entries are the redelegation entries.
Entries []RedelegationEntry
}
type RedelegationEntry struct {
// creation_height defines the height which the redelegation took place.
CreationHeight int64
// completion_time defines the unix time for redelegation completion.
CompletionTime time.Time
// initial_balance defines the initial balance when redelegation started.
InitialBalance github_com_cosmos_cosmos_sdk_types.Int `
// shares_dst is the amount of destination-validator shares created by redelegation.
SharesDst github_com_cosmos_cosmos_sdk_types.Dec
}