From 07f73dee5a1ccd8375840a64a31429510bfec985 Mon Sep 17 00:00:00 2001 From: Marius Poke Date: Thu, 2 May 2024 14:23:15 +0200 Subject: [PATCH] refactor: remove redundant code from MakeConsumerGenesis (#1807) * remove redundant code from MakeConsumerGenesis * fix tests (cherry picked from commit 28d6982e7a3e671bd54840840210c6dac6d884a1) --- testutil/keeper/expectations.go | 2 +- x/ccv/provider/keeper/proposal.go | 26 ++------------------------ x/ccv/provider/keeper/proposal_test.go | 2 +- 3 files changed, 4 insertions(+), 26 deletions(-) diff --git a/testutil/keeper/expectations.go b/testutil/keeper/expectations.go index 5a37f3a164..34446bd0c3 100644 --- a/testutil/keeper/expectations.go +++ b/testutil/keeper/expectations.go @@ -58,7 +58,7 @@ func GetMocksForMakeConsumerGenesis(ctx sdk.Context, mocks *MockedKeepers, mocks.MockClientKeeper.EXPECT().GetSelfConsensusState(gomock.Any(), clienttypes.GetSelfHeight(ctx)).Return(&ibctmtypes.ConsensusState{}, nil).Times(1), - mocks.MockStakingKeeper.EXPECT().IterateLastValidatorPowers(gomock.Any(), gomock.Any()).Times(1), + mocks.MockStakingKeeper.EXPECT().GetLastValidators(gomock.Any()).Times(1), } } diff --git a/x/ccv/provider/keeper/proposal.go b/x/ccv/provider/keeper/proposal.go index 1d2bf19a4f..2e8c9b46d4 100644 --- a/x/ccv/provider/keeper/proposal.go +++ b/x/ccv/provider/keeper/proposal.go @@ -14,7 +14,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" tmtypes "github.com/cometbft/cometbft/types" @@ -260,29 +259,8 @@ func (k Keeper) MakeConsumerGenesis( return gen, nil, errorsmod.Wrapf(clienttypes.ErrConsensusStateNotFound, "error %s getting self consensus state for: %s", err, height) } - var lastPowers []stakingtypes.LastValidatorPower - - k.stakingKeeper.IterateLastValidatorPowers(ctx, func(addr sdk.ValAddress, power int64) (stop bool) { - lastPowers = append(lastPowers, stakingtypes.LastValidatorPower{Address: addr.String(), Power: power}) - return false - }) - - var bondedValidators []stakingtypes.Validator - - for _, p := range lastPowers { - addr, err := sdk.ValAddressFromBech32(p.Address) - if err != nil { - return gen, nil, err - } - - val, found := k.stakingKeeper.GetValidator(ctx, addr) - if !found { - return gen, nil, errorsmod.Wrapf(stakingtypes.ErrNoValidatorFound, "error getting validator from LastValidatorPowers: %s", err) - } - - // gather all the bonded validators in order to construct the consumer validator set for consumer chain `chainID` - bondedValidators = append(bondedValidators, val) - } + // get the bonded validators from the staking module + bondedValidators := k.stakingKeeper.GetLastValidators(ctx) if topN, found := k.GetTopN(ctx, chainID); found && topN > 0 { // in a Top-N chain, we automatically opt in all validators that belong to the top N diff --git a/x/ccv/provider/keeper/proposal_test.go b/x/ccv/provider/keeper/proposal_test.go index 9b769d2f94..80b4531b75 100644 --- a/x/ccv/provider/keeper/proposal_test.go +++ b/x/ccv/provider/keeper/proposal_test.go @@ -173,7 +173,7 @@ func TestCreateConsumerClient(t *testing.T) { mocks.MockStakingKeeper.EXPECT().UnbondingTime(gomock.Any()).Times(0) mocks.MockClientKeeper.EXPECT().CreateClient(gomock.Any(), gomock.Any(), gomock.Any()).Times(0) mocks.MockClientKeeper.EXPECT().GetSelfConsensusState(gomock.Any(), gomock.Any()).Times(0) - mocks.MockStakingKeeper.EXPECT().IterateLastValidatorPowers(gomock.Any(), gomock.Any()).Times(0) + mocks.MockStakingKeeper.EXPECT().GetLastValidators(gomock.Any()).Times(0) }, expClientCreated: false, },