Skip to content

Commit

Permalink
Merge branch 'master' into fix/tx-fees-null-guard
Browse files Browse the repository at this point in the history
  • Loading branch information
clockworkgr authored Oct 11, 2021
2 parents e413364 + 3458f64 commit eb7d286
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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() {
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
// 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

0 comments on commit eb7d286

Please sign in to comment.