From 10511d140dfcd84bb98171c67c4e4a727185047f Mon Sep 17 00:00:00 2001 From: Srinivas Baride Date: Sun, 11 Jul 2021 21:41:44 +0530 Subject: [PATCH] Improved validation of provider types --- x/provider/types/errors.go | 22 ++++++------- x/provider/types/msg.go | 63 +++++++++++++++++++----------------- x/provider/types/params.go | 19 +++++++++-- x/provider/types/provider.go | 32 +++++++++++++----- 4 files changed, 86 insertions(+), 50 deletions(-) diff --git a/x/provider/types/errors.go b/x/provider/types/errors.go index 744c4176..118fc589 100644 --- a/x/provider/types/errors.go +++ b/x/provider/types/errors.go @@ -5,15 +5,15 @@ import ( ) var ( - ErrorMarshal = errors.Register(ModuleName, 101, "error occurred while marshalling") - ErrorUnmarshal = errors.Register(ModuleName, 102, "error occurred while unmarshalling") - ErrorUnknownMsgType = errors.Register(ModuleName, 103, "unknown message type") - ErrorUnknownQueryType = errors.Register(ModuleName, 104, "unknown query type") - ErrorInvalidFieldName = errors.Register(ModuleName, 105, "invalid field name; expected length between 1 and 64") - ErrorDuplicateProvider = errors.Register(ModuleName, 106, "duplicate provider") - ErrorProviderDoesNotExist = errors.Register(ModuleName, 107, "provider does not exist") - ErrorInvalidFieldFrom = errors.Register(ModuleName, 108, "invalid field from; expected a valid address") - ErrorInvalidFieldIdentity = errors.Register(ModuleName, 109, "invalid field identity; expected length between 0 and 64") - ErrorInvalidFieldWebsite = errors.Register(ModuleName, 110, "invalid field website; expected length between 0 and 64") - ErrorInvalidFieldDescription = errors.Register(ModuleName, 111, "invalid field description; expected length between 0 and 256") + ErrorInvalidField = errors.Register(ModuleName, 101, "invalid field") + ErrorInvalidFrom = errors.Register(ModuleName, 102, "invalid from") + ErrorInvalidName = errors.Register(ModuleName, 103, "invalid name") + ErrorInvalidIdentity = errors.Register(ModuleName, 104, "invalid identity") + ErrorInvalidWebsite = errors.Register(ModuleName, 105, "invalid website") + ErrorInvalidDescription = errors.Register(ModuleName, 106, "invalid description") +) + +var ( + ErrorDuplicateProvider = errors.Register(ModuleName, 201, "duplicate provider") + ErrorProviderDoesNotExist = errors.Register(ModuleName, 202, "provider does not exist") ) diff --git a/x/provider/types/msg.go b/x/provider/types/msg.go index bf2db71b..72852f02 100644 --- a/x/provider/types/msg.go +++ b/x/provider/types/msg.go @@ -2,8 +2,10 @@ package types import ( "fmt" + "net/url" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/errors" hubtypes "github.com/sentinel-official/hub/types" ) @@ -32,28 +34,31 @@ func (m *MsgRegisterRequest) Type() string { } func (m *MsgRegisterRequest) ValidateBasic() error { + if m.From == "" { + return errors.Wrap(ErrorInvalidFrom, "from cannot be empty") + } if _, err := sdk.AccAddressFromBech32(m.From); err != nil { - return ErrorInvalidFieldFrom + return errors.Wrapf(ErrorInvalidFrom, "%s", err) } - - // Name length should be between 1 and 64 - if len(m.Name) == 0 || len(m.Name) > 64 { - return ErrorInvalidFieldName + if m.Name == "" { + return errors.Wrap(ErrorInvalidName, "name cannot be empty") + } + if len(m.Name) > 64 { + return errors.Wrapf(ErrorInvalidName, "name length cannot be greater than %d", 64) } - - // Identity length should be between 0 and 64 if len(m.Identity) > 64 { - return ErrorInvalidFieldIdentity + return errors.Wrapf(ErrorInvalidIdentity, "identity length cannot be greater than %d", 64) } - - // Website length should be between 0 and 64 - if len(m.Website) > 64 { - return ErrorInvalidFieldWebsite + if m.Website != "" { + if len(m.Website) > 64 { + return errors.Wrapf(ErrorInvalidWebsite, "website length cannot be greater than %d", 64) + } + if _, err := url.ParseRequestURI(m.Website); err != nil { + return errors.Wrapf(ErrorInvalidWebsite, "%s", err) + } } - - // Description length should be between 0 and 256 if len(m.Description) > 256 { - return ErrorInvalidFieldDescription + return errors.Wrapf(ErrorInvalidDescription, "description length cannot be greater than %d", 256) } return nil @@ -91,28 +96,28 @@ func (m *MsgUpdateRequest) Type() string { } func (m MsgUpdateRequest) ValidateBasic() error { + if m.From == "" { + return errors.Wrap(ErrorInvalidFrom, "from cannot be empty") + } if _, err := hubtypes.ProvAddressFromBech32(m.From); err != nil { - return ErrorInvalidFieldFrom + return errors.Wrapf(ErrorInvalidFrom, "%s", err) } - - // Name length should be between 0 and 64 if len(m.Name) > 64 { - return ErrorInvalidFieldName + return errors.Wrapf(ErrorInvalidName, "name length cannot be greater than %d", 64) } - - // Identity length should be between 0 and 64 if len(m.Identity) > 64 { - return ErrorInvalidFieldIdentity + return errors.Wrapf(ErrorInvalidIdentity, "identity length cannot be greater than %d", 64) } - - // Website length should be between 0 and 64 - if len(m.Website) > 64 { - return ErrorInvalidFieldWebsite + if m.Website != "" { + if len(m.Website) > 64 { + return errors.Wrapf(ErrorInvalidWebsite, "website length cannot be greater than %d", 64) + } + if _, err := url.ParseRequestURI(m.Website); err != nil { + return errors.Wrapf(ErrorInvalidWebsite, "%s", err) + } } - - // Description length should be between 0 and 256 if len(m.Description) > 256 { - return ErrorInvalidFieldDescription + return errors.Wrapf(ErrorInvalidDescription, "description length cannot be greater than %d", 256) } return nil diff --git a/x/provider/types/params.go b/x/provider/types/params.go index fd723dfd..1bf032ea 100644 --- a/x/provider/types/params.go +++ b/x/provider/types/params.go @@ -20,8 +20,11 @@ var ( ) func (p *Params) Validate() error { + if p.Deposit.IsNegative() { + return fmt.Errorf("deposit cannot be negative") + } if !p.Deposit.IsValid() { - return fmt.Errorf("deposit should be valid") + return fmt.Errorf("invalid deposit %s", p.Deposit) } return nil @@ -32,7 +35,19 @@ func (p *Params) ParamSetPairs() params.ParamSetPairs { { Key: KeyDeposit, Value: &p.Deposit, - ValidatorFn: func(_ interface{}) error { + ValidatorFn: func(v interface{}) error { + value, ok := v.(sdk.Coin) + if !ok { + return fmt.Errorf("invalid parameter type %T", v) + } + + if value.IsNegative() { + return fmt.Errorf("deposit cannot be negative") + } + if !value.IsValid() { + return fmt.Errorf("invalid deposit %s", value) + } + return nil }, }, diff --git a/x/provider/types/provider.go b/x/provider/types/provider.go index 820d6221..1039b3a0 100644 --- a/x/provider/types/provider.go +++ b/x/provider/types/provider.go @@ -2,6 +2,9 @@ package types import ( "fmt" + "net/url" + + "github.com/cosmos/cosmos-sdk/types/errors" hubtypes "github.com/sentinel-official/hub/types" ) @@ -20,23 +23,36 @@ func (p *Provider) GetAddress() hubtypes.ProvAddress { } func (p *Provider) Validate() error { + if p.Address == "" { + return fmt.Errorf("address cannot be empty") + } if _, err := hubtypes.ProvAddressFromBech32(p.Address); err != nil { - return fmt.Errorf("address should not be nil or empty") + return errors.Wrapf(err, "invalid address %s", p.Address) } - if len(p.Name) == 0 || len(p.Name) > 64 { - return fmt.Errorf("name length should be between 1 and 64") + if p.Name == "" { + return fmt.Errorf("name cannot be empty") + } + if len(p.Name) > 64 { + return fmt.Errorf("name length cannot be greater than %d", 64) } if len(p.Identity) > 64 { - return fmt.Errorf("identity length should be between 0 and 64") + return fmt.Errorf("identity length cannot be greater than %d", 64) } - if len(p.Website) > 64 { - return fmt.Errorf("website length should be between 0 and 64") + if p.Website != "" { + if len(p.Website) > 64 { + return fmt.Errorf("website length cannot be greater than %d", 64) + } + if _, err := url.ParseRequestURI(p.Website); err != nil { + return errors.Wrapf(err, "invalid website %s", p.Website) + } } if len(p.Description) > 256 { - return fmt.Errorf("description length should be between 0 and 256") + return fmt.Errorf("description length cannot be greater than %d", 256) } return nil } -type Providers []Provider +type ( + Providers []Provider +)