Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Validate consumer commission rate against minimal rate #1834

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions testutil/keeper/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions x/ccv/provider/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,15 @@ func (k Keeper) HandleSetConsumerCommissionRate(ctx sdk.Context, chainID string,
types.ErrUnknownConsumerChainId,
"unknown consumer chain, with id: %s", chainID)
}

// validate against the minimum commission rate
minRate := k.stakingKeeper.MinCommissionRate(ctx)
if commissionRate.LT(minRate) {
return errorsmod.Wrapf(
stakingtypes.ErrCommissionLTMinRate,
"commission rate cannot be less than %s", minRate,
)
}
// set per-consumer chain commission rate for the validator address
return k.SetConsumerCommissionRate(
ctx,
Expand Down
23 changes: 22 additions & 1 deletion x/ccv/provider/keeper/partial_set_security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestHandleOptOutFromTopNChain(t *testing.T) {
}

func TestHandleSetConsumerCommissionRate(t *testing.T) {
providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()

providerAddr := types.NewProviderConsAddress([]byte("providerAddr"))
Expand All @@ -166,12 +166,33 @@ func TestHandleSetConsumerCommissionRate(t *testing.T) {
_, found := providerKeeper.GetConsumerCommissionRate(ctx, chainID, providerAddr)
require.False(t, found)

mocks.MockStakingKeeper.EXPECT().MinCommissionRate(ctx).Return(sdk.ZeroDec()).Times(1)
require.NoError(t, providerKeeper.HandleSetConsumerCommissionRate(ctx, chainID, providerAddr, sdk.OneDec()))

// check that the commission rate is now set
cr, found := providerKeeper.GetConsumerCommissionRate(ctx, chainID, providerAddr)
require.Equal(t, sdk.OneDec(), cr)
require.True(t, found)

// set minimum rate of 1/2
commissionRate := sdk.NewDec(1).Quo(sdk.NewDec(2))
mocks.MockStakingKeeper.EXPECT().MinCommissionRate(ctx).Return(commissionRate).AnyTimes()

// try to set a rate slightly below the minimum
require.Error(t, providerKeeper.HandleSetConsumerCommissionRate(
ctx,
chainID,
providerAddr,
commissionRate.Sub(sdk.NewDec(1).Quo(sdk.NewDec(100)))), // 0.5 - 0.01
"commission rate should be rejected (below min), but is not",
)

// set a valid commission equal to the minimum
require.NoError(t, providerKeeper.HandleSetConsumerCommissionRate(ctx, chainID, providerAddr, commissionRate))
// check that the rate was set
cr, found = providerKeeper.GetConsumerCommissionRate(ctx, chainID, providerAddr)
require.Equal(t, commissionRate, cr)
require.True(t, found)
}

func TestOptInTopNValidators(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions x/ccv/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type StakingKeeper interface {
GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sdk.ValAddress) (ubds []stakingtypes.UnbondingDelegation)
GetRedelegationsFromSrcValidator(ctx sdk.Context, valAddr sdk.ValAddress) (reds []stakingtypes.Redelegation)
GetUnbondingType(ctx sdk.Context, id uint64) (unbondingType stakingtypes.UnbondingType, found bool)
MinCommissionRate(ctx sdk.Context) math.LegacyDec
}

// SlashingKeeper defines the contract expected to perform ccv slashing
Expand Down
Loading