Skip to content

Commit

Permalink
refactor: migrate x/staking Params to use collections (#17778)
Browse files Browse the repository at this point in the history
  • Loading branch information
atheeshp authored Sep 18, 2023
1 parent a0b39a1 commit 2811270
Show file tree
Hide file tree
Showing 26 changed files with 70 additions and 107 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* (x/staking) [#17778](https://github.com/cosmos/cosmos-sdk/pull/17778) Use collections for `Params`
* remove from `Keeper`: `GetParams`, `SetParams`
* (types/simulation) [#17737](https://github.com/cosmos/cosmos-sdk/pull/17737) Remove unused parameter from `RandomFees`
* (x/staking) [#17486](https://github.com/cosmos/cosmos-sdk/pull/17486) Use collections for `RedelegationQueueKey`:
* remove from `types`: `GetRedelegationTimeKey`
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/auth/migrations/v2/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestMigrateVestingAccounts(t *testing.T) {

ctx = app.BaseApp.NewContext(false)
ctx = ctx.WithHeaderInfo(header.Info{Time: time.Now()})
err = stakingKeeper.SetParams(ctx, stakingtypes.DefaultParams())
err = stakingKeeper.Params.Set(ctx, stakingtypes.DefaultParams())
require.NoError(t, err)
lastAccNum := uint64(1000)
createBaseAccount := func(addr sdk.AccAddress) *authtypes.BaseAccount {
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/distribution/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestGRPCValidatorOutstandingRewards(t *testing.T) {
assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))))

// Set default staking params
assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams()))
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, stakingtypes.DefaultParams()))

qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestGRPCValidatorCommission(t *testing.T) {
assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))))

// Set default staking params
assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams()))
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, stakingtypes.DefaultParams()))

qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)
Expand Down Expand Up @@ -490,7 +490,7 @@ func TestGRPCDelegationRewards(t *testing.T) {
assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))))

// Set default staking params
assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams()))
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, stakingtypes.DefaultParams()))

qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func initFixture(tb testing.TB) *fixture {
)

stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
require.NoError(tb, stakingKeeper.Params.Set(newCtx, stakingtypes.DefaultParams()))

distrKeeper := distrkeeper.NewKeeper(
cdc, runtime.NewKVStoreService(keys[distrtypes.StoreKey]), accountKeeper, bankKeeper, stakingKeeper, distrtypes.ModuleName, authority.String(),
Expand Down Expand Up @@ -896,7 +897,7 @@ func TestMsgDepositValidatorRewardsPool(t *testing.T) {
require.NoError(t, f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))))

// Set default staking params
require.NoError(t, f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams()))
require.NoError(t, f.stakingKeeper.Params.Set(f.sdkCtx, stakingtypes.DefaultParams()))

addr := sdk.AccAddress("addr")
addr1 := sdk.AccAddress(PKS[0].Address())
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func initFixture(tb testing.TB) *fixture {
assert.NilError(tb, slashingKeeper.Params.Set(sdkCtx, testutil.TestParams()))

// set default staking params
assert.NilError(tb, stakingKeeper.SetParams(sdkCtx, stakingtypes.DefaultParams()))
assert.NilError(tb, stakingKeeper.Params.Set(sdkCtx, stakingtypes.DefaultParams()))

return &fixture{
app: integrationApp,
Expand All @@ -175,7 +175,7 @@ func TestHandleDoubleSign(t *testing.T) {
populateValidators(t, f)

power := int64(100)
stakingParams, err := f.stakingKeeper.GetParams(ctx)
stakingParams, err := f.stakingKeeper.Params.Get(ctx)
assert.NilError(t, err)
operatorAddr, valpubkey := valAddresses[0], pubkeys[0]
tstaking := stakingtestutil.NewHelper(t, ctx, f.stakingKeeper)
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestHandleDoubleSign_TooOld(t *testing.T) {
populateValidators(t, f)

power := int64(100)
stakingParams, err := f.stakingKeeper.GetParams(ctx)
stakingParams, err := f.stakingKeeper.Params.Get(ctx)
assert.NilError(t, err)
operatorAddr, valpubkey := valAddresses[0], pubkeys[0]

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/gov/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func initFixture(tb testing.TB) *fixture {
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))

// set default staking params
err := stakingKeeper.SetParams(newCtx, stakingtypes.DefaultParams())
err := stakingKeeper.Params.Set(newCtx, stakingtypes.DefaultParams())
assert.NilError(tb, err)
distrKeeper := distrkeeper.NewKeeper(
cdc, runtime.NewKVStoreService(keys[distrtypes.StoreKey]), accountKeeper, bankKeeper, stakingKeeper, distrtypes.ModuleName, authority.String(),
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func initFixture(tb testing.TB) *fixture {
slashingtypes.RegisterQueryServer(integrationApp.QueryHelper(), slashingkeeper.NewQuerier(slashingKeeper))

// set default staking params
err := stakingKeeper.SetParams(sdkCtx, stakingtypes.DefaultParams())
err := stakingKeeper.Params.Set(sdkCtx, stakingtypes.DefaultParams())
assert.NilError(tb, err)
// TestParams set the SignedBlocksWindow to 1000 and MaxMissedBlocksPerWindow to 500
err = slashingKeeper.Params.Set(sdkCtx, testutil.TestParams())
Expand Down Expand Up @@ -142,10 +142,10 @@ func TestUnJailNotBonded(t *testing.T) {
t.Parallel()
f := initFixture(t)

p, err := f.stakingKeeper.GetParams(f.ctx)
p, err := f.stakingKeeper.Params.Get(f.ctx)
assert.NilError(t, err)
p.MaxValidators = 5
assert.NilError(t, f.stakingKeeper.SetParams(f.ctx, p))
assert.NilError(t, f.stakingKeeper.Params.Set(f.ctx, p))
pks := simtestutil.CreateTestPubKeys(6)
tstaking := stakingtestutil.NewHelper(t, f.ctx, f.stakingKeeper)

Expand Down Expand Up @@ -346,10 +346,10 @@ func TestValidatorDippingInAndOut(t *testing.T) {
t.Parallel()
f := initFixture(t)

params, err := f.stakingKeeper.GetParams(f.ctx)
params, err := f.stakingKeeper.Params.Get(f.ctx)
require.NoError(t, err)
params.MaxValidators = 1
err = f.stakingKeeper.SetParams(f.ctx, params)
err = f.stakingKeeper.Params.Set(f.ctx, params)
assert.NilError(t, err)
power := int64(100)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/staking/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func initFixture(tb testing.TB) *fixture {
types.RegisterQueryServer(integrationApp.QueryHelper(), stakingkeeper.NewQuerier(stakingKeeper))

// set default staking params
assert.NilError(tb, stakingKeeper.SetParams(sdkCtx, types.DefaultParams()))
assert.NilError(tb, stakingKeeper.Params.Set(sdkCtx, types.DefaultParams()))

f := fixture{
app: integrationApp,
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/staking/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
stakingtypes.RegisterQueryServer(integrationApp.QueryHelper(), stakingkeeper.NewQuerier(stakingKeeper))

// set default staking params
assert.NilError(t, stakingKeeper.SetParams(ctx, stakingtypes.DefaultParams()))
assert.NilError(t, stakingKeeper.Params.Set(ctx, stakingtypes.DefaultParams()))

// set pools
startTokens := stakingKeeper.TokensFromConsensusPower(ctx, 10)
Expand Down Expand Up @@ -840,7 +840,7 @@ func TestGRPCParams(t *testing.T) {
MinCommissionRate: math.LegacyNewDecWithPrec(rapid.Int64Range(0, 100).Draw(rt, "commission"), 2),
}

err := f.stakingKeeper.SetParams(f.ctx, params)
err := f.stakingKeeper.Params.Set(f.ctx, params)
assert.NilError(t, err)

testdata.DeterministicIterations(f.ctx, t, &stakingtypes.QueryParamsRequest{}, f.queryClient.Params, 0, true)
Expand All @@ -855,7 +855,7 @@ func TestGRPCParams(t *testing.T) {
MinCommissionRate: math.LegacyNewDecWithPrec(5, 2),
}

err := f.stakingKeeper.SetParams(f.ctx, params)
err := f.stakingKeeper.Params.Set(f.ctx, params)
assert.NilError(t, err)

testdata.DeterministicIterations(f.ctx, t, &stakingtypes.QueryParamsRequest{}, f.queryClient.Params, 1114, false)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/staking/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestInitGenesis(t *testing.T) {
}
assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, bondedVal))

params, err := f.stakingKeeper.GetParams(f.sdkCtx)
params, err := f.stakingKeeper.Params.Get(f.sdkCtx)
assert.NilError(t, err)

validators, err := f.stakingKeeper.GetAllValidators(f.sdkCtx)
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) {
genesisValidators, err := f.stakingKeeper.GetAllValidators(f.sdkCtx)
assert.NilError(t, err)

params, err := f.stakingKeeper.GetParams(f.sdkCtx)
params, err := f.stakingKeeper.Params.Get(f.sdkCtx)
assert.NilError(t, err)
delegations := []types.Delegation{}
validators := make([]types.Validator, size)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/staking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestGRPCQueryDelegatorValidators(t *testing.T) {
qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)

params, err := f.stakingKeeper.GetParams(ctx)
params, err := f.stakingKeeper.Params.Get(ctx)
assert.NilError(t, err)
delValidators, err := f.stakingKeeper.GetDelegatorValidators(ctx, addrs[0], params.MaxValidators)
assert.NilError(t, err)
Expand Down Expand Up @@ -717,7 +717,7 @@ func TestGRPCQueryPoolParameters(t *testing.T) {
// Query Params
resp, err := queryClient.Params(gocontext.Background(), &types.QueryParamsRequest{})
assert.NilError(t, err)
params, err := f.stakingKeeper.GetParams(ctx)
params, err := f.stakingKeeper.Params.Get(ctx)
assert.NilError(t, err)
assert.DeepEqual(t, params, resp.Params)
}
Expand Down
30 changes: 15 additions & 15 deletions tests/integration/staking/keeper/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func TestUpdateBondedValidatorsDecreaseCliff(t *testing.T) {
notBondedPool := f.stakingKeeper.GetNotBondedPool(f.sdkCtx)

// create keeper parameters
params, err := f.stakingKeeper.GetParams(f.sdkCtx)
params, err := f.stakingKeeper.Params.Get(f.sdkCtx)
assert.NilError(t, err)
params.MaxValidators = uint32(maxVals)
assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, params))
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, params))

bondDenom, err := f.stakingKeeper.BondDenom(f.sdkCtx)
assert.NilError(t, err)
Expand Down Expand Up @@ -262,10 +262,10 @@ func TestGetValidatorSortingMixed(t *testing.T) {
f.accountKeeper.SetModuleAccount(f.sdkCtx, bondedPool)

// now 2 max resValidators
params, err := f.stakingKeeper.GetParams(f.sdkCtx)
params, err := f.stakingKeeper.Params.Get(f.sdkCtx)
assert.NilError(t, err)
params.MaxValidators = 2
assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, params))
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, params))

// initialize some validators into the state
amts := []math.Int{
Expand Down Expand Up @@ -317,11 +317,11 @@ func TestGetValidatorsEdgeCases(t *testing.T) {
f, addrs, _ := bootstrapValidatorTest(t, 1000, 20)

// set max validators to 2
params, err := f.stakingKeeper.GetParams(f.sdkCtx)
params, err := f.stakingKeeper.Params.Get(f.sdkCtx)
assert.NilError(t, err)
nMax := uint32(2)
params.MaxValidators = nMax
assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, params))
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, params))
// initialize some validators into the state
powers := []int64{0, 100, 400, 400}
var validators [4]types.Validator
Expand Down Expand Up @@ -436,10 +436,10 @@ func TestValidatorBondHeight(t *testing.T) {
f, addrs, _ := bootstrapValidatorTest(t, 1000, 20)

// now 2 max resValidators
params, err := f.stakingKeeper.GetParams(f.sdkCtx)
params, err := f.stakingKeeper.Params.Get(f.sdkCtx)
assert.NilError(t, err)
params.MaxValidators = 2
assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, params))
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, params))
// initialize some validators into the state
var validators [3]types.Validator
validators[0] = testutil.NewValidator(t, sdk.ValAddress(PKs[0].Address().Bytes()), PKs[0])
Expand Down Expand Up @@ -483,11 +483,11 @@ func TestValidatorBondHeight(t *testing.T) {

func TestFullValidatorSetPowerChange(t *testing.T) {
f, addrs, _ := bootstrapValidatorTest(t, 1000, 20)
params, err := f.stakingKeeper.GetParams(f.sdkCtx)
params, err := f.stakingKeeper.Params.Get(f.sdkCtx)
assert.NilError(t, err)
max := 2
params.MaxValidators = uint32(2)
assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, params))
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, params))

// initialize some validators into the state
powers := []int64{0, 100, 400, 400, 200}
Expand Down Expand Up @@ -676,7 +676,7 @@ func TestApplyAndReturnValidatorSetUpdatesWithCliffValidator(t *testing.T) {
f, addrs, _ := bootstrapValidatorTest(t, 1000, 20)
params := types.DefaultParams()
params.MaxValidators = 2
err := f.stakingKeeper.SetParams(f.sdkCtx, params)
err := f.stakingKeeper.Params.Set(f.sdkCtx, params)
assert.NilError(t, err)
powers := []int64{10, 20, 5}
var validators [5]types.Validator
Expand Down Expand Up @@ -712,11 +712,11 @@ func TestApplyAndReturnValidatorSetUpdatesWithCliffValidator(t *testing.T) {

func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) {
f, _, _ := bootstrapValidatorTest(t, 1000, 20)
params, err := f.stakingKeeper.GetParams(f.sdkCtx)
params, err := f.stakingKeeper.Params.Get(f.sdkCtx)
assert.NilError(t, err)
params.MaxValidators = uint32(3)

assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, params))
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, params))

powers := []int64{100, 100}
var validators [2]types.Validator
Expand Down Expand Up @@ -798,11 +798,11 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) {

func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) {
f, _, _ := bootstrapValidatorTest(t, 1000, 20)
params, err := f.stakingKeeper.GetParams(f.sdkCtx)
params, err := f.stakingKeeper.Params.Get(f.sdkCtx)
assert.NilError(t, err)
params.MaxValidators = uint32(2)

assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, params))
assert.NilError(t, f.stakingKeeper.Params.Set(f.sdkCtx, params))

powers := []int64{100, 200, 300}
var validators [3]types.Validator
Expand Down
15 changes: 0 additions & 15 deletions x/evidence/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion x/evidence/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type (
StakingKeeper interface {
ConsensusAddressCodec() address.Codec
ValidatorByConsAddr(context.Context, sdk.ConsAddress) (stakingtypes.ValidatorI, error)
GetParams(ctx context.Context) (params stakingtypes.Params, err error)
}

// SlashingKeeper defines the slashing module interface contract needed by the
Expand Down
8 changes: 4 additions & 4 deletions x/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondingValidator() {
validator, err = keeper.GetValidator(ctx, addrVals[0])
require.NoError(err)
require.Equal(blockHeight, validator.UnbondingHeight)
params, err := keeper.GetParams(ctx)
params, err := keeper.Params.Get(ctx)
require.NoError(err)
require.True(blockTime.Add(params.UnbondingTime).Equal(validator.UnbondingTime))

Expand Down Expand Up @@ -571,7 +571,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondedValidator() {
validator, err = keeper.GetValidator(ctx, addrVals[0])
require.NoError(err)
require.Equal(ctx.BlockHeight(), validator.UnbondingHeight)
params, err := keeper.GetParams(ctx)
params, err := keeper.Params.Get(ctx)
require.NoError(err)
require.True(ctx.HeaderInfo().Time.Add(params.UnbondingTime).Equal(validator.UnbondingTime))

Expand Down Expand Up @@ -958,7 +958,7 @@ func (s *KeeperTestSuite) TestRedelegateFromUnbondingValidator() {
validator, err = keeper.GetValidator(ctx, addrVals[0])
require.NoError(err)
require.Equal(blockHeight, validator.UnbondingHeight)
params, err := keeper.GetParams(ctx)
params, err := keeper.Params.Get(ctx)
require.NoError(err)
require.True(blockTime.Add(params.UnbondingTime).Equal(validator.UnbondingTime))

Expand Down Expand Up @@ -1036,7 +1036,7 @@ func (s *KeeperTestSuite) TestRedelegateFromUnbondedValidator() {
validator, err = keeper.GetValidator(ctx, addrVals[0])
require.NoError(err)
require.Equal(ctx.HeaderInfo().Height, validator.UnbondingHeight)
params, err := keeper.GetParams(ctx)
params, err := keeper.Params.Get(ctx)
require.NoError(err)
require.True(ctx.HeaderInfo().Time.Add(params.UnbondingTime).Equal(validator.UnbondingTime))

Expand Down
4 changes: 2 additions & 2 deletions x/staking/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) (res
sdkCtx = sdkCtx.WithBlockHeight(1 - sdk.ValidatorUpdateDelay)
ctx = sdkCtx

if err := k.SetParams(ctx, data.Params); err != nil {
if err := k.Params.Set(ctx, data.Params); err != nil {
panic(err)
}

Expand Down Expand Up @@ -251,7 +251,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
panic(err)
}

params, err := k.GetParams(ctx)
params, err := k.Params.Get(ctx)
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit 2811270

Please sign in to comment.