-
Couldn't load subscription status.
- Fork 31
Staking Notes
- New
Validatorentry is created and written in store. - New
Delegationentry is added in store. - Tokens are transfered from delegator account to module account (
BondedPoolmodule account orNotBondedPoolmodule acount). - Delegation amount is added to
Validator.Tokens.
x/staking/keeper/msg_server.go CreateValidator()
x/staking/keeper/delegation.go Delegate()
-
Find/Create
Delegationentry 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 toBondedPoolmodule account -
Validator.IsUnbondedORValidator.IsUnbonding(Validator Not Bonded): Tokens from delegator account sent toNotBondedPoolmodule account
-
-
k.AddValidatorTokensAndShares(): add tokens and shares onValidatorentry in store -
Update
Delegationentry in store
Pretty much the same as the MsgCreateValidator. Except not doing Validator initial setup.
- New
Delegationentry is added in store. - Tokens are transfered from Delegator account to module account (
BondedPoolmodule account orNotBondedPoolmodule acount). - Delegation amount is added to
Validator.Tokens.
- Delete/Update existing
Delegationentry (relates to source validator) in store. - Create/Update the
Delegationentry (relates to destination validator) in store. - Tokens are still in module accounts, transfer may happen between these module accounts (
BondedPoolmodule account orNotBondedPoolmodule 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)
Redelegationentry is created/updated.
x/staking/keeper/delegation.go BeginRedelegation()
- Get
Delegationentry from store, identifier (DelegatorAddr,SrcValidatorOperator) - Get
SrcValidatorfrom store. - Delete shares on
Delegationentry. - Delete/Update
Delegationin store. -
k.RemoveValidatorTokensAndShares(): remove tokens and shares on sourceValidator, update sourceValidatorin store.
-
Find/Create
Delegationentry from store, identifier (DelegatorAddr,DstValidatorOperator) -
subtractAccount == false:
- Source
ValidatorNot Bonded -> destinationValidatorBonded: sent tokens fromNotBondedPooltoBondedPoolmodule account - Source
ValidatorBonded -> destinationValidatorNot Bonded: sent tokens fromBondedPooltoNotBondedPoolmodule account - Other cases, do nothing
- Undefined case, panic
-
k.AddValidatorTokensAndShares(): add tokens and shares on destinationValidatorin store -
Update
Delegationin 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
Redelegationentry in store. - Insert a corresponding entry to
RedelegationQueue. The queue is for checking and completing matureRedelegationentries atEndBlocker.
- Delete/Update existing
Delegationentry on Validator in store. - Tokens are still in module accounts, transfer may happen between these module accounts (
BondedPoolmodule account orNotBondedPoolmodule acount). - Undelegation amount is removed from
Validator.Tokens. Shares is removed fromValidator.DelegatorShares(total shares) -
UnbondingDelegationentry 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
Delegationentry from store, identifier (DelegatorAddr,ValidatorOperator) - Get
Validatorin store. - Delete shares on
Delegationentry. - Delete/Update
Delegationentry in store. -
k.RemoveValidatorTokensAndShares(): remove tokens and shares onValidator, updateValidatorin store.
k.bondedTokensToNotBonded(): sent tokens from BondedPool module account to NotBondedPool module account.
- Create/Update
UnbindingDelegationentry in store. - Insert a corresponding entry to
UnbondingDelegationQueue. The queue is for checking and completing matureUnbindingDelegationentries atEndBlocker.
x/staking/abci.go EndBlocker()
x/staking/keeper/val_state_change.go BlockValidatorUpdates()
- Loop through
UBDQueue(kind of like an index for finding matureUnbindingDelegationentries), find and delete mature entry in it. - Mature
UnbondingDelegationentry will trigger transfer of tokens, fromNotBondedPoolmodule account toDelegatoraccount. - Then the
UnbondingDelegationentry is removed from store.
x/staking/keeper/delegation.go CompleteUnbonding()
k.bankKeeper.UndelegateCoinsFromModuleToAccount()
- Loop through
RedelegationQueue(kind of like an index for finding matureRedelegationentries), find and delete mature entry in it. - Deleting the
Redelegationentries 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
}