Skip to content

Commit

Permalink
chore: improve the validations of parameters (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing authored Sep 15, 2023
1 parent e6377de commit bf87a12
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
7 changes: 5 additions & 2 deletions x/gov/keeper/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@ func (k Keeper) SyncParams(ctx sdk.Context, destChainId sdk.ChainID, cpc govv1.C
Target: addresses,
}

encodedPackage := pack.MustSerialize()
encodedPackage, err := pack.Serialize()
if err != nil {
return sdkerrors.Wrapf(types.ErrInvalidSyncParamPackage, "fail to serialize, err: %s", err.Error())
}

if !k.crossChainKeeper.IsDestChainSupported(destChainId) {
return sdkerrors.Wrapf(types.ErrChainNotSupported, "destination chain (%d) is not supported", destChainId)
}

_, err := k.crossChainKeeper.CreateRawIBCPackageWithFee(
_, err = k.crossChainKeeper.CreateRawIBCPackageWithFee(
ctx,
destChainId,
types.SyncParamsChannelID,
Expand Down
6 changes: 3 additions & 3 deletions x/gov/types/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ var (
}
)

func (p SyncParamsPackage) MustSerialize() []byte {
func (p SyncParamsPackage) Serialize() ([]byte, error) {
encodedBytes, err := syncParamsPackageArgs.Pack(&p)
if err != nil {
panic("encode params change sync package error")
return nil, err
}
return encodedBytes
return encodedBytes, nil
}
2 changes: 1 addition & 1 deletion x/gov/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (
ErrAddressSizeNotMatch = errors.Register(ModuleName, 25, "number of old address not equal to new addresses")
ErrAddressNotValid = errors.Register(ModuleName, 26, "address format is not valid")
ErrExceedParamsChangeLimit = errors.Register(ModuleName, 27, "exceed params change limit")
ErrInvalidUpgradeProposal = errors.Register(ModuleName, 28, "invalid sync params package")
ErrInvalidSyncParamPackage = errors.Register(ModuleName, 28, "invalid sync params package")
ErrInvalidValue = errors.Register(ModuleName, 29, "decode hex value failed")

ErrChainNotSupported = errors.Register(ModuleName, 30, "crosschain: chain is not supported")
Expand Down
7 changes: 7 additions & 0 deletions x/gov/types/v1/crosschain.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package v1

import (
"encoding/hex"
"strings"

sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
Expand Down Expand Up @@ -36,6 +38,11 @@ func (m *CrossChainParamsChange) ValidateBasic() error {
if err != nil {
return types.ErrAddressNotValid
}
} else {
_, err := hex.DecodeString(value)
if err != nil {
return sdkerrors.Wrapf(types.ErrInvalidValue, "value is not valid %s", value)
}
}
_, err := sdk.AccAddressFromHexUnsafe(target)
if err != nil {
Expand Down

0 comments on commit bf87a12

Please sign in to comment.