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

refactor: remove class&nft id validation #13836

Merged
merged 7 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 0 additions & 6 deletions x/nft/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,6 @@ $ %s query %s nfts <class-id> --owner=<owner>
return err
}

if len(classID) > 0 {
if err := nft.ValidateClassID(classID); err != nil {
return err
}
}

if len(owner) == 0 && len(classID) == 0 {
return errors.ErrInvalidRequest.Wrap("must provide at least one of classID or owner")
}
Expand Down
9 changes: 5 additions & 4 deletions x/nft/genesis.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package nft

import (
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// ValidateGenesis check the given genesis state has no integrity issues
func ValidateGenesis(data GenesisState) error {
for _, class := range data.Classes {
if err := ValidateClassID(class.Id); err != nil {
return err
if len(class.Id) == 0 {
return errors.Wrap(ErrInvalidID, "Empty class id")
}
}
for _, entry := range data.Entries {
for _, nft := range entry.Nfts {
if err := ValidateNFTID(nft.Id); err != nil {
return err
if len(nft.Id) == 0 {
return errors.Wrap(ErrInvalidID, "Empty nft id")
}
if _, err := sdk.AccAddressFromBech32(entry.Owner); err != nil {
return err
Expand Down
34 changes: 15 additions & 19 deletions x/nft/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -18,8 +19,8 @@ func (k Keeper) Balance(goCtx context.Context, r *nft.QueryBalanceRequest) (*nft
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}

if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
if len(r.ClassId) == 0 {
return nil, errors.Wrap(nft.ErrInvalidID, "Empty class id")
}

owner, err := sdk.AccAddressFromBech32(r.Owner)
Expand All @@ -38,12 +39,12 @@ func (k Keeper) Owner(goCtx context.Context, r *nft.QueryOwnerRequest) (*nft.Que
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}

if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
if len(r.ClassId) == 0 {
return nil, errors.Wrap(nft.ErrInvalidID, "Empty class id")
}

if err := nft.ValidateNFTID(r.Id); err != nil {
return nil, err
if len(r.Id) == 0 {
return nil, errors.Wrap(nft.ErrInvalidID, "Empty nft id")
}

ctx := sdk.UnwrapSDKContext(goCtx)
Expand All @@ -57,8 +58,8 @@ func (k Keeper) Supply(goCtx context.Context, r *nft.QuerySupplyRequest) (*nft.Q
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}

if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
if len(r.ClassId) == 0 {
return nil, errors.Wrap(nft.ErrInvalidID, "Empty class id")
}
ctx := sdk.UnwrapSDKContext(goCtx)
supply := k.GetTotalSupply(ctx, r.ClassId)
Expand All @@ -73,11 +74,6 @@ func (k Keeper) NFTs(goCtx context.Context, r *nft.QueryNFTsRequest) (*nft.Query

var err error
var owner sdk.AccAddress
if len(r.ClassId) > 0 {
if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
}
}

if len(r.Owner) > 0 {
owner, err = sdk.AccAddressFromBech32(r.Owner)
Expand Down Expand Up @@ -138,11 +134,11 @@ func (k Keeper) NFT(goCtx context.Context, r *nft.QueryNFTRequest) (*nft.QueryNF
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}

if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
if len(r.ClassId) == 0 {
return nil, errors.Wrap(nft.ErrInvalidID, "Empty class id")
}
if err := nft.ValidateNFTID(r.Id); err != nil {
return nil, err
if len(r.Id) == 0 {
return nil, errors.Wrap(nft.ErrInvalidID, "Empty nft id")
}

ctx := sdk.UnwrapSDKContext(goCtx)
Expand All @@ -159,8 +155,8 @@ func (k Keeper) Class(goCtx context.Context, r *nft.QueryClassRequest) (*nft.Que
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}

if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
if len(r.ClassId) == 0 {
return nil, errors.Wrap(nft.ErrInvalidID, "Empty class id")
}

ctx := sdk.UnwrapSDKContext(goCtx)
Expand Down
14 changes: 7 additions & 7 deletions x/nft/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *TestSuite) TestBalance() {
func(index int, require *require.Assertions) {
req = &nft.QueryBalanceRequest{}
},
"invalid class id",
"Empty class id",
0,
func(index int, require *require.Assertions, res *nft.QueryBalanceResponse, expBalance uint64) {},
},
Expand Down Expand Up @@ -95,7 +95,7 @@ func (s *TestSuite) TestOwner() {
Id: testID,
}
},
"invalid class id",
"Empty class id",
func(index int, require *require.Assertions, res *nft.QueryOwnerResponse) {},
},
{
Expand All @@ -105,7 +105,7 @@ func (s *TestSuite) TestOwner() {
ClassId: testClassID,
}
},
"invalid nft id",
"Empty nft id",
func(index int, require *require.Assertions, res *nft.QueryOwnerResponse) {},
},
{
Expand Down Expand Up @@ -180,7 +180,7 @@ func (s *TestSuite) TestSupply() {
func(index int, require *require.Assertions) {
req = &nft.QuerySupplyRequest{}
},
"invalid class id",
"Empty class id",
0,
func(index int, require *require.Assertions, res *nft.QuerySupplyResponse, supply uint64) {},
},
Expand Down Expand Up @@ -393,7 +393,7 @@ func (s *TestSuite) TestNFT() {
func(index int, require *require.Assertions) {
req = &nft.QueryNFTRequest{}
},
"invalid class id",
"Empty class id",
func(index int, require *require.Assertions, res *nft.QueryNFTResponse) {},
},
{
Expand All @@ -403,7 +403,7 @@ func (s *TestSuite) TestNFT() {
ClassId: testClassID,
}
},
"invalid nft id",
"Empty nft id",
func(index int, require *require.Assertions, res *nft.QueryNFTResponse) {},
},
{
Expand Down Expand Up @@ -480,7 +480,7 @@ func (s *TestSuite) TestClass() {
func(index int, require *require.Assertions) {
req = &nft.QueryClassRequest{}
},
"invalid class id",
"Empty class id",
func(index int, require *require.Assertions, res *nft.QueryClassResponse) {},
},
{
Expand Down
13 changes: 7 additions & 6 deletions x/nft/msgs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nft

import (
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand All @@ -14,22 +15,22 @@ var _ sdk.Msg = &MsgSend{}

// ValidateBasic implements the Msg.ValidateBasic method.
func (m MsgSend) ValidateBasic() error {
if err := ValidateClassID(m.ClassId); err != nil {
return sdkerrors.Wrapf(ErrInvalidID, "Invalid class id (%s)", m.ClassId)
if len(m.ClassId) == 0 {
return errors.Wrap(ErrInvalidID, "Empty class id")
}

if err := ValidateNFTID(m.Id); err != nil {
return sdkerrors.Wrapf(ErrInvalidID, "Invalid nft id (%s)", m.Id)
if len(m.Id) == 0 {
return errors.Wrap(ErrInvalidID, "Empty nft id")
}

_, err := sdk.AccAddressFromBech32(m.Sender)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid sender address (%s)", m.Sender)
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid sender address (%s)", m.Sender)
}

_, err = sdk.AccAddressFromBech32(m.Receiver)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid receiver address (%s)", m.Receiver)
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid receiver address (%s)", m.Receiver)
}
return nil
}
Expand Down
35 changes: 0 additions & 35 deletions x/nft/validation.go

This file was deleted.