Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kirdatatjana committed Oct 24, 2024
1 parent a78038f commit 5ef9ff2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
22 changes: 7 additions & 15 deletions x/ccv/provider/keeper/power_shaping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -627,33 +628,24 @@ 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
}

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)
}
}
Expand Down
5 changes: 2 additions & 3 deletions x/ccv/provider/keeper/power_shaping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

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

Expand Down

0 comments on commit 5ef9ff2

Please sign in to comment.