Skip to content

Commit

Permalink
refactor: remove redundant code from MakeConsumerGenesis (backport #1807
Browse files Browse the repository at this point in the history
) (#1856)

refactor: remove redundant code from MakeConsumerGenesis (#1807)

* remove redundant code from MakeConsumerGenesis

* fix tests

(cherry picked from commit 28d6982)

Co-authored-by: Marius Poke <[email protected]>
  • Loading branch information
mergify[bot] and mpoke authored May 2, 2024
1 parent 71666b1 commit 6bab323
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 26 deletions.
2 changes: 1 addition & 1 deletion testutil/keeper/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
26 changes: 2 additions & 24 deletions x/ccv/provider/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/provider/keeper/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down

0 comments on commit 6bab323

Please sign in to comment.