diff --git a/x/ccv/provider/keeper/power_shaping.go b/x/ccv/provider/keeper/power_shaping.go index 8dda5437d1..6828ec0032 100644 --- a/x/ccv/provider/keeper/power_shaping.go +++ b/x/ccv/provider/keeper/power_shaping.go @@ -89,6 +89,7 @@ func (k Keeper) UpdateMinimumPowerInTopN(ctx sdk.Context, consumerId string, old } // CapValidatorSet caps the provided `validators` if chain with `consumerId` is an Opt In chain with a validator-set cap. +// If cap is `k`, `CapValidatorSet` returns the first `k` validators from `validators`. func (k Keeper) CapValidatorSet( ctx sdk.Context, powerShapingParameters types.PowerShapingParameters, @@ -333,7 +334,7 @@ func (k Keeper) SetConsumerPowerShapingParameters(ctx sdk.Context, consumerId st store.Set(types.ConsumerIdToPowerShapingParametersKey(consumerId), bz) - // update allowlist, denylist and priority indexes if needed + // update allowlist, denylist and prioritylist indexes if needed if !equalStringSlices(oldParameters.Allowlist, parameters.Allowlist) { k.UpdateAllowlist(ctx, consumerId, parameters.Allowlist) } @@ -627,9 +628,9 @@ func (k Keeper) UpdatePrioritylist(ctx sdk.Context, consumerId string, priorityl } } -// FilterAndSortPriorityList filters the priority list to include only validators that can validate the chain +// PartitionBasedOnPriorityList filters the priority list to include only validators that can validate the chain // and splits the validators into priority and non-priority sets. -func (k Keeper) FilterAndSortPriorityList(ctx sdk.Context, consumerId string, nextValidators []types.ConsensusValidator) ([]types.ConsensusValidator, []types.ConsensusValidator) { +func (k Keeper) PartitionBasedOnPriorityList(ctx sdk.Context, consumerId string, nextValidators []types.ConsensusValidator) ([]types.ConsensusValidator, []types.ConsensusValidator) { validatorMap := make(map[string]types.ConsensusValidator) for _, v := range nextValidators { validatorMap[string(v.ProviderConsAddr)] = v @@ -637,23 +638,14 @@ func (k Keeper) FilterAndSortPriorityList(ctx sdk.Context, consumerId string, ne priorityValidators := make([]types.ConsensusValidator, 0) nonPriorityValidators := make([]types.ConsensusValidator, 0) - addedAddresses := make(map[string]bool) // Form priorityValidators for _, validator := range nextValidators { addr := providertypes.NewProviderConsAddress(validator.ProviderConsAddr) if k.IsPrioritylisted(ctx, consumerId, addr) { - if !addedAddresses[string(validator.ProviderConsAddr)] { - priorityValidators = append(priorityValidators, validator) - addedAddresses[string(validator.ProviderConsAddr)] = true - } - } - } - - // Add remaining validators to nonPriorityValidators - for _, validator := range nextValidators { - address := string(validator.ProviderConsAddr) - if !addedAddresses[address] { + priorityValidators = append(priorityValidators, validator) + } else { + // Add remaining validators to nonPriorityValidators nonPriorityValidators = append(nonPriorityValidators, validator) } } diff --git a/x/ccv/provider/keeper/power_shaping_test.go b/x/ccv/provider/keeper/power_shaping_test.go index 65a890ba25..8ce8746ed6 100644 --- a/x/ccv/provider/keeper/power_shaping_test.go +++ b/x/ccv/provider/keeper/power_shaping_test.go @@ -250,7 +250,6 @@ func TestCapValidatorSet(t *testing.T) { providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) defer ctrl.Finish() - // Define validator addresses with clear names valAddrA := "cosmosvalcons1ezyrq65s3gshhx5585w6mpusq3xsj3ayzf4uv6" valAddrB := "cosmosvalcons1nx7n5uh0ztxsynn4sje6eyq2ud6rc6klc96w39" valAddrC := "cosmosvalcons1qmq08eruchr5sf5s3rwz7djpr5a25f7xw4mceq" @@ -265,7 +264,7 @@ func TestCapValidatorSet(t *testing.T) { // Initial error check powerShapingParameters, err := providerKeeper.GetConsumerPowerShapingParameters(ctx, CONSUMER_ID) require.Error(t, err) - priorityValidators, nonPriorityValidators := providerKeeper.FilterAndSortPriorityList(ctx, CONSUMER_ID, validators) + priorityValidators, nonPriorityValidators := providerKeeper.PartitionBasedOnPriorityList(ctx, CONSUMER_ID, validators) consumerValidators := providerKeeper.CapValidatorSet(ctx, powerShapingParameters, append(priorityValidators, nonPriorityValidators...)) require.Equal(t, []providertypes.ConsensusValidator{validatorD, validatorC, validatorB, validatorA}, consumerValidators) @@ -354,7 +353,7 @@ func TestCapValidatorSet(t *testing.T) { require.NoError(t, err) require.Equal(t, tc.powerShapingParameters, powerShapingParameters) } - priorityValidators, nonPriorityValidators := providerKeeper.FilterAndSortPriorityList(ctx, CONSUMER_ID, validators) + priorityValidators, nonPriorityValidators := providerKeeper.PartitionBasedOnPriorityList(ctx, CONSUMER_ID, validators) consumerValidators := providerKeeper.CapValidatorSet(ctx, powerShapingParameters, append(priorityValidators, nonPriorityValidators...)) require.Equal(t, tc.expectedValidators, consumerValidators) }) diff --git a/x/ccv/provider/keeper/validator_set_update.go b/x/ccv/provider/keeper/validator_set_update.go index 9df0c96650..265911d5d2 100644 --- a/x/ccv/provider/keeper/validator_set_update.go +++ b/x/ccv/provider/keeper/validator_set_update.go @@ -233,7 +233,7 @@ func (k Keeper) ComputeNextValidators( return []types.ConsensusValidator{}, err } - priorityValidators, nonPriorityValidators := k.FilterAndSortPriorityList(ctx, consumerId, nextValidators) + priorityValidators, nonPriorityValidators := k.PartitionBasedOnPriorityList(ctx, consumerId, nextValidators) nextValidators = k.CapValidatorSet(ctx, powerShapingParameters, append(priorityValidators, nonPriorityValidators...))