Skip to content

Commit

Permalink
refactor!: use store service in x/nft (#15588)
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica authored Mar 29, 2023
1 parent a7b80d5 commit 24344fb
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 81 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* (x/auth) [#15517](https://github.com/cosmos/cosmos-sdk/pull/15517) `NewAccountKeeper` now takes a `KVStoreService` instead of a `StoreKey` and methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context`.
* (x/nft) [#15588](https://github.com/cosmos/cosmos-sdk/pull/15588) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey` and methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context`.
* (x/auth) [#15520](https://github.com/cosmos/cosmos-sdk/pull/15520) `NewAccountKeeper` now takes a `KVStoreService` instead of a `StoreKey` and methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context`.
* (x/consensus) [#15517](https://github.com/cosmos/cosmos-sdk/pull/15517) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`.
* (x/bank) [#15477](https://github.com/cosmos/cosmos-sdk/pull/15477) `banktypes.NewMsgMultiSend` and `keeper.InputOutputCoins` only accept one input.
* (mempool) [#15328](https://github.com/cosmos/cosmos-sdk/pull/15328) The `PriorityNonceMempool` is now generic over type `C comparable` and takes a single `PriorityNonceMempoolConfig[C]` argument. See `DefaultPriorityNonceMempoolConfig` for how to construct the configuration and a `TxPriority` type.
Expand Down
1 change: 1 addition & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The following modules `NewKeeper` function now take a `KVStoreService` instead o

* `x/auth`
* `x/consensus`
* `x/nft`

When not using depinject, the `runtime.NewKVStoreService` method can be used to create a `KVStoreService` from a `StoreKey`:

Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func NewSimApp(
),
)

app.NFTKeeper = nftkeeper.NewKeeper(keys[nftkeeper.StoreKey], appCodec, app.AccountKeeper, app.BankKeeper)
app.NFTKeeper = nftkeeper.NewKeeper(runtime.NewKVStoreService(keys[nftkeeper.StoreKey]), appCodec, app.AccountKeeper, app.BankKeeper)

// create evidence keeper with router
evidenceKeeper := evidencekeeper.NewKeeper(
Expand Down
46 changes: 27 additions & 19 deletions x/nft/keeper/class.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
package keeper

import (
"context"

"cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/nft"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/runtime"
)

// SaveClass defines a method for creating a new nft class
func (k Keeper) SaveClass(ctx sdk.Context, class nft.Class) error {
func (k Keeper) SaveClass(ctx context.Context, class nft.Class) error {
if k.HasClass(ctx, class.Id) {
return errors.Wrap(nft.ErrClassExists, class.Id)
}
bz, err := k.cdc.Marshal(&class)
if err != nil {
return errors.Wrap(err, "Marshal nft.Class failed")
}
store := ctx.KVStore(k.storeKey)
store.Set(classStoreKey(class.Id), bz)
return nil
store := k.storeService.OpenKVStore(ctx)
return store.Set(classStoreKey(class.Id), bz)
}

// UpdateClass defines a method for updating an exist nft class
func (k Keeper) UpdateClass(ctx sdk.Context, class nft.Class) error {
func (k Keeper) UpdateClass(ctx context.Context, class nft.Class) error {
if !k.HasClass(ctx, class.Id) {
return errors.Wrap(nft.ErrClassNotExists, class.Id)
}
bz, err := k.cdc.Marshal(&class)
if err != nil {
return errors.Wrap(err, "Marshal nft.Class failed")
}
store := ctx.KVStore(k.storeKey)
store.Set(classStoreKey(class.Id), bz)
return nil
store := k.storeService.OpenKVStore(ctx)
return store.Set(classStoreKey(class.Id), bz)
}

// GetClass defines a method for returning the class information of the specified id
func (k Keeper) GetClass(ctx sdk.Context, classID string) (nft.Class, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(classStoreKey(classID))

func (k Keeper) GetClass(ctx context.Context, classID string) (nft.Class, bool) {
store := k.storeService.OpenKVStore(ctx)
var class nft.Class

bz, err := store.Get(classStoreKey(classID))
if err != nil {
return class, false
}

if len(bz) == 0 {
return class, false
}
Expand All @@ -50,9 +54,9 @@ func (k Keeper) GetClass(ctx sdk.Context, classID string) (nft.Class, bool) {
}

// GetClasses defines a method for returning all classes information
func (k Keeper) GetClasses(ctx sdk.Context) (classes []*nft.Class) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, ClassKey)
func (k Keeper) GetClasses(ctx context.Context) (classes []*nft.Class) {
store := k.storeService.OpenKVStore(ctx)
iterator := storetypes.KVStorePrefixIterator(runtime.KVStoreAdapter(store), ClassKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var class nft.Class
Expand All @@ -63,7 +67,11 @@ func (k Keeper) GetClasses(ctx sdk.Context) (classes []*nft.Class) {
}

// HasClass determines whether the specified classID exist
func (k Keeper) HasClass(ctx sdk.Context, classID string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(classStoreKey(classID))
func (k Keeper) HasClass(ctx context.Context, classID string) bool {
store := k.storeService.OpenKVStore(ctx)
has, err := store.Has(classStoreKey(classID))
if err != nil {
panic(err)
}
return has
}
5 changes: 3 additions & 2 deletions x/nft/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"cosmossdk.io/store/prefix"
"cosmossdk.io/x/nft"

"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
Expand Down Expand Up @@ -174,8 +175,8 @@ func (k Keeper) Classes(goCtx context.Context, r *nft.QueryClassesRequest) (*nft
}

ctx := sdk.UnwrapSDKContext(goCtx)
store := ctx.KVStore(k.storeKey)
classStore := prefix.NewStore(store, ClassKey)
store := k.storeService.OpenKVStore(ctx)
classStore := prefix.NewStore(runtime.KVStoreAdapter(store), ClassKey)

var classes []*nft.Class
pageRes, err := query.Paginate(classStore, r.Pagination, func(_ []byte, value []byte) error {
Expand Down
16 changes: 8 additions & 8 deletions x/nft/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package keeper

import (
storetypes "cosmossdk.io/store/types"
store "cosmossdk.io/core/store"
"cosmossdk.io/x/nft"

"github.com/cosmos/cosmos-sdk/codec"
)

// Keeper of the nft store
type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
bk nft.BankKeeper
cdc codec.BinaryCodec
storeService store.KVStoreService
bk nft.BankKeeper
}

// NewKeeper creates a new nft Keeper instance
func NewKeeper(key storetypes.StoreKey,
func NewKeeper(storeService store.KVStoreService,
cdc codec.BinaryCodec, ak nft.AccountKeeper, bk nft.BankKeeper,
) Keeper {
// ensure nft module account is set
Expand All @@ -24,8 +24,8 @@ func NewKeeper(key storetypes.StoreKey,
}

return Keeper{
cdc: cdc,
storeKey: key,
bk: bk,
cdc: cdc,
storeService: storeService,
bk: bk,
}
}
4 changes: 3 additions & 1 deletion x/nft/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
nfttestutil "cosmossdk.io/x/nft/testutil"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -50,6 +51,7 @@ func (s *TestSuite) SetupTest() {
s.encCfg = moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{})

key := storetypes.NewKVStoreKey(nft.StoreKey)
storeService := runtime.NewKVStoreService(key)
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
ctx := testCtx.Ctx.WithBlockHeader(cmtproto.Header{Time: cmttime.Now()})

Expand All @@ -59,7 +61,7 @@ func (s *TestSuite) SetupTest() {
bankKeeper := nfttestutil.NewMockBankKeeper(ctrl)
accountKeeper.EXPECT().GetModuleAddress("nft").Return(s.addrs[0]).AnyTimes()

nftKeeper := keeper.NewKeeper(key, s.encCfg.Codec, accountKeeper, bankKeeper)
nftKeeper := keeper.NewKeeper(storeService, s.encCfg.Codec, accountKeeper, bankKeeper)
queryHelper := baseapp.NewQueryServerTestHelper(ctx, s.encCfg.InterfaceRegistry)
nft.RegisterQueryServer(queryHelper, nftKeeper)

Expand Down
Loading

0 comments on commit 24344fb

Please sign in to comment.