Skip to content

Staking Notes

Yang SONG edited this page Dec 10, 2021 · 4 revisions

Staking Notes

MsgCreateValidator

  • 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 or NotBondedPool module acount).
  • Delegation amount is added to Validator.Tokens.

x/staking/keeper/msg_server.go CreateValidator()

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/keeper/msg_server.go#L30

k.Keeper.Delegate()

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/keeper/msg_server.go#L111

x/staking/keeper/delegation.go Delegate()

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/keeper/delegation.go#L551

  1. Find/Create Delegation from store, identifier (DelegatorAddr, ValidatorOperator)

  2. subtractAccount == true

  • k.bankKeeper.DelegateCoinsFromAccountToModule(): Remove tokens from Delegator, add tokens to module account

    • Validator.IsBonded: Tokens from Delegator account sent to BondedPool module account
    • Validator.IsUnbonded OR Validator.IsUnbonding (Validator Not Bonded): Tokens from Delegator account sent to NotBondedPool module account
  1. k.AddValidatorTokensAndShares(): add tokens and shares on Validator in store

  2. Update Delegation in store

MsgDelegate

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 or NotBondedPool module acount).
  • Delegation amount is added to Validator.Tokens (AKA ValidatorOperator account).

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/keeper/msg_server.go#L198

MsgBeginRedelegate

  • Delete/Update existing Delegation entry on SrcValidator in store.
  • Create/Update Delegation entry on DstValidator in store.
  • Tokens are still in module accounts, transfer may happen between these module accounts (BondedPool module account or NotBondedPool module acount).
  • Redelegation amount is removed from source Validator.Tokens.
  • Redelegation amount is added to destination Validator.Tokens.
  • Redelegation entry is inserted to RedelegationQueue.

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/keeper/msg_server.go#L257

x/staking/keeper/delegation.go BeginRedelegation()

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/keeper/delegation.go#L822

1. k.Unbond()

  • 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 SrcValidator, update SrcValidator in store.

2. k.Delegate()

  1. Find/Create Delegation from store, identifier (DelegatorAddr, DstValidatorOperator)

  2. subtractAccount == false:

  • SrcValidator Not Bonded -> DstValidator Bonded: sent tokens from NotBondedPool to BondedPool module account
  • SrcValidator Bonded -> DstValidator Not Bonded: sent tokens from BondedPool to NotBondedPool module account
  • Other cases, do nothing
  • Undefined case, panic
  1. k.AddValidatorTokensAndShares(): add tokens and shares on DstValidator in store

  2. Update Delegation in store

3. k.getBeginInfo()

Calculate completion time for RedelegationQueueEntry

  1. Validator.IsBonded: Wait for A full unbonding time (longest wait)
  2. Validator.IsUnbonded: Compelte right away
  3. Validator.IsUnbonding: Wait until validator is unbonding is done

4. (Optional) k.InsertRedelegationQueue()

  • Add Redelegation entry to RedelegationQueue.

MsgUndelegate

  • 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 or NotBondedPool module acount).
  • Undelegation amount is removed from Validator.Tokens.
  • UnbondingDelegation entry is inserted to UnbondingDelegationQueue.

The tokens are removed from Validator (instantly). But only when it is complete (mature), the tokens will be sent back to delegator.

x/staking/keeper/msg_server.go Undelegate()

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/keeper/msg_server.go#L325

  • k.Keeper.Undelegate(): x/staking/keeper/delegation.go Undelegate()

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/keeper/delegation.go#L743

1. k.Unbond()

  • Get Delegation entry from store, identifier (DelegatorAddr, ValidatorOperator)
  • Get Validator in store.
  • Delete shares on Delegation entry.
  • Delete/Update Delegation on Validator in store.
  • k.RemoveValidatorTokensAndShares(): remove tokens and shares on Validator, update Validator in store.

2. (Optional) Validator.IsBonded

k.bondedTokensToNotBonded(): sent tokens from BondedPool module account to NotBondedPool module account.

3. Calculate completion time

4. k.InsertUBDQueue()

Add UnbondingDelegation entry to UnbondingDelegationQueue.

EndBlocker

x/staking/abci.go EndBlocker()

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/abci.go#L23

x/staking/keeper/val_state_change.go BlockValidatorUpdates()

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/keeper/val_state_change.go#L17

Complete Unbonding

Mature UnBonding Entry will trigger transfer of tokens, from NotBondedPool module account to Delegator account.

x/staking/keeper/delegation.go CompleteUnbonding()

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/keeper/delegation.go#L775

  • k.bankKeeper.UndelegateCoinsFromModuleToAccount()

Complete Redelegation

It is just deleting those entries, not moving funds in any account.

x/staking/keeper/delegation.go CompleteRedelegation()

https://github.com/cosmos/cosmos-sdk/blob/fec35777f9646c162b79e98409179e374374e6e0/x/staking/keeper/delegation.go#L881

Clone this wiki locally