Skip to content

Commit

Permalink
refactor: revert 10254 (cosmos#10777)
Browse files Browse the repository at this point in the history
## Description

Revert cosmos#10254

Closes: cosmos#10750

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
alexanderbez committed Dec 15, 2021
1 parent 6f58963 commit 45cad1d
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 61 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ Ref: https://keepachangelog.com/en/1.0.0/
### State Machine Breaking

* [\#10536](https://github.com/cosmos/cosmos-sdk/pull/10536]) Enable `SetSequence` for `ModuleAccount`.
* (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
6 changes: 1 addition & 5 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,7 @@ func (k Keeper) Unbond(
validator = k.mustGetValidator(ctx, validator.GetOperator())
}

// 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() {
if delegation.Shares.IsZero() {
err = k.RemoveDelegation(ctx, delegation)
} else {
k.SetDelegation(ctx, delegation)
Expand Down
11 changes: 0 additions & 11 deletions x/staking/migrations/v045/keys.go

This file was deleted.

45 changes: 1 addition & 44 deletions x/staking/migrations/v045/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,23 @@ 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"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
v040staking "github.com/cosmos/cosmos-sdk/x/staking/migrations/v040"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

// 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.
// - Setting the MinCommissionRate param in the paramstore
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, paramstore paramtypes.Subspace) error {
store := ctx.KVStore(storeKey)

migrateParamsStore(ctx, paramstore)

return purgeDelegations(store, cdc)
return nil
}

func migrateParamsStore(ctx sdk.Context, paramstore paramtypes.Subspace) {
paramstore.WithKeyTable(types.ParamKeyTable())
paramstore.Set(ctx, types.KeyMinCommissionRate, types.DefaultMinCommissionRate)
}

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
// 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
}

0 comments on commit 45cad1d

Please sign in to comment.