From 853f7788bcaf71b84240d9304e13c103fedd83d1 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Tue, 25 Jul 2023 13:20:16 +0530 Subject: [PATCH 01/18] migrate Validators to use collections --- simapp/export.go | 4 +- .../auth/migrations/v2/store_test.go | 4 +- .../distribution/keeper/grpc_query_test.go | 2 +- .../distribution/keeper/msg_server_test.go | 2 +- .../evidence/keeper/infraction_test.go | 2 +- tests/integration/gov/keeper/common_test.go | 6 +- tests/integration/gov/keeper/tally_test.go | 18 ++--- .../integration/staking/keeper/common_test.go | 4 +- .../staking/keeper/determinstic_test.go | 2 +- .../staking/keeper/genesis_test.go | 6 +- .../staking/keeper/msg_server_test.go | 2 +- .../integration/staking/keeper/slash_test.go | 10 +-- .../staking/keeper/unbonding_test.go | 16 ++-- .../staking/keeper/validator_bench_test.go | 8 +- .../staking/keeper/validator_test.go | 80 +++++++++---------- .../staking/simulation/operations_test.go | 2 +- x/distribution/simulation/operations_test.go | 2 +- x/slashing/app_test.go | 2 +- x/slashing/simulation/operations_test.go | 2 +- x/staking/app_test.go | 4 +- x/staking/keeper/alias_functions.go | 4 +- x/staking/keeper/delegation.go | 14 ++-- x/staking/keeper/delegation_test.go | 27 ++++--- x/staking/keeper/genesis.go | 4 +- x/staking/keeper/grpc_query.go | 8 +- x/staking/keeper/grpc_query_test.go | 2 +- x/staking/keeper/historical_info_test.go | 4 +- x/staking/keeper/invariants.go | 2 +- x/staking/keeper/keeper.go | 2 + x/staking/keeper/msg_server.go | 12 +-- x/staking/keeper/msg_server_test.go | 10 +-- x/staking/keeper/query_utils.go | 4 +- x/staking/keeper/slash.go | 2 +- x/staking/keeper/slash_test.go | 10 +-- x/staking/keeper/test_common.go | 4 +- x/staking/keeper/unbonding.go | 6 +- x/staking/keeper/unbonding_test.go | 4 +- x/staking/keeper/val_state_change.go | 10 +-- x/staking/keeper/validator.go | 40 +++------- x/staking/keeper/validator_test.go | 62 +++++++------- x/staking/migrations/v2/store_test.go | 3 +- x/staking/simulation/operations.go | 2 +- x/staking/testutil/helpers.go | 2 +- x/staking/types/keys.go | 6 +- 44 files changed, 203 insertions(+), 219 deletions(-) diff --git a/simapp/export.go b/simapp/export.go index 6f611a130e7a..b9fb8707dd02 100644 --- a/simapp/export.go +++ b/simapp/export.go @@ -203,7 +203,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [] for ; iter.Valid(); iter.Next() { addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key())) - validator, err := app.StakingKeeper.GetValidator(ctx, addr) + validator, err := app.StakingKeeper.Validators.Get(ctx, addr) if err != nil { panic("expected validator, not found") } @@ -213,7 +213,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [] validator.Jailed = true } - if err = app.StakingKeeper.SetValidator(ctx, validator); err != nil { + if err = app.StakingKeeper.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { panic(err) } counter++ diff --git a/tests/integration/auth/migrations/v2/store_test.go b/tests/integration/auth/migrations/v2/store_test.go index ca25f97ecd7b..c649aab1a807 100644 --- a/tests/integration/auth/migrations/v2/store_test.go +++ b/tests/integration/auth/migrations/v2/store_test.go @@ -669,7 +669,7 @@ func TestMigrateVestingAccounts(t *testing.T) { delegatorAddr := addrs[0] _, valAddr := createValidator(t, ctx, bankKeeper, stakingKeeper, tc.tokenAmount*2) - validator, err := stakingKeeper.GetValidator(ctx, valAddr) + validator, err := stakingKeeper.Validators.Get(ctx, valAddr) require.NoError(t, err) tc.prepareFunc(ctx, validator, delegatorAddr) @@ -779,7 +779,7 @@ func createValidator(t *testing.T, ctx sdk.Context, bankKeeper bankkeeper.Keeper val1, err := stakingtypes.NewValidator(valAddrs[0], pks[0], stakingtypes.Description{}) require.NoError(t, err) - require.NoError(t, stakingKeeper.SetValidator(ctx, val1)) + require.NoError(t, stakingKeeper.Validators.Set(ctx, val1.GetOperator(), val1)) require.NoError(t, stakingKeeper.SetValidatorByConsAddr(ctx, val1)) require.NoError(t, stakingKeeper.SetNewValidatorByPowerIndex(ctx, val1)) diff --git a/tests/integration/distribution/keeper/grpc_query_test.go b/tests/integration/distribution/keeper/grpc_query_test.go index cebe57eac71e..0c663cc9bbca 100644 --- a/tests/integration/distribution/keeper/grpc_query_test.go +++ b/tests/integration/distribution/keeper/grpc_query_test.go @@ -503,7 +503,7 @@ func TestGRPCDelegationRewards(t *testing.T) { tstaking.Commission = stakingtypes.NewCommissionRates(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) tstaking.CreateValidator(f.valAddr, valConsPk0, math.NewInt(initialStake), true) - val, found := f.stakingKeeper.GetValidator(f.sdkCtx, f.valAddr) + val, found := f.stakingKeeper.Validators.Get(f.sdkCtx, f.valAddr) assert.Assert(t, found) // setup delegation diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index 4469b1404b21..911bc4d5d9de 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -181,7 +181,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { assert.NilError(t, err) validator.DelegatorShares = math.LegacyNewDec(100) validator.Tokens = math.NewInt(1000000) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validator)) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator)) // set module account coins initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000)) diff --git a/tests/integration/evidence/keeper/infraction_test.go b/tests/integration/evidence/keeper/infraction_test.go index f716cf4f0101..e1d66a53968c 100644 --- a/tests/integration/evidence/keeper/infraction_test.go +++ b/tests/integration/evidence/keeper/infraction_test.go @@ -244,7 +244,7 @@ func TestHandleDoubleSign(t *testing.T) { // require we be able to unbond now ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) del, _ := f.stakingKeeper.GetDelegation(ctx, sdk.AccAddress(operatorAddr), operatorAddr) - validator, _ := f.stakingKeeper.GetValidator(ctx, operatorAddr) + validator, _ := f.stakingKeeper.Validators.Get(ctx, operatorAddr) totalBond := validator.TokensFromShares(del.GetShares()).TruncateInt() tstaking.Ctx = ctx tstaking.Denom = stakingParams.BondDenom diff --git a/tests/integration/gov/keeper/common_test.go b/tests/integration/gov/keeper/common_test.go index c642ff1f1798..fcd17dd53415 100644 --- a/tests/integration/gov/keeper/common_test.go +++ b/tests/integration/gov/keeper/common_test.go @@ -48,9 +48,9 @@ func createValidators(t *testing.T, f *fixture, powers []int64) ([]sdk.AccAddres val3, err := stakingtypes.NewValidator(valAddrs[2], pks[2], stakingtypes.Description{}) assert.NilError(t, err) - assert.NilError(t, f.stakingKeeper.SetValidator(f.ctx, val1)) - assert.NilError(t, f.stakingKeeper.SetValidator(f.ctx, val2)) - assert.NilError(t, f.stakingKeeper.SetValidator(f.ctx, val3)) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.ctx, val1.GetOperator(), val1)) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.ctx, val2.GetOperator(), val2)) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.ctx, val3.GetOperator(), val3)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.ctx, val1)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.ctx, val2)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.ctx, val3)) diff --git a/tests/integration/gov/keeper/tally_test.go b/tests/integration/gov/keeper/tally_test.go index a30ff271846c..9eaa0f12a679 100644 --- a/tests/integration/gov/keeper/tally_test.go +++ b/tests/integration/gov/keeper/tally_test.go @@ -275,7 +275,7 @@ func TestTallyDelgatorOverride(t *testing.T) { addrs, valAddrs := createValidators(t, f, []int64{5, 6, 7}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 30) - val1, found := f.stakingKeeper.GetValidator(ctx, valAddrs[0]) + val1, found := f.stakingKeeper.Validators.Get(ctx, valAddrs[0]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[4], delTokens, stakingtypes.Unbonded, val1, true) @@ -314,7 +314,7 @@ func TestTallyDelgatorInherit(t *testing.T) { addrs, vals := createValidators(t, f, []int64{5, 6, 7}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 30) - val3, found := f.stakingKeeper.GetValidator(ctx, vals[2]) + val3, found := f.stakingKeeper.Validators.Get(ctx, vals[2]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val3, true) @@ -352,9 +352,9 @@ func TestTallyDelgatorMultipleOverride(t *testing.T) { addrs, vals := createValidators(t, f, []int64{5, 6, 7}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 10) - val1, found := f.stakingKeeper.GetValidator(ctx, vals[0]) + val1, found := f.stakingKeeper.Validators.Get(ctx, vals[0]) assert.Assert(t, found) - val2, found := f.stakingKeeper.GetValidator(ctx, vals[1]) + val2, found := f.stakingKeeper.Validators.Get(ctx, vals[1]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val1, true) @@ -397,9 +397,9 @@ func TestTallyDelgatorMultipleInherit(t *testing.T) { addrs, vals := createValidators(t, f, []int64{5, 6, 7}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 10) - val2, found := f.stakingKeeper.GetValidator(ctx, vals[1]) + val2, found := f.stakingKeeper.Validators.Get(ctx, vals[1]) assert.Assert(t, found) - val3, found := f.stakingKeeper.GetValidator(ctx, vals[2]) + val3, found := f.stakingKeeper.Validators.Get(ctx, vals[2]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val2, true) @@ -439,9 +439,9 @@ func TestTallyJailedValidator(t *testing.T) { addrs, valAddrs := createValidators(t, f, []int64{25, 6, 7}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 10) - val2, found := f.stakingKeeper.GetValidator(ctx, valAddrs[1]) + val2, found := f.stakingKeeper.Validators.Get(ctx, valAddrs[1]) assert.Assert(t, found) - val3, found := f.stakingKeeper.GetValidator(ctx, valAddrs[2]) + val3, found := f.stakingKeeper.Validators.Get(ctx, valAddrs[2]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val2, true) @@ -485,7 +485,7 @@ func TestTallyValidatorMultipleDelegations(t *testing.T) { addrs, valAddrs := createValidators(t, f, []int64{10, 10, 10}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 10) - val2, found := f.stakingKeeper.GetValidator(ctx, valAddrs[1]) + val2, found := f.stakingKeeper.Validators.Get(ctx, valAddrs[1]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[0], delTokens, stakingtypes.Unbonded, val2, true) diff --git a/tests/integration/staking/keeper/common_test.go b/tests/integration/staking/keeper/common_test.go index 1645fe79686b..bc0ae4fa1c5e 100644 --- a/tests/integration/staking/keeper/common_test.go +++ b/tests/integration/staking/keeper/common_test.go @@ -75,8 +75,8 @@ func createValidators(t *testing.T, f *fixture, powers []int64) ([]sdk.AccAddres val2 := testutil.NewValidator(t, valAddrs[1], pks[1]) vals := []types.Validator{val1, val2} - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, val1)) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, val2)) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, val1.GetOperator(), val1)) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, val2.GetOperator(), val2)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.sdkCtx, val1)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.sdkCtx, val2)) assert.NilError(t, f.stakingKeeper.SetNewValidatorByPowerIndex(f.sdkCtx, val1)) diff --git a/tests/integration/staking/keeper/determinstic_test.go b/tests/integration/staking/keeper/determinstic_test.go index 85c6ff94e506..049d96dc6250 100644 --- a/tests/integration/staking/keeper/determinstic_test.go +++ b/tests/integration/staking/keeper/determinstic_test.go @@ -234,7 +234,7 @@ func createAndSetValidator(t *testing.T, rt *rapid.T, f *deterministicFixture) s func setValidator(t *testing.T, f *deterministicFixture, validator stakingtypes.Validator) { t.Helper() - assert.NilError(t, f.stakingKeeper.SetValidator(f.ctx, validator)) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.ctx, validator.GetOperator(), validator)) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.ctx, validator)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.ctx, validator)) assert.NilError(t, f.stakingKeeper.Hooks().AfterValidatorCreated(f.ctx, validator.GetOperator())) diff --git a/tests/integration/staking/keeper/genesis_test.go b/tests/integration/staking/keeper/genesis_test.go index 8884318b02df..683779078cc0 100644 --- a/tests/integration/staking/keeper/genesis_test.go +++ b/tests/integration/staking/keeper/genesis_test.go @@ -42,7 +42,7 @@ func TestInitGenesis(t *testing.T) { DelegatorShares: math.LegacyNewDecFromInt(valTokens), Description: types.NewDescription("hoop", "", "", "", ""), } - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, bondedVal)) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, bondedVal.GetOperator(), bondedVal)) params, err := f.stakingKeeper.GetParams(f.sdkCtx) assert.NilError(t, err) @@ -117,11 +117,11 @@ func TestInitGenesis(t *testing.T) { } // now make sure the validators are bonded and intra-tx counters are correct - resVal, found := (f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[1]))) + resVal, found := (f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[1]))) assert.Assert(t, found) assert.Equal(t, types.Bonded, resVal.Status) - resVal, found = (f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[2]))) + resVal, found = (f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[2]))) assert.Assert(t, found) assert.Equal(t, types.Bonded, resVal.Status) diff --git a/tests/integration/staking/keeper/msg_server_test.go b/tests/integration/staking/keeper/msg_server_test.go index 9edcc02d96ce..c4a9263dd66b 100644 --- a/tests/integration/staking/keeper/msg_server_test.go +++ b/tests/integration/staking/keeper/msg_server_test.go @@ -43,7 +43,7 @@ func TestCancelUnbondingDelegation(t *testing.T) { validator, err := types.NewValidator(valAddr, PKs[0], types.NewDescription("Validator", "", "", "", "")) validator.Status = types.Bonded assert.NilError(t, err) - assert.NilError(t, f.stakingKeeper.SetValidator(ctx, validator)) + assert.NilError(t, f.stakingKeeper.Validators.Set(ctx, validator.GetOperator(), validator)) validatorAddr, err := sdk.ValAddressFromBech32(validator.OperatorAddress) assert.NilError(t, err) diff --git a/tests/integration/staking/keeper/slash_test.go b/tests/integration/staking/keeper/slash_test.go index 4b0bd47e9fdf..d1fd5b60864c 100644 --- a/tests/integration/staking/keeper/slash_test.go +++ b/tests/integration/staking/keeper/slash_test.go @@ -130,7 +130,7 @@ func TestSlashRedelegation(t *testing.T) { assert.NilError(t, f.stakingKeeper.SetDelegation(f.sdkCtx, del)) // started redelegating prior to the current height, stake didn't contribute to infraction - validator, found := f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) + validator, found := f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) assert.Assert(t, found) slashAmount, err := f.stakingKeeper.SlashRedelegation(f.sdkCtx, validator, rd, 1, fraction) assert.NilError(t, err) @@ -139,7 +139,7 @@ func TestSlashRedelegation(t *testing.T) { // after the expiration time, no longer eligible for slashing f.sdkCtx = f.sdkCtx.WithBlockHeader(cmtproto.Header{Time: time.Unix(10, 0)}) assert.NilError(t, f.stakingKeeper.SetRedelegation(f.sdkCtx, rd)) - validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) + validator, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) assert.Assert(t, found) slashAmount, err = f.stakingKeeper.SlashRedelegation(f.sdkCtx, validator, rd, 0, fraction) assert.NilError(t, err) @@ -150,7 +150,7 @@ func TestSlashRedelegation(t *testing.T) { // test valid slash, before expiration timestamp and to which stake contributed f.sdkCtx = f.sdkCtx.WithBlockHeader(cmtproto.Header{Time: time.Unix(0, 0)}) assert.NilError(t, f.stakingKeeper.SetRedelegation(f.sdkCtx, rd)) - validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) + validator, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) assert.Assert(t, found) slashAmount, err = f.stakingKeeper.SlashRedelegation(f.sdkCtx, validator, rd, 0, fraction) assert.NilError(t, err) @@ -197,7 +197,7 @@ func TestSlashAtNegativeHeight(t *testing.T) { // end block applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1) - validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, validator.GetOperator()) + validator, found = f.stakingKeeper.Validators.Get(f.sdkCtx, validator.GetOperator()) assert.Assert(t, found) // power decreased assert.Equal(t, int64(5), validator.GetConsensusPower(f.stakingKeeper.PowerReduction(f.sdkCtx))) @@ -235,7 +235,7 @@ func TestSlashValidatorAtCurrentHeight(t *testing.T) { // end block applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1) - validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, validator.GetOperator()) + validator, found = f.stakingKeeper.Validators.Get(f.sdkCtx, validator.GetOperator()) assert.Assert(t, found) // power decreased assert.Equal(t, int64(5), validator.GetConsensusPower(f.stakingKeeper.PowerReduction(f.sdkCtx))) diff --git a/tests/integration/staking/keeper/unbonding_test.go b/tests/integration/staking/keeper/unbonding_test.go index 9ed6aab2fcab..178470fedabd 100644 --- a/tests/integration/staking/keeper/unbonding_test.go +++ b/tests/integration/staking/keeper/unbonding_test.go @@ -156,7 +156,7 @@ func doValidatorUnbonding( hookCalled *bool, ) (validator types.Validator) { t.Helper() - validator, found := stakingKeeper.GetValidator(ctx, addrVal) + validator, found := stakingKeeper.Validators.Get(ctx, addrVal) assert.Assert(t, found) // Check that status is bonded assert.Equal(t, types.BondStatus(3), validator.Status) @@ -198,7 +198,7 @@ func TestValidatorUnbondingOnHold1(t *testing.T) { assert.NilError(t, f.stakingKeeper.UnbondAllMatureValidators(f.sdkCtx)) // Check that validator unbonding is not complete (is not mature yet) - validator, found := f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[0]) + validator, found := f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[0]) assert.Assert(t, found) assert.Equal(t, types.Unbonding, validator.Status) unbondingVals, err := f.stakingKeeper.GetUnbondingValidators(f.sdkCtx, completionTime, completionHeight) @@ -212,7 +212,7 @@ func TestValidatorUnbondingOnHold1(t *testing.T) { assert.NilError(t, f.stakingKeeper.UnbondAllMatureValidators(f.sdkCtx)) // Check that validator unbonding is complete - validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[0]) + validator, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[0]) assert.Assert(t, found) assert.Equal(t, types.Unbonded, validator.Status) unbondingVals, err = f.stakingKeeper.GetUnbondingValidators(f.sdkCtx, completionTime, completionHeight) @@ -258,10 +258,10 @@ func TestValidatorUnbondingOnHold2(t *testing.T) { assert.NilError(t, f.stakingKeeper.UnbondAllMatureValidators(f.sdkCtx)) // Check that unbonding is not complete for both validators - validator1, found := f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[0]) + validator1, found := f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[0]) assert.Assert(t, found) assert.Equal(t, types.Unbonding, validator1.Status) - validator2, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) + validator2, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) assert.Assert(t, found) assert.Equal(t, types.Unbonding, validator2.Status) unbondingVals, err := f.stakingKeeper.GetUnbondingValidators(f.sdkCtx, completionTime, completionHeight) @@ -278,10 +278,10 @@ func TestValidatorUnbondingOnHold2(t *testing.T) { assert.NilError(t, f.stakingKeeper.UnbondAllMatureValidators(f.sdkCtx)) // Check that unbonding is complete for validator1, but not for validator2 - validator1, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[0]) + validator1, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[0]) assert.Assert(t, found) assert.Equal(t, types.Unbonded, validator1.Status) - validator2, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) + validator2, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) assert.Assert(t, found) assert.Equal(t, types.Unbonding, validator2.Status) unbondingVals, err = f.stakingKeeper.GetUnbondingValidators(f.sdkCtx, completionTime, completionHeight) @@ -297,7 +297,7 @@ func TestValidatorUnbondingOnHold2(t *testing.T) { assert.NilError(t, f.stakingKeeper.UnbondAllMatureValidators(f.sdkCtx)) // Check that unbonding is complete for validator2 - validator2, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) + validator2, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) assert.Assert(t, found) assert.Equal(t, types.Unbonded, validator2.Status) unbondingVals, err = f.stakingKeeper.GetUnbondingValidators(f.sdkCtx, completionTime, completionHeight) diff --git a/tests/integration/staking/keeper/validator_bench_test.go b/tests/integration/staking/keeper/validator_bench_test.go index 06bca8ee875d..dbac3170d7d9 100644 --- a/tests/integration/staking/keeper/validator_bench_test.go +++ b/tests/integration/staking/keeper/validator_bench_test.go @@ -28,7 +28,7 @@ func BenchmarkGetValidator(b *testing.B) { f, _, valAddrs, vals := initValidators(b, totalPower, len(powers), powers) for _, validator := range vals { - if err := f.stakingKeeper.SetValidator(f.sdkCtx, validator); err != nil { + if err := f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator); err != nil { panic(err) } } @@ -36,7 +36,7 @@ func BenchmarkGetValidator(b *testing.B) { b.ResetTimer() for n := 0; n < b.N; n++ { for _, addr := range valAddrs { - _, _ = f.stakingKeeper.GetValidator(f.sdkCtx, addr) + _, _ = f.stakingKeeper.Validators.Get(f.sdkCtx, addr) } } } @@ -53,7 +53,7 @@ func BenchmarkGetValidatorDelegations(b *testing.B) { f, _, valAddrs, vals := initValidators(b, totalPower, len(powers), powers) for _, validator := range vals { - if err := f.stakingKeeper.SetValidator(f.sdkCtx, validator); err != nil { + if err := f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator); err != nil { panic(err) } } @@ -94,7 +94,7 @@ func BenchmarkGetValidatorDelegationsLegacy(b *testing.B) { f, _, valAddrs, vals := initValidators(b, totalPower, len(powers), powers) for _, validator := range vals { - if err := f.stakingKeeper.SetValidator(f.sdkCtx, validator); err != nil { + if err := f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator); err != nil { panic(err) } } diff --git a/tests/integration/staking/keeper/validator_test.go b/tests/integration/staking/keeper/validator_test.go index 11bdf0824684..50b5f91d6270 100644 --- a/tests/integration/staking/keeper/validator_test.go +++ b/tests/integration/staking/keeper/validator_test.go @@ -117,7 +117,7 @@ func TestUpdateBondedValidatorsDecreaseCliff(t *testing.T) { valAddr := validators[valIdx].OperatorAddress addr, err := sdk.ValAddressFromBech32(valAddr) assert.NilError(t, err) - val, _ := f.stakingKeeper.GetValidator(f.sdkCtx, addr) + val, _ := f.stakingKeeper.Validators.Get(f.sdkCtx, addr) assert.Equal( t, status, val.GetStatus(), @@ -154,7 +154,7 @@ func TestSlashToZeroPowerRemoved(t *testing.T) { // apply TM updates applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, -1) // validator should be unbonding - validator, _ = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[0]) + validator, _ = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[0]) assert.Equal(t, validator.GetStatus(), types.Unbonding) } @@ -285,15 +285,15 @@ func TestGetValidatorSortingMixed(t *testing.T) { keeper.TestingUpdateValidator(f.stakingKeeper, f.sdkCtx, validators[i], true) } - val0, found := f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[0])) + val0, found := f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[0])) assert.Assert(t, found) - val1, found := f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[1])) + val1, found := f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[1])) assert.Assert(t, found) - val2, found := f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[2])) + val2, found := f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[2])) assert.Assert(t, found) - val3, found := f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[3])) + val3, found := f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[3])) assert.Assert(t, found) - val4, found := f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[4])) + val4, found := f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[4])) assert.Assert(t, found) assert.Equal(t, types.Bonded, val0.Status) assert.Equal(t, types.Unbonding, val1.Status) @@ -378,7 +378,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) { // validator 3 enters bonded validator set f.sdkCtx = f.sdkCtx.WithBlockHeight(40) - validators[3], err = f.stakingKeeper.GetValidator(f.sdkCtx, validators[3].GetOperator()) + validators[3], err = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[3].GetOperator()) assert.NilError(t, err) assert.NilError(t, f.stakingKeeper.DeleteValidatorByPowerIndex(f.sdkCtx, validators[3])) validators[3], _ = validators[3].AddTokensFromDel(f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 1)) @@ -425,7 +425,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) { assert.Equal(t, nMax, uint32(len(resValidators))) assert.Assert(ValEq(t, validators[0], resValidators[0])) assert.Assert(ValEq(t, validators[2], resValidators[1])) - _, exists := f.stakingKeeper.GetValidator(f.sdkCtx, validators[3].GetOperator()) + _, exists := f.stakingKeeper.Validators.Get(f.sdkCtx, validators[3].GetOperator()) assert.Assert(t, exists) } @@ -496,7 +496,7 @@ func TestFullValidatorSetPowerChange(t *testing.T) { keeper.TestingUpdateValidator(f.stakingKeeper, f.sdkCtx, validators[i], true) } for i := range powers { - validators[i], err = f.stakingKeeper.GetValidator(f.sdkCtx, validators[i].GetOperator()) + validators[i], err = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[i].GetOperator()) assert.NilError(t, err) } assert.Equal(t, types.Unbonded, validators[0].Status) @@ -539,14 +539,14 @@ func TestApplyAndReturnValidatorSetUpdatesAllNone(t *testing.T) { // test from nothing to something // tendermintUpdate set: {} -> {c1, c3} applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 0) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[0])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[0].GetOperator(), validators[0])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[0])) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[1])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[1].GetOperator(), validators[1])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[1])) updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 2) - validators[0], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[0].GetOperator()) - validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[1].GetOperator()) + validators[0], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[0].GetOperator()) + validators[1], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[1].GetOperator()) assert.DeepEqual(t, validators[0].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1]) assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) } @@ -633,26 +633,26 @@ func TestApplyAndReturnValidatorSetUpdatesInserted(t *testing.T) { // test validtor added at the beginning // tendermintUpdate set: {} -> {c0} - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[2])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[2].GetOperator(), validators[2])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[2])) updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1) - validators[2], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[2].GetOperator()) + validators[2], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[2].GetOperator()) assert.DeepEqual(t, validators[2].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) // test validtor added at the beginning // tendermintUpdate set: {} -> {c0} - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[3])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[3].GetOperator(), validators[3])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[3])) updates = applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1) - validators[3], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[3].GetOperator()) + validators[3], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[3].GetOperator()) assert.DeepEqual(t, validators[3].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) // test validtor added at the end // tendermintUpdate set: {} -> {c0} - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[4])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[4].GetOperator(), validators[4])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[4])) updates = applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1) - validators[4], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[4].GetOperator()) + validators[4], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[4].GetOperator()) assert.DeepEqual(t, validators[4].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) } @@ -684,10 +684,10 @@ func TestApplyAndReturnValidatorSetUpdatesWithCliffValidator(t *testing.T) { tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 10) validators[2], _ = validators[2].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[2])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[2].GetOperator(), validators[2])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[2])) updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 2) - validators[2], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[2].GetOperator()) + validators[2], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[2].GetOperator()) assert.DeepEqual(t, validators[0].ABCIValidatorUpdateZero(), updates[1]) assert.DeepEqual(t, validators[2].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) } @@ -712,14 +712,14 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, power) validators[i], _ = validators[i].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[i])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[i].GetOperator(), validators[i])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[i])) } // verify initial CometBFT updates are correct updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, len(validators)) - validators[0], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[0].GetOperator()) - validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[1].GetOperator()) + validators[0], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[0].GetOperator()) + validators[1], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[1].GetOperator()) assert.DeepEqual(t, validators[0].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1]) @@ -732,7 +732,7 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, power) validators[i], _ = validators[i].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[i])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[i].GetOperator(), validators[i])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[i])) } @@ -745,10 +745,10 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { validator := testutil.NewValidator(t, valAddr, valPubKey) validator, _ = validator.AddTokensFromDel(amt) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validator)) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator)) validator, _ = validator.RemoveDelShares(math.LegacyNewDecFromInt(amt)) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validator)) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator)) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validator)) // add a new validator that increases in power @@ -758,14 +758,14 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { validator = testutil.NewValidator(t, valAddr, valPubKey) tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 500) validator, _ = validator.AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validator)) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator)) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validator)) // verify initial CometBFT updates are correct updates = applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, len(validators)+1) - validator, _ = f.stakingKeeper.GetValidator(f.sdkCtx, validator.GetOperator()) - validators[0], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[0].GetOperator()) - validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[1].GetOperator()) + validator, _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validator.GetOperator()) + validators[0], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[0].GetOperator()) + validators[1], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[1].GetOperator()) assert.DeepEqual(t, validator.ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) assert.DeepEqual(t, validators[0].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1]) assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[2]) @@ -791,14 +791,14 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) { validators[i] = newMonikerValidator(t, valAddr, valPubKey, moniker) tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, power) validators[i], _ = validators[i].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[i])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[i].GetOperator(), validators[i])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[i])) } // verify initial CometBFT updates are correct updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 2) - validators[2], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[2].GetOperator()) - validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[1].GetOperator()) + validators[2], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[2].GetOperator()) + validators[1], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[1].GetOperator()) assert.DeepEqual(t, validators[2].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1]) @@ -807,13 +807,13 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) { // delegate to validator with lowest power but not enough to bond f.sdkCtx = f.sdkCtx.WithBlockHeight(1) - validators[0], err = f.stakingKeeper.GetValidator(f.sdkCtx, validators[0].GetOperator()) + validators[0], err = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[0].GetOperator()) assert.NilError(t, err) assert.NilError(t, f.stakingKeeper.DeleteValidatorByPowerIndex(f.sdkCtx, validators[0])) tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 1) validators[0], _ = validators[0].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[0])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[0].GetOperator(), validators[0])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[0])) // verify initial CometBFT updates are correct @@ -823,19 +823,19 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) { // lowest power in a single block context (height) f.sdkCtx = f.sdkCtx.WithBlockHeight(2) - validators[1], err = f.stakingKeeper.GetValidator(f.sdkCtx, validators[1].GetOperator()) + validators[1], err = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[1].GetOperator()) assert.NilError(t, err) assert.NilError(t, f.stakingKeeper.DeleteValidatorByPowerIndex(f.sdkCtx, validators[0])) validators[0], _ = validators[0].RemoveDelShares(validators[0].DelegatorShares) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[0])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[0].GetOperator(), validators[0])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[0])) applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 0) assert.NilError(t, f.stakingKeeper.DeleteValidatorByPowerIndex(f.sdkCtx, validators[1])) tokens = f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 250) validators[1], _ = validators[1].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[1])) + assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[1].GetOperator(), validators[1])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[1])) // verify initial CometBFT updates are correct diff --git a/tests/integration/staking/simulation/operations_test.go b/tests/integration/staking/simulation/operations_test.go index 7fb8bbeb3dd1..21fda51dd1c0 100644 --- a/tests/integration/staking/simulation/operations_test.go +++ b/tests/integration/staking/simulation/operations_test.go @@ -378,7 +378,7 @@ func (s *SimTestSuite) getTestingValidator(ctx sdk.Context, commission types.Com validator.DelegatorShares = math.LegacyNewDec(100) validator.Tokens = s.stakingKeeper.TokensFromConsensusPower(ctx, 100) - s.Require().NoError(s.stakingKeeper.SetValidator(ctx, validator)) + s.Require().NoError(s.stakingKeeper.Validators.Set(ctx, validator.GetOperator(), validator)) return validator } diff --git a/x/distribution/simulation/operations_test.go b/x/distribution/simulation/operations_test.go index 830c07460ee3..4d2431a68ef3 100644 --- a/x/distribution/simulation/operations_test.go +++ b/x/distribution/simulation/operations_test.go @@ -306,7 +306,7 @@ func (suite *SimTestSuite) getTestingValidator(accounts []simtypes.Account, comm validator.DelegatorShares = math.LegacyNewDec(100) validator.Tokens = math.NewInt(1000000) - suite.Require().NoError(suite.stakingKeeper.SetValidator(suite.ctx, validator)) + suite.Require().NoError(suite.stakingKeeper.Validators.Set(suite.ctx, validator.GetOperator(), validator)) return validator } diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index 0ad0ac89550a..1857ea050696 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -94,7 +94,7 @@ func TestSlashingMsgs(t *testing.T) { _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1}) require.NoError(t, err) ctxCheck = baseApp.NewContext(true) - validator, err := stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1)) + validator, err := stakingKeeper.Validators.Get(ctxCheck, sdk.ValAddress(addr1)) require.NoError(t, err) require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress) diff --git a/x/slashing/simulation/operations_test.go b/x/slashing/simulation/operations_test.go index 8c555fb6682f..3d82004207ce 100644 --- a/x/slashing/simulation/operations_test.go +++ b/x/slashing/simulation/operations_test.go @@ -219,7 +219,7 @@ func getTestingValidator(ctx sdk.Context, stakingKeeper *stakingkeeper.Keeper, a validator.DelegatorShares = math.LegacyNewDec(100) validator.Tokens = math.NewInt(1000000) - err = stakingKeeper.SetValidator(ctx, validator) + err = stakingKeeper.Validators.Set(ctx, validator.GetOperator(), validator) if err != nil { return stakingtypes.Validator{}, err } diff --git a/x/staking/app_test.go b/x/staking/app_test.go index 04a026780d53..d2bad852ecf4 100644 --- a/x/staking/app_test.go +++ b/x/staking/app_test.go @@ -82,7 +82,7 @@ func TestStakingMsgs(t *testing.T) { _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1}) require.NoError(t, err) ctxCheck = app.BaseApp.NewContext(true) - validator, err := stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1)) + validator, err := stakingKeeper.Validators.Get(ctxCheck, sdk.ValAddress(addr1)) require.NoError(t, err) require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress) @@ -101,7 +101,7 @@ func TestStakingMsgs(t *testing.T) { require.NoError(t, err) ctxCheck = app.BaseApp.NewContext(true) - validator, err = stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1)) + validator, err = stakingKeeper.Validators.Get(ctxCheck, sdk.ValAddress(addr1)) require.NoError(t, err) require.Equal(t, description, validator.Description) diff --git a/x/staking/keeper/alias_functions.go b/x/staking/keeper/alias_functions.go index 4379043cc6d6..8e26b463ceca 100644 --- a/x/staking/keeper/alias_functions.go +++ b/x/staking/keeper/alias_functions.go @@ -82,7 +82,7 @@ func (k Keeper) IterateLastValidators(ctx context.Context, fn func(index int64, for ; iterator.Valid(); iterator.Next() { address := types.AddressFromLastValidatorPowerKey(iterator.Key()) - validator, err := k.GetValidator(ctx, address) + validator, err := k.Validators.Get(ctx, address) if err != nil { return err } @@ -98,7 +98,7 @@ func (k Keeper) IterateLastValidators(ctx context.Context, fn func(index int64, // Validator gets the Validator interface for a particular address func (k Keeper) Validator(ctx context.Context, address sdk.ValAddress) (types.ValidatorI, error) { - return k.GetValidator(ctx, address) + return k.Validators.Get(ctx, address) } // ValidatorByConsAddr gets the validator interface for a particular pubkey diff --git a/x/staking/keeper/delegation.go b/x/staking/keeper/delegation.go index e5849b8daa8e..dad0155ff61f 100644 --- a/x/staking/keeper/delegation.go +++ b/x/staking/keeper/delegation.go @@ -308,7 +308,7 @@ func (k Keeper) GetDelegatorBonded(ctx context.Context, delegator sdk.AccAddress if err != nil { panic(err) // shouldn't happen } - validator, err := k.GetValidator(ctx, validatorAddr) + validator, err := k.Validators.Get(ctx, validatorAddr) if err == nil { shares := delegation.Shares tokens := validator.TokensFromSharesTruncated(shares) @@ -992,7 +992,7 @@ func (k Keeper) Unbond( } // get validator - validator, err := k.GetValidator(ctx, valAddr) + validator, err := k.Validators.Get(ctx, valAddr) if err != nil { return amount, err } @@ -1061,7 +1061,7 @@ func (k Keeper) Unbond( func (k Keeper) getBeginInfo( ctx context.Context, valSrcAddr sdk.ValAddress, ) (completionTime time.Time, height int64, completeNow bool, err error) { - validator, err := k.GetValidator(ctx, valSrcAddr) + validator, err := k.Validators.Get(ctx, valSrcAddr) if err != nil && errors.Is(err, types.ErrNoValidatorFound) { return completionTime, height, false, nil } @@ -1099,7 +1099,7 @@ func (k Keeper) getBeginInfo( func (k Keeper) Undelegate( ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, sharesAmount math.LegacyDec, ) (time.Time, math.Int, error) { - validator, err := k.GetValidator(ctx, valAddr) + validator, err := k.Validators.Get(ctx, valAddr) if err != nil { return time.Time{}, math.Int{}, err } @@ -1216,14 +1216,14 @@ func (k Keeper) BeginRedelegation( return time.Time{}, types.ErrSelfRedelegation } - dstValidator, err := k.GetValidator(ctx, valDstAddr) + dstValidator, err := k.Validators.Get(ctx, valDstAddr) if errors.Is(err, types.ErrNoValidatorFound) { return time.Time{}, types.ErrBadRedelegationDst } else if err != nil { return time.Time{}, err } - srcValidator, err := k.GetValidator(ctx, valSrcAddr) + srcValidator, err := k.Validators.Get(ctx, valSrcAddr) if errors.Is(err, types.ErrNoValidatorFound) { return time.Time{}, types.ErrBadRedelegationSrc } else if err != nil { @@ -1345,7 +1345,7 @@ func (k Keeper) CompleteRedelegation( func (k Keeper) ValidateUnbondAmount( ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, amt math.Int, ) (shares math.LegacyDec, err error) { - validator, err := k.GetValidator(ctx, valAddr) + validator, err := k.Validators.Get(ctx, valAddr) if err != nil { return shares, err } diff --git a/x/staking/keeper/delegation_test.go b/x/staking/keeper/delegation_test.go index ab689985a78f..f45662b48075 100644 --- a/x/staking/keeper/delegation_test.go +++ b/x/staking/keeper/delegation_test.go @@ -5,6 +5,7 @@ import ( "github.com/golang/mock/gomock" + "cosmossdk.io/collections" "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec/address" @@ -385,7 +386,7 @@ func (s *KeeperTestSuite) TestUnbondDelegation() { delegation, err = keeper.GetDelegation(ctx, delAddrs[0], valAddrs[0]) require.NoError(err) - validator, err = keeper.GetValidator(ctx, valAddrs[0]) + validator, err = keeper.Validators.Get(ctx, valAddrs[0]) require.NoError(err) remainingTokens := startTokens.Sub(bondTokens) @@ -437,7 +438,7 @@ func (s *KeeperTestSuite) TestUndelegateSelfDelegationBelowMinSelfDelegation() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err = keeper.GetValidator(ctx, addrVals[0]) + validator, err = keeper.Validators.Get(ctx, addrVals[0]) require.NoError(err) require.Equal(keeper.TokensFromConsensusPower(ctx, 14), validator.Tokens) require.Equal(stakingtypes.Unbonding, validator.Status) @@ -493,7 +494,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondingValidator() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err = keeper.GetValidator(ctx, addrVals[0]) + validator, err = keeper.Validators.Get(ctx, addrVals[0]) require.NoError(err) require.Equal(blockHeight, validator.UnbondingHeight) params, err := keeper.GetParams(ctx) @@ -564,7 +565,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondedValidator() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err = keeper.GetValidator(ctx, addrVals[0]) + validator, err = keeper.Validators.Get(ctx, addrVals[0]) require.NoError(err) require.Equal(ctx.BlockHeight(), validator.UnbondingHeight) params, err := keeper.GetParams(ctx) @@ -577,7 +578,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondedValidator() { require.NoError(err) // Make sure validator is still in state because there is still an outstanding delegation - validator, err = keeper.GetValidator(ctx, addrVals[0]) + validator, err = keeper.Validators.Get(ctx, addrVals[0]) require.NoError(err) require.Equal(validator.Status, stakingtypes.Unbonded) @@ -594,8 +595,8 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondedValidator() { require.Equal(amount3, remainingTokens) // now validator should be deleted from state - validator, err = keeper.GetValidator(ctx, addrVals[0]) - require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) + validator, err = keeper.Validators.Get(ctx, addrVals[0]) + require.ErrorIs(err, collections.ErrNotFound) } func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() { @@ -651,7 +652,7 @@ func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() { require.Equal(amount2, delTokens) // validator should still be in state and still be in unbonding state - validator, err = keeper.GetValidator(ctx, addrVals[0]) + validator, err = keeper.Validators.Get(ctx, addrVals[0]) require.NoError(err) require.Equal(validator.Status, stakingtypes.Unbonding) @@ -661,8 +662,8 @@ func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() { require.NoError(err) // validator should now be deleted from state - _, err = keeper.GetValidator(ctx, addrVals[0]) - require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) + _, err = keeper.Validators.Get(ctx, addrVals[0]) + require.ErrorIs(err, collections.ErrNotFound) } // Make sure that that the retrieving the delegations doesn't affect the state @@ -893,7 +894,7 @@ func (s *KeeperTestSuite) TestRedelegateSelfDelegation() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 2) - validator, err = keeper.GetValidator(ctx, addrVals[0]) + validator, err = keeper.Validators.Get(ctx, addrVals[0]) require.NoError(err) require.Equal(valTokens, validator.Tokens) require.Equal(stakingtypes.Unbonding, validator.Status) @@ -951,7 +952,7 @@ func (s *KeeperTestSuite) TestRedelegateFromUnbondingValidator() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err = keeper.GetValidator(ctx, addrVals[0]) + validator, err = keeper.Validators.Get(ctx, addrVals[0]) require.NoError(err) require.Equal(blockHeight, validator.UnbondingHeight) params, err := keeper.GetParams(ctx) @@ -1029,7 +1030,7 @@ func (s *KeeperTestSuite) TestRedelegateFromUnbondedValidator() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err = keeper.GetValidator(ctx, addrVals[0]) + validator, err = keeper.Validators.Get(ctx, addrVals[0]) require.NoError(err) require.Equal(ctx.BlockHeight(), validator.UnbondingHeight) params, err := keeper.GetParams(ctx) diff --git a/x/staking/keeper/genesis.go b/x/staking/keeper/genesis.go index 65a53c0cbe46..35bd9c476b7f 100644 --- a/x/staking/keeper/genesis.go +++ b/x/staking/keeper/genesis.go @@ -39,7 +39,7 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) (res } for _, validator := range data.Validators { - if err := k.SetValidator(ctx, validator); err != nil { + if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { panic(err) } @@ -183,7 +183,7 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) (res panic(err) } - validator, err := k.GetValidator(ctx, valAddr) + validator, err := k.Validators.Get(ctx, valAddr) if err != nil { panic(fmt.Sprintf("validator %s not found", lv.Address)) } diff --git a/x/staking/keeper/grpc_query.go b/x/staking/keeper/grpc_query.go index 7b72ec54a7a2..7bfdac337f5c 100644 --- a/x/staking/keeper/grpc_query.go +++ b/x/staking/keeper/grpc_query.go @@ -77,7 +77,7 @@ func (k Querier) Validator(ctx context.Context, req *types.QueryValidatorRequest return nil, err } - validator, err := k.GetValidator(ctx, valAddr) + validator, err := k.Keeper.Validators.Get(ctx, valAddr) if err != nil { return nil, status.Errorf(codes.NotFound, "validator %s not found", req.ValidatorAddr) } @@ -457,7 +457,7 @@ func (k Querier) DelegatorValidators(ctx context.Context, req *types.QueryDelega return err } - validator, err := k.GetValidator(ctx, valAddr) + validator, err := k.Keeper.Validators.Get(ctx, valAddr) if err != nil { return err } @@ -575,7 +575,7 @@ func delegationToDelegationResponse(ctx context.Context, k *Keeper, del types.De return types.DelegationResponse{}, err } - val, err := k.GetValidator(ctx, valAddr) + val, err := k.Validators.Get(ctx, valAddr) if err != nil { return types.DelegationResponse{}, err } @@ -631,7 +631,7 @@ func redelegationsToRedelegationResponses(ctx context.Context, k *Keeper, redels return nil, err } - val, err := k.GetValidator(ctx, valDstAddr) + val, err := k.Validators.Get(ctx, valDstAddr) if err != nil { return nil, err } diff --git a/x/staking/keeper/grpc_query_test.go b/x/staking/keeper/grpc_query_test.go index b55539d1b538..2a5412cfcfd1 100644 --- a/x/staking/keeper/grpc_query_test.go +++ b/x/staking/keeper/grpc_query_test.go @@ -14,7 +14,7 @@ func (s *KeeperTestSuite) TestGRPCQueryValidator() { require := s.Require() validator := testutil.NewValidator(s.T(), sdk.ValAddress(PKs[0].Address().Bytes()), PKs[0]) - require.NoError(keeper.SetValidator(ctx, validator)) + require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) var req *types.QueryValidatorRequest testCases := []struct { msg string diff --git a/x/staking/keeper/historical_info_test.go b/x/staking/keeper/historical_info_test.go index d05700936689..37ccf9b2a2ff 100644 --- a/x/staking/keeper/historical_info_test.go +++ b/x/staking/keeper/historical_info_test.go @@ -87,12 +87,12 @@ func (s *KeeperTestSuite) TestTrackHistoricalInfo() { val1 := testutil.NewValidator(s.T(), addrVals[2], PKs[2]) val1.Status = stakingtypes.Bonded // when not bonded, consensus power is Zero val1.Tokens = keeper.TokensFromConsensusPower(ctx, 10) - require.NoError(keeper.SetValidator(ctx, val1)) + require.NoError(keeper.Validators.Set(ctx, val1.GetOperator(), val1)) require.NoError(keeper.SetLastValidatorPower(ctx, val1.GetOperator(), 10)) val2 := testutil.NewValidator(s.T(), addrVals[3], PKs[3]) val1.Status = stakingtypes.Bonded val2.Tokens = keeper.TokensFromConsensusPower(ctx, 80) - require.NoError(keeper.SetValidator(ctx, val2)) + require.NoError(keeper.Validators.Set(ctx, val2.GetOperator(), val2)) require.NoError(keeper.SetLastValidatorPower(ctx, val2.GetOperator(), 80)) vals := []stakingtypes.Validator{val1, val2} diff --git a/x/staking/keeper/invariants.go b/x/staking/keeper/invariants.go index 3c749925bc62..429feeadcb09 100644 --- a/x/staking/keeper/invariants.go +++ b/x/staking/keeper/invariants.go @@ -114,7 +114,7 @@ func NonNegativePowerInvariant(k *Keeper) sdk.Invariant { panic(err) } for ; iterator.Valid(); iterator.Next() { - validator, err := k.GetValidator(ctx, iterator.Value()) + validator, err := k.Validators.Get(ctx, iterator.Value()) if err != nil { panic(fmt.Sprintf("validator record not found for address: %X\n", iterator.Value())) } diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 35b04a8deb90..45ba5576f833 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -35,6 +35,7 @@ type Keeper struct { Schema collections.Schema LastTotalPower collections.Item[math.Int] ValidatorUpdates collections.Item[types.ValidatorUpdates] + Validators collections.Map[sdk.ValAddress, types.Validator] } // NewKeeper creates a new staking Keeper instance @@ -77,6 +78,7 @@ func NewKeeper( consensusAddressCodec: consensusAddressCodec, LastTotalPower: collections.NewItem(sb, types.LastTotalPowerKey, "last_total_power", sdk.IntValue), ValidatorUpdates: collections.NewItem(sb, types.ValidatorUpdatesKey, "validator_updates", codec.CollValue[types.ValidatorUpdates](cdc)), + Validators: collections.NewMap(sb, types.ValidatorsKey, "validators", sdk.ValAddressKey, codec.CollValue[types.Validator](cdc)), } schema, err := sb.Build() diff --git a/x/staking/keeper/msg_server.go b/x/staking/keeper/msg_server.go index d7fb9744a72b..f96e8ab204a7 100644 --- a/x/staking/keeper/msg_server.go +++ b/x/staking/keeper/msg_server.go @@ -52,7 +52,7 @@ func (k msgServer) CreateValidator(ctx context.Context, msg *types.MsgCreateVali } // check to see if the pubkey or sender has been registered before - if _, err := k.GetValidator(ctx, valAddr); err == nil { + if _, err := k.Validators.Get(ctx, valAddr); err == nil { return nil, types.ErrValidatorOwnerExists } @@ -116,7 +116,7 @@ func (k msgServer) CreateValidator(ctx context.Context, msg *types.MsgCreateVali validator.MinSelfDelegation = msg.MinSelfDelegation - err = k.SetValidator(ctx, validator) + err = k.Validators.Set(ctx, validator.GetOperator(), validator) if err != nil { return nil, err } @@ -189,7 +189,7 @@ func (k msgServer) EditValidator(ctx context.Context, msg *types.MsgEditValidato } // validator must already be registered - validator, err := k.GetValidator(ctx, valAddr) + validator, err := k.Validators.Get(ctx, valAddr) if err != nil { return nil, err } @@ -228,7 +228,7 @@ func (k msgServer) EditValidator(ctx context.Context, msg *types.MsgEditValidato validator.MinSelfDelegation = *msg.MinSelfDelegation } - err = k.SetValidator(ctx, validator) + err = k.Validators.Set(ctx, validator.GetOperator(), validator) if err != nil { return nil, err } @@ -264,7 +264,7 @@ func (k msgServer) Delegate(ctx context.Context, msg *types.MsgDelegate) (*types ) } - validator, err := k.GetValidator(ctx, valAddr) + validator, err := k.Validators.Get(ctx, valAddr) if err != nil { return nil, err } @@ -495,7 +495,7 @@ func (k msgServer) CancelUnbondingDelegation(ctx context.Context, msg *types.Msg ) } - validator, err := k.GetValidator(ctx, valAddr) + validator, err := k.Validators.Get(ctx, valAddr) if err != nil { return nil, err } diff --git a/x/staking/keeper/msg_server_test.go b/x/staking/keeper/msg_server_test.go index 82b30abe13f0..c0985e8b49d6 100644 --- a/x/staking/keeper/msg_server_test.go +++ b/x/staking/keeper/msg_server_test.go @@ -340,7 +340,7 @@ func (s *KeeperTestSuite) TestMsgEditValidator() { MinSelfDelegation: &newSelfDel, }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "change commmission rate in <24hrs", @@ -477,7 +477,7 @@ func (s *KeeperTestSuite) TestMsgDelegate() { Amount: sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: keeper.TokensFromConsensusPower(s.ctx, int64(100))}, }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "zero amount", @@ -630,7 +630,7 @@ func (s *KeeperTestSuite) TestMsgBeginRedelegate() { Amount: sdk.NewCoin(sdk.DefaultBondDenom, shares.RoundInt()), }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "self redelegation", @@ -769,7 +769,7 @@ func (s *KeeperTestSuite) TestMsgUndelegate() { Amount: sdk.NewCoin(sdk.DefaultBondDenom, shares.RoundInt()), }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "amount greater than delegated shares amount", @@ -938,7 +938,7 @@ func (s *KeeperTestSuite) TestMsgCancelUnbondingDelegation() { CreationHeight: 10, }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "amount is greater than balance", diff --git a/x/staking/keeper/query_utils.go b/x/staking/keeper/query_utils.go index f7951fc2944f..bcff5a71c72b 100644 --- a/x/staking/keeper/query_utils.go +++ b/x/staking/keeper/query_utils.go @@ -32,7 +32,7 @@ func (k Keeper) GetDelegatorValidators( return nil, err } - validator, err := k.GetValidator(ctx, valAddr) + validator, err := k.Validators.Get(ctx, valAddr) if err != nil { return nil, err } @@ -58,7 +58,7 @@ func (k Keeper) GetDelegatorValidator( return validator, err } - return k.GetValidator(ctx, valAddr) + return k.Validators.Get(ctx, valAddr) } // GetAllDelegatorDelegations returns all delegations of a delegator diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index fc98a44585b8..a2ed981acc02 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -325,7 +325,7 @@ func (k Keeper) SlashRedelegation(ctx context.Context, srcValidator types.Valida return math.ZeroInt(), err } - dstValidator, err := k.GetValidator(ctx, valDstAddr) + dstValidator, err := k.Validators.Get(ctx, valDstAddr) if err != nil { return math.ZeroInt(), err } diff --git a/x/staking/keeper/slash_test.go b/x/staking/keeper/slash_test.go index 39a8f817a193..afd8f530bffc 100644 --- a/x/staking/keeper/slash_test.go +++ b/x/staking/keeper/slash_test.go @@ -17,21 +17,21 @@ func (s *KeeperTestSuite) TestRevocation() { validator := testutil.NewValidator(s.T(), valAddr, PKs[0]) // initial state - require.NoError(keeper.SetValidator(ctx, validator)) + require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) require.NoError(keeper.SetValidatorByConsAddr(ctx, validator)) - val, err := keeper.GetValidator(ctx, valAddr) + val, err := keeper.Validators.Get(ctx, valAddr) require.NoError(err) require.False(val.IsJailed()) // test jail require.NoError(keeper.Jail(ctx, consAddr)) - val, err = keeper.GetValidator(ctx, valAddr) + val, err = keeper.Validators.Get(ctx, valAddr) require.NoError(err) require.True(val.IsJailed()) // test unjail require.NoError(keeper.Unjail(ctx, consAddr)) - val, err = keeper.GetValidator(ctx, valAddr) + val, err = keeper.Validators.Get(ctx, valAddr) require.NoError(err) require.False(val.IsJailed()) } @@ -43,7 +43,7 @@ func (s *KeeperTestSuite) TestSlashAtFutureHeight() { consAddr := sdk.ConsAddress(PKs[0].Address()) validator := testutil.NewValidator(s.T(), sdk.ValAddress(PKs[0].Address().Bytes()), PKs[0]) - require.NoError(keeper.SetValidator(ctx, validator)) + require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) require.NoError(keeper.SetValidatorByConsAddr(ctx, validator)) fraction := sdkmath.LegacyNewDecWithPrec(5, 1) diff --git a/x/staking/keeper/test_common.go b/x/staking/keeper/test_common.go index 812c0b562bf3..48fb7eb3cfcd 100644 --- a/x/staking/keeper/test_common.go +++ b/x/staking/keeper/test_common.go @@ -22,7 +22,7 @@ func ValidatorByPowerIndexExists(ctx context.Context, keeper *Keeper, power []by // TestingUpdateValidator updates a validator for testing func TestingUpdateValidator(keeper *Keeper, ctx sdk.Context, validator types.Validator, apply bool) types.Validator { - err := keeper.SetValidator(ctx, validator) + err := keeper.Validators.Set(ctx, validator.GetOperator(), validator) if err != nil { panic(err) } @@ -64,7 +64,7 @@ func TestingUpdateValidator(keeper *Keeper, ctx sdk.Context, validator types.Val panic(err) } - validator, err = keeper.GetValidator(ctx, validator.GetOperator()) + validator, err = keeper.Validators.Get(ctx, validator.GetOperator()) if err != nil { panic(err) } diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index a0fb55de8c46..6da38a2764fa 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -216,7 +216,7 @@ func (k Keeper) SetRedelegationByUnbondingID(ctx context.Context, red types.Rede } // SetValidatorByUnbondingID sets an index to look up a Validator by the unbondingID corresponding to its current unbonding -// Note, it does not set the validator itself, use SetValidator(ctx, val) for that +// Note, it does not set the validator itself, use Validators.Set(ctx, val.GetOperator(), val) for that func (k Keeper) SetValidatorByUnbondingID(ctx context.Context, val types.Validator, id uint64) error { store := k.storeService.OpenKVStore(ctx) @@ -405,7 +405,7 @@ func (k Keeper) validatorUnbondingCanComplete(ctx context.Context, id uint64) er ) } val.UnbondingOnHoldRefCount-- - return k.SetValidator(ctx, val) + return k.Validators.Set(ctx, val.GetOperator(), val) } // PutUnbondingOnHold allows an external module to stop an unbonding operation, @@ -474,5 +474,5 @@ func (k Keeper) putValidatorOnHold(ctx context.Context, id uint64) error { } val.UnbondingOnHoldRefCount++ - return k.SetValidator(ctx, val) + return k.Validators.Set(ctx, val.GetOperator(), val) } diff --git a/x/staking/keeper/unbonding_test.go b/x/staking/keeper/unbonding_test.go index 93f817170628..bfe88a2d6e13 100644 --- a/x/staking/keeper/unbonding_test.go +++ b/x/staking/keeper/unbonding_test.go @@ -244,7 +244,7 @@ func (s *KeeperTestSuite) TestValidatorByUnbondingIDAccessors() { for i, tc := range cases { s.Run(tc.name, func() { if tc.exists.setValidator { - require.NoError(s.stakingKeeper.SetValidator(s.ctx, tc.validator)) + require.NoError(s.stakingKeeper.Validators.Set(s.ctx, tc.validator.GetOperator(), tc.validator)) } if tc.exists.setValidatorByUnbondingID { @@ -327,7 +327,7 @@ func (s *KeeperTestSuite) TestUnbondingCanComplete() { require.ErrorIs(err, types.ErrNoValidatorFound) val := testutil.NewValidator(s.T(), valAddrs[0], PKs[0]) - require.NoError(s.stakingKeeper.SetValidator(s.ctx, val)) + require.NoError(s.stakingKeeper.Validators.Set(s.ctx, val.GetOperator(), val)) require.NoError(s.stakingKeeper.SetValidatorByUnbondingID(s.ctx, val, unbondingID)) err = s.stakingKeeper.UnbondingCanComplete(s.ctx, unbondingID) require.ErrorIs(err, types.ErrUnbondingOnHoldRefCountNegative) diff --git a/x/staking/keeper/val_state_change.go b/x/staking/keeper/val_state_change.go index 43f1f463353d..e1f2d68a5167 100644 --- a/x/staking/keeper/val_state_change.go +++ b/x/staking/keeper/val_state_change.go @@ -307,7 +307,7 @@ func (k Keeper) jailValidator(ctx context.Context, validator types.Validator) er } validator.Jailed = true - if err := k.SetValidator(ctx, validator); err != nil { + if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { return err } @@ -321,7 +321,7 @@ func (k Keeper) unjailValidator(ctx context.Context, validator types.Validator) } validator.Jailed = false - if err := k.SetValidator(ctx, validator); err != nil { + if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { return err } @@ -338,7 +338,7 @@ func (k Keeper) bondValidator(ctx context.Context, validator types.Validator) (t validator = validator.UpdateStatus(types.Bonded) // save the now bonded validator record to the two referenced stores - if err := k.SetValidator(ctx, validator); err != nil { + if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { return validator, err } @@ -396,7 +396,7 @@ func (k Keeper) BeginUnbondingValidator(ctx context.Context, validator types.Val validator.UnbondingIds = append(validator.UnbondingIds, id) // save the now unbonded validator record and power index - if err = k.SetValidator(ctx, validator); err != nil { + if err = k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { return validator, err } @@ -433,7 +433,7 @@ func (k Keeper) BeginUnbondingValidator(ctx context.Context, validator types.Val // perform all the store operations for when a validator status becomes unbonded func (k Keeper) completeUnbondingValidator(ctx context.Context, validator types.Validator) (types.Validator, error) { validator = validator.UpdateStatus(types.Unbonded) - if err := k.SetValidator(ctx, validator); err != nil { + if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { return validator, err } diff --git a/x/staking/keeper/validator.go b/x/staking/keeper/validator.go index e92c49047157..a49635d81968 100644 --- a/x/staking/keeper/validator.go +++ b/x/staking/keeper/validator.go @@ -17,23 +17,8 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) -// GetValidator gets a single validator -func (k Keeper) GetValidator(ctx context.Context, addr sdk.ValAddress) (validator types.Validator, err error) { - store := k.storeService.OpenKVStore(ctx) - value, err := store.Get(types.GetValidatorKey(addr)) - if err != nil { - return validator, err - } - - if value == nil { - return validator, types.ErrNoValidatorFound - } - - return types.UnmarshalValidator(k.cdc, value) -} - func (k Keeper) mustGetValidator(ctx context.Context, addr sdk.ValAddress) types.Validator { - validator, err := k.GetValidator(ctx, addr) + validator, err := k.Validators.Get(ctx, addr) if err != nil { panic(fmt.Sprintf("validator record not found for address: %X\n", addr)) } @@ -53,7 +38,7 @@ func (k Keeper) GetValidatorByConsAddr(ctx context.Context, consAddr sdk.ConsAdd return validator, types.ErrNoValidatorFound } - return k.GetValidator(ctx, opAddr) + return k.Validators.Get(ctx, opAddr) } func (k Keeper) mustGetValidatorByConsAddr(ctx context.Context, consAddr sdk.ConsAddress) types.Validator { @@ -65,13 +50,6 @@ func (k Keeper) mustGetValidatorByConsAddr(ctx context.Context, consAddr sdk.Con return validator } -// SetValidator sets the main record holding validator details -func (k Keeper) SetValidator(ctx context.Context, validator types.Validator) error { - store := k.storeService.OpenKVStore(ctx) - bz := types.MustMarshalValidator(k.cdc, &validator) - return store.Set(types.GetValidatorKey(validator.GetOperator()), bz) -} - // SetValidatorByConsAddr sets a validator by conesensus address func (k Keeper) SetValidatorByConsAddr(ctx context.Context, validator types.Validator) error { consPk, err := validator.GetConsAddr() @@ -115,7 +93,7 @@ func (k Keeper) AddValidatorTokensAndShares(ctx context.Context, validator types } validator, addedShares = validator.AddTokensFromDel(tokensToAdd) - err = k.SetValidator(ctx, validator) + err = k.Validators.Set(ctx, validator.GetOperator(), validator) if err != nil { return validator, addedShares, err } @@ -133,7 +111,7 @@ func (k Keeper) RemoveValidatorTokensAndShares(ctx context.Context, validator ty return valOut, removedTokens, err } validator, removedTokens = validator.RemoveDelShares(sharesToRemove) - err = k.SetValidator(ctx, validator) + err = k.Validators.Set(ctx, validator.GetOperator(), validator) if err != nil { return validator, removedTokens, err } @@ -151,7 +129,7 @@ func (k Keeper) RemoveValidatorTokens(ctx context.Context, } validator = validator.RemoveTokens(tokensToRemove) - if err := k.SetValidator(ctx, validator); err != nil { + if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { return validator, err } @@ -194,7 +172,7 @@ func (k Keeper) UpdateValidatorCommission(ctx context.Context, // except for the bonded validator index which is only handled in ApplyAndReturnTendermintUpdates func (k Keeper) RemoveValidator(ctx context.Context, address sdk.ValAddress) error { // first retrieve the old validator record - validator, err := k.GetValidator(ctx, address) + validator, err := k.Validators.Get(ctx, address) if errors.Is(err, types.ErrNoValidatorFound) { return nil } @@ -214,7 +192,7 @@ func (k Keeper) RemoveValidator(ctx context.Context, address sdk.ValAddress) err // delete the old validator record store := k.storeService.OpenKVStore(ctx) - if err = store.Delete(types.GetValidatorKey(address)); err != nil { + if err = k.Validators.Remove(ctx, address); err != nil { return err } @@ -407,7 +385,7 @@ func (k Keeper) GetLastValidators(ctx context.Context) (validators []types.Valid } address := types.AddressFromLastValidatorPowerKey(iterator.Key()) - validator, err := k.GetValidator(ctx, address) + validator, err := k.Validators.Get(ctx, address) if err != nil { return nil, err } @@ -550,7 +528,7 @@ func (k Keeper) UnbondAllMatureValidators(ctx context.Context) error { if err != nil { return err } - val, err := k.GetValidator(ctx, addr) + val, err := k.Validators.Get(ctx, addr) if err != nil { return errorsmod.Wrap(err, "validator in the unbonding queue was not found") } diff --git a/x/staking/keeper/validator_test.go b/x/staking/keeper/validator_test.go index 06bc75f11840..c751ae0be218 100644 --- a/x/staking/keeper/validator_test.go +++ b/x/staking/keeper/validator_test.go @@ -1,11 +1,13 @@ package keeper_test import ( + "fmt" "time" abci "github.com/cometbft/cometbft/abci/types" "github.com/golang/mock/gomock" + "cosmossdk.io/collections" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -37,14 +39,14 @@ func (s *KeeperTestSuite) TestValidator() { require.Equal(stakingtypes.Unbonded, validator.Status) require.Equal(valTokens, validator.Tokens) require.Equal(valTokens, validator.DelegatorShares.RoundInt()) - require.NoError(keeper.SetValidator(ctx, validator)) + require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) require.NoError(keeper.SetValidatorByPowerIndex(ctx, validator)) require.NoError(keeper.SetValidatorByConsAddr(ctx, validator)) // ensure update s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.NotBondedPoolName, stakingtypes.BondedPoolName, gomock.Any()) updates := s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err := keeper.GetValidator(ctx, valAddr) + validator, err := keeper.Validators.Get(ctx, valAddr) require.NoError(err) require.Equal(validator.ABCIValidatorUpdate(keeper.PowerReduction(ctx)), updates[0]) @@ -108,8 +110,8 @@ func (s *KeeperTestSuite) TestValidatorBasics() { require.Equal(keeper.TokensFromConsensusPower(ctx, 7), validators[2].Tokens) // check the empty keeper first - _, err := keeper.GetValidator(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) - require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) + _, err := keeper.Validators.Get(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) + require.ErrorIs(err, collections.ErrNotFound) resVals, err := keeper.GetLastValidators(ctx) require.NoError(err) require.Zero(len(resVals)) @@ -122,7 +124,7 @@ func (s *KeeperTestSuite) TestValidatorBasics() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.NotBondedPoolName, stakingtypes.BondedPoolName, gomock.Any()) validators[0] = stakingkeeper.TestingUpdateValidator(keeper, ctx, validators[0], true) require.NoError(keeper.SetValidatorByConsAddr(ctx, validators[0])) - resVal, err := keeper.GetValidator(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) + resVal, err := keeper.Validators.Get(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) require.NoError(err) require.True(validators[0].MinEqual(&resVal)) @@ -146,7 +148,7 @@ func (s *KeeperTestSuite) TestValidatorBasics() { validators[0].Tokens = keeper.TokensFromConsensusPower(ctx, 10) validators[0].DelegatorShares = math.LegacyNewDecFromInt(validators[0].Tokens) validators[0] = stakingkeeper.TestingUpdateValidator(keeper, ctx, validators[0], true) - resVal, err = keeper.GetValidator(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) + resVal, err = keeper.Validators.Get(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) require.NoError(err) require.True(validators[0].MinEqual(&resVal)) @@ -160,10 +162,10 @@ func (s *KeeperTestSuite) TestValidatorBasics() { validators[1] = stakingkeeper.TestingUpdateValidator(keeper, ctx, validators[1], true) s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.NotBondedPoolName, stakingtypes.BondedPoolName, gomock.Any()) validators[2] = stakingkeeper.TestingUpdateValidator(keeper, ctx, validators[2], true) - resVal, err = keeper.GetValidator(ctx, sdk.ValAddress(PKs[1].Address().Bytes())) + resVal, err = keeper.Validators.Get(ctx, sdk.ValAddress(PKs[1].Address().Bytes())) require.NoError(err) require.True(validators[1].MinEqual(&resVal)) - resVal, err = keeper.GetValidator(ctx, sdk.ValAddress(PKs[2].Address().Bytes())) + resVal, err = keeper.Validators.Get(ctx, sdk.ValAddress(PKs[2].Address().Bytes())) require.NoError(err) require.True(validators[2].MinEqual(&resVal)) @@ -178,14 +180,14 @@ func (s *KeeperTestSuite) TestValidatorBasics() { // shouldn't be able to remove if there are still tokens left validators[1].Status = stakingtypes.Unbonded - require.NoError(keeper.SetValidator(ctx, validators[1])) + require.NoError(keeper.Validators.Set(ctx, validators[1].GetOperator(), validators[1])) require.EqualError(keeper.RemoveValidator(ctx, validators[1].GetOperator()), "attempting to remove a validator which still contains tokens: failed to remove validator") - validators[1].Tokens = math.ZeroInt() // ...remove all tokens - require.NoError(keeper.SetValidator(ctx, validators[1])) // ...set the validator - require.NoError(keeper.RemoveValidator(ctx, validators[1].GetOperator())) // Now it can be removed. - _, err = keeper.GetValidator(ctx, sdk.ValAddress(PKs[1].Address().Bytes())) - require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) + validators[1].Tokens = math.ZeroInt() // ...remove all tokens + require.NoError(keeper.Validators.Set(ctx, validators[1].GetOperator(), validators[1])) // ...set the validator + require.NoError(keeper.RemoveValidator(ctx, validators[1].GetOperator())) // Now it can be removed. + _, err = keeper.Validators.Get(ctx, sdk.ValAddress(PKs[1].Address().Bytes())) + require.ErrorIs(err, collections.ErrNotFound) } func (s *KeeperTestSuite) TestUpdateValidatorByPowerIndex() { @@ -204,7 +206,7 @@ func (s *KeeperTestSuite) TestUpdateValidatorByPowerIndex() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.NotBondedPoolName, stakingtypes.BondedPoolName, gomock.Any()) stakingkeeper.TestingUpdateValidator(keeper, ctx, validator, true) - validator, err := keeper.GetValidator(ctx, valAddr) + validator, err := keeper.Validators.Get(ctx, valAddr) require.NoError(err) require.Equal(valTokens, validator.Tokens) @@ -218,7 +220,7 @@ func (s *KeeperTestSuite) TestUpdateValidatorByPowerIndex() { stakingkeeper.TestingUpdateValidator(keeper, ctx, validator, true) // update the validator, possibly kicking it out require.False(stakingkeeper.ValidatorByPowerIndexExists(ctx, keeper, power)) - validator, err = keeper.GetValidator(ctx, valAddr) + validator, err = keeper.Validators.Get(ctx, valAddr) require.NoError(err) power = stakingtypes.GetValidatorsByPowerIndexKey(validator, keeper.PowerReduction(ctx)) @@ -297,8 +299,8 @@ func (s *KeeperTestSuite) TestUpdateValidatorCommission() { val1, _ = val1.SetInitialCommission(commission1) val2, _ = val2.SetInitialCommission(commission2) - require.NoError(keeper.SetValidator(ctx, val1)) - require.NoError(keeper.SetValidator(ctx, val2)) + require.NoError(keeper.Validators.Set(ctx, val1.GetOperator(), val1)) + require.NoError(keeper.Validators.Set(ctx, val2.GetOperator(), val2)) testCases := []struct { validator stakingtypes.Validator @@ -324,10 +326,10 @@ func (s *KeeperTestSuite) TestUpdateValidatorCommission() { ) tc.validator.Commission = commission - err = keeper.SetValidator(ctx, tc.validator) + err = keeper.Validators.Set(ctx, tc.validator.GetOperator(), tc.validator) require.NoError(err) - val, err := keeper.GetValidator(ctx, tc.validator.GetOperator()) + val, err := keeper.Validators.Get(ctx, tc.validator.GetOperator()) require.NoError(err, "expected to find validator for test case #%d with rate: %s", i, tc.newRate, ) @@ -355,18 +357,18 @@ func (s *KeeperTestSuite) TestValidatorToken() { validator, _, err := keeper.AddValidatorTokensAndShares(ctx, validator, addTokens) require.NoError(err) require.Equal(addTokens, validator.Tokens) - validator, _ = keeper.GetValidator(ctx, valAddr) + validator, _ = keeper.Validators.Get(ctx, valAddr) require.Equal(math.LegacyNewDecFromInt(addTokens), validator.DelegatorShares) _, _, err = keeper.RemoveValidatorTokensAndShares(ctx, validator, math.LegacyNewDecFromInt(delTokens)) require.NoError(err) - validator, _ = keeper.GetValidator(ctx, valAddr) + validator, _ = keeper.Validators.Get(ctx, valAddr) require.Equal(delTokens, validator.Tokens) require.True(validator.DelegatorShares.Equal(math.LegacyNewDecFromInt(delTokens))) _, err = keeper.RemoveValidatorTokens(ctx, validator, delTokens) require.NoError(err) - validator, _ = keeper.GetValidator(ctx, valAddr) + validator, _ = keeper.Validators.Get(ctx, valAddr) require.True(validator.Tokens.IsZero()) } @@ -410,27 +412,27 @@ func (s *KeeperTestSuite) TestUnbondingValidator() { // check unbonding mature validators ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime) err = keeper.UnbondAllMatureValidators(ctx) - require.EqualError(err, "validator in the unbonding queue was not found: validator does not exist") + require.ErrorContains(err, fmt.Sprintf("validator in the unbonding queue was not found: %v", collections.ErrNotFound)) - require.NoError(keeper.SetValidator(ctx, validator)) + require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime) err = keeper.UnbondAllMatureValidators(ctx) require.EqualError(err, "unexpected validator in unbonding queue; status was not unbonding") validator.Status = stakingtypes.Unbonding - require.NoError(keeper.SetValidator(ctx, validator)) + require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) require.NoError(keeper.UnbondAllMatureValidators(ctx)) - validator, err = keeper.GetValidator(ctx, valAddr) - require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) + validator, err = keeper.Validators.Get(ctx, valAddr) + require.ErrorIs(err, collections.ErrNotFound) require.NoError(keeper.SetUnbondingValidatorsQueue(ctx, endTime, endHeight, []string{valAddr.String()})) validator = testutil.NewValidator(s.T(), valAddr, valPubKey) validator, _ = validator.AddTokensFromDel(addTokens) validator.Status = stakingtypes.Unbonding - require.NoError(keeper.SetValidator(ctx, validator)) + require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) require.NoError(keeper.UnbondAllMatureValidators(ctx)) - validator, err = keeper.GetValidator(ctx, valAddr) + validator, err = keeper.Validators.Get(ctx, valAddr) require.NoError(err) require.Equal(stakingtypes.Unbonded, validator.Status) } diff --git a/x/staking/migrations/v2/store_test.go b/x/staking/migrations/v2/store_test.go index 20c55e5bfcbb..489a57b33395 100644 --- a/x/staking/migrations/v2/store_test.go +++ b/x/staking/migrations/v2/store_test.go @@ -12,6 +12,7 @@ import ( sdktestuil "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/address" v1 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v1" v2 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v2" "github.com/cosmos/cosmos-sdk/x/staking/testutil" @@ -54,7 +55,7 @@ func TestStoreMigration(t *testing.T) { { "ValidatorsKey", v1.GetValidatorKey(valAddr1), - types.GetValidatorKey(valAddr1), + append(types.ValidatorsKey, address.MustLengthPrefix(valAddr1)...), }, { "ValidatorsByConsAddrKey", diff --git a/x/staking/simulation/operations.go b/x/staking/simulation/operations.go index e5a15c6d409f..a6feaff656ca 100644 --- a/x/staking/simulation/operations.go +++ b/x/staking/simulation/operations.go @@ -120,7 +120,7 @@ func SimulateMsgCreateValidator( address := sdk.ValAddress(simAccount.Address) // ensure the validator doesn't exist already - _, err := k.GetValidator(ctx, address) + _, err := k.Validators.Get(ctx, address) if err == nil { return simtypes.NoOpMsg(types.ModuleName, msgType, "validator already exists"), nil, nil } diff --git a/x/staking/testutil/helpers.go b/x/staking/testutil/helpers.go index 7ed64e46131b..5cd524ee96c9 100644 --- a/x/staking/testutil/helpers.go +++ b/x/staking/testutil/helpers.go @@ -110,7 +110,7 @@ func (sh *Helper) Undelegate(delegator sdk.AccAddress, val sdk.ValAddress, amoun // CheckValidator asserts that a validor exists and has a given status (if status!="") // and if has a right jailed flag. func (sh *Helper) CheckValidator(addr sdk.ValAddress, status stakingtypes.BondStatus, jailed bool) stakingtypes.Validator { - v, err := sh.k.GetValidator(sh.Ctx, addr) + v, err := sh.k.Validators.Get(sh.Ctx, addr) require.NoError(sh.t, err) require.Equal(sh.t, jailed, v.Jailed, "wrong Jalied status") if status >= 0 { diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 57689058c9ec..1d29580b6e7b 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -34,9 +34,9 @@ var ( LastValidatorPowerKey = []byte{0x11} // prefix for each key to a validator index, for bonded validators LastTotalPowerKey = collections.NewPrefix(18) // prefix for the total power - ValidatorsKey = []byte{0x21} // prefix for each key to a validator - ValidatorsByConsAddrKey = []byte{0x22} // prefix for each key to a validator index, by pubkey - ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power + ValidatorsKey = collections.NewPrefix(33) // prefix for each key to a validator + ValidatorsByConsAddrKey = []byte{0x22} // prefix for each key to a validator index, by pubkey + ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power DelegationKey = []byte{0x31} // key for a delegation UnbondingDelegationKey = []byte{0x32} // key for an unbonding-delegation From 37d1e02597291e84bc72f599ca6cda694819665b Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Tue, 8 Aug 2023 12:22:46 +0530 Subject: [PATCH 02/18] Revert "migrate Validators to use collections" This reverts commit 853f7788bcaf71b84240d9304e13c103fedd83d1. --- simapp/export.go | 4 +- .../auth/migrations/v2/store_test.go | 4 +- .../distribution/keeper/grpc_query_test.go | 2 +- .../distribution/keeper/msg_server_test.go | 2 +- .../evidence/keeper/infraction_test.go | 2 +- tests/integration/gov/keeper/common_test.go | 6 +- tests/integration/gov/keeper/tally_test.go | 18 ++--- .../integration/staking/keeper/common_test.go | 4 +- .../staking/keeper/determinstic_test.go | 2 +- .../staking/keeper/genesis_test.go | 6 +- .../staking/keeper/msg_server_test.go | 2 +- .../integration/staking/keeper/slash_test.go | 10 +-- .../staking/keeper/unbonding_test.go | 16 ++-- .../staking/keeper/validator_bench_test.go | 8 +- .../staking/keeper/validator_test.go | 80 +++++++++---------- .../staking/simulation/operations_test.go | 2 +- x/distribution/simulation/operations_test.go | 2 +- x/slashing/app_test.go | 2 +- x/slashing/simulation/operations_test.go | 2 +- x/staking/app_test.go | 4 +- x/staking/keeper/alias_functions.go | 4 +- x/staking/keeper/delegation.go | 14 ++-- x/staking/keeper/delegation_test.go | 27 +++---- x/staking/keeper/genesis.go | 4 +- x/staking/keeper/grpc_query.go | 8 +- x/staking/keeper/grpc_query_test.go | 2 +- x/staking/keeper/historical_info_test.go | 4 +- x/staking/keeper/invariants.go | 2 +- x/staking/keeper/keeper.go | 2 - x/staking/keeper/msg_server.go | 12 +-- x/staking/keeper/msg_server_test.go | 10 +-- x/staking/keeper/query_utils.go | 4 +- x/staking/keeper/slash.go | 2 +- x/staking/keeper/slash_test.go | 10 +-- x/staking/keeper/test_common.go | 4 +- x/staking/keeper/unbonding.go | 6 +- x/staking/keeper/unbonding_test.go | 4 +- x/staking/keeper/val_state_change.go | 10 +-- x/staking/keeper/validator.go | 40 +++++++--- x/staking/keeper/validator_test.go | 62 +++++++------- x/staking/migrations/v2/store_test.go | 3 +- x/staking/simulation/operations.go | 2 +- x/staking/testutil/helpers.go | 2 +- x/staking/types/keys.go | 6 +- 44 files changed, 219 insertions(+), 203 deletions(-) diff --git a/simapp/export.go b/simapp/export.go index b9fb8707dd02..6f611a130e7a 100644 --- a/simapp/export.go +++ b/simapp/export.go @@ -203,7 +203,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [] for ; iter.Valid(); iter.Next() { addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key())) - validator, err := app.StakingKeeper.Validators.Get(ctx, addr) + validator, err := app.StakingKeeper.GetValidator(ctx, addr) if err != nil { panic("expected validator, not found") } @@ -213,7 +213,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [] validator.Jailed = true } - if err = app.StakingKeeper.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { + if err = app.StakingKeeper.SetValidator(ctx, validator); err != nil { panic(err) } counter++ diff --git a/tests/integration/auth/migrations/v2/store_test.go b/tests/integration/auth/migrations/v2/store_test.go index c649aab1a807..ca25f97ecd7b 100644 --- a/tests/integration/auth/migrations/v2/store_test.go +++ b/tests/integration/auth/migrations/v2/store_test.go @@ -669,7 +669,7 @@ func TestMigrateVestingAccounts(t *testing.T) { delegatorAddr := addrs[0] _, valAddr := createValidator(t, ctx, bankKeeper, stakingKeeper, tc.tokenAmount*2) - validator, err := stakingKeeper.Validators.Get(ctx, valAddr) + validator, err := stakingKeeper.GetValidator(ctx, valAddr) require.NoError(t, err) tc.prepareFunc(ctx, validator, delegatorAddr) @@ -779,7 +779,7 @@ func createValidator(t *testing.T, ctx sdk.Context, bankKeeper bankkeeper.Keeper val1, err := stakingtypes.NewValidator(valAddrs[0], pks[0], stakingtypes.Description{}) require.NoError(t, err) - require.NoError(t, stakingKeeper.Validators.Set(ctx, val1.GetOperator(), val1)) + require.NoError(t, stakingKeeper.SetValidator(ctx, val1)) require.NoError(t, stakingKeeper.SetValidatorByConsAddr(ctx, val1)) require.NoError(t, stakingKeeper.SetNewValidatorByPowerIndex(ctx, val1)) diff --git a/tests/integration/distribution/keeper/grpc_query_test.go b/tests/integration/distribution/keeper/grpc_query_test.go index 0c663cc9bbca..cebe57eac71e 100644 --- a/tests/integration/distribution/keeper/grpc_query_test.go +++ b/tests/integration/distribution/keeper/grpc_query_test.go @@ -503,7 +503,7 @@ func TestGRPCDelegationRewards(t *testing.T) { tstaking.Commission = stakingtypes.NewCommissionRates(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) tstaking.CreateValidator(f.valAddr, valConsPk0, math.NewInt(initialStake), true) - val, found := f.stakingKeeper.Validators.Get(f.sdkCtx, f.valAddr) + val, found := f.stakingKeeper.GetValidator(f.sdkCtx, f.valAddr) assert.Assert(t, found) // setup delegation diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index 911bc4d5d9de..4469b1404b21 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -181,7 +181,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { assert.NilError(t, err) validator.DelegatorShares = math.LegacyNewDec(100) validator.Tokens = math.NewInt(1000000) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator)) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validator)) // set module account coins initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000)) diff --git a/tests/integration/evidence/keeper/infraction_test.go b/tests/integration/evidence/keeper/infraction_test.go index e1d66a53968c..f716cf4f0101 100644 --- a/tests/integration/evidence/keeper/infraction_test.go +++ b/tests/integration/evidence/keeper/infraction_test.go @@ -244,7 +244,7 @@ func TestHandleDoubleSign(t *testing.T) { // require we be able to unbond now ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) del, _ := f.stakingKeeper.GetDelegation(ctx, sdk.AccAddress(operatorAddr), operatorAddr) - validator, _ := f.stakingKeeper.Validators.Get(ctx, operatorAddr) + validator, _ := f.stakingKeeper.GetValidator(ctx, operatorAddr) totalBond := validator.TokensFromShares(del.GetShares()).TruncateInt() tstaking.Ctx = ctx tstaking.Denom = stakingParams.BondDenom diff --git a/tests/integration/gov/keeper/common_test.go b/tests/integration/gov/keeper/common_test.go index fcd17dd53415..c642ff1f1798 100644 --- a/tests/integration/gov/keeper/common_test.go +++ b/tests/integration/gov/keeper/common_test.go @@ -48,9 +48,9 @@ func createValidators(t *testing.T, f *fixture, powers []int64) ([]sdk.AccAddres val3, err := stakingtypes.NewValidator(valAddrs[2], pks[2], stakingtypes.Description{}) assert.NilError(t, err) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.ctx, val1.GetOperator(), val1)) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.ctx, val2.GetOperator(), val2)) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.ctx, val3.GetOperator(), val3)) + assert.NilError(t, f.stakingKeeper.SetValidator(f.ctx, val1)) + assert.NilError(t, f.stakingKeeper.SetValidator(f.ctx, val2)) + assert.NilError(t, f.stakingKeeper.SetValidator(f.ctx, val3)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.ctx, val1)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.ctx, val2)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.ctx, val3)) diff --git a/tests/integration/gov/keeper/tally_test.go b/tests/integration/gov/keeper/tally_test.go index 9eaa0f12a679..a30ff271846c 100644 --- a/tests/integration/gov/keeper/tally_test.go +++ b/tests/integration/gov/keeper/tally_test.go @@ -275,7 +275,7 @@ func TestTallyDelgatorOverride(t *testing.T) { addrs, valAddrs := createValidators(t, f, []int64{5, 6, 7}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 30) - val1, found := f.stakingKeeper.Validators.Get(ctx, valAddrs[0]) + val1, found := f.stakingKeeper.GetValidator(ctx, valAddrs[0]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[4], delTokens, stakingtypes.Unbonded, val1, true) @@ -314,7 +314,7 @@ func TestTallyDelgatorInherit(t *testing.T) { addrs, vals := createValidators(t, f, []int64{5, 6, 7}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 30) - val3, found := f.stakingKeeper.Validators.Get(ctx, vals[2]) + val3, found := f.stakingKeeper.GetValidator(ctx, vals[2]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val3, true) @@ -352,9 +352,9 @@ func TestTallyDelgatorMultipleOverride(t *testing.T) { addrs, vals := createValidators(t, f, []int64{5, 6, 7}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 10) - val1, found := f.stakingKeeper.Validators.Get(ctx, vals[0]) + val1, found := f.stakingKeeper.GetValidator(ctx, vals[0]) assert.Assert(t, found) - val2, found := f.stakingKeeper.Validators.Get(ctx, vals[1]) + val2, found := f.stakingKeeper.GetValidator(ctx, vals[1]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val1, true) @@ -397,9 +397,9 @@ func TestTallyDelgatorMultipleInherit(t *testing.T) { addrs, vals := createValidators(t, f, []int64{5, 6, 7}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 10) - val2, found := f.stakingKeeper.Validators.Get(ctx, vals[1]) + val2, found := f.stakingKeeper.GetValidator(ctx, vals[1]) assert.Assert(t, found) - val3, found := f.stakingKeeper.Validators.Get(ctx, vals[2]) + val3, found := f.stakingKeeper.GetValidator(ctx, vals[2]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val2, true) @@ -439,9 +439,9 @@ func TestTallyJailedValidator(t *testing.T) { addrs, valAddrs := createValidators(t, f, []int64{25, 6, 7}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 10) - val2, found := f.stakingKeeper.Validators.Get(ctx, valAddrs[1]) + val2, found := f.stakingKeeper.GetValidator(ctx, valAddrs[1]) assert.Assert(t, found) - val3, found := f.stakingKeeper.Validators.Get(ctx, valAddrs[2]) + val3, found := f.stakingKeeper.GetValidator(ctx, valAddrs[2]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val2, true) @@ -485,7 +485,7 @@ func TestTallyValidatorMultipleDelegations(t *testing.T) { addrs, valAddrs := createValidators(t, f, []int64{10, 10, 10}) delTokens := f.stakingKeeper.TokensFromConsensusPower(ctx, 10) - val2, found := f.stakingKeeper.Validators.Get(ctx, valAddrs[1]) + val2, found := f.stakingKeeper.GetValidator(ctx, valAddrs[1]) assert.Assert(t, found) _, err := f.stakingKeeper.Delegate(ctx, addrs[0], delTokens, stakingtypes.Unbonded, val2, true) diff --git a/tests/integration/staking/keeper/common_test.go b/tests/integration/staking/keeper/common_test.go index bc0ae4fa1c5e..1645fe79686b 100644 --- a/tests/integration/staking/keeper/common_test.go +++ b/tests/integration/staking/keeper/common_test.go @@ -75,8 +75,8 @@ func createValidators(t *testing.T, f *fixture, powers []int64) ([]sdk.AccAddres val2 := testutil.NewValidator(t, valAddrs[1], pks[1]) vals := []types.Validator{val1, val2} - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, val1.GetOperator(), val1)) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, val2.GetOperator(), val2)) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, val1)) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, val2)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.sdkCtx, val1)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.sdkCtx, val2)) assert.NilError(t, f.stakingKeeper.SetNewValidatorByPowerIndex(f.sdkCtx, val1)) diff --git a/tests/integration/staking/keeper/determinstic_test.go b/tests/integration/staking/keeper/determinstic_test.go index 049d96dc6250..85c6ff94e506 100644 --- a/tests/integration/staking/keeper/determinstic_test.go +++ b/tests/integration/staking/keeper/determinstic_test.go @@ -234,7 +234,7 @@ func createAndSetValidator(t *testing.T, rt *rapid.T, f *deterministicFixture) s func setValidator(t *testing.T, f *deterministicFixture, validator stakingtypes.Validator) { t.Helper() - assert.NilError(t, f.stakingKeeper.Validators.Set(f.ctx, validator.GetOperator(), validator)) + assert.NilError(t, f.stakingKeeper.SetValidator(f.ctx, validator)) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.ctx, validator)) assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.ctx, validator)) assert.NilError(t, f.stakingKeeper.Hooks().AfterValidatorCreated(f.ctx, validator.GetOperator())) diff --git a/tests/integration/staking/keeper/genesis_test.go b/tests/integration/staking/keeper/genesis_test.go index 683779078cc0..8884318b02df 100644 --- a/tests/integration/staking/keeper/genesis_test.go +++ b/tests/integration/staking/keeper/genesis_test.go @@ -42,7 +42,7 @@ func TestInitGenesis(t *testing.T) { DelegatorShares: math.LegacyNewDecFromInt(valTokens), Description: types.NewDescription("hoop", "", "", "", ""), } - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, bondedVal.GetOperator(), bondedVal)) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, bondedVal)) params, err := f.stakingKeeper.GetParams(f.sdkCtx) assert.NilError(t, err) @@ -117,11 +117,11 @@ func TestInitGenesis(t *testing.T) { } // now make sure the validators are bonded and intra-tx counters are correct - resVal, found := (f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[1]))) + resVal, found := (f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[1]))) assert.Assert(t, found) assert.Equal(t, types.Bonded, resVal.Status) - resVal, found = (f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[2]))) + resVal, found = (f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[2]))) assert.Assert(t, found) assert.Equal(t, types.Bonded, resVal.Status) diff --git a/tests/integration/staking/keeper/msg_server_test.go b/tests/integration/staking/keeper/msg_server_test.go index c4a9263dd66b..9edcc02d96ce 100644 --- a/tests/integration/staking/keeper/msg_server_test.go +++ b/tests/integration/staking/keeper/msg_server_test.go @@ -43,7 +43,7 @@ func TestCancelUnbondingDelegation(t *testing.T) { validator, err := types.NewValidator(valAddr, PKs[0], types.NewDescription("Validator", "", "", "", "")) validator.Status = types.Bonded assert.NilError(t, err) - assert.NilError(t, f.stakingKeeper.Validators.Set(ctx, validator.GetOperator(), validator)) + assert.NilError(t, f.stakingKeeper.SetValidator(ctx, validator)) validatorAddr, err := sdk.ValAddressFromBech32(validator.OperatorAddress) assert.NilError(t, err) diff --git a/tests/integration/staking/keeper/slash_test.go b/tests/integration/staking/keeper/slash_test.go index d1fd5b60864c..4b0bd47e9fdf 100644 --- a/tests/integration/staking/keeper/slash_test.go +++ b/tests/integration/staking/keeper/slash_test.go @@ -130,7 +130,7 @@ func TestSlashRedelegation(t *testing.T) { assert.NilError(t, f.stakingKeeper.SetDelegation(f.sdkCtx, del)) // started redelegating prior to the current height, stake didn't contribute to infraction - validator, found := f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) + validator, found := f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) assert.Assert(t, found) slashAmount, err := f.stakingKeeper.SlashRedelegation(f.sdkCtx, validator, rd, 1, fraction) assert.NilError(t, err) @@ -139,7 +139,7 @@ func TestSlashRedelegation(t *testing.T) { // after the expiration time, no longer eligible for slashing f.sdkCtx = f.sdkCtx.WithBlockHeader(cmtproto.Header{Time: time.Unix(10, 0)}) assert.NilError(t, f.stakingKeeper.SetRedelegation(f.sdkCtx, rd)) - validator, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) + validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) assert.Assert(t, found) slashAmount, err = f.stakingKeeper.SlashRedelegation(f.sdkCtx, validator, rd, 0, fraction) assert.NilError(t, err) @@ -150,7 +150,7 @@ func TestSlashRedelegation(t *testing.T) { // test valid slash, before expiration timestamp and to which stake contributed f.sdkCtx = f.sdkCtx.WithBlockHeader(cmtproto.Header{Time: time.Unix(0, 0)}) assert.NilError(t, f.stakingKeeper.SetRedelegation(f.sdkCtx, rd)) - validator, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) + validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) assert.Assert(t, found) slashAmount, err = f.stakingKeeper.SlashRedelegation(f.sdkCtx, validator, rd, 0, fraction) assert.NilError(t, err) @@ -197,7 +197,7 @@ func TestSlashAtNegativeHeight(t *testing.T) { // end block applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1) - validator, found = f.stakingKeeper.Validators.Get(f.sdkCtx, validator.GetOperator()) + validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, validator.GetOperator()) assert.Assert(t, found) // power decreased assert.Equal(t, int64(5), validator.GetConsensusPower(f.stakingKeeper.PowerReduction(f.sdkCtx))) @@ -235,7 +235,7 @@ func TestSlashValidatorAtCurrentHeight(t *testing.T) { // end block applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1) - validator, found = f.stakingKeeper.Validators.Get(f.sdkCtx, validator.GetOperator()) + validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, validator.GetOperator()) assert.Assert(t, found) // power decreased assert.Equal(t, int64(5), validator.GetConsensusPower(f.stakingKeeper.PowerReduction(f.sdkCtx))) diff --git a/tests/integration/staking/keeper/unbonding_test.go b/tests/integration/staking/keeper/unbonding_test.go index 178470fedabd..9ed6aab2fcab 100644 --- a/tests/integration/staking/keeper/unbonding_test.go +++ b/tests/integration/staking/keeper/unbonding_test.go @@ -156,7 +156,7 @@ func doValidatorUnbonding( hookCalled *bool, ) (validator types.Validator) { t.Helper() - validator, found := stakingKeeper.Validators.Get(ctx, addrVal) + validator, found := stakingKeeper.GetValidator(ctx, addrVal) assert.Assert(t, found) // Check that status is bonded assert.Equal(t, types.BondStatus(3), validator.Status) @@ -198,7 +198,7 @@ func TestValidatorUnbondingOnHold1(t *testing.T) { assert.NilError(t, f.stakingKeeper.UnbondAllMatureValidators(f.sdkCtx)) // Check that validator unbonding is not complete (is not mature yet) - validator, found := f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[0]) + validator, found := f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[0]) assert.Assert(t, found) assert.Equal(t, types.Unbonding, validator.Status) unbondingVals, err := f.stakingKeeper.GetUnbondingValidators(f.sdkCtx, completionTime, completionHeight) @@ -212,7 +212,7 @@ func TestValidatorUnbondingOnHold1(t *testing.T) { assert.NilError(t, f.stakingKeeper.UnbondAllMatureValidators(f.sdkCtx)) // Check that validator unbonding is complete - validator, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[0]) + validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[0]) assert.Assert(t, found) assert.Equal(t, types.Unbonded, validator.Status) unbondingVals, err = f.stakingKeeper.GetUnbondingValidators(f.sdkCtx, completionTime, completionHeight) @@ -258,10 +258,10 @@ func TestValidatorUnbondingOnHold2(t *testing.T) { assert.NilError(t, f.stakingKeeper.UnbondAllMatureValidators(f.sdkCtx)) // Check that unbonding is not complete for both validators - validator1, found := f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[0]) + validator1, found := f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[0]) assert.Assert(t, found) assert.Equal(t, types.Unbonding, validator1.Status) - validator2, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) + validator2, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) assert.Assert(t, found) assert.Equal(t, types.Unbonding, validator2.Status) unbondingVals, err := f.stakingKeeper.GetUnbondingValidators(f.sdkCtx, completionTime, completionHeight) @@ -278,10 +278,10 @@ func TestValidatorUnbondingOnHold2(t *testing.T) { assert.NilError(t, f.stakingKeeper.UnbondAllMatureValidators(f.sdkCtx)) // Check that unbonding is complete for validator1, but not for validator2 - validator1, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[0]) + validator1, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[0]) assert.Assert(t, found) assert.Equal(t, types.Unbonded, validator1.Status) - validator2, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) + validator2, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) assert.Assert(t, found) assert.Equal(t, types.Unbonding, validator2.Status) unbondingVals, err = f.stakingKeeper.GetUnbondingValidators(f.sdkCtx, completionTime, completionHeight) @@ -297,7 +297,7 @@ func TestValidatorUnbondingOnHold2(t *testing.T) { assert.NilError(t, f.stakingKeeper.UnbondAllMatureValidators(f.sdkCtx)) // Check that unbonding is complete for validator2 - validator2, found = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[1]) + validator2, found = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[1]) assert.Assert(t, found) assert.Equal(t, types.Unbonded, validator2.Status) unbondingVals, err = f.stakingKeeper.GetUnbondingValidators(f.sdkCtx, completionTime, completionHeight) diff --git a/tests/integration/staking/keeper/validator_bench_test.go b/tests/integration/staking/keeper/validator_bench_test.go index dbac3170d7d9..06bca8ee875d 100644 --- a/tests/integration/staking/keeper/validator_bench_test.go +++ b/tests/integration/staking/keeper/validator_bench_test.go @@ -28,7 +28,7 @@ func BenchmarkGetValidator(b *testing.B) { f, _, valAddrs, vals := initValidators(b, totalPower, len(powers), powers) for _, validator := range vals { - if err := f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator); err != nil { + if err := f.stakingKeeper.SetValidator(f.sdkCtx, validator); err != nil { panic(err) } } @@ -36,7 +36,7 @@ func BenchmarkGetValidator(b *testing.B) { b.ResetTimer() for n := 0; n < b.N; n++ { for _, addr := range valAddrs { - _, _ = f.stakingKeeper.Validators.Get(f.sdkCtx, addr) + _, _ = f.stakingKeeper.GetValidator(f.sdkCtx, addr) } } } @@ -53,7 +53,7 @@ func BenchmarkGetValidatorDelegations(b *testing.B) { f, _, valAddrs, vals := initValidators(b, totalPower, len(powers), powers) for _, validator := range vals { - if err := f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator); err != nil { + if err := f.stakingKeeper.SetValidator(f.sdkCtx, validator); err != nil { panic(err) } } @@ -94,7 +94,7 @@ func BenchmarkGetValidatorDelegationsLegacy(b *testing.B) { f, _, valAddrs, vals := initValidators(b, totalPower, len(powers), powers) for _, validator := range vals { - if err := f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator); err != nil { + if err := f.stakingKeeper.SetValidator(f.sdkCtx, validator); err != nil { panic(err) } } diff --git a/tests/integration/staking/keeper/validator_test.go b/tests/integration/staking/keeper/validator_test.go index 50b5f91d6270..11bdf0824684 100644 --- a/tests/integration/staking/keeper/validator_test.go +++ b/tests/integration/staking/keeper/validator_test.go @@ -117,7 +117,7 @@ func TestUpdateBondedValidatorsDecreaseCliff(t *testing.T) { valAddr := validators[valIdx].OperatorAddress addr, err := sdk.ValAddressFromBech32(valAddr) assert.NilError(t, err) - val, _ := f.stakingKeeper.Validators.Get(f.sdkCtx, addr) + val, _ := f.stakingKeeper.GetValidator(f.sdkCtx, addr) assert.Equal( t, status, val.GetStatus(), @@ -154,7 +154,7 @@ func TestSlashToZeroPowerRemoved(t *testing.T) { // apply TM updates applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, -1) // validator should be unbonding - validator, _ = f.stakingKeeper.Validators.Get(f.sdkCtx, addrVals[0]) + validator, _ = f.stakingKeeper.GetValidator(f.sdkCtx, addrVals[0]) assert.Equal(t, validator.GetStatus(), types.Unbonding) } @@ -285,15 +285,15 @@ func TestGetValidatorSortingMixed(t *testing.T) { keeper.TestingUpdateValidator(f.stakingKeeper, f.sdkCtx, validators[i], true) } - val0, found := f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[0])) + val0, found := f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[0])) assert.Assert(t, found) - val1, found := f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[1])) + val1, found := f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[1])) assert.Assert(t, found) - val2, found := f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[2])) + val2, found := f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[2])) assert.Assert(t, found) - val3, found := f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[3])) + val3, found := f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[3])) assert.Assert(t, found) - val4, found := f.stakingKeeper.Validators.Get(f.sdkCtx, sdk.ValAddress(addrs[4])) + val4, found := f.stakingKeeper.GetValidator(f.sdkCtx, sdk.ValAddress(addrs[4])) assert.Assert(t, found) assert.Equal(t, types.Bonded, val0.Status) assert.Equal(t, types.Unbonding, val1.Status) @@ -378,7 +378,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) { // validator 3 enters bonded validator set f.sdkCtx = f.sdkCtx.WithBlockHeight(40) - validators[3], err = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[3].GetOperator()) + validators[3], err = f.stakingKeeper.GetValidator(f.sdkCtx, validators[3].GetOperator()) assert.NilError(t, err) assert.NilError(t, f.stakingKeeper.DeleteValidatorByPowerIndex(f.sdkCtx, validators[3])) validators[3], _ = validators[3].AddTokensFromDel(f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 1)) @@ -425,7 +425,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) { assert.Equal(t, nMax, uint32(len(resValidators))) assert.Assert(ValEq(t, validators[0], resValidators[0])) assert.Assert(ValEq(t, validators[2], resValidators[1])) - _, exists := f.stakingKeeper.Validators.Get(f.sdkCtx, validators[3].GetOperator()) + _, exists := f.stakingKeeper.GetValidator(f.sdkCtx, validators[3].GetOperator()) assert.Assert(t, exists) } @@ -496,7 +496,7 @@ func TestFullValidatorSetPowerChange(t *testing.T) { keeper.TestingUpdateValidator(f.stakingKeeper, f.sdkCtx, validators[i], true) } for i := range powers { - validators[i], err = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[i].GetOperator()) + validators[i], err = f.stakingKeeper.GetValidator(f.sdkCtx, validators[i].GetOperator()) assert.NilError(t, err) } assert.Equal(t, types.Unbonded, validators[0].Status) @@ -539,14 +539,14 @@ func TestApplyAndReturnValidatorSetUpdatesAllNone(t *testing.T) { // test from nothing to something // tendermintUpdate set: {} -> {c1, c3} applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 0) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[0].GetOperator(), validators[0])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[0])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[0])) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[1].GetOperator(), validators[1])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[1])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[1])) updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 2) - validators[0], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[0].GetOperator()) - validators[1], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[1].GetOperator()) + validators[0], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[0].GetOperator()) + validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[1].GetOperator()) assert.DeepEqual(t, validators[0].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1]) assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) } @@ -633,26 +633,26 @@ func TestApplyAndReturnValidatorSetUpdatesInserted(t *testing.T) { // test validtor added at the beginning // tendermintUpdate set: {} -> {c0} - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[2].GetOperator(), validators[2])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[2])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[2])) updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1) - validators[2], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[2].GetOperator()) + validators[2], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[2].GetOperator()) assert.DeepEqual(t, validators[2].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) // test validtor added at the beginning // tendermintUpdate set: {} -> {c0} - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[3].GetOperator(), validators[3])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[3])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[3])) updates = applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1) - validators[3], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[3].GetOperator()) + validators[3], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[3].GetOperator()) assert.DeepEqual(t, validators[3].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) // test validtor added at the end // tendermintUpdate set: {} -> {c0} - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[4].GetOperator(), validators[4])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[4])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[4])) updates = applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1) - validators[4], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[4].GetOperator()) + validators[4], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[4].GetOperator()) assert.DeepEqual(t, validators[4].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) } @@ -684,10 +684,10 @@ func TestApplyAndReturnValidatorSetUpdatesWithCliffValidator(t *testing.T) { tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 10) validators[2], _ = validators[2].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[2].GetOperator(), validators[2])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[2])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[2])) updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 2) - validators[2], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[2].GetOperator()) + validators[2], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[2].GetOperator()) assert.DeepEqual(t, validators[0].ABCIValidatorUpdateZero(), updates[1]) assert.DeepEqual(t, validators[2].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) } @@ -712,14 +712,14 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, power) validators[i], _ = validators[i].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[i].GetOperator(), validators[i])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[i])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[i])) } // verify initial CometBFT updates are correct updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, len(validators)) - validators[0], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[0].GetOperator()) - validators[1], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[1].GetOperator()) + validators[0], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[0].GetOperator()) + validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[1].GetOperator()) assert.DeepEqual(t, validators[0].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1]) @@ -732,7 +732,7 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, power) validators[i], _ = validators[i].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[i].GetOperator(), validators[i])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[i])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[i])) } @@ -745,10 +745,10 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { validator := testutil.NewValidator(t, valAddr, valPubKey) validator, _ = validator.AddTokensFromDel(amt) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator)) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validator)) validator, _ = validator.RemoveDelShares(math.LegacyNewDecFromInt(amt)) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator)) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validator)) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validator)) // add a new validator that increases in power @@ -758,14 +758,14 @@ func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) { validator = testutil.NewValidator(t, valAddr, valPubKey) tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 500) validator, _ = validator.AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validator.GetOperator(), validator)) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validator)) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validator)) // verify initial CometBFT updates are correct updates = applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, len(validators)+1) - validator, _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validator.GetOperator()) - validators[0], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[0].GetOperator()) - validators[1], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[1].GetOperator()) + validator, _ = f.stakingKeeper.GetValidator(f.sdkCtx, validator.GetOperator()) + validators[0], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[0].GetOperator()) + validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[1].GetOperator()) assert.DeepEqual(t, validator.ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) assert.DeepEqual(t, validators[0].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1]) assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[2]) @@ -791,14 +791,14 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) { validators[i] = newMonikerValidator(t, valAddr, valPubKey, moniker) tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, power) validators[i], _ = validators[i].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[i].GetOperator(), validators[i])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[i])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[i])) } // verify initial CometBFT updates are correct updates := applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 2) - validators[2], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[2].GetOperator()) - validators[1], _ = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[1].GetOperator()) + validators[2], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[2].GetOperator()) + validators[1], _ = f.stakingKeeper.GetValidator(f.sdkCtx, validators[1].GetOperator()) assert.DeepEqual(t, validators[2].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[0]) assert.DeepEqual(t, validators[1].ABCIValidatorUpdate(f.stakingKeeper.PowerReduction(f.sdkCtx)), updates[1]) @@ -807,13 +807,13 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) { // delegate to validator with lowest power but not enough to bond f.sdkCtx = f.sdkCtx.WithBlockHeight(1) - validators[0], err = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[0].GetOperator()) + validators[0], err = f.stakingKeeper.GetValidator(f.sdkCtx, validators[0].GetOperator()) assert.NilError(t, err) assert.NilError(t, f.stakingKeeper.DeleteValidatorByPowerIndex(f.sdkCtx, validators[0])) tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 1) validators[0], _ = validators[0].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[0].GetOperator(), validators[0])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[0])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[0])) // verify initial CometBFT updates are correct @@ -823,19 +823,19 @@ func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) { // lowest power in a single block context (height) f.sdkCtx = f.sdkCtx.WithBlockHeight(2) - validators[1], err = f.stakingKeeper.Validators.Get(f.sdkCtx, validators[1].GetOperator()) + validators[1], err = f.stakingKeeper.GetValidator(f.sdkCtx, validators[1].GetOperator()) assert.NilError(t, err) assert.NilError(t, f.stakingKeeper.DeleteValidatorByPowerIndex(f.sdkCtx, validators[0])) validators[0], _ = validators[0].RemoveDelShares(validators[0].DelegatorShares) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[0].GetOperator(), validators[0])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[0])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[0])) applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 0) assert.NilError(t, f.stakingKeeper.DeleteValidatorByPowerIndex(f.sdkCtx, validators[1])) tokens = f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 250) validators[1], _ = validators[1].AddTokensFromDel(tokens) - assert.NilError(t, f.stakingKeeper.Validators.Set(f.sdkCtx, validators[1].GetOperator(), validators[1])) + assert.NilError(t, f.stakingKeeper.SetValidator(f.sdkCtx, validators[1])) assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.sdkCtx, validators[1])) // verify initial CometBFT updates are correct diff --git a/tests/integration/staking/simulation/operations_test.go b/tests/integration/staking/simulation/operations_test.go index 21fda51dd1c0..7fb8bbeb3dd1 100644 --- a/tests/integration/staking/simulation/operations_test.go +++ b/tests/integration/staking/simulation/operations_test.go @@ -378,7 +378,7 @@ func (s *SimTestSuite) getTestingValidator(ctx sdk.Context, commission types.Com validator.DelegatorShares = math.LegacyNewDec(100) validator.Tokens = s.stakingKeeper.TokensFromConsensusPower(ctx, 100) - s.Require().NoError(s.stakingKeeper.Validators.Set(ctx, validator.GetOperator(), validator)) + s.Require().NoError(s.stakingKeeper.SetValidator(ctx, validator)) return validator } diff --git a/x/distribution/simulation/operations_test.go b/x/distribution/simulation/operations_test.go index 4d2431a68ef3..830c07460ee3 100644 --- a/x/distribution/simulation/operations_test.go +++ b/x/distribution/simulation/operations_test.go @@ -306,7 +306,7 @@ func (suite *SimTestSuite) getTestingValidator(accounts []simtypes.Account, comm validator.DelegatorShares = math.LegacyNewDec(100) validator.Tokens = math.NewInt(1000000) - suite.Require().NoError(suite.stakingKeeper.Validators.Set(suite.ctx, validator.GetOperator(), validator)) + suite.Require().NoError(suite.stakingKeeper.SetValidator(suite.ctx, validator)) return validator } diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index 1857ea050696..0ad0ac89550a 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -94,7 +94,7 @@ func TestSlashingMsgs(t *testing.T) { _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1}) require.NoError(t, err) ctxCheck = baseApp.NewContext(true) - validator, err := stakingKeeper.Validators.Get(ctxCheck, sdk.ValAddress(addr1)) + validator, err := stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1)) require.NoError(t, err) require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress) diff --git a/x/slashing/simulation/operations_test.go b/x/slashing/simulation/operations_test.go index 3d82004207ce..8c555fb6682f 100644 --- a/x/slashing/simulation/operations_test.go +++ b/x/slashing/simulation/operations_test.go @@ -219,7 +219,7 @@ func getTestingValidator(ctx sdk.Context, stakingKeeper *stakingkeeper.Keeper, a validator.DelegatorShares = math.LegacyNewDec(100) validator.Tokens = math.NewInt(1000000) - err = stakingKeeper.Validators.Set(ctx, validator.GetOperator(), validator) + err = stakingKeeper.SetValidator(ctx, validator) if err != nil { return stakingtypes.Validator{}, err } diff --git a/x/staking/app_test.go b/x/staking/app_test.go index d2bad852ecf4..04a026780d53 100644 --- a/x/staking/app_test.go +++ b/x/staking/app_test.go @@ -82,7 +82,7 @@ func TestStakingMsgs(t *testing.T) { _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1}) require.NoError(t, err) ctxCheck = app.BaseApp.NewContext(true) - validator, err := stakingKeeper.Validators.Get(ctxCheck, sdk.ValAddress(addr1)) + validator, err := stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1)) require.NoError(t, err) require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress) @@ -101,7 +101,7 @@ func TestStakingMsgs(t *testing.T) { require.NoError(t, err) ctxCheck = app.BaseApp.NewContext(true) - validator, err = stakingKeeper.Validators.Get(ctxCheck, sdk.ValAddress(addr1)) + validator, err = stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1)) require.NoError(t, err) require.Equal(t, description, validator.Description) diff --git a/x/staking/keeper/alias_functions.go b/x/staking/keeper/alias_functions.go index 8e26b463ceca..4379043cc6d6 100644 --- a/x/staking/keeper/alias_functions.go +++ b/x/staking/keeper/alias_functions.go @@ -82,7 +82,7 @@ func (k Keeper) IterateLastValidators(ctx context.Context, fn func(index int64, for ; iterator.Valid(); iterator.Next() { address := types.AddressFromLastValidatorPowerKey(iterator.Key()) - validator, err := k.Validators.Get(ctx, address) + validator, err := k.GetValidator(ctx, address) if err != nil { return err } @@ -98,7 +98,7 @@ func (k Keeper) IterateLastValidators(ctx context.Context, fn func(index int64, // Validator gets the Validator interface for a particular address func (k Keeper) Validator(ctx context.Context, address sdk.ValAddress) (types.ValidatorI, error) { - return k.Validators.Get(ctx, address) + return k.GetValidator(ctx, address) } // ValidatorByConsAddr gets the validator interface for a particular pubkey diff --git a/x/staking/keeper/delegation.go b/x/staking/keeper/delegation.go index dad0155ff61f..e5849b8daa8e 100644 --- a/x/staking/keeper/delegation.go +++ b/x/staking/keeper/delegation.go @@ -308,7 +308,7 @@ func (k Keeper) GetDelegatorBonded(ctx context.Context, delegator sdk.AccAddress if err != nil { panic(err) // shouldn't happen } - validator, err := k.Validators.Get(ctx, validatorAddr) + validator, err := k.GetValidator(ctx, validatorAddr) if err == nil { shares := delegation.Shares tokens := validator.TokensFromSharesTruncated(shares) @@ -992,7 +992,7 @@ func (k Keeper) Unbond( } // get validator - validator, err := k.Validators.Get(ctx, valAddr) + validator, err := k.GetValidator(ctx, valAddr) if err != nil { return amount, err } @@ -1061,7 +1061,7 @@ func (k Keeper) Unbond( func (k Keeper) getBeginInfo( ctx context.Context, valSrcAddr sdk.ValAddress, ) (completionTime time.Time, height int64, completeNow bool, err error) { - validator, err := k.Validators.Get(ctx, valSrcAddr) + validator, err := k.GetValidator(ctx, valSrcAddr) if err != nil && errors.Is(err, types.ErrNoValidatorFound) { return completionTime, height, false, nil } @@ -1099,7 +1099,7 @@ func (k Keeper) getBeginInfo( func (k Keeper) Undelegate( ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, sharesAmount math.LegacyDec, ) (time.Time, math.Int, error) { - validator, err := k.Validators.Get(ctx, valAddr) + validator, err := k.GetValidator(ctx, valAddr) if err != nil { return time.Time{}, math.Int{}, err } @@ -1216,14 +1216,14 @@ func (k Keeper) BeginRedelegation( return time.Time{}, types.ErrSelfRedelegation } - dstValidator, err := k.Validators.Get(ctx, valDstAddr) + dstValidator, err := k.GetValidator(ctx, valDstAddr) if errors.Is(err, types.ErrNoValidatorFound) { return time.Time{}, types.ErrBadRedelegationDst } else if err != nil { return time.Time{}, err } - srcValidator, err := k.Validators.Get(ctx, valSrcAddr) + srcValidator, err := k.GetValidator(ctx, valSrcAddr) if errors.Is(err, types.ErrNoValidatorFound) { return time.Time{}, types.ErrBadRedelegationSrc } else if err != nil { @@ -1345,7 +1345,7 @@ func (k Keeper) CompleteRedelegation( func (k Keeper) ValidateUnbondAmount( ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, amt math.Int, ) (shares math.LegacyDec, err error) { - validator, err := k.Validators.Get(ctx, valAddr) + validator, err := k.GetValidator(ctx, valAddr) if err != nil { return shares, err } diff --git a/x/staking/keeper/delegation_test.go b/x/staking/keeper/delegation_test.go index f45662b48075..ab689985a78f 100644 --- a/x/staking/keeper/delegation_test.go +++ b/x/staking/keeper/delegation_test.go @@ -5,7 +5,6 @@ import ( "github.com/golang/mock/gomock" - "cosmossdk.io/collections" "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec/address" @@ -386,7 +385,7 @@ func (s *KeeperTestSuite) TestUnbondDelegation() { delegation, err = keeper.GetDelegation(ctx, delAddrs[0], valAddrs[0]) require.NoError(err) - validator, err = keeper.Validators.Get(ctx, valAddrs[0]) + validator, err = keeper.GetValidator(ctx, valAddrs[0]) require.NoError(err) remainingTokens := startTokens.Sub(bondTokens) @@ -438,7 +437,7 @@ func (s *KeeperTestSuite) TestUndelegateSelfDelegationBelowMinSelfDelegation() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err = keeper.Validators.Get(ctx, addrVals[0]) + validator, err = keeper.GetValidator(ctx, addrVals[0]) require.NoError(err) require.Equal(keeper.TokensFromConsensusPower(ctx, 14), validator.Tokens) require.Equal(stakingtypes.Unbonding, validator.Status) @@ -494,7 +493,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondingValidator() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err = keeper.Validators.Get(ctx, addrVals[0]) + validator, err = keeper.GetValidator(ctx, addrVals[0]) require.NoError(err) require.Equal(blockHeight, validator.UnbondingHeight) params, err := keeper.GetParams(ctx) @@ -565,7 +564,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondedValidator() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err = keeper.Validators.Get(ctx, addrVals[0]) + validator, err = keeper.GetValidator(ctx, addrVals[0]) require.NoError(err) require.Equal(ctx.BlockHeight(), validator.UnbondingHeight) params, err := keeper.GetParams(ctx) @@ -578,7 +577,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondedValidator() { require.NoError(err) // Make sure validator is still in state because there is still an outstanding delegation - validator, err = keeper.Validators.Get(ctx, addrVals[0]) + validator, err = keeper.GetValidator(ctx, addrVals[0]) require.NoError(err) require.Equal(validator.Status, stakingtypes.Unbonded) @@ -595,8 +594,8 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondedValidator() { require.Equal(amount3, remainingTokens) // now validator should be deleted from state - validator, err = keeper.Validators.Get(ctx, addrVals[0]) - require.ErrorIs(err, collections.ErrNotFound) + validator, err = keeper.GetValidator(ctx, addrVals[0]) + require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) } func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() { @@ -652,7 +651,7 @@ func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() { require.Equal(amount2, delTokens) // validator should still be in state and still be in unbonding state - validator, err = keeper.Validators.Get(ctx, addrVals[0]) + validator, err = keeper.GetValidator(ctx, addrVals[0]) require.NoError(err) require.Equal(validator.Status, stakingtypes.Unbonding) @@ -662,8 +661,8 @@ func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() { require.NoError(err) // validator should now be deleted from state - _, err = keeper.Validators.Get(ctx, addrVals[0]) - require.ErrorIs(err, collections.ErrNotFound) + _, err = keeper.GetValidator(ctx, addrVals[0]) + require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) } // Make sure that that the retrieving the delegations doesn't affect the state @@ -894,7 +893,7 @@ func (s *KeeperTestSuite) TestRedelegateSelfDelegation() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 2) - validator, err = keeper.Validators.Get(ctx, addrVals[0]) + validator, err = keeper.GetValidator(ctx, addrVals[0]) require.NoError(err) require.Equal(valTokens, validator.Tokens) require.Equal(stakingtypes.Unbonding, validator.Status) @@ -952,7 +951,7 @@ func (s *KeeperTestSuite) TestRedelegateFromUnbondingValidator() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err = keeper.Validators.Get(ctx, addrVals[0]) + validator, err = keeper.GetValidator(ctx, addrVals[0]) require.NoError(err) require.Equal(blockHeight, validator.UnbondingHeight) params, err := keeper.GetParams(ctx) @@ -1030,7 +1029,7 @@ func (s *KeeperTestSuite) TestRedelegateFromUnbondedValidator() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, gomock.Any()) s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err = keeper.Validators.Get(ctx, addrVals[0]) + validator, err = keeper.GetValidator(ctx, addrVals[0]) require.NoError(err) require.Equal(ctx.BlockHeight(), validator.UnbondingHeight) params, err := keeper.GetParams(ctx) diff --git a/x/staking/keeper/genesis.go b/x/staking/keeper/genesis.go index 35bd9c476b7f..65a53c0cbe46 100644 --- a/x/staking/keeper/genesis.go +++ b/x/staking/keeper/genesis.go @@ -39,7 +39,7 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) (res } for _, validator := range data.Validators { - if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { + if err := k.SetValidator(ctx, validator); err != nil { panic(err) } @@ -183,7 +183,7 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) (res panic(err) } - validator, err := k.Validators.Get(ctx, valAddr) + validator, err := k.GetValidator(ctx, valAddr) if err != nil { panic(fmt.Sprintf("validator %s not found", lv.Address)) } diff --git a/x/staking/keeper/grpc_query.go b/x/staking/keeper/grpc_query.go index 7bfdac337f5c..7b72ec54a7a2 100644 --- a/x/staking/keeper/grpc_query.go +++ b/x/staking/keeper/grpc_query.go @@ -77,7 +77,7 @@ func (k Querier) Validator(ctx context.Context, req *types.QueryValidatorRequest return nil, err } - validator, err := k.Keeper.Validators.Get(ctx, valAddr) + validator, err := k.GetValidator(ctx, valAddr) if err != nil { return nil, status.Errorf(codes.NotFound, "validator %s not found", req.ValidatorAddr) } @@ -457,7 +457,7 @@ func (k Querier) DelegatorValidators(ctx context.Context, req *types.QueryDelega return err } - validator, err := k.Keeper.Validators.Get(ctx, valAddr) + validator, err := k.GetValidator(ctx, valAddr) if err != nil { return err } @@ -575,7 +575,7 @@ func delegationToDelegationResponse(ctx context.Context, k *Keeper, del types.De return types.DelegationResponse{}, err } - val, err := k.Validators.Get(ctx, valAddr) + val, err := k.GetValidator(ctx, valAddr) if err != nil { return types.DelegationResponse{}, err } @@ -631,7 +631,7 @@ func redelegationsToRedelegationResponses(ctx context.Context, k *Keeper, redels return nil, err } - val, err := k.Validators.Get(ctx, valDstAddr) + val, err := k.GetValidator(ctx, valDstAddr) if err != nil { return nil, err } diff --git a/x/staking/keeper/grpc_query_test.go b/x/staking/keeper/grpc_query_test.go index 2a5412cfcfd1..b55539d1b538 100644 --- a/x/staking/keeper/grpc_query_test.go +++ b/x/staking/keeper/grpc_query_test.go @@ -14,7 +14,7 @@ func (s *KeeperTestSuite) TestGRPCQueryValidator() { require := s.Require() validator := testutil.NewValidator(s.T(), sdk.ValAddress(PKs[0].Address().Bytes()), PKs[0]) - require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) + require.NoError(keeper.SetValidator(ctx, validator)) var req *types.QueryValidatorRequest testCases := []struct { msg string diff --git a/x/staking/keeper/historical_info_test.go b/x/staking/keeper/historical_info_test.go index 37ccf9b2a2ff..d05700936689 100644 --- a/x/staking/keeper/historical_info_test.go +++ b/x/staking/keeper/historical_info_test.go @@ -87,12 +87,12 @@ func (s *KeeperTestSuite) TestTrackHistoricalInfo() { val1 := testutil.NewValidator(s.T(), addrVals[2], PKs[2]) val1.Status = stakingtypes.Bonded // when not bonded, consensus power is Zero val1.Tokens = keeper.TokensFromConsensusPower(ctx, 10) - require.NoError(keeper.Validators.Set(ctx, val1.GetOperator(), val1)) + require.NoError(keeper.SetValidator(ctx, val1)) require.NoError(keeper.SetLastValidatorPower(ctx, val1.GetOperator(), 10)) val2 := testutil.NewValidator(s.T(), addrVals[3], PKs[3]) val1.Status = stakingtypes.Bonded val2.Tokens = keeper.TokensFromConsensusPower(ctx, 80) - require.NoError(keeper.Validators.Set(ctx, val2.GetOperator(), val2)) + require.NoError(keeper.SetValidator(ctx, val2)) require.NoError(keeper.SetLastValidatorPower(ctx, val2.GetOperator(), 80)) vals := []stakingtypes.Validator{val1, val2} diff --git a/x/staking/keeper/invariants.go b/x/staking/keeper/invariants.go index 429feeadcb09..3c749925bc62 100644 --- a/x/staking/keeper/invariants.go +++ b/x/staking/keeper/invariants.go @@ -114,7 +114,7 @@ func NonNegativePowerInvariant(k *Keeper) sdk.Invariant { panic(err) } for ; iterator.Valid(); iterator.Next() { - validator, err := k.Validators.Get(ctx, iterator.Value()) + validator, err := k.GetValidator(ctx, iterator.Value()) if err != nil { panic(fmt.Sprintf("validator record not found for address: %X\n", iterator.Value())) } diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 45ba5576f833..35b04a8deb90 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -35,7 +35,6 @@ type Keeper struct { Schema collections.Schema LastTotalPower collections.Item[math.Int] ValidatorUpdates collections.Item[types.ValidatorUpdates] - Validators collections.Map[sdk.ValAddress, types.Validator] } // NewKeeper creates a new staking Keeper instance @@ -78,7 +77,6 @@ func NewKeeper( consensusAddressCodec: consensusAddressCodec, LastTotalPower: collections.NewItem(sb, types.LastTotalPowerKey, "last_total_power", sdk.IntValue), ValidatorUpdates: collections.NewItem(sb, types.ValidatorUpdatesKey, "validator_updates", codec.CollValue[types.ValidatorUpdates](cdc)), - Validators: collections.NewMap(sb, types.ValidatorsKey, "validators", sdk.ValAddressKey, codec.CollValue[types.Validator](cdc)), } schema, err := sb.Build() diff --git a/x/staking/keeper/msg_server.go b/x/staking/keeper/msg_server.go index f96e8ab204a7..d7fb9744a72b 100644 --- a/x/staking/keeper/msg_server.go +++ b/x/staking/keeper/msg_server.go @@ -52,7 +52,7 @@ func (k msgServer) CreateValidator(ctx context.Context, msg *types.MsgCreateVali } // check to see if the pubkey or sender has been registered before - if _, err := k.Validators.Get(ctx, valAddr); err == nil { + if _, err := k.GetValidator(ctx, valAddr); err == nil { return nil, types.ErrValidatorOwnerExists } @@ -116,7 +116,7 @@ func (k msgServer) CreateValidator(ctx context.Context, msg *types.MsgCreateVali validator.MinSelfDelegation = msg.MinSelfDelegation - err = k.Validators.Set(ctx, validator.GetOperator(), validator) + err = k.SetValidator(ctx, validator) if err != nil { return nil, err } @@ -189,7 +189,7 @@ func (k msgServer) EditValidator(ctx context.Context, msg *types.MsgEditValidato } // validator must already be registered - validator, err := k.Validators.Get(ctx, valAddr) + validator, err := k.GetValidator(ctx, valAddr) if err != nil { return nil, err } @@ -228,7 +228,7 @@ func (k msgServer) EditValidator(ctx context.Context, msg *types.MsgEditValidato validator.MinSelfDelegation = *msg.MinSelfDelegation } - err = k.Validators.Set(ctx, validator.GetOperator(), validator) + err = k.SetValidator(ctx, validator) if err != nil { return nil, err } @@ -264,7 +264,7 @@ func (k msgServer) Delegate(ctx context.Context, msg *types.MsgDelegate) (*types ) } - validator, err := k.Validators.Get(ctx, valAddr) + validator, err := k.GetValidator(ctx, valAddr) if err != nil { return nil, err } @@ -495,7 +495,7 @@ func (k msgServer) CancelUnbondingDelegation(ctx context.Context, msg *types.Msg ) } - validator, err := k.Validators.Get(ctx, valAddr) + validator, err := k.GetValidator(ctx, valAddr) if err != nil { return nil, err } diff --git a/x/staking/keeper/msg_server_test.go b/x/staking/keeper/msg_server_test.go index c0985e8b49d6..82b30abe13f0 100644 --- a/x/staking/keeper/msg_server_test.go +++ b/x/staking/keeper/msg_server_test.go @@ -340,7 +340,7 @@ func (s *KeeperTestSuite) TestMsgEditValidator() { MinSelfDelegation: &newSelfDel, }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "change commmission rate in <24hrs", @@ -477,7 +477,7 @@ func (s *KeeperTestSuite) TestMsgDelegate() { Amount: sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: keeper.TokensFromConsensusPower(s.ctx, int64(100))}, }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "zero amount", @@ -630,7 +630,7 @@ func (s *KeeperTestSuite) TestMsgBeginRedelegate() { Amount: sdk.NewCoin(sdk.DefaultBondDenom, shares.RoundInt()), }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "self redelegation", @@ -769,7 +769,7 @@ func (s *KeeperTestSuite) TestMsgUndelegate() { Amount: sdk.NewCoin(sdk.DefaultBondDenom, shares.RoundInt()), }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "amount greater than delegated shares amount", @@ -938,7 +938,7 @@ func (s *KeeperTestSuite) TestMsgCancelUnbondingDelegation() { CreationHeight: 10, }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "amount is greater than balance", diff --git a/x/staking/keeper/query_utils.go b/x/staking/keeper/query_utils.go index bcff5a71c72b..f7951fc2944f 100644 --- a/x/staking/keeper/query_utils.go +++ b/x/staking/keeper/query_utils.go @@ -32,7 +32,7 @@ func (k Keeper) GetDelegatorValidators( return nil, err } - validator, err := k.Validators.Get(ctx, valAddr) + validator, err := k.GetValidator(ctx, valAddr) if err != nil { return nil, err } @@ -58,7 +58,7 @@ func (k Keeper) GetDelegatorValidator( return validator, err } - return k.Validators.Get(ctx, valAddr) + return k.GetValidator(ctx, valAddr) } // GetAllDelegatorDelegations returns all delegations of a delegator diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index a2ed981acc02..fc98a44585b8 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -325,7 +325,7 @@ func (k Keeper) SlashRedelegation(ctx context.Context, srcValidator types.Valida return math.ZeroInt(), err } - dstValidator, err := k.Validators.Get(ctx, valDstAddr) + dstValidator, err := k.GetValidator(ctx, valDstAddr) if err != nil { return math.ZeroInt(), err } diff --git a/x/staking/keeper/slash_test.go b/x/staking/keeper/slash_test.go index afd8f530bffc..39a8f817a193 100644 --- a/x/staking/keeper/slash_test.go +++ b/x/staking/keeper/slash_test.go @@ -17,21 +17,21 @@ func (s *KeeperTestSuite) TestRevocation() { validator := testutil.NewValidator(s.T(), valAddr, PKs[0]) // initial state - require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) + require.NoError(keeper.SetValidator(ctx, validator)) require.NoError(keeper.SetValidatorByConsAddr(ctx, validator)) - val, err := keeper.Validators.Get(ctx, valAddr) + val, err := keeper.GetValidator(ctx, valAddr) require.NoError(err) require.False(val.IsJailed()) // test jail require.NoError(keeper.Jail(ctx, consAddr)) - val, err = keeper.Validators.Get(ctx, valAddr) + val, err = keeper.GetValidator(ctx, valAddr) require.NoError(err) require.True(val.IsJailed()) // test unjail require.NoError(keeper.Unjail(ctx, consAddr)) - val, err = keeper.Validators.Get(ctx, valAddr) + val, err = keeper.GetValidator(ctx, valAddr) require.NoError(err) require.False(val.IsJailed()) } @@ -43,7 +43,7 @@ func (s *KeeperTestSuite) TestSlashAtFutureHeight() { consAddr := sdk.ConsAddress(PKs[0].Address()) validator := testutil.NewValidator(s.T(), sdk.ValAddress(PKs[0].Address().Bytes()), PKs[0]) - require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) + require.NoError(keeper.SetValidator(ctx, validator)) require.NoError(keeper.SetValidatorByConsAddr(ctx, validator)) fraction := sdkmath.LegacyNewDecWithPrec(5, 1) diff --git a/x/staking/keeper/test_common.go b/x/staking/keeper/test_common.go index 48fb7eb3cfcd..812c0b562bf3 100644 --- a/x/staking/keeper/test_common.go +++ b/x/staking/keeper/test_common.go @@ -22,7 +22,7 @@ func ValidatorByPowerIndexExists(ctx context.Context, keeper *Keeper, power []by // TestingUpdateValidator updates a validator for testing func TestingUpdateValidator(keeper *Keeper, ctx sdk.Context, validator types.Validator, apply bool) types.Validator { - err := keeper.Validators.Set(ctx, validator.GetOperator(), validator) + err := keeper.SetValidator(ctx, validator) if err != nil { panic(err) } @@ -64,7 +64,7 @@ func TestingUpdateValidator(keeper *Keeper, ctx sdk.Context, validator types.Val panic(err) } - validator, err = keeper.Validators.Get(ctx, validator.GetOperator()) + validator, err = keeper.GetValidator(ctx, validator.GetOperator()) if err != nil { panic(err) } diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index 6da38a2764fa..a0fb55de8c46 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -216,7 +216,7 @@ func (k Keeper) SetRedelegationByUnbondingID(ctx context.Context, red types.Rede } // SetValidatorByUnbondingID sets an index to look up a Validator by the unbondingID corresponding to its current unbonding -// Note, it does not set the validator itself, use Validators.Set(ctx, val.GetOperator(), val) for that +// Note, it does not set the validator itself, use SetValidator(ctx, val) for that func (k Keeper) SetValidatorByUnbondingID(ctx context.Context, val types.Validator, id uint64) error { store := k.storeService.OpenKVStore(ctx) @@ -405,7 +405,7 @@ func (k Keeper) validatorUnbondingCanComplete(ctx context.Context, id uint64) er ) } val.UnbondingOnHoldRefCount-- - return k.Validators.Set(ctx, val.GetOperator(), val) + return k.SetValidator(ctx, val) } // PutUnbondingOnHold allows an external module to stop an unbonding operation, @@ -474,5 +474,5 @@ func (k Keeper) putValidatorOnHold(ctx context.Context, id uint64) error { } val.UnbondingOnHoldRefCount++ - return k.Validators.Set(ctx, val.GetOperator(), val) + return k.SetValidator(ctx, val) } diff --git a/x/staking/keeper/unbonding_test.go b/x/staking/keeper/unbonding_test.go index bfe88a2d6e13..93f817170628 100644 --- a/x/staking/keeper/unbonding_test.go +++ b/x/staking/keeper/unbonding_test.go @@ -244,7 +244,7 @@ func (s *KeeperTestSuite) TestValidatorByUnbondingIDAccessors() { for i, tc := range cases { s.Run(tc.name, func() { if tc.exists.setValidator { - require.NoError(s.stakingKeeper.Validators.Set(s.ctx, tc.validator.GetOperator(), tc.validator)) + require.NoError(s.stakingKeeper.SetValidator(s.ctx, tc.validator)) } if tc.exists.setValidatorByUnbondingID { @@ -327,7 +327,7 @@ func (s *KeeperTestSuite) TestUnbondingCanComplete() { require.ErrorIs(err, types.ErrNoValidatorFound) val := testutil.NewValidator(s.T(), valAddrs[0], PKs[0]) - require.NoError(s.stakingKeeper.Validators.Set(s.ctx, val.GetOperator(), val)) + require.NoError(s.stakingKeeper.SetValidator(s.ctx, val)) require.NoError(s.stakingKeeper.SetValidatorByUnbondingID(s.ctx, val, unbondingID)) err = s.stakingKeeper.UnbondingCanComplete(s.ctx, unbondingID) require.ErrorIs(err, types.ErrUnbondingOnHoldRefCountNegative) diff --git a/x/staking/keeper/val_state_change.go b/x/staking/keeper/val_state_change.go index e1f2d68a5167..43f1f463353d 100644 --- a/x/staking/keeper/val_state_change.go +++ b/x/staking/keeper/val_state_change.go @@ -307,7 +307,7 @@ func (k Keeper) jailValidator(ctx context.Context, validator types.Validator) er } validator.Jailed = true - if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { + if err := k.SetValidator(ctx, validator); err != nil { return err } @@ -321,7 +321,7 @@ func (k Keeper) unjailValidator(ctx context.Context, validator types.Validator) } validator.Jailed = false - if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { + if err := k.SetValidator(ctx, validator); err != nil { return err } @@ -338,7 +338,7 @@ func (k Keeper) bondValidator(ctx context.Context, validator types.Validator) (t validator = validator.UpdateStatus(types.Bonded) // save the now bonded validator record to the two referenced stores - if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { + if err := k.SetValidator(ctx, validator); err != nil { return validator, err } @@ -396,7 +396,7 @@ func (k Keeper) BeginUnbondingValidator(ctx context.Context, validator types.Val validator.UnbondingIds = append(validator.UnbondingIds, id) // save the now unbonded validator record and power index - if err = k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { + if err = k.SetValidator(ctx, validator); err != nil { return validator, err } @@ -433,7 +433,7 @@ func (k Keeper) BeginUnbondingValidator(ctx context.Context, validator types.Val // perform all the store operations for when a validator status becomes unbonded func (k Keeper) completeUnbondingValidator(ctx context.Context, validator types.Validator) (types.Validator, error) { validator = validator.UpdateStatus(types.Unbonded) - if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { + if err := k.SetValidator(ctx, validator); err != nil { return validator, err } diff --git a/x/staking/keeper/validator.go b/x/staking/keeper/validator.go index a49635d81968..e92c49047157 100644 --- a/x/staking/keeper/validator.go +++ b/x/staking/keeper/validator.go @@ -17,8 +17,23 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) +// GetValidator gets a single validator +func (k Keeper) GetValidator(ctx context.Context, addr sdk.ValAddress) (validator types.Validator, err error) { + store := k.storeService.OpenKVStore(ctx) + value, err := store.Get(types.GetValidatorKey(addr)) + if err != nil { + return validator, err + } + + if value == nil { + return validator, types.ErrNoValidatorFound + } + + return types.UnmarshalValidator(k.cdc, value) +} + func (k Keeper) mustGetValidator(ctx context.Context, addr sdk.ValAddress) types.Validator { - validator, err := k.Validators.Get(ctx, addr) + validator, err := k.GetValidator(ctx, addr) if err != nil { panic(fmt.Sprintf("validator record not found for address: %X\n", addr)) } @@ -38,7 +53,7 @@ func (k Keeper) GetValidatorByConsAddr(ctx context.Context, consAddr sdk.ConsAdd return validator, types.ErrNoValidatorFound } - return k.Validators.Get(ctx, opAddr) + return k.GetValidator(ctx, opAddr) } func (k Keeper) mustGetValidatorByConsAddr(ctx context.Context, consAddr sdk.ConsAddress) types.Validator { @@ -50,6 +65,13 @@ func (k Keeper) mustGetValidatorByConsAddr(ctx context.Context, consAddr sdk.Con return validator } +// SetValidator sets the main record holding validator details +func (k Keeper) SetValidator(ctx context.Context, validator types.Validator) error { + store := k.storeService.OpenKVStore(ctx) + bz := types.MustMarshalValidator(k.cdc, &validator) + return store.Set(types.GetValidatorKey(validator.GetOperator()), bz) +} + // SetValidatorByConsAddr sets a validator by conesensus address func (k Keeper) SetValidatorByConsAddr(ctx context.Context, validator types.Validator) error { consPk, err := validator.GetConsAddr() @@ -93,7 +115,7 @@ func (k Keeper) AddValidatorTokensAndShares(ctx context.Context, validator types } validator, addedShares = validator.AddTokensFromDel(tokensToAdd) - err = k.Validators.Set(ctx, validator.GetOperator(), validator) + err = k.SetValidator(ctx, validator) if err != nil { return validator, addedShares, err } @@ -111,7 +133,7 @@ func (k Keeper) RemoveValidatorTokensAndShares(ctx context.Context, validator ty return valOut, removedTokens, err } validator, removedTokens = validator.RemoveDelShares(sharesToRemove) - err = k.Validators.Set(ctx, validator.GetOperator(), validator) + err = k.SetValidator(ctx, validator) if err != nil { return validator, removedTokens, err } @@ -129,7 +151,7 @@ func (k Keeper) RemoveValidatorTokens(ctx context.Context, } validator = validator.RemoveTokens(tokensToRemove) - if err := k.Validators.Set(ctx, validator.GetOperator(), validator); err != nil { + if err := k.SetValidator(ctx, validator); err != nil { return validator, err } @@ -172,7 +194,7 @@ func (k Keeper) UpdateValidatorCommission(ctx context.Context, // except for the bonded validator index which is only handled in ApplyAndReturnTendermintUpdates func (k Keeper) RemoveValidator(ctx context.Context, address sdk.ValAddress) error { // first retrieve the old validator record - validator, err := k.Validators.Get(ctx, address) + validator, err := k.GetValidator(ctx, address) if errors.Is(err, types.ErrNoValidatorFound) { return nil } @@ -192,7 +214,7 @@ func (k Keeper) RemoveValidator(ctx context.Context, address sdk.ValAddress) err // delete the old validator record store := k.storeService.OpenKVStore(ctx) - if err = k.Validators.Remove(ctx, address); err != nil { + if err = store.Delete(types.GetValidatorKey(address)); err != nil { return err } @@ -385,7 +407,7 @@ func (k Keeper) GetLastValidators(ctx context.Context) (validators []types.Valid } address := types.AddressFromLastValidatorPowerKey(iterator.Key()) - validator, err := k.Validators.Get(ctx, address) + validator, err := k.GetValidator(ctx, address) if err != nil { return nil, err } @@ -528,7 +550,7 @@ func (k Keeper) UnbondAllMatureValidators(ctx context.Context) error { if err != nil { return err } - val, err := k.Validators.Get(ctx, addr) + val, err := k.GetValidator(ctx, addr) if err != nil { return errorsmod.Wrap(err, "validator in the unbonding queue was not found") } diff --git a/x/staking/keeper/validator_test.go b/x/staking/keeper/validator_test.go index c751ae0be218..06bc75f11840 100644 --- a/x/staking/keeper/validator_test.go +++ b/x/staking/keeper/validator_test.go @@ -1,13 +1,11 @@ package keeper_test import ( - "fmt" "time" abci "github.com/cometbft/cometbft/abci/types" "github.com/golang/mock/gomock" - "cosmossdk.io/collections" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -39,14 +37,14 @@ func (s *KeeperTestSuite) TestValidator() { require.Equal(stakingtypes.Unbonded, validator.Status) require.Equal(valTokens, validator.Tokens) require.Equal(valTokens, validator.DelegatorShares.RoundInt()) - require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) + require.NoError(keeper.SetValidator(ctx, validator)) require.NoError(keeper.SetValidatorByPowerIndex(ctx, validator)) require.NoError(keeper.SetValidatorByConsAddr(ctx, validator)) // ensure update s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.NotBondedPoolName, stakingtypes.BondedPoolName, gomock.Any()) updates := s.applyValidatorSetUpdates(ctx, keeper, 1) - validator, err := keeper.Validators.Get(ctx, valAddr) + validator, err := keeper.GetValidator(ctx, valAddr) require.NoError(err) require.Equal(validator.ABCIValidatorUpdate(keeper.PowerReduction(ctx)), updates[0]) @@ -110,8 +108,8 @@ func (s *KeeperTestSuite) TestValidatorBasics() { require.Equal(keeper.TokensFromConsensusPower(ctx, 7), validators[2].Tokens) // check the empty keeper first - _, err := keeper.Validators.Get(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) - require.ErrorIs(err, collections.ErrNotFound) + _, err := keeper.GetValidator(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) + require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) resVals, err := keeper.GetLastValidators(ctx) require.NoError(err) require.Zero(len(resVals)) @@ -124,7 +122,7 @@ func (s *KeeperTestSuite) TestValidatorBasics() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.NotBondedPoolName, stakingtypes.BondedPoolName, gomock.Any()) validators[0] = stakingkeeper.TestingUpdateValidator(keeper, ctx, validators[0], true) require.NoError(keeper.SetValidatorByConsAddr(ctx, validators[0])) - resVal, err := keeper.Validators.Get(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) + resVal, err := keeper.GetValidator(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) require.NoError(err) require.True(validators[0].MinEqual(&resVal)) @@ -148,7 +146,7 @@ func (s *KeeperTestSuite) TestValidatorBasics() { validators[0].Tokens = keeper.TokensFromConsensusPower(ctx, 10) validators[0].DelegatorShares = math.LegacyNewDecFromInt(validators[0].Tokens) validators[0] = stakingkeeper.TestingUpdateValidator(keeper, ctx, validators[0], true) - resVal, err = keeper.Validators.Get(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) + resVal, err = keeper.GetValidator(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) require.NoError(err) require.True(validators[0].MinEqual(&resVal)) @@ -162,10 +160,10 @@ func (s *KeeperTestSuite) TestValidatorBasics() { validators[1] = stakingkeeper.TestingUpdateValidator(keeper, ctx, validators[1], true) s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.NotBondedPoolName, stakingtypes.BondedPoolName, gomock.Any()) validators[2] = stakingkeeper.TestingUpdateValidator(keeper, ctx, validators[2], true) - resVal, err = keeper.Validators.Get(ctx, sdk.ValAddress(PKs[1].Address().Bytes())) + resVal, err = keeper.GetValidator(ctx, sdk.ValAddress(PKs[1].Address().Bytes())) require.NoError(err) require.True(validators[1].MinEqual(&resVal)) - resVal, err = keeper.Validators.Get(ctx, sdk.ValAddress(PKs[2].Address().Bytes())) + resVal, err = keeper.GetValidator(ctx, sdk.ValAddress(PKs[2].Address().Bytes())) require.NoError(err) require.True(validators[2].MinEqual(&resVal)) @@ -180,14 +178,14 @@ func (s *KeeperTestSuite) TestValidatorBasics() { // shouldn't be able to remove if there are still tokens left validators[1].Status = stakingtypes.Unbonded - require.NoError(keeper.Validators.Set(ctx, validators[1].GetOperator(), validators[1])) + require.NoError(keeper.SetValidator(ctx, validators[1])) require.EqualError(keeper.RemoveValidator(ctx, validators[1].GetOperator()), "attempting to remove a validator which still contains tokens: failed to remove validator") - validators[1].Tokens = math.ZeroInt() // ...remove all tokens - require.NoError(keeper.Validators.Set(ctx, validators[1].GetOperator(), validators[1])) // ...set the validator - require.NoError(keeper.RemoveValidator(ctx, validators[1].GetOperator())) // Now it can be removed. - _, err = keeper.Validators.Get(ctx, sdk.ValAddress(PKs[1].Address().Bytes())) - require.ErrorIs(err, collections.ErrNotFound) + validators[1].Tokens = math.ZeroInt() // ...remove all tokens + require.NoError(keeper.SetValidator(ctx, validators[1])) // ...set the validator + require.NoError(keeper.RemoveValidator(ctx, validators[1].GetOperator())) // Now it can be removed. + _, err = keeper.GetValidator(ctx, sdk.ValAddress(PKs[1].Address().Bytes())) + require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) } func (s *KeeperTestSuite) TestUpdateValidatorByPowerIndex() { @@ -206,7 +204,7 @@ func (s *KeeperTestSuite) TestUpdateValidatorByPowerIndex() { s.bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), stakingtypes.NotBondedPoolName, stakingtypes.BondedPoolName, gomock.Any()) stakingkeeper.TestingUpdateValidator(keeper, ctx, validator, true) - validator, err := keeper.Validators.Get(ctx, valAddr) + validator, err := keeper.GetValidator(ctx, valAddr) require.NoError(err) require.Equal(valTokens, validator.Tokens) @@ -220,7 +218,7 @@ func (s *KeeperTestSuite) TestUpdateValidatorByPowerIndex() { stakingkeeper.TestingUpdateValidator(keeper, ctx, validator, true) // update the validator, possibly kicking it out require.False(stakingkeeper.ValidatorByPowerIndexExists(ctx, keeper, power)) - validator, err = keeper.Validators.Get(ctx, valAddr) + validator, err = keeper.GetValidator(ctx, valAddr) require.NoError(err) power = stakingtypes.GetValidatorsByPowerIndexKey(validator, keeper.PowerReduction(ctx)) @@ -299,8 +297,8 @@ func (s *KeeperTestSuite) TestUpdateValidatorCommission() { val1, _ = val1.SetInitialCommission(commission1) val2, _ = val2.SetInitialCommission(commission2) - require.NoError(keeper.Validators.Set(ctx, val1.GetOperator(), val1)) - require.NoError(keeper.Validators.Set(ctx, val2.GetOperator(), val2)) + require.NoError(keeper.SetValidator(ctx, val1)) + require.NoError(keeper.SetValidator(ctx, val2)) testCases := []struct { validator stakingtypes.Validator @@ -326,10 +324,10 @@ func (s *KeeperTestSuite) TestUpdateValidatorCommission() { ) tc.validator.Commission = commission - err = keeper.Validators.Set(ctx, tc.validator.GetOperator(), tc.validator) + err = keeper.SetValidator(ctx, tc.validator) require.NoError(err) - val, err := keeper.Validators.Get(ctx, tc.validator.GetOperator()) + val, err := keeper.GetValidator(ctx, tc.validator.GetOperator()) require.NoError(err, "expected to find validator for test case #%d with rate: %s", i, tc.newRate, ) @@ -357,18 +355,18 @@ func (s *KeeperTestSuite) TestValidatorToken() { validator, _, err := keeper.AddValidatorTokensAndShares(ctx, validator, addTokens) require.NoError(err) require.Equal(addTokens, validator.Tokens) - validator, _ = keeper.Validators.Get(ctx, valAddr) + validator, _ = keeper.GetValidator(ctx, valAddr) require.Equal(math.LegacyNewDecFromInt(addTokens), validator.DelegatorShares) _, _, err = keeper.RemoveValidatorTokensAndShares(ctx, validator, math.LegacyNewDecFromInt(delTokens)) require.NoError(err) - validator, _ = keeper.Validators.Get(ctx, valAddr) + validator, _ = keeper.GetValidator(ctx, valAddr) require.Equal(delTokens, validator.Tokens) require.True(validator.DelegatorShares.Equal(math.LegacyNewDecFromInt(delTokens))) _, err = keeper.RemoveValidatorTokens(ctx, validator, delTokens) require.NoError(err) - validator, _ = keeper.Validators.Get(ctx, valAddr) + validator, _ = keeper.GetValidator(ctx, valAddr) require.True(validator.Tokens.IsZero()) } @@ -412,27 +410,27 @@ func (s *KeeperTestSuite) TestUnbondingValidator() { // check unbonding mature validators ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime) err = keeper.UnbondAllMatureValidators(ctx) - require.ErrorContains(err, fmt.Sprintf("validator in the unbonding queue was not found: %v", collections.ErrNotFound)) + require.EqualError(err, "validator in the unbonding queue was not found: validator does not exist") - require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) + require.NoError(keeper.SetValidator(ctx, validator)) ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime) err = keeper.UnbondAllMatureValidators(ctx) require.EqualError(err, "unexpected validator in unbonding queue; status was not unbonding") validator.Status = stakingtypes.Unbonding - require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) + require.NoError(keeper.SetValidator(ctx, validator)) require.NoError(keeper.UnbondAllMatureValidators(ctx)) - validator, err = keeper.Validators.Get(ctx, valAddr) - require.ErrorIs(err, collections.ErrNotFound) + validator, err = keeper.GetValidator(ctx, valAddr) + require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) require.NoError(keeper.SetUnbondingValidatorsQueue(ctx, endTime, endHeight, []string{valAddr.String()})) validator = testutil.NewValidator(s.T(), valAddr, valPubKey) validator, _ = validator.AddTokensFromDel(addTokens) validator.Status = stakingtypes.Unbonding - require.NoError(keeper.Validators.Set(ctx, validator.GetOperator(), validator)) + require.NoError(keeper.SetValidator(ctx, validator)) require.NoError(keeper.UnbondAllMatureValidators(ctx)) - validator, err = keeper.Validators.Get(ctx, valAddr) + validator, err = keeper.GetValidator(ctx, valAddr) require.NoError(err) require.Equal(stakingtypes.Unbonded, validator.Status) } diff --git a/x/staking/migrations/v2/store_test.go b/x/staking/migrations/v2/store_test.go index 489a57b33395..20c55e5bfcbb 100644 --- a/x/staking/migrations/v2/store_test.go +++ b/x/staking/migrations/v2/store_test.go @@ -12,7 +12,6 @@ import ( sdktestuil "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/address" v1 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v1" v2 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v2" "github.com/cosmos/cosmos-sdk/x/staking/testutil" @@ -55,7 +54,7 @@ func TestStoreMigration(t *testing.T) { { "ValidatorsKey", v1.GetValidatorKey(valAddr1), - append(types.ValidatorsKey, address.MustLengthPrefix(valAddr1)...), + types.GetValidatorKey(valAddr1), }, { "ValidatorsByConsAddrKey", diff --git a/x/staking/simulation/operations.go b/x/staking/simulation/operations.go index a6feaff656ca..e5a15c6d409f 100644 --- a/x/staking/simulation/operations.go +++ b/x/staking/simulation/operations.go @@ -120,7 +120,7 @@ func SimulateMsgCreateValidator( address := sdk.ValAddress(simAccount.Address) // ensure the validator doesn't exist already - _, err := k.Validators.Get(ctx, address) + _, err := k.GetValidator(ctx, address) if err == nil { return simtypes.NoOpMsg(types.ModuleName, msgType, "validator already exists"), nil, nil } diff --git a/x/staking/testutil/helpers.go b/x/staking/testutil/helpers.go index 5cd524ee96c9..7ed64e46131b 100644 --- a/x/staking/testutil/helpers.go +++ b/x/staking/testutil/helpers.go @@ -110,7 +110,7 @@ func (sh *Helper) Undelegate(delegator sdk.AccAddress, val sdk.ValAddress, amoun // CheckValidator asserts that a validor exists and has a given status (if status!="") // and if has a right jailed flag. func (sh *Helper) CheckValidator(addr sdk.ValAddress, status stakingtypes.BondStatus, jailed bool) stakingtypes.Validator { - v, err := sh.k.Validators.Get(sh.Ctx, addr) + v, err := sh.k.GetValidator(sh.Ctx, addr) require.NoError(sh.t, err) require.Equal(sh.t, jailed, v.Jailed, "wrong Jalied status") if status >= 0 { diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 1d29580b6e7b..57689058c9ec 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -34,9 +34,9 @@ var ( LastValidatorPowerKey = []byte{0x11} // prefix for each key to a validator index, for bonded validators LastTotalPowerKey = collections.NewPrefix(18) // prefix for the total power - ValidatorsKey = collections.NewPrefix(33) // prefix for each key to a validator - ValidatorsByConsAddrKey = []byte{0x22} // prefix for each key to a validator index, by pubkey - ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power + ValidatorsKey = []byte{0x21} // prefix for each key to a validator + ValidatorsByConsAddrKey = []byte{0x22} // prefix for each key to a validator index, by pubkey + ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power DelegationKey = []byte{0x31} // key for a delegation UnbondingDelegationKey = []byte{0x32} // key for an unbonding-delegation From 67c56df2734084d76fe4b407451f5e57c11fccd2 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Tue, 8 Aug 2023 13:03:18 +0530 Subject: [PATCH 03/18] wip --- x/staking/keeper/keeper.go | 2 ++ x/staking/keeper/validator.go | 11 ++++------- x/staking/migrations/v2/store_test.go | 7 ++++++- x/staking/simulation/decoder_test.go | 3 ++- x/staking/types/keys.go | 12 ++++++------ 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 351ea8cf8612..300f968d9de1 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -41,6 +41,7 @@ type Keeper struct { UnbondingID collections.Sequence ValidatorByConsensusAddress collections.Map[sdk.ConsAddress, sdk.ValAddress] UnbondingType collections.Map[uint64, uint64] + Validators collections.Map[sdk.ValAddress, []byte] } // NewKeeper creates a new staking Keeper instance @@ -98,6 +99,7 @@ func NewKeeper( collcodec.KeyToValueCodec(sdk.ValAddressKey), ), UnbondingType: collections.NewMap(sb, types.UnbondingTypeKey, "unbonding_type", collections.Uint64Key, collections.Uint64Value), + Validators: collections.NewMap(sb, types.ValidatorsKey, "validators", sdk.ValAddressKey, collections.BytesValue), } schema, err := sb.Build() diff --git a/x/staking/keeper/validator.go b/x/staking/keeper/validator.go index 2c812bb5d17b..f1628e5ee086 100644 --- a/x/staking/keeper/validator.go +++ b/x/staking/keeper/validator.go @@ -22,12 +22,10 @@ import ( // GetValidator gets a single validator func (k Keeper) GetValidator(ctx context.Context, addr sdk.ValAddress) (validator types.Validator, err error) { - store := k.storeService.OpenKVStore(ctx) - value, err := store.Get(types.GetValidatorKey(addr)) - if err != nil { + value, err := k.Validators.Get(ctx, addr) + if err != nil && !errors.Is(err, collections.ErrNotFound) { return validator, err } - if value == nil { return validator, types.ErrNoValidatorFound } @@ -69,9 +67,8 @@ func (k Keeper) mustGetValidatorByConsAddr(ctx context.Context, consAddr sdk.Con // SetValidator sets the main record holding validator details func (k Keeper) SetValidator(ctx context.Context, validator types.Validator) error { - store := k.storeService.OpenKVStore(ctx) bz := types.MustMarshalValidator(k.cdc, &validator) - return store.Set(types.GetValidatorKey(validator.GetOperator()), bz) + return k.Validators.Set(ctx, validator.GetOperator(), bz) } // SetValidatorByConsAddr sets a validator by conesensus address @@ -216,7 +213,7 @@ func (k Keeper) RemoveValidator(ctx context.Context, address sdk.ValAddress) err // delete the old validator record store := k.storeService.OpenKVStore(ctx) - if err = store.Delete(types.GetValidatorKey(address)); err != nil { + if err = k.Validators.Remove(ctx, address); err != nil { return err } diff --git a/x/staking/migrations/v2/store_test.go b/x/staking/migrations/v2/store_test.go index da808b2a3aa8..3d35faeb08e1 100644 --- a/x/staking/migrations/v2/store_test.go +++ b/x/staking/migrations/v2/store_test.go @@ -12,6 +12,7 @@ import ( sdktestuil "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/address" v1 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v1" v2 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v2" "github.com/cosmos/cosmos-sdk/x/staking/testutil" @@ -54,7 +55,7 @@ func TestStoreMigration(t *testing.T) { { "ValidatorsKey", v1.GetValidatorKey(valAddr1), - types.GetValidatorKey(valAddr1), + getValidatorKey(valAddr1), }, { "ValidatorsByConsAddrKey", @@ -138,3 +139,7 @@ func TestStoreMigration(t *testing.T) { }) } } + +func getValidatorKey(operatorAddr sdk.ValAddress) []byte { + return append(types.ValidatorsKey, address.MustLengthPrefix(operatorAddr)...) +} diff --git a/x/staking/simulation/decoder_test.go b/x/staking/simulation/decoder_test.go index 0179794929b1..af2e8b87bcf0 100644 --- a/x/staking/simulation/decoder_test.go +++ b/x/staking/simulation/decoder_test.go @@ -12,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" + sdkaddress "github.com/cosmos/cosmos-sdk/types/address" "github.com/cosmos/cosmos-sdk/types/kv" "github.com/cosmos/cosmos-sdk/types/module/testutil" "github.com/cosmos/cosmos-sdk/x/staking/simulation" @@ -40,7 +41,7 @@ func TestDecodeStore(t *testing.T) { kvPairs := kv.Pairs{ Pairs: []kv.Pair{ {Key: types.LastTotalPowerKey, Value: oneIntBz}, - {Key: types.GetValidatorKey(valAddr1), Value: cdc.MustMarshal(&val)}, + {Key: append(types.ValidatorsKey, sdkaddress.MustLengthPrefix(valAddr1)...), Value: cdc.MustMarshal(&val)}, {Key: types.LastValidatorPowerKey, Value: valAddr1.Bytes()}, {Key: types.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshal(&del)}, {Key: types.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshal(&ubd)}, diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 61da6f3eda24..45bc3f4a0b30 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -34,7 +34,7 @@ var ( LastValidatorPowerKey = []byte{0x11} // prefix for each key to a validator index, for bonded validators LastTotalPowerKey = collections.NewPrefix(18) // prefix for the total power - ValidatorsKey = []byte{0x21} // prefix for each key to a validator + ValidatorsKey = collections.NewPrefix(33) // prefix for each key to a validator ValidatorsByConsAddrKey = collections.NewPrefix(34) // prefix for each key to a validator index, by pubkey ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power @@ -78,11 +78,11 @@ func GetUnbondingIndexKey(id uint64) []byte { return append(UnbondingIndexKey, bz...) } -// GetValidatorKey creates the key for the validator with address -// VALUE: staking/Validator -func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { - return append(ValidatorsKey, address.MustLengthPrefix(operatorAddr)...) -} +// // GetValidatorKey creates the key for the validator with address +// // VALUE: staking/Validator +// func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { +// return append(ValidatorsKey, address.MustLengthPrefix(operatorAddr)...) +// } // AddressFromValidatorsKey creates the validator operator address from ValidatorsKey func AddressFromValidatorsKey(key []byte) []byte { From 19c6504b9807bfd2f3ebbcb00189122d3c703028 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 10 Aug 2023 11:32:17 +0530 Subject: [PATCH 04/18] fix tests --- .../staking/keeper/deterministic_test.go | 16 ++++++++-------- x/staking/keeper/unbonding.go | 7 +++---- x/staking/simulation/decoder_test.go | 5 ----- x/staking/types/keys.go | 10 +++++----- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/tests/integration/staking/keeper/deterministic_test.go b/tests/integration/staking/keeper/deterministic_test.go index 15b607ad9586..3592c22a1f9c 100644 --- a/tests/integration/staking/keeper/deterministic_test.go +++ b/tests/integration/staking/keeper/deterministic_test.go @@ -355,7 +355,7 @@ func TestGRPCValidator(t *testing.T) { ValidatorAddr: val.OperatorAddress, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Validator, 1915, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Validator, 1912, false) } func TestGRPCValidators(t *testing.T) { @@ -381,7 +381,7 @@ func TestGRPCValidators(t *testing.T) { getStaticValidator(t, f) getStaticValidator2(t, f) - testdata.DeterministicIterations(f.ctx, t, &stakingtypes.QueryValidatorsRequest{}, f.queryClient.Validators, 2862, false) + testdata.DeterministicIterations(f.ctx, t, &stakingtypes.QueryValidatorsRequest{}, f.queryClient.Validators, 2853, false) } func TestGRPCValidatorDelegations(t *testing.T) { @@ -420,7 +420,7 @@ func TestGRPCValidatorDelegations(t *testing.T) { ValidatorAddr: validator.OperatorAddress, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.ValidatorDelegations, 14475, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.ValidatorDelegations, 14466, false) } func TestGRPCValidatorUnbondingDelegations(t *testing.T) { @@ -499,7 +499,7 @@ func TestGRPCDelegation(t *testing.T) { DelegatorAddr: delegator1, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Delegation, 4635, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Delegation, 4632, false) } func TestGRPCUnbondingDelegation(t *testing.T) { @@ -572,7 +572,7 @@ func TestGRPCDelegatorDelegations(t *testing.T) { DelegatorAddr: delegator1, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorDelegations, 4238, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorDelegations, 4235, false) } func TestGRPCDelegatorValidator(t *testing.T) { @@ -606,7 +606,7 @@ func TestGRPCDelegatorValidator(t *testing.T) { ValidatorAddr: validator.OperatorAddress, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorValidator, 3563, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorValidator, 3560, false) } func TestGRPCDelegatorUnbondingDelegations(t *testing.T) { @@ -736,7 +736,7 @@ func TestGRPCDelegatorValidators(t *testing.T) { assert.NilError(t, err) req := &stakingtypes.QueryDelegatorValidatorsRequest{DelegatorAddr: delegator1} - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorValidators, 3166, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorValidators, 3163, false) } func TestGRPCPool(t *testing.T) { @@ -816,7 +816,7 @@ func TestGRPCRedelegations(t *testing.T) { DstValidatorAddr: validator2, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Redelegations, 3920, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Redelegations, 3917, false) } func TestGRPCParams(t *testing.T) { diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index bd874392b86e..ce3be3bec109 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -113,8 +113,6 @@ func (k Keeper) GetRedelegationByUnbondingID(ctx context.Context, id uint64) (re // GetValidatorByUnbondingID returns the validator that is unbonding with a certain unbonding op ID func (k Keeper) GetValidatorByUnbondingID(ctx context.Context, id uint64) (val types.Validator, err error) { - store := k.storeService.OpenKVStore(ctx) - valKey, err := k.UnbondingIndex.Get(ctx, id) if err != nil { if errors.Is(err, collections.ErrNotFound) { @@ -127,8 +125,8 @@ func (k Keeper) GetValidatorByUnbondingID(ctx context.Context, id uint64) (val t return types.Validator{}, types.ErrNoValidatorFound } - value, err := store.Get(valKey) - if err != nil { + value, err := k.Validators.Get(ctx, valKey) + if err != nil && !errors.Is(err, collections.ErrNotFound) { return types.Validator{}, err } @@ -203,6 +201,7 @@ func (k Keeper) SetValidatorByUnbondingID(ctx context.Context, val types.Validat } valKey := types.GetValidatorKey(valAddr) + valKey = valKey[2:] // leave prefix bytes if err = k.UnbondingIndex.Set(ctx, id, valKey); err != nil { return err } diff --git a/x/staking/simulation/decoder_test.go b/x/staking/simulation/decoder_test.go index e88e5a51b4f1..9874e8db5095 100644 --- a/x/staking/simulation/decoder_test.go +++ b/x/staking/simulation/decoder_test.go @@ -12,7 +12,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" - sdkaddress "github.com/cosmos/cosmos-sdk/types/address" "github.com/cosmos/cosmos-sdk/types/kv" "github.com/cosmos/cosmos-sdk/types/module/testutil" "github.com/cosmos/cosmos-sdk/x/staking/simulation" @@ -30,8 +29,6 @@ func TestDecodeStore(t *testing.T) { dec := simulation.NewDecodeStore(cdc) bondTime := time.Now().UTC() - val, err := types.NewValidator(valAddr1.String(), delPk1, types.NewDescription("test", "test", "test", "test", "test")) - require.NoError(t, err) ubd := types.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, math.OneInt(), 1, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos")) red := types.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, math.OneInt(), math.LegacyOneDec(), 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos")) oneIntBz, err := math.OneInt().Marshal() @@ -40,7 +37,6 @@ func TestDecodeStore(t *testing.T) { kvPairs := kv.Pairs{ Pairs: []kv.Pair{ {Key: types.LastTotalPowerKey, Value: oneIntBz}, - {Key: append(types.ValidatorsKey, sdkaddress.MustLengthPrefix(valAddr1)...), Value: cdc.MustMarshal(&val)}, {Key: types.LastValidatorPowerKey, Value: valAddr1.Bytes()}, {Key: types.GetUBDKey(delAddr1, valAddr1), Value: cdc.MustMarshal(&ubd)}, {Key: types.GetREDKey(delAddr1, valAddr1, valAddr1), Value: cdc.MustMarshal(&red)}, @@ -53,7 +49,6 @@ func TestDecodeStore(t *testing.T) { expectedLog string }{ {"LastTotalPower", fmt.Sprintf("%v\n%v", math.OneInt(), math.OneInt())}, - {"Validator", fmt.Sprintf("%v\n%v", val, val)}, {"LastValidatorPower/ValidatorsByConsAddr/ValidatorsByPowerIndex", fmt.Sprintf("%v\n%v", valAddr1, valAddr1)}, {"UnbondingDelegation", fmt.Sprintf("%v\n%v", ubd, ubd)}, {"Redelegation", fmt.Sprintf("%v\n%v", red, red)}, diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 4e7d3a059fd8..8de4b66e8f0e 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -71,11 +71,11 @@ const ( UnbondingType_ValidatorUnbonding ) -// // GetValidatorKey creates the key for the validator with address -// // VALUE: staking/Validator -// func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { -// return append(ValidatorsKey, address.MustLengthPrefix(operatorAddr)...) -// } +// GetValidatorKey creates the key for the validator with address +// VALUE: staking/Validator +func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { + return append(ValidatorsKey, address.MustLengthPrefix(operatorAddr)...) +} // AddressFromValidatorsKey creates the validator operator address from ValidatorsKey func AddressFromValidatorsKey(key []byte) []byte { From 37e1327d0e0777de3f07f479a4b3b5d1a20483af Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Thu, 10 Aug 2023 11:42:33 +0530 Subject: [PATCH 05/18] nit: improve comment --- x/staking/keeper/unbonding.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index ce3be3bec109..1848fdd2ec85 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -201,7 +201,7 @@ func (k Keeper) SetValidatorByUnbondingID(ctx context.Context, val types.Validat } valKey := types.GetValidatorKey(valAddr) - valKey = valKey[2:] // leave prefix bytes + valKey = valKey[2:] // leave prefix bytes and address length if err = k.UnbondingIndex.Set(ctx, id, valKey); err != nil { return err } From 5b8b650c935be62614cdaa01078cb523e425a515 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 16 Aug 2023 12:43:00 +0530 Subject: [PATCH 06/18] fix test --- x/staking/migrations/v2/store_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/staking/migrations/v2/store_test.go b/x/staking/migrations/v2/store_test.go index 72e0af01bcac..6cc45bb59e79 100644 --- a/x/staking/migrations/v2/store_test.go +++ b/x/staking/migrations/v2/store_test.go @@ -13,7 +13,7 @@ import ( sdktestuil "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/address" + sdkaddress "github.com/cosmos/cosmos-sdk/types/address" v1 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v1" v2 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v2" "github.com/cosmos/cosmos-sdk/x/staking/testutil" @@ -142,5 +142,5 @@ func TestStoreMigration(t *testing.T) { } func getValidatorKey(operatorAddr sdk.ValAddress) []byte { - return append(types.ValidatorsKey, address.MustLengthPrefix(operatorAddr)...) + return append(types.ValidatorsKey, sdkaddress.MustLengthPrefix(operatorAddr)...) } From 6fd522b0d5c6f5ce7cbcf45429ac27641cc502c9 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Fri, 18 Aug 2023 16:05:13 +0530 Subject: [PATCH 07/18] fix compatibility --- x/staking/keeper/keeper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 5ae99bc43fc6..de77460fa3ea 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -121,7 +121,7 @@ func NewKeeper( codec.CollValue[types.Redelegation](cdc), ), UnbondingIndex: collections.NewMap(sb, types.UnbondingIndexKey, "unbonding_index", collections.Uint64Key, collections.BytesValue), - Validators: collections.NewMap(sb, types.ValidatorsKey, "validators", sdk.ValAddressKey, collections.BytesValue), + Validators: collections.NewMap(sb, types.ValidatorsKey, "validators", sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), collections.BytesValue), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility } schema, err := sb.Build() From 04a0fcfc98f70f5696b073f6b04c91e0b0e8ed58 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Fri, 18 Aug 2023 16:43:06 +0530 Subject: [PATCH 08/18] fix integration tests --- .../staking/keeper/deterministic_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/integration/staking/keeper/deterministic_test.go b/tests/integration/staking/keeper/deterministic_test.go index 1d1b279db159..fb27be24ea96 100644 --- a/tests/integration/staking/keeper/deterministic_test.go +++ b/tests/integration/staking/keeper/deterministic_test.go @@ -358,7 +358,7 @@ func TestGRPCValidator(t *testing.T) { ValidatorAddr: val.OperatorAddress, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Validator, 1912, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Validator, 1915, false) } func TestGRPCValidators(t *testing.T) { @@ -384,7 +384,7 @@ func TestGRPCValidators(t *testing.T) { getStaticValidator(t, f) getStaticValidator2(t, f) - testdata.DeterministicIterations(f.ctx, t, &stakingtypes.QueryValidatorsRequest{}, f.queryClient.Validators, 2853, false) + testdata.DeterministicIterations(f.ctx, t, &stakingtypes.QueryValidatorsRequest{}, f.queryClient.Validators, 2862, false) } func TestGRPCValidatorDelegations(t *testing.T) { @@ -423,7 +423,7 @@ func TestGRPCValidatorDelegations(t *testing.T) { ValidatorAddr: validator.OperatorAddress, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.ValidatorDelegations, 14466, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.ValidatorDelegations, 14475, false) } func TestGRPCValidatorUnbondingDelegations(t *testing.T) { @@ -503,7 +503,7 @@ func TestGRPCDelegation(t *testing.T) { DelegatorAddr: delegator1, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Delegation, 4632, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Delegation, 4635, false) } func TestGRPCUnbondingDelegation(t *testing.T) { @@ -578,7 +578,7 @@ func TestGRPCDelegatorDelegations(t *testing.T) { DelegatorAddr: delegator1, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorDelegations, 4235, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorDelegations, 4238, false) } func TestGRPCDelegatorValidator(t *testing.T) { @@ -612,7 +612,7 @@ func TestGRPCDelegatorValidator(t *testing.T) { ValidatorAddr: validator.OperatorAddress, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorValidator, 3560, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorValidator, 3563, false) } func TestGRPCDelegatorUnbondingDelegations(t *testing.T) { @@ -743,7 +743,7 @@ func TestGRPCDelegatorValidators(t *testing.T) { assert.NilError(t, err) req := &stakingtypes.QueryDelegatorValidatorsRequest{DelegatorAddr: delegator1} - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorValidators, 3163, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.DelegatorValidators, 3166, false) } func TestGRPCPool(t *testing.T) { @@ -823,7 +823,7 @@ func TestGRPCRedelegations(t *testing.T) { DstValidatorAddr: validator2, } - testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Redelegations, 3917, false) + testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.Redelegations, 3920, false) } func TestGRPCParams(t *testing.T) { From ed4b3c2038ed441f19abb006a742f08998802254 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Fri, 18 Aug 2023 17:01:19 +0530 Subject: [PATCH 09/18] remove GetValidatorKey --- CHANGELOG.md | 2 ++ x/staking/keeper/unbonding.go | 4 +--- x/staking/types/keys.go | 6 ------ 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52fc4329f1d4..c081d432f77b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (x/staking) [#17123](https://github.com/cosmos/cosmos-sdk/pull/17123) Use collections for `Validators`: + * remove from `types`: `GetValidatorKey` * (x/staking) [#17315](https://github.com/cosmos/cosmos-sdk/pull/17044) Use collections for `RedelegationKey`: * remove from `keeper`: `GetRedelegation` * (types) `module.BeginBlockAppModule` has been replaced by Core API `appmodule.HasBeginBlocker`. diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index 1848fdd2ec85..b3d5ccd0f8de 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -200,9 +200,7 @@ func (k Keeper) SetValidatorByUnbondingID(ctx context.Context, val types.Validat return err } - valKey := types.GetValidatorKey(valAddr) - valKey = valKey[2:] // leave prefix bytes and address length - if err = k.UnbondingIndex.Set(ctx, id, valKey); err != nil { + if err = k.UnbondingIndex.Set(ctx, id, valAddr); err != nil { return err } diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index f9d3cb9cb246..efe60d47b4bb 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -72,12 +72,6 @@ const ( UnbondingType_ValidatorUnbonding ) -// GetValidatorKey creates the key for the validator with address -// VALUE: staking/Validator -func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { - return append(ValidatorsKey, address.MustLengthPrefix(operatorAddr)...) -} - // AddressFromValidatorsKey creates the validator operator address from ValidatorsKey func AddressFromValidatorsKey(key []byte) []byte { kv.AssertKeyAtLeastLength(key, 3) From 6f91527107be6c54a09fd3f3523752c8d8dd186c Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Tue, 22 Aug 2023 11:49:13 +0530 Subject: [PATCH 10/18] add diff test for validator --- x/staking/keeper/keeper_test.go | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/x/staking/keeper/keeper_test.go b/x/staking/keeper/keeper_test.go index 46aabdcabb4c..39932ad47f55 100644 --- a/x/staking/keeper/keeper_test.go +++ b/x/staking/keeper/keeper_test.go @@ -2,6 +2,7 @@ package keeper_test import ( "testing" + "time" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" cmttime "github.com/cometbft/cometbft/types/time" @@ -13,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec/address" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" @@ -39,11 +41,13 @@ type KeeperTestSuite struct { accountKeeper *stakingtestutil.MockAccountKeeper queryClient stakingtypes.QueryClient msgServer stakingtypes.MsgServer + key *storetypes.KVStoreKey } func (s *KeeperTestSuite) SetupTest() { require := s.Require() key := storetypes.NewKVStoreKey(stakingtypes.StoreKey) + s.key = key storeService := runtime.NewKVStoreService(key) testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test")) ctx := testCtx.Ctx.WithBlockHeader(cmtproto.Header{Time: cmttime.Now()}) @@ -112,3 +116,37 @@ func (s *KeeperTestSuite) TestLastTotalPower() { func TestKeeperTestSuite(t *testing.T) { suite.Run(t, new(KeeperTestSuite)) } + +func (s *KeeperTestSuite) TestDiffCollsMigration() { + s.SetupTest() + pkAny, err := codectypes.NewAnyWithValue(PKs[0]) + s.Require().NoError(err) + + _, valAddrs := createValAddrs(100) + err = testutil.DiffCollectionsMigration( + s.ctx, + s.key, + 100, + func(i int64) { + val := stakingtypes.Validator{ + OperatorAddress: valAddrs[i].String(), + ConsensusPubkey: pkAny, + Jailed: false, + Status: stakingtypes.Bonded, + Tokens: sdk.DefaultPowerReduction, + DelegatorShares: math.LegacyOneDec(), + Description: stakingtypes.Description{}, + UnbondingHeight: int64(0), + UnbondingTime: time.Unix(0, 0).UTC(), + Commission: stakingtypes.NewCommission(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()), + MinSelfDelegation: math.ZeroInt(), + } + + err := s.stakingKeeper.SetValidator(s.ctx, val) + s.Require().NoError(err) + }, + "6a8737af6309d53494a601e900832ec27763adefd7fe8ff104477d8130d7405f", + ) + + s.Require().NoError(err) +} From 3cbc149461d6cc59cce3fb73f10ca88ad7de6507 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Tue, 22 Aug 2023 17:37:49 +0530 Subject: [PATCH 11/18] update validators to use Validator type instead of bytes --- .../staking/keeper/msg_server_test.go | 2 +- x/staking/keeper/delegation_test.go | 4 +- x/staking/keeper/keeper.go | 4 +- x/staking/keeper/keeper_test.go | 37 ++++++++++++++++++- x/staking/keeper/msg_server_test.go | 10 ++--- x/staking/keeper/unbonding.go | 12 +----- x/staking/keeper/unbonding_test.go | 3 +- x/staking/keeper/validator.go | 13 ++----- x/staking/keeper/validator_test.go | 10 +++-- 9 files changed, 59 insertions(+), 36 deletions(-) diff --git a/tests/integration/staking/keeper/msg_server_test.go b/tests/integration/staking/keeper/msg_server_test.go index f84c305795dc..782438275bb6 100644 --- a/tests/integration/staking/keeper/msg_server_test.go +++ b/tests/integration/staking/keeper/msg_server_test.go @@ -113,7 +113,7 @@ func TestCancelUnbondingDelegation(t *testing.T) { Amount: unbondingAmount, CreationHeight: 10, }, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "invalid delegator address", diff --git a/x/staking/keeper/delegation_test.go b/x/staking/keeper/delegation_test.go index bfe84d66e771..0dc5fcc8d171 100644 --- a/x/staking/keeper/delegation_test.go +++ b/x/staking/keeper/delegation_test.go @@ -598,7 +598,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondedValidator() { // now validator should be deleted from state validator, err = keeper.GetValidator(ctx, addrVals[0]) - require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) + require.ErrorIs(err, collections.ErrNotFound) } func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() { @@ -665,7 +665,7 @@ func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() { // validator should now be deleted from state _, err = keeper.GetValidator(ctx, addrVals[0]) - require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) + require.ErrorIs(err, collections.ErrNotFound) } // Make sure that that the retrieving the delegations doesn't affect the state diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index de77460fa3ea..173cfd01fc1a 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -44,7 +44,7 @@ type Keeper struct { Redelegations collections.Map[collections.Triple[[]byte, []byte, []byte], types.Redelegation] Delegations collections.Map[collections.Pair[sdk.AccAddress, sdk.ValAddress], types.Delegation] UnbondingIndex collections.Map[uint64, []byte] - Validators collections.Map[sdk.ValAddress, []byte] + Validators collections.Map[sdk.ValAddress, types.Validator] } // NewKeeper creates a new staking Keeper instance @@ -121,7 +121,7 @@ func NewKeeper( codec.CollValue[types.Redelegation](cdc), ), UnbondingIndex: collections.NewMap(sb, types.UnbondingIndexKey, "unbonding_index", collections.Uint64Key, collections.BytesValue), - Validators: collections.NewMap(sb, types.ValidatorsKey, "validators", sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), collections.BytesValue), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility + Validators: collections.NewMap(sb, types.ValidatorsKey, "validators", sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), codec.CollValue[types.Validator](cdc)), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility } schema, err := sb.Build() diff --git a/x/staking/keeper/keeper_test.go b/x/staking/keeper/keeper_test.go index 39932ad47f55..cee7ebe1bb36 100644 --- a/x/staking/keeper/keeper_test.go +++ b/x/staking/keeper/keeper_test.go @@ -13,12 +13,14 @@ import ( storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/address" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + addresstypes "github.com/cosmos/cosmos-sdk/types/address" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" @@ -42,6 +44,7 @@ type KeeperTestSuite struct { queryClient stakingtypes.QueryClient msgServer stakingtypes.MsgServer key *storetypes.KVStoreKey + cdc codec.Codec } func (s *KeeperTestSuite) SetupTest() { @@ -52,6 +55,7 @@ func (s *KeeperTestSuite) SetupTest() { testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test")) ctx := testCtx.Ctx.WithBlockHeader(cmtproto.Header{Time: cmttime.Now()}) encCfg := moduletestutil.MakeTestEncodingConfig() + s.cdc = encCfg.Codec ctrl := gomock.NewController(s.T()) accountKeeper := stakingtestutil.NewMockAccountKeeper(ctrl) @@ -117,12 +121,44 @@ func TestKeeperTestSuite(t *testing.T) { suite.Run(t, new(KeeperTestSuite)) } +func getValidatorKey(operatorAddr sdk.ValAddress) []byte { + validatorsKey := []byte{0x21} + return append(validatorsKey, addresstypes.MustLengthPrefix(operatorAddr)...) +} + func (s *KeeperTestSuite) TestDiffCollsMigration() { s.SetupTest() pkAny, err := codectypes.NewAnyWithValue(PKs[0]) s.Require().NoError(err) _, valAddrs := createValAddrs(100) + + err = testutil.DiffCollectionsMigration( + s.ctx, + s.key, + 100, + func(i int64) { + val := stakingtypes.Validator{ + OperatorAddress: valAddrs[i].String(), + ConsensusPubkey: pkAny, + Jailed: false, + Status: stakingtypes.Bonded, + Tokens: sdk.DefaultPowerReduction, + DelegatorShares: math.LegacyOneDec(), + Description: stakingtypes.Description{}, + UnbondingHeight: int64(0), + UnbondingTime: time.Unix(0, 0).UTC(), + Commission: stakingtypes.NewCommission(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()), + MinSelfDelegation: math.ZeroInt(), + } + valBz := stakingtypes.MustMarshalValidator(s.cdc, &val) + // legacy Set method + s.ctx.KVStore(s.key).Set(getValidatorKey(valAddrs[i]), valBz) + }, + "6a8737af6309d53494a601e900832ec27763adefd7fe8ff104477d8130d7405f", + ) + s.Require().NoError(err) + err = testutil.DiffCollectionsMigration( s.ctx, s.key, @@ -147,6 +183,5 @@ func (s *KeeperTestSuite) TestDiffCollsMigration() { }, "6a8737af6309d53494a601e900832ec27763adefd7fe8ff104477d8130d7405f", ) - s.Require().NoError(err) } diff --git a/x/staking/keeper/msg_server_test.go b/x/staking/keeper/msg_server_test.go index bc3fa965e338..1ad77215de5a 100644 --- a/x/staking/keeper/msg_server_test.go +++ b/x/staking/keeper/msg_server_test.go @@ -341,7 +341,7 @@ func (s *KeeperTestSuite) TestMsgEditValidator() { MinSelfDelegation: &newSelfDel, }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "change commmission rate in <24hrs", @@ -478,7 +478,7 @@ func (s *KeeperTestSuite) TestMsgDelegate() { Amount: sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: keeper.TokensFromConsensusPower(s.ctx, int64(100))}, }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "zero amount", @@ -631,7 +631,7 @@ func (s *KeeperTestSuite) TestMsgBeginRedelegate() { Amount: sdk.NewCoin(sdk.DefaultBondDenom, shares.RoundInt()), }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "self redelegation", @@ -770,7 +770,7 @@ func (s *KeeperTestSuite) TestMsgUndelegate() { Amount: sdk.NewCoin(sdk.DefaultBondDenom, shares.RoundInt()), }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "amount greater than delegated shares amount", @@ -939,7 +939,7 @@ func (s *KeeperTestSuite) TestMsgCancelUnbondingDelegation() { CreationHeight: 10, }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "amount is greater than balance", diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index b3d5ccd0f8de..a318c81a0324 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -125,17 +125,7 @@ func (k Keeper) GetValidatorByUnbondingID(ctx context.Context, id uint64) (val t return types.Validator{}, types.ErrNoValidatorFound } - value, err := k.Validators.Get(ctx, valKey) - if err != nil && !errors.Is(err, collections.ErrNotFound) { - return types.Validator{}, err - } - - if value == nil { - return types.Validator{}, types.ErrNoValidatorFound - } - - val, err = types.UnmarshalValidator(k.cdc, value) - // An error here means that what we got wasn't the right type + val, err = k.Validators.Get(ctx, valKey) if err != nil { return types.Validator{}, err } diff --git a/x/staking/keeper/unbonding_test.go b/x/staking/keeper/unbonding_test.go index 65e7c9de835b..eb997f6e07a1 100644 --- a/x/staking/keeper/unbonding_test.go +++ b/x/staking/keeper/unbonding_test.go @@ -3,6 +3,7 @@ package keeper_test import ( "time" + "cosmossdk.io/collections" "cosmossdk.io/math" addresscodec "github.com/cosmos/cosmos-sdk/codec/address" @@ -263,7 +264,7 @@ func (s *KeeperTestSuite) TestValidatorByUnbondingIDAccessors() { require.NoError(err) require.Equal(tc.validator, val) } else { - require.ErrorIs(err, types.ErrNoValidatorFound) + require.Error(err, collections.ErrNotFound) } }) } diff --git a/x/staking/keeper/validator.go b/x/staking/keeper/validator.go index 4d7ea2922122..a0a90d2ee43e 100644 --- a/x/staking/keeper/validator.go +++ b/x/staking/keeper/validator.go @@ -22,15 +22,11 @@ import ( // GetValidator gets a single validator func (k Keeper) GetValidator(ctx context.Context, addr sdk.ValAddress) (validator types.Validator, err error) { - value, err := k.Validators.Get(ctx, addr) - if err != nil && !errors.Is(err, collections.ErrNotFound) { + validator, err = k.Validators.Get(ctx, addr) + if err != nil { return validator, err } - if value == nil { - return validator, types.ErrNoValidatorFound - } - - return types.UnmarshalValidator(k.cdc, value) + return validator, nil } func (k Keeper) mustGetValidator(ctx context.Context, addr sdk.ValAddress) types.Validator { @@ -67,12 +63,11 @@ func (k Keeper) mustGetValidatorByConsAddr(ctx context.Context, consAddr sdk.Con // SetValidator sets the main record holding validator details func (k Keeper) SetValidator(ctx context.Context, validator types.Validator) error { - bz := types.MustMarshalValidator(k.cdc, &validator) valBz, err := k.ValidatorAddressCodec().StringToBytes(validator.GetOperator()) if err != nil { panic(err) } - return k.Validators.Set(ctx, sdk.ValAddress(valBz), bz) + return k.Validators.Set(ctx, sdk.ValAddress(valBz), validator) } // SetValidatorByConsAddr sets a validator by conesensus address diff --git a/x/staking/keeper/validator_test.go b/x/staking/keeper/validator_test.go index f5015f7844a2..7a66f4faa6dd 100644 --- a/x/staking/keeper/validator_test.go +++ b/x/staking/keeper/validator_test.go @@ -1,11 +1,13 @@ package keeper_test import ( + "fmt" "time" abci "github.com/cometbft/cometbft/abci/types" "github.com/golang/mock/gomock" + "cosmossdk.io/collections" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -109,7 +111,7 @@ func (s *KeeperTestSuite) TestValidatorBasics() { // check the empty keeper first _, err := keeper.GetValidator(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) - require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) + require.ErrorIs(err, collections.ErrNotFound) resVals, err := keeper.GetLastValidators(ctx) require.NoError(err) require.Zero(len(resVals)) @@ -188,7 +190,7 @@ func (s *KeeperTestSuite) TestValidatorBasics() { require.NoError(keeper.SetValidator(ctx, validators[1])) // ...set the validator require.NoError(keeper.RemoveValidator(ctx, bz)) // Now it can be removed. _, err = keeper.GetValidator(ctx, sdk.ValAddress(PKs[1].Address().Bytes())) - require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) + require.ErrorIs(err, collections.ErrNotFound) } func (s *KeeperTestSuite) TestUpdateValidatorByPowerIndex() { @@ -416,7 +418,7 @@ func (s *KeeperTestSuite) TestUnbondingValidator() { // check unbonding mature validators ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime) err = keeper.UnbondAllMatureValidators(ctx) - require.EqualError(err, "validator in the unbonding queue was not found: validator does not exist") + require.ErrorContains(err, fmt.Sprintf("validator in the unbonding queue was not found: %s", collections.ErrNotFound)) require.NoError(keeper.SetValidator(ctx, validator)) ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime) @@ -428,7 +430,7 @@ func (s *KeeperTestSuite) TestUnbondingValidator() { require.NoError(keeper.SetValidator(ctx, validator)) require.NoError(keeper.UnbondAllMatureValidators(ctx)) validator, err = keeper.GetValidator(ctx, valAddr) - require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) + require.ErrorIs(err, collections.ErrNotFound) require.NoError(keeper.SetUnbondingValidatorsQueue(ctx, endTime, endHeight, []string{valAddr.String()})) validator = testutil.NewValidator(s.T(), valAddr, valPubKey) From 65b394b4936fb5446950ab8c6f207559e9b0ca29 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Tue, 22 Aug 2023 17:40:38 +0530 Subject: [PATCH 12/18] remove panic --- x/staking/keeper/validator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staking/keeper/validator.go b/x/staking/keeper/validator.go index a0a90d2ee43e..311ca53fedef 100644 --- a/x/staking/keeper/validator.go +++ b/x/staking/keeper/validator.go @@ -65,7 +65,7 @@ func (k Keeper) mustGetValidatorByConsAddr(ctx context.Context, consAddr sdk.Con func (k Keeper) SetValidator(ctx context.Context, validator types.Validator) error { valBz, err := k.ValidatorAddressCodec().StringToBytes(validator.GetOperator()) if err != nil { - panic(err) + return err } return k.Validators.Set(ctx, sdk.ValAddress(valBz), validator) } From 6b9d0f10e6ad00f7ad885ca2d8309e7fd10ca757 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 23 Aug 2023 11:54:17 +0530 Subject: [PATCH 13/18] fix integration tests --- tests/integration/distribution/keeper/grpc_query_test.go | 6 +++--- tests/integration/distribution/keeper/msg_server_test.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/integration/distribution/keeper/grpc_query_test.go b/tests/integration/distribution/keeper/grpc_query_test.go index 995885e6480a..d5fa927baf17 100644 --- a/tests/integration/distribution/keeper/grpc_query_test.go +++ b/tests/integration/distribution/keeper/grpc_query_test.go @@ -124,7 +124,7 @@ func TestGRPCValidatorOutstandingRewards(t *testing.T) { name: "invalid address", msg: &types.QueryValidatorOutstandingRewardsRequest{ValidatorAddress: sdk.ValAddress("addr1_______________").String()}, expPass: false, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "valid request", @@ -192,7 +192,7 @@ func TestGRPCValidatorCommission(t *testing.T) { name: "invalid validator", msg: &types.QueryValidatorCommissionRequest{ValidatorAddress: sdk.ValAddress("addr1_______________").String()}, expPass: false, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "valid request", @@ -571,7 +571,7 @@ func TestGRPCDelegationRewards(t *testing.T) { ValidatorAddress: valAddr2.String(), }, expPass: false, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "valid request", diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index b0f579d5bc48..59d6c14c254d 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -264,7 +264,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { ValidatorAddress: sdk.ValAddress(sdk.AccAddress(PKS[2].Address())).String(), }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, { name: "valid msg", @@ -948,7 +948,7 @@ func TestMsgDepositValidatorRewardsPool(t *testing.T) { Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, math.NewInt(100))), }, expErr: true, - expErrMsg: "validator does not exist", + expErrMsg: "not found", }, } From 5b57bdd7e4f1976fcd446d46c3a1cbcaa3190c6d Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 23 Aug 2023 16:58:19 +0530 Subject: [PATCH 14/18] retain same error on collections not found error --- .../distribution/keeper/grpc_query_test.go | 6 +++--- .../distribution/keeper/msg_server_test.go | 4 ++-- tests/integration/staking/keeper/msg_server_test.go | 2 +- x/staking/keeper/delegation_test.go | 4 ++-- x/staking/keeper/msg_server_test.go | 12 ++++++------ x/staking/keeper/unbonding.go | 3 +++ x/staking/keeper/unbonding_test.go | 3 +-- x/staking/keeper/validator.go | 3 +++ x/staking/keeper/validator_test.go | 9 ++++----- 9 files changed, 25 insertions(+), 21 deletions(-) diff --git a/tests/integration/distribution/keeper/grpc_query_test.go b/tests/integration/distribution/keeper/grpc_query_test.go index d5fa927baf17..995885e6480a 100644 --- a/tests/integration/distribution/keeper/grpc_query_test.go +++ b/tests/integration/distribution/keeper/grpc_query_test.go @@ -124,7 +124,7 @@ func TestGRPCValidatorOutstandingRewards(t *testing.T) { name: "invalid address", msg: &types.QueryValidatorOutstandingRewardsRequest{ValidatorAddress: sdk.ValAddress("addr1_______________").String()}, expPass: false, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "valid request", @@ -192,7 +192,7 @@ func TestGRPCValidatorCommission(t *testing.T) { name: "invalid validator", msg: &types.QueryValidatorCommissionRequest{ValidatorAddress: sdk.ValAddress("addr1_______________").String()}, expPass: false, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "valid request", @@ -571,7 +571,7 @@ func TestGRPCDelegationRewards(t *testing.T) { ValidatorAddress: valAddr2.String(), }, expPass: false, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "valid request", diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index 59d6c14c254d..b0f579d5bc48 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -264,7 +264,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { ValidatorAddress: sdk.ValAddress(sdk.AccAddress(PKS[2].Address())).String(), }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "valid msg", @@ -948,7 +948,7 @@ func TestMsgDepositValidatorRewardsPool(t *testing.T) { Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, math.NewInt(100))), }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, } diff --git a/tests/integration/staking/keeper/msg_server_test.go b/tests/integration/staking/keeper/msg_server_test.go index 782438275bb6..f84c305795dc 100644 --- a/tests/integration/staking/keeper/msg_server_test.go +++ b/tests/integration/staking/keeper/msg_server_test.go @@ -113,7 +113,7 @@ func TestCancelUnbondingDelegation(t *testing.T) { Amount: unbondingAmount, CreationHeight: 10, }, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "invalid delegator address", diff --git a/x/staking/keeper/delegation_test.go b/x/staking/keeper/delegation_test.go index 0dc5fcc8d171..bfe84d66e771 100644 --- a/x/staking/keeper/delegation_test.go +++ b/x/staking/keeper/delegation_test.go @@ -598,7 +598,7 @@ func (s *KeeperTestSuite) TestUndelegateFromUnbondedValidator() { // now validator should be deleted from state validator, err = keeper.GetValidator(ctx, addrVals[0]) - require.ErrorIs(err, collections.ErrNotFound) + require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) } func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() { @@ -665,7 +665,7 @@ func (s *KeeperTestSuite) TestUnbondingAllDelegationFromValidator() { // validator should now be deleted from state _, err = keeper.GetValidator(ctx, addrVals[0]) - require.ErrorIs(err, collections.ErrNotFound) + require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) } // Make sure that that the retrieving the delegations doesn't affect the state diff --git a/x/staking/keeper/msg_server_test.go b/x/staking/keeper/msg_server_test.go index 1ad77215de5a..f305a05fd17e 100644 --- a/x/staking/keeper/msg_server_test.go +++ b/x/staking/keeper/msg_server_test.go @@ -341,7 +341,7 @@ func (s *KeeperTestSuite) TestMsgEditValidator() { MinSelfDelegation: &newSelfDel, }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "change commmission rate in <24hrs", @@ -478,7 +478,7 @@ func (s *KeeperTestSuite) TestMsgDelegate() { Amount: sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: keeper.TokensFromConsensusPower(s.ctx, int64(100))}, }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "zero amount", @@ -631,7 +631,7 @@ func (s *KeeperTestSuite) TestMsgBeginRedelegate() { Amount: sdk.NewCoin(sdk.DefaultBondDenom, shares.RoundInt()), }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "self redelegation", @@ -770,7 +770,7 @@ func (s *KeeperTestSuite) TestMsgUndelegate() { Amount: sdk.NewCoin(sdk.DefaultBondDenom, shares.RoundInt()), }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "amount greater than delegated shares amount", @@ -898,7 +898,7 @@ func (s *KeeperTestSuite) TestMsgCancelUnbondingDelegation() { expErrMsg: "invalid delegator address: decoding bech32 failed", }, { - name: "entry not found at height", + name: "entry validator does not exist at height", input: &stakingtypes.MsgCancelUnbondingDelegation{ DelegatorAddress: Addr.String(), ValidatorAddress: ValAddr.String(), @@ -939,7 +939,7 @@ func (s *KeeperTestSuite) TestMsgCancelUnbondingDelegation() { CreationHeight: 10, }, expErr: true, - expErrMsg: "not found", + expErrMsg: "validator does not exist", }, { name: "amount is greater than balance", diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index a318c81a0324..1ab118ba4876 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -127,6 +127,9 @@ func (k Keeper) GetValidatorByUnbondingID(ctx context.Context, id uint64) (val t val, err = k.Validators.Get(ctx, valKey) if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return types.Validator{}, types.ErrNoValidatorFound + } return types.Validator{}, err } diff --git a/x/staking/keeper/unbonding_test.go b/x/staking/keeper/unbonding_test.go index eb997f6e07a1..fe1ed4b2b6b1 100644 --- a/x/staking/keeper/unbonding_test.go +++ b/x/staking/keeper/unbonding_test.go @@ -3,7 +3,6 @@ package keeper_test import ( "time" - "cosmossdk.io/collections" "cosmossdk.io/math" addresscodec "github.com/cosmos/cosmos-sdk/codec/address" @@ -264,7 +263,7 @@ func (s *KeeperTestSuite) TestValidatorByUnbondingIDAccessors() { require.NoError(err) require.Equal(tc.validator, val) } else { - require.Error(err, collections.ErrNotFound) + require.Error(err, types.ErrNoValidatorFound) } }) } diff --git a/x/staking/keeper/validator.go b/x/staking/keeper/validator.go index 311ca53fedef..4169110024c6 100644 --- a/x/staking/keeper/validator.go +++ b/x/staking/keeper/validator.go @@ -24,6 +24,9 @@ import ( func (k Keeper) GetValidator(ctx context.Context, addr sdk.ValAddress) (validator types.Validator, err error) { validator, err = k.Validators.Get(ctx, addr) if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return types.Validator{}, types.ErrNoValidatorFound + } return validator, err } return validator, nil diff --git a/x/staking/keeper/validator_test.go b/x/staking/keeper/validator_test.go index 7a66f4faa6dd..5f6c3e833eff 100644 --- a/x/staking/keeper/validator_test.go +++ b/x/staking/keeper/validator_test.go @@ -7,7 +7,6 @@ import ( abci "github.com/cometbft/cometbft/abci/types" "github.com/golang/mock/gomock" - "cosmossdk.io/collections" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -111,7 +110,7 @@ func (s *KeeperTestSuite) TestValidatorBasics() { // check the empty keeper first _, err := keeper.GetValidator(ctx, sdk.ValAddress(PKs[0].Address().Bytes())) - require.ErrorIs(err, collections.ErrNotFound) + require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) resVals, err := keeper.GetLastValidators(ctx) require.NoError(err) require.Zero(len(resVals)) @@ -190,7 +189,7 @@ func (s *KeeperTestSuite) TestValidatorBasics() { require.NoError(keeper.SetValidator(ctx, validators[1])) // ...set the validator require.NoError(keeper.RemoveValidator(ctx, bz)) // Now it can be removed. _, err = keeper.GetValidator(ctx, sdk.ValAddress(PKs[1].Address().Bytes())) - require.ErrorIs(err, collections.ErrNotFound) + require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) } func (s *KeeperTestSuite) TestUpdateValidatorByPowerIndex() { @@ -418,7 +417,7 @@ func (s *KeeperTestSuite) TestUnbondingValidator() { // check unbonding mature validators ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime) err = keeper.UnbondAllMatureValidators(ctx) - require.ErrorContains(err, fmt.Sprintf("validator in the unbonding queue was not found: %s", collections.ErrNotFound)) + require.ErrorContains(err, fmt.Sprintf("validator in the unbonding queue was not found: %s", stakingtypes.ErrNoValidatorFound)) require.NoError(keeper.SetValidator(ctx, validator)) ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime) @@ -430,7 +429,7 @@ func (s *KeeperTestSuite) TestUnbondingValidator() { require.NoError(keeper.SetValidator(ctx, validator)) require.NoError(keeper.UnbondAllMatureValidators(ctx)) validator, err = keeper.GetValidator(ctx, valAddr) - require.ErrorIs(err, collections.ErrNotFound) + require.ErrorIs(err, stakingtypes.ErrNoValidatorFound) require.NoError(keeper.SetUnbondingValidatorsQueue(ctx, endTime, endHeight, []string{valAddr.String()})) validator = testutil.NewValidator(s.T(), valAddr, valPubKey) From 3b4067db2a13b00f7cee5c985824da46d15f5b4d Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 23 Aug 2023 17:05:07 +0530 Subject: [PATCH 15/18] cleanup --- x/staking/keeper/msg_server_test.go | 2 +- x/staking/keeper/unbonding_test.go | 2 +- x/staking/keeper/validator_test.go | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/x/staking/keeper/msg_server_test.go b/x/staking/keeper/msg_server_test.go index f305a05fd17e..bc3fa965e338 100644 --- a/x/staking/keeper/msg_server_test.go +++ b/x/staking/keeper/msg_server_test.go @@ -898,7 +898,7 @@ func (s *KeeperTestSuite) TestMsgCancelUnbondingDelegation() { expErrMsg: "invalid delegator address: decoding bech32 failed", }, { - name: "entry validator does not exist at height", + name: "entry not found at height", input: &stakingtypes.MsgCancelUnbondingDelegation{ DelegatorAddress: Addr.String(), ValidatorAddress: ValAddr.String(), diff --git a/x/staking/keeper/unbonding_test.go b/x/staking/keeper/unbonding_test.go index fe1ed4b2b6b1..65e7c9de835b 100644 --- a/x/staking/keeper/unbonding_test.go +++ b/x/staking/keeper/unbonding_test.go @@ -263,7 +263,7 @@ func (s *KeeperTestSuite) TestValidatorByUnbondingIDAccessors() { require.NoError(err) require.Equal(tc.validator, val) } else { - require.Error(err, types.ErrNoValidatorFound) + require.ErrorIs(err, types.ErrNoValidatorFound) } }) } diff --git a/x/staking/keeper/validator_test.go b/x/staking/keeper/validator_test.go index 5f6c3e833eff..f5015f7844a2 100644 --- a/x/staking/keeper/validator_test.go +++ b/x/staking/keeper/validator_test.go @@ -1,7 +1,6 @@ package keeper_test import ( - "fmt" "time" abci "github.com/cometbft/cometbft/abci/types" @@ -417,7 +416,7 @@ func (s *KeeperTestSuite) TestUnbondingValidator() { // check unbonding mature validators ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime) err = keeper.UnbondAllMatureValidators(ctx) - require.ErrorContains(err, fmt.Sprintf("validator in the unbonding queue was not found: %s", stakingtypes.ErrNoValidatorFound)) + require.EqualError(err, "validator in the unbonding queue was not found: validator does not exist") require.NoError(keeper.SetValidator(ctx, validator)) ctx = ctx.WithBlockHeight(endHeight).WithBlockTime(endTime) From e78ffa4a87410600d4caf59678cded8bfe49f47d Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Mon, 28 Aug 2023 11:39:23 +0530 Subject: [PATCH 16/18] address review comments --- x/staking/keeper/keeper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 61994332e84c..943c863b201a 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -44,7 +44,7 @@ type Keeper struct { Redelegations collections.Map[collections.Triple[[]byte, []byte, []byte], types.Redelegation] Delegations collections.Map[collections.Pair[sdk.AccAddress, sdk.ValAddress], types.Delegation] UnbondingIndex collections.Map[uint64, []byte] - Validators collections.Map[sdk.ValAddress, types.Validator] + Validators collections.Map[[]byte, types.Validator] RedelegationsByValDst collections.Map[collections.Triple[[]byte, []byte, []byte], []byte] RedelegationsByValSrc collections.Map[collections.Triple[[]byte, []byte, []byte], []byte] } @@ -146,7 +146,7 @@ func NewKeeper( ), collections.BytesValue, ), - Validators: collections.NewMap(sb, types.ValidatorsKey, "validators", sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), codec.CollValue[types.Validator](cdc)), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility + Validators: collections.NewMap(sb, types.ValidatorsKey, "validators", sdk.LengthPrefixedBytesKey, codec.CollValue[types.Validator](cdc)), // sdk.LengthPrefixedBytesKey is needed to retain state compatibility } schema, err := sb.Build() From 225478d0cf5e43108fa2c2e83fc7594f9a0efdad Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Mon, 28 Aug 2023 16:30:01 +0530 Subject: [PATCH 17/18] revert changes from unbonding.go --- CHANGELOG.md | 3 +-- x/staking/keeper/keeper_test.go | 8 ++++---- x/staking/keeper/unbonding.go | 18 +++++++++++++----- x/staking/types/keys.go | 6 ++++++ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f51e8c50932c..5cfb9864cb50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,8 +56,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes -* (x/staking) [#17123](https://github.com/cosmos/cosmos-sdk/pull/17123) Use collections for `Validators`: - * remove from `types`: `GetValidatorKey` +* (x/staking) [#17123](https://github.com/cosmos/cosmos-sdk/pull/17123) Use collections for `Validators` * (x/staking) [#17270](https://github.com/cosmos/cosmos-sdk/pull/17270) Use collections for `UnbondingDelegation`: * remove from `types`: `GetUBDsKey` * remove from `Keeper`: `IterateUnbondingDelegations`, `IterateDelegatorUnbondingDelegations` diff --git a/x/staking/keeper/keeper_test.go b/x/staking/keeper/keeper_test.go index 425d27776814..66f0227a89f4 100644 --- a/x/staking/keeper/keeper_test.go +++ b/x/staking/keeper/keeper_test.go @@ -299,6 +299,10 @@ func (s *KeeperTestSuite) TestUnbondingDelegationsMigrationToColls() { s.Require().NoError(err) } +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, new(KeeperTestSuite)) +} + func (s *KeeperTestSuite) TestValidatorsMigrationToColls() { s.SetupTest() pkAny, err := codectypes.NewAnyWithValue(PKs[0]) @@ -358,7 +362,3 @@ func (s *KeeperTestSuite) TestValidatorsMigrationToColls() { ) s.Require().NoError(err) } - -func TestKeeperTestSuite(t *testing.T) { - suite.Run(t, new(KeeperTestSuite)) -} diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index 2623fb223a35..73a10412f139 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -109,6 +109,7 @@ func (k Keeper) GetRedelegationByUnbondingID(ctx context.Context, id uint64) (re // GetValidatorByUnbondingID returns the validator that is unbonding with a certain unbonding op ID func (k Keeper) GetValidatorByUnbondingID(ctx context.Context, id uint64) (val types.Validator, err error) { + store := k.storeService.OpenKVStore(ctx) valKey, err := k.UnbondingIndex.Get(ctx, id) if err != nil { if errors.Is(err, collections.ErrNotFound) { @@ -120,12 +121,18 @@ func (k Keeper) GetValidatorByUnbondingID(ctx context.Context, id uint64) (val t if valKey == nil { return types.Validator{}, types.ErrNoValidatorFound } + value, err := store.Get(valKey) + if err != nil { + return types.Validator{}, err + } + + if value == nil { + return types.Validator{}, types.ErrNoValidatorFound + } - val, err = k.Validators.Get(ctx, valKey) + val, err = types.UnmarshalValidator(k.cdc, value) + // An error here means that what we got wasn't the right type if err != nil { - if errors.Is(err, collections.ErrNotFound) { - return types.Validator{}, types.ErrNoValidatorFound - } return types.Validator{}, err } @@ -189,7 +196,8 @@ func (k Keeper) SetValidatorByUnbondingID(ctx context.Context, val types.Validat return err } - if err = k.UnbondingIndex.Set(ctx, id, valAddr); err != nil { + valKey := types.GetValidatorKey(valAddr) + if err = k.UnbondingIndex.Set(ctx, id, valKey); err != nil { return err } diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 437542deea4e..3e4c08427bd3 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -72,6 +72,12 @@ const ( UnbondingType_ValidatorUnbonding ) +// GetValidatorKey creates the key for the validator with address +// VALUE: staking/Validator +func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { + return append(ValidatorsKey, address.MustLengthPrefix(operatorAddr)...) +} + // AddressFromValidatorsKey creates the validator operator address from ValidatorsKey func AddressFromValidatorsKey(key []byte) []byte { kv.AssertKeyAtLeastLength(key, 3) From 260ec306154e415a02eff8b17e28c6ca75913f88 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Mon, 28 Aug 2023 16:31:02 +0530 Subject: [PATCH 18/18] nits --- x/staking/keeper/unbonding.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index 73a10412f139..b3507526b207 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -110,6 +110,7 @@ func (k Keeper) GetRedelegationByUnbondingID(ctx context.Context, id uint64) (re // GetValidatorByUnbondingID returns the validator that is unbonding with a certain unbonding op ID func (k Keeper) GetValidatorByUnbondingID(ctx context.Context, id uint64) (val types.Validator, err error) { store := k.storeService.OpenKVStore(ctx) + valKey, err := k.UnbondingIndex.Get(ctx, id) if err != nil { if errors.Is(err, collections.ErrNotFound) { @@ -121,6 +122,7 @@ func (k Keeper) GetValidatorByUnbondingID(ctx context.Context, id uint64) (val t if valKey == nil { return types.Validator{}, types.ErrNoValidatorFound } + value, err := store.Get(valKey) if err != nil { return types.Validator{}, err