From 630b1ac4d454978f26538f5843c17fec40e79235 Mon Sep 17 00:00:00 2001 From: JoowonYun Date: Tue, 2 Apr 2024 11:41:57 +0900 Subject: [PATCH] fix: follow up staking module --- go.mod | 2 +- x/staking/keeper/pool.go | 5 ++- x/staking/keeper/val_state_change.go | 59 ++++------------------------ x/staking/module.go | 3 -- 4 files changed, 12 insertions(+), 57 deletions(-) diff --git a/go.mod b/go.mod index 30d77944..4e822b4b 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/x/staking/keeper/pool.go b/x/staking/keeper/pool.go index 2e25210c..23959738 100644 --- a/x/staking/keeper/pool.go +++ b/x/staking/keeper/pool.go @@ -1,12 +1,13 @@ 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) @@ -14,7 +15,7 @@ func (k Keeper) notBondedTokensToBonded(ctx sdk.Context, tokens sdk.Int) { } // 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) diff --git a/x/staking/keeper/val_state_change.go b/x/staking/keeper/val_state_change.go index 64f7b3f2..13691081 100644 --- a/x/staking/keeper/val_state_change.go +++ b/x/staking/keeper/val_state_change.go @@ -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" ) @@ -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. @@ -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 } @@ -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) { @@ -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 diff --git a/x/staking/module.go b/x/staking/module.go index da2134ba..5bdfe093 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -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