Skip to content

Commit

Permalink
add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Oct 3, 2024
1 parent 557a39b commit d8fa332
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
3 changes: 3 additions & 0 deletions docs/docs/build/modules/02-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,9 @@ where `update-consumer-msg.json` contains:
"denylist":[],
"min_stake": "1000",
"allow_inactive_vals":true
},
"allowlisted_reward_denoms": {
"denoms": ["ibc/0025F8A87464A471E66B234C4F93AEC5B4DA3D42D7986451A059273426290DD5"]
}
}
```
Expand Down
10 changes: 0 additions & 10 deletions x/ccv/provider/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,6 @@ func (k msgServer) CreateConsumer(goCtx context.Context, msg *types.MsgCreateCon
}

if msg.AllowlistedRewardDenoms != nil {
if len(msg.AllowlistedRewardDenoms.Denoms) > types.MaxAllowlistedRewardDenomsPerChain {
return &resp, errorsmod.Wrapf(types.ErrInvalidAllowlistedRewardDenoms,
fmt.Sprintf("a consumer chain cannot allowlist more than %d reward denoms", types.MaxAllowlistedRewardDenomsPerChain))
}

err := k.UpdateAllowlistedRewardDenoms(ctx, consumerId, msg.AllowlistedRewardDenoms.Denoms)
if err != nil {
return &resp, errorsmod.Wrapf(types.ErrInvalidAllowlistedRewardDenoms,
Expand Down Expand Up @@ -603,11 +598,6 @@ func (k msgServer) UpdateConsumer(goCtx context.Context, msg *types.MsgUpdateCon
}

if msg.AllowlistedRewardDenoms != nil {
if len(msg.AllowlistedRewardDenoms.Denoms) > types.MaxAllowlistedRewardDenomsPerChain {
return &resp, errorsmod.Wrapf(types.ErrInvalidAllowlistedRewardDenoms,
fmt.Sprintf("a consumer chain cannot allowlist more than %d reward denoms", types.MaxAllowlistedRewardDenomsPerChain))
}

if err := k.UpdateAllowlistedRewardDenoms(ctx, consumerId, msg.AllowlistedRewardDenoms.Denoms); err != nil {
return &resp, errorsmod.Wrapf(types.ErrInvalidAllowlistedRewardDenoms,
"cannot update allowlisted reward denoms: %s", err.Error())
Expand Down
27 changes: 27 additions & 0 deletions x/ccv/provider/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"encoding/json"
"fmt"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
"strings"

ibctmtypes "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
Expand Down Expand Up @@ -328,6 +329,12 @@ func (msg MsgCreateConsumer) ValidateBasic() error {
}
}

if msg.AllowlistedRewardDenoms != nil {
if err := ValidateAllowlistedRewardDenoms(*msg.AllowlistedRewardDenoms); err != nil {
return errorsmod.Wrapf(ErrInvalidMsgCreateConsumer, "AllowlistedRewardDenoms: %s", err.Error())
}
}

return nil
}

Expand Down Expand Up @@ -373,6 +380,12 @@ func (msg MsgUpdateConsumer) ValidateBasic() error {
}
}

if msg.AllowlistedRewardDenoms != nil {
if err := ValidateAllowlistedRewardDenoms(*msg.AllowlistedRewardDenoms); err != nil {
return errorsmod.Wrapf(ErrInvalidMsgUpdateConsumer, "AllowlistedRewardDenoms: %s", err.Error())
}
}

return nil
}

Expand Down Expand Up @@ -518,6 +531,20 @@ func ValidatePowerShapingParameters(powerShapingParameters PowerShapingParameter
return nil
}

// ValidateAllowlistedRewardDenoms validates the provided allowlisted reward denoms
func ValidateAllowlistedRewardDenoms(allowlistedRewardDenoms AllowlistedRewardDenoms) error {
if len(allowlistedRewardDenoms.Denoms) > MaxAllowlistedRewardDenomsPerChain {
return errorsmod.Wrapf(ErrInvalidAllowlistedRewardDenoms, "More than %d denoms", MaxAllowlistedRewardDenomsPerChain)
}

for _, denom := range allowlistedRewardDenoms.Denoms {
if err := types.ValidateIBCDenom(denom); err != nil {
return errorsmod.Wrapf(ErrInvalidAllowlistedRewardDenoms, "Invalid denom (%s): %s", denom, err.Error())
}
}
return nil
}

// ValidateInitializationParameters validates that all the provided parameters are in the expected range
func ValidateInitializationParameters(initializationParameters ConsumerInitializationParameters) error {
if initializationParameters.InitialHeight.IsZero() {
Expand Down

0 comments on commit d8fa332

Please sign in to comment.