Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: x/staking - remove delegation with amount is zero #10254

Merged
merged 19 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### State Machine Breaking

* (x/staking) [#10254](https://github.com/cosmos/cosmos-sdk/pull/10254) Instead of using the shares to determine if a delegation should be removed, use the truncated (token) amount.
* (store) [#10247](https://github.com/cosmos/cosmos-sdk/pull/10247) Charge gas for the key length in gas meter.
* (store) [#10218](https://github.com/cosmos/cosmos-sdk/pull/10218) Charge gas even when there are no entries while seeking.
* (x/auth)[\#9596](https://github.com/cosmos/cosmos-sdk/pull/9596) Enable creating periodic vesting accounts with a transactions instead of requiring them to be created in genesis.
Expand Down
13 changes: 11 additions & 2 deletions x/staking/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func InitGenesis(
}

keeper.SetDelegation(ctx, delegation)

// Call the after-modification hook if not exported
if !data.Exported {
if err := keeper.AfterDelegationModified(ctx, delegatorAddress, delegation.GetValidatorAddr()); err != nil {
Expand Down Expand Up @@ -110,15 +111,18 @@ func InitGenesis(
if bondedPool == nil {
panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName))
}
// TODO remove with genesis 2-phases refactor https://github.com/cosmos/cosmos-sdk/issues/2862

// TODO: remove with genesis 2-phases refactor https://github.com/cosmos/cosmos-sdk/issues/2862
bondedBalance := bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress())
if bondedBalance.IsZero() {
accountKeeper.SetModuleAccount(ctx, bondedPool)
}

// if balance is different from bonded coins panic because genesis is most likely malformed
if !bondedBalance.IsEqual(bondedCoins) {
panic(fmt.Sprintf("bonded pool balance is different from bonded coins: %s <-> %s", bondedBalance, bondedCoins))
}

notBondedPool := keeper.GetNotBondedPool(ctx)
if notBondedPool == nil {
panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
Expand All @@ -128,17 +132,21 @@ func InitGenesis(
if notBondedBalance.IsZero() {
accountKeeper.SetModuleAccount(ctx, notBondedPool)
}
// if balance is different from non bonded coins panic because genesis is most likely malformed

// If balance is different from non bonded coins panic because genesis is most
// likely malformed.
if !notBondedBalance.IsEqual(notBondedCoins) {
panic(fmt.Sprintf("not bonded pool balance is different from not bonded coins: %s <-> %s", notBondedBalance, notBondedCoins))
}

// don't need to run Tendermint updates if we exported
if data.Exported {
for _, lv := range data.LastValidatorPowers {
valAddr, err := sdk.ValAddressFromBech32(lv.Address)
if err != nil {
panic(err)
}

keeper.SetLastValidatorPower(ctx, valAddr, lv.Power)
validator, found := keeper.GetValidator(ctx, valAddr)

Expand All @@ -152,6 +160,7 @@ func InitGenesis(
}
} else {
var err error

res, err = keeper.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
panic(err)
Expand Down
8 changes: 5 additions & 3 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,11 @@ func (k Keeper) Unbond(
validator = k.mustGetValidator(ctx, validator.GetOperator())
}

// remove the delegation
if delegation.Shares.IsZero() {
// Remove the delegation if the resulting shares yield a truncated zero amount
// of tokens in the delegation. Note, in this this case, the shares themselves
// may not be zero, but rather a small fractional amount. Otherwise, we update
// the delegation object.
if validator.TokensFromShares(delegation.Shares).TruncateInt().IsZero() || delegation.Shares.IsZero() {
Comment on lines +683 to +687
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexanderbez do you think this could break delegator shares invariant, causing #10750 ?

@aleem1314 did some debugging on this, here's some logs:

++++++++++++++++++++++++++++++++++++
TOKENS-FROM-SHARES =  0.937500000000000000  validator.TokensFromShares(delegation.Shares).TruncateInt() =  0
DELEGATIONS SHARES =  0.937500000000000000
VALIDATOR =  cosmosvaloper14rmvn8ymw7mdjwyjhmkzestgkcdskvkx6ke9e5
DELEGATOR =  cosmos15rxvlc2xaf6sjq5fjq45gfapdjsjr97jy6wmjc
BLOCKHEIGHT =  32
BLOCKTIME =  2103-10-01 17:18:06 +0000 UTC
++++++++++++++++++++++++++++++++++++

just reverting this line and simulations pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm could be! But what invariant is broken exactly? The delegation should be deleted here. Is there some invariant that expects this not to be deleted?

Copy link
Contributor

@amaury1093 amaury1093 Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DelegatorSharesInvariant one. Maybe it should be updated to take TruncateInt into account? The error message is:

broken delegator shares invariance:
        validator.DelegatorShares: 34148342106.937500000000000000
        sum of Delegator.Shares: 34148342106.000000000000000000

which soulds like some truncating error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's keep the discussion in the new issue created 👍

err = k.RemoveDelegation(ctx, delegation)
} else {
k.SetDelegation(ctx, delegation)
Expand All @@ -696,7 +699,6 @@ func (k Keeper) Unbond(
// remove the shares and coins from the validator
// NOTE that the amount is later (in keeper.Delegation) moved between staking module pools
validator, amount = k.RemoveValidatorTokensAndShares(ctx, validator, shares)

if validator.DelegatorShares.IsZero() && validator.IsUnbonded() {
// if not unbonded, we must instead remove validator in EndBlocker once it finishes its unbonding period
k.RemoveValidator(ctx, validator.GetOperator())
Expand Down
2 changes: 0 additions & 2 deletions x/staking/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,11 @@ func PositiveDelegationInvariant(k Keeper) sdk.Invariant {
for _, delegation := range delegations {
if delegation.Shares.IsNegative() {
count++

msg += fmt.Sprintf("\tdelegation with negative shares: %+v\n", delegation)
}

if delegation.Shares.IsZero() {
count++

msg += fmt.Sprintf("\tdelegation with zero shares: %+v\n", delegation)
}
}
Expand Down
10 changes: 9 additions & 1 deletion x/staking/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
v043 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v043"
v045 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v045"
)

// Migrator is a struct for handling in-place store migrations.
Expand All @@ -12,10 +13,17 @@ type Migrator struct {

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
return Migrator{
keeper: keeper,
}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v043.MigrateStore(ctx, m.keeper.storeKey)
}

// Migrate2to3 migrates x/staking state from consensus version 2 to 3.
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v045.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
}
4 changes: 4 additions & 0 deletions x/staking/migrations/v040/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ func (v Validator) String() string {
return string(out)
}

func (v Validator) TokensFromShares(shares sdk.Dec) sdk.Dec {
return (shares.MulInt(v.Tokens)).Quo(v.DelegatorShares)
}

// Validators is a collection of Validator
type Validators []Validator

Expand Down
11 changes: 11 additions & 0 deletions x/staking/migrations/v045/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package v045

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
v040staking "github.com/cosmos/cosmos-sdk/x/staking/migrations/v040"
)

func getValidatorKey(operatorAddr sdk.ValAddress) []byte {
return append(v040staking.ValidatorsKey, address.MustLengthPrefix(operatorAddr)...)
}
57 changes: 57 additions & 0 deletions x/staking/migrations/v045/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package v045

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
v040staking "github.com/cosmos/cosmos-sdk/x/staking/migrations/v040"
)

// MigrateStore performs in-place store migrations from v0.43/v0.44 to v0.45.
// The migration includes:
//
// - Removing delegations that have a zero share or token amount.
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)

return purgeDelegations(store, cdc)
}

func purgeDelegations(store sdk.KVStore, cdc codec.BinaryCodec) error {
prefixDelStore := prefix.NewStore(store, v040staking.DelegationKey)

delStoreIter := prefixDelStore.Iterator(nil, nil)
defer delStoreIter.Close()

valCache := make(map[string]v040staking.Validator)

for ; delStoreIter.Valid(); delStoreIter.Next() {
var delegation v040staking.Delegation
if err := cdc.Unmarshal(delStoreIter.Value(), &delegation); err != nil {
return err
}

validator, ok := valCache[delegation.ValidatorAddress]
if !ok {
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
return err
}

if err := cdc.Unmarshal(store.Get(getValidatorKey(valAddr)), &validator); err != nil {
return err
}

valCache[delegation.ValidatorAddress] = validator
}

// TODO: On-chain, we call BeforeDelegationRemoved prior to removing the
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
// object from state. Do we need to do the same here?
if validator.TokensFromShares(delegation.Shares).TruncateInt().IsZero() || delegation.Shares.IsZero() {
prefixDelStore.Delete(delStoreIter.Key())
}
}

return nil
}
7 changes: 6 additions & 1 deletion x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

const (
consensusVersion uint64 = 3
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
Expand Down Expand Up @@ -140,6 +144,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {

m := keeper.NewMigrator(am.keeper)
cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2)
cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3)
}

// InitGenesis performs genesis initialization for the staking module. It returns
Expand All @@ -160,7 +165,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 2 }
func (AppModule) ConsensusVersion() uint64 { return consensusVersion }

// BeginBlock returns the begin blocker for the staking module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
Expand Down