Skip to content

Commit

Permalink
chore(nft): remove address.String() calls from nft (#17846)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Sep 25, 2023
1 parent 284c955 commit f9c5fd4
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 48 deletions.
8 changes: 6 additions & 2 deletions x/nft/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *nft.GenesisState {
nfts := k.GetNFTsOfClass(ctx, class.Id)
for i, n := range nfts {
owner := k.GetOwner(ctx, n.ClassId, n.Id)
nftArr, ok := nftMap[owner.String()]
ownerStr, err := k.ac.BytesToString(owner.Bytes())
if err != nil {
panic(err)
}
nftArr, ok := nftMap[ownerStr]
if !ok {
nftArr = make([]*nft.NFT, 0)
}
nftMap[owner.String()] = append(nftArr, &nfts[i])
nftMap[ownerStr] = append(nftArr, &nfts[i])
}
}

Expand Down
9 changes: 8 additions & 1 deletion x/nft/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ func (k Keeper) Owner(goCtx context.Context, r *nft.QueryOwnerRequest) (*nft.Que

ctx := sdk.UnwrapSDKContext(goCtx)
owner := k.GetOwner(ctx, r.ClassId, r.Id)
return &nft.QueryOwnerResponse{Owner: owner.String()}, nil
if owner.Empty() {
return &nft.QueryOwnerResponse{Owner: ""}, nil
}
ownerstr, err := k.ac.BytesToString(owner.Bytes())
if err != nil {
return nil, err
}
return &nft.QueryOwnerResponse{Owner: ownerstr}, nil
}

// Supply return the number of NFTs from the given class, same as totalSupply of ERC721.
Expand Down
10 changes: 5 additions & 5 deletions x/nft/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *TestSuite) TestBalance() {
s.TestMint()
req = &nft.QueryBalanceRequest{
ClassId: testClassID,
Owner: s.addrs[0].String(),
Owner: s.encodedAddrs[0],
}
},
"",
Expand Down Expand Up @@ -145,7 +145,7 @@ func (s *TestSuite) TestOwner() {
ClassId: testClassID,
Id: testID,
}
owner = s.addrs[0].String()
owner = s.encodedAddrs[0]
},
"",
func(index int, require *require.Assertions, res *nft.QueryOwnerResponse) {
Expand Down Expand Up @@ -275,7 +275,7 @@ func (s *TestSuite) TestNFTs() {
"success,empty ClassId and no nft",
func(index int, require *require.Assertions) {
req = &nft.QueryNFTsRequest{
Owner: s.addrs[1].String(),
Owner: s.encodedAddrs[1],
}
s.TestSaveClass()
},
Expand Down Expand Up @@ -323,7 +323,7 @@ func (s *TestSuite) TestNFTs() {
}

req = &nft.QueryNFTsRequest{
Owner: s.addrs[2].String(),
Owner: s.encodedAddrs[2],
}
},
"",
Expand All @@ -348,7 +348,7 @@ func (s *TestSuite) TestNFTs() {
func(index int, require *require.Assertions) {
req = &nft.QueryNFTsRequest{
ClassId: testClassID,
Owner: s.addrs[0].String(),
Owner: s.encodedAddrs[0],
}
nfts = []*nft.NFT{
{
Expand Down
11 changes: 9 additions & 2 deletions x/nft/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type TestSuite struct {

ctx sdk.Context
addrs []sdk.AccAddress
encodedAddrs []string
queryClient nft.QueryClient
nftKeeper keeper.Keeper
accountKeeper *nfttestutil.MockAccountKeeper
Expand All @@ -64,6 +65,12 @@ func (s *TestSuite) SetupTest() {
accountKeeper.EXPECT().GetModuleAddress("nft").Return(s.addrs[0]).AnyTimes()
accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()

for _, addr := range s.addrs {
st, err := accountKeeper.AddressCodec().BytesToString(addr.Bytes())
s.Require().NoError(err)
s.encodedAddrs = append(s.encodedAddrs, st)
}

s.accountKeeper = accountKeeper

nftKeeper := keeper.NewKeeper(storeService, s.encCfg.Codec, accountKeeper, bankKeeper)
Expand Down Expand Up @@ -348,7 +355,7 @@ func (s *TestSuite) TestExportGenesis() {
expGenesis := &nft.GenesisState{
Classes: []*nft.Class{&class},
Entries: []*nft.Entry{{
Owner: s.addrs[0].String(),
Owner: s.encodedAddrs[0],
Nfts: []*nft.NFT{&expNFT},
}},
}
Expand All @@ -373,7 +380,7 @@ func (s *TestSuite) TestInitGenesis() {
expGenesis := &nft.GenesisState{
Classes: []*nft.Class{&expClass},
Entries: []*nft.Entry{{
Owner: s.addrs[0].String(),
Owner: s.encodedAddrs[0],
Nfts: []*nft.NFT{&expNFT},
}},
}
Expand Down
28 changes: 14 additions & 14 deletions x/nft/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *TestSuite) TestSend() {
expGenesis := &nft.GenesisState{
Classes: []*nft.Class{&ExpClass},
Entries: []*nft.Entry{{
Owner: s.addrs[0].String(),
Owner: s.encodedAddrs[0],
Nfts: []*nft.NFT{&ExpNFT},
}},
}
Expand All @@ -55,8 +55,8 @@ func (s *TestSuite) TestSend() {
req: &nft.MsgSend{
ClassId: testClassID,
Id: "",
Sender: s.addrs[0].String(),
Receiver: s.addrs[1].String(),
Sender: s.encodedAddrs[0],
Receiver: s.encodedAddrs[1],
},
expErr: true,
errMsg: "empty nft id",
Expand All @@ -66,8 +66,8 @@ func (s *TestSuite) TestSend() {
req: &nft.MsgSend{
ClassId: "",
Id: testID,
Sender: s.addrs[0].String(),
Receiver: s.addrs[1].String(),
Sender: s.encodedAddrs[0],
Receiver: s.encodedAddrs[1],
},
expErr: true,
errMsg: "empty class id",
Expand All @@ -77,8 +77,8 @@ func (s *TestSuite) TestSend() {
req: &nft.MsgSend{
ClassId: "invalid ClassId",
Id: testID,
Sender: s.addrs[0].String(),
Receiver: s.addrs[1].String(),
Sender: s.encodedAddrs[0],
Receiver: s.encodedAddrs[1],
},
expErr: true,
errMsg: "unauthorized",
Expand All @@ -88,8 +88,8 @@ func (s *TestSuite) TestSend() {
req: &nft.MsgSend{
ClassId: testClassID,
Id: "invalid Id",
Sender: s.addrs[0].String(),
Receiver: s.addrs[1].String(),
Sender: s.encodedAddrs[0],
Receiver: s.encodedAddrs[1],
},
expErr: true,
errMsg: "unauthorized",
Expand All @@ -99,19 +99,19 @@ func (s *TestSuite) TestSend() {
req: &nft.MsgSend{
ClassId: testClassID,
Id: testID,
Sender: s.addrs[1].String(),
Receiver: s.addrs[2].String(),
Sender: s.encodedAddrs[1],
Receiver: s.encodedAddrs[2],
},
expErr: true,
errMsg: fmt.Sprintf("%s is not the owner of nft %s", s.addrs[1].String(), testID),
errMsg: fmt.Sprintf("%s is not the owner of nft %s", s.encodedAddrs[1], testID),
},
{
name: "valid transaction",
req: &nft.MsgSend{
ClassId: testClassID,
Id: testID,
Sender: s.addrs[0].String(),
Receiver: s.addrs[1].String(),
Sender: s.encodedAddrs[0],
Receiver: s.encodedAddrs[1],
},
expErr: false,
errMsg: "",
Expand Down
29 changes: 15 additions & 14 deletions x/nft/keeper/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ func (k Keeper) Mint(ctx context.Context, token nft.NFT, receiver sdk.AccAddress
return errors.Wrap(nft.ErrNFTExists, token.Id)
}

k.mintWithNoCheck(ctx, token, receiver)
return nil
return k.mintWithNoCheck(ctx, token, receiver)
}

// mintWithNoCheck defines a method for minting a new nft
// Note: this method does not check whether the class already exists in nft.
// The upper-layer application needs to check it when it needs to use it.
func (k Keeper) mintWithNoCheck(ctx context.Context, token nft.NFT, receiver sdk.AccAddress) {
func (k Keeper) mintWithNoCheck(ctx context.Context, token nft.NFT, receiver sdk.AccAddress) error {
k.setNFT(ctx, token)
k.setOwner(ctx, token.ClassId, token.Id, receiver)
k.incrTotalSupply(ctx, token.ClassId)

err := sdk.UnwrapSDKContext(ctx).EventManager().EmitTypedEvent(&nft.EventMint{
recStr, err := k.ac.BytesToString(receiver.Bytes())
if err != nil {
return err
}
return sdk.UnwrapSDKContext(ctx).EventManager().EmitTypedEvent(&nft.EventMint{
ClassId: token.ClassId,
Id: token.Id,
Owner: receiver.String(),
Owner: recStr,
})
if err != nil {
panic(err)
}
}

// Burn defines a method for burning a nft from a specific account.
Expand Down Expand Up @@ -71,15 +71,16 @@ func (k Keeper) burnWithNoCheck(ctx context.Context, classID, nftID string) erro

k.deleteOwner(ctx, classID, nftID, owner)
k.decrTotalSupply(ctx, classID)
err := sdk.UnwrapSDKContext(ctx).EventManager().EmitTypedEvent(&nft.EventBurn{
ClassId: classID,
Id: nftID,
Owner: owner.String(),
})
ownerStr, err := k.ac.BytesToString(owner.Bytes())
if err != nil {
return err
}
return nil

return sdk.UnwrapSDKContext(ctx).EventManager().EmitTypedEvent(&nft.EventBurn{
ClassId: classID,
Id: nftID,
Owner: ownerStr,
})
}

// Update defines a method for updating an exist nft
Expand Down
4 changes: 3 additions & 1 deletion x/nft/keeper/nft_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func (k Keeper) BatchMint(ctx context.Context,
}

checked[token.ClassId] = true
k.mintWithNoCheck(ctx, token, receiver)
if err := k.mintWithNoCheck(ctx, token, receiver); err != nil {
return err
}
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions x/nft/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ func (AppModule) ConsensusVersion() uint64 { return 1 }
// AppModuleSimulation functions

// GenerateGenesisState creates a randomized GenState of the nft module.
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
func (am AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState, am.accountKeeper.AddressCodec())
}

// RegisterStoreDecoder registers a decoder for nft module's types
Expand Down
13 changes: 9 additions & 4 deletions x/nft/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package simulation
import (
"math/rand"

"cosmossdk.io/core/address"
"cosmossdk.io/x/nft"

"github.com/cosmos/cosmos-sdk/types/module"
Expand All @@ -25,12 +26,16 @@ func genClasses(r *rand.Rand, accounts []simtypes.Account) []*nft.Class {
}

// genNFT returns a slice of nft.
func genNFT(r *rand.Rand, classID string, accounts []simtypes.Account) []*nft.Entry {
func genNFT(r *rand.Rand, classID string, accounts []simtypes.Account, ac address.Codec) []*nft.Entry {
entries := make([]*nft.Entry, len(accounts)-1)
for i := 0; i < len(accounts)-1; i++ {
owner := accounts[i]
oast, err := ac.BytesToString(owner.Address.Bytes())
if err != nil {
panic(err)
}
entries[i] = &nft.Entry{
Owner: owner.Address.String(),
Owner: oast,
Nfts: []*nft.NFT{
{
ClassId: classID,
Expand All @@ -44,7 +49,7 @@ func genNFT(r *rand.Rand, classID string, accounts []simtypes.Account) []*nft.En
}

// RandomizedGenState generates a random GenesisState for nft.
func RandomizedGenState(simState *module.SimulationState) {
func RandomizedGenState(simState *module.SimulationState, ac address.Codec) {
var classes []*nft.Class
simState.AppParams.GetOrGenerate(
"nft", &classes, simState.Rand,
Expand All @@ -56,7 +61,7 @@ func RandomizedGenState(simState *module.SimulationState) {
"nft", &entries, simState.Rand,
func(r *rand.Rand) {
class := classes[r.Int63n(int64(len(classes)))]
entries = genNFT(r, class.Id, simState.Accounts)
entries = genNFT(r, class.Id, simState.Accounts, ac)
},
)

Expand Down
3 changes: 2 additions & 1 deletion x/nft/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
nftmodule "cosmossdk.io/x/nft/module"
"cosmossdk.io/x/nft/simulation"

addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/types/module"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
Expand All @@ -33,7 +34,7 @@ func TestRandomizedGenState(t *testing.T) {
GenState: make(map[string]json.RawMessage),
}

simulation.RandomizedGenState(&simState)
simulation.RandomizedGenState(&simState, addresscodec.NewBech32Codec("cosmos"))
var nftGenesis nft.GenesisState
simState.Cdc.MustUnmarshalJSON(simState.GenState[nft.ModuleName], &nftGenesis)

Expand Down
14 changes: 12 additions & 2 deletions x/nft/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,21 @@ func SimulateMsgSend(
return simtypes.NoOpMsg(nft.ModuleName, TypeMsgSend, err.Error()), nil, err
}

senderStr, err := ak.AddressCodec().BytesToString(senderAcc.GetAddress().Bytes())
if err != nil {
return simtypes.NoOpMsg(nft.ModuleName, TypeMsgSend, err.Error()), nil, err
}

recieverStr, err := ak.AddressCodec().BytesToString(receiver.Address.Bytes())
if err != nil {
return simtypes.NoOpMsg(nft.ModuleName, TypeMsgSend, err.Error()), nil, err
}

msg := &nft.MsgSend{
ClassId: n.ClassId,
Id: n.Id,
Sender: senderAcc.GetAddress().String(),
Receiver: receiver.Address.String(),
Sender: senderStr,
Receiver: recieverStr,
}

tx, err := simtestutil.GenSignedMockTx(
Expand Down

0 comments on commit f9c5fd4

Please sign in to comment.