Skip to content

Commit

Permalink
fix: follow up staking module
Browse files Browse the repository at this point in the history
  • Loading branch information
JoowonYun committed Apr 3, 2024
1 parent 01730b7 commit 630b1ac
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 57 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
cosmossdk.io/api v0.3.1
cosmossdk.io/errors v1.0.1
cosmossdk.io/math v1.3.0
cosmossdk.io/simapp v0.0.0-20230608160436-666c345ad23d
github.com/CosmWasm/wasmd v0.45.0
github.com/cometbft/cometbft v0.37.5
Expand Down Expand Up @@ -41,7 +42,6 @@ require (
cosmossdk.io/core v0.6.1 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/log v1.3.1 // indirect
cosmossdk.io/math v1.3.0 // indirect
cosmossdk.io/tools/rosetta v0.2.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
Expand Down
5 changes: 3 additions & 2 deletions x/staking/keeper/pool.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package keeper

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

// notBondedTokensToBonded transfers coins from the not bonded to the bonded pool within staking
func (k Keeper) notBondedTokensToBonded(ctx sdk.Context, tokens sdk.Int) {
func (k Keeper) notBondedTokensToBonded(ctx sdk.Context, tokens math.Int) {
coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), tokens))
if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.NotBondedPoolName, types.BondedPoolName, coins); err != nil {
panic(err)
}
}

// bondedTokensToNotBonded transfers coins from the bonded to the not bonded pool within staking
func (k Keeper) bondedTokensToNotBonded(ctx sdk.Context, tokens sdk.Int) {
func (k Keeper) bondedTokensToNotBonded(ctx sdk.Context, tokens math.Int) {
coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), tokens))
if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.BondedPoolName, types.NotBondedPoolName, coins); err != nil {
panic(err)
Expand Down
59 changes: 8 additions & 51 deletions x/staking/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
gogotypes "github.com/cosmos/gogoproto/types"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand Down Expand Up @@ -109,8 +111,8 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab
params := k.GetParams(ctx)
maxValidators := params.MaxValidators
powerReduction := k.PowerReduction(ctx)
totalPower := sdk.ZeroInt()
amtFromBondedToNotBonded, amtFromNotBondedToBonded := sdk.ZeroInt(), sdk.ZeroInt()
totalPower := math.ZeroInt()
amtFromBondedToNotBonded, amtFromNotBondedToBonded := math.ZeroInt(), math.ZeroInt()

// Retrieve the last validator set.
// The persistent set is updated later in this function.
Expand Down Expand Up @@ -239,6 +241,9 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab
k.SetLastTotalPower(ctx, totalPower)
}

// set the list of validator updates
k.SetValidatorUpdates(ctx, updates)

return updates, err
}

Expand All @@ -249,7 +254,7 @@ func (k Keeper) bondedToUnbonding(ctx sdk.Context, validator types.Validator) (t
panic(fmt.Sprintf("bad state transition bondedToUnbonding, validator: %v\n", validator))
}

return k.beginUnbondingValidator(ctx, validator)
return k.BeginUnbondingValidator(ctx, validator)
}

func (k Keeper) unbondingToBonded(ctx sdk.Context, validator types.Validator) (types.Validator, error) {
Expand Down Expand Up @@ -295,54 +300,6 @@ func (k Keeper) bondValidator(ctx sdk.Context, validator types.Validator) (types
return validator, err
}

// perform all the store operations for when a validator begins unbonding
func (k Keeper) beginUnbondingValidator(ctx sdk.Context, validator types.Validator) (types.Validator, error) {
params := k.GetParams(ctx)

// delete the validator by power index, as the key will change
k.DeleteValidatorByPowerIndex(ctx, validator)

// sanity check
if validator.Status != types.Bonded {
panic(fmt.Sprintf("should not already be unbonded or unbonding, validator: %v\n", validator))
}

id := k.IncrementUnbondingID(ctx)

validator = validator.UpdateStatus(types.Unbonding)

// set the unbonding completion time and completion height appropriately
validator.UnbondingTime = ctx.BlockHeader().Time.Add(params.UnbondingTime)
validator.UnbondingHeight = ctx.BlockHeader().Height

validator.UnbondingIds = append(validator.UnbondingIds, id)

// save the now unbonded validator record and power index
k.SetValidator(ctx, validator)
k.SetValidatorByPowerIndex(ctx, validator)

// Adds to unbonding validator queue
k.InsertUnbondingValidatorQueue(ctx, validator)

// trigger hook
consAddr, err := validator.GetConsAddr()
if err != nil {
return validator, err
}

if err := k.Hooks().AfterValidatorBeginUnbonding(ctx, consAddr, validator.GetOperator()); err != nil {
return validator, err
}

k.SetValidatorByUnbondingID(ctx, validator, id)

if err := k.Hooks().AfterUnbondingInitiated(ctx, id); err != nil {
return validator, err
}

return validator, nil
}

// map of operator bech32-addresses to serialized power
// We use bech32 strings here, because we can't have slices as keys: map[[]byte][]byte
type validatorsByAddr map[string][]byte
Expand Down
3 changes: 0 additions & 3 deletions x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ type AppModule struct {
staking.AppModule

keeper *keeper.Keeper

// legacySubspace is used solely for migration of x/params managed parameters
legacySubspace exported.Subspace
}

// NewAppModule creates a new AppModule object
Expand Down

0 comments on commit 630b1ac

Please sign in to comment.