Skip to content

Commit 24344fb

Browse files
refactor!: use store service in x/nft (#15588)
1 parent a7b80d5 commit 24344fb

10 files changed

Lines changed: 108 additions & 81 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
105105

106106
### API Breaking Changes
107107

108-
* (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`.
108+
* (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`.
109+
* (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`.
109110
* (x/consensus) [#15517](https://github.com/cosmos/cosmos-sdk/pull/15517) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`.
110111
* (x/bank) [#15477](https://github.com/cosmos/cosmos-sdk/pull/15477) `banktypes.NewMsgMultiSend` and `keeper.InputOutputCoins` only accept one input.
111112
* (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.

UPGRADING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The following modules `NewKeeper` function now take a `KVStoreService` instead o
6868

6969
* `x/auth`
7070
* `x/consensus`
71+
* `x/nft`
7172

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

simapp/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ func NewSimApp(
366366
),
367367
)
368368

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

371371
// create evidence keeper with router
372372
evidenceKeeper := evidencekeeper.NewKeeper(

x/nft/keeper/class.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
11
package keeper
22

33
import (
4+
"context"
5+
46
"cosmossdk.io/errors"
57
storetypes "cosmossdk.io/store/types"
68
"cosmossdk.io/x/nft"
79

8-
sdk "github.com/cosmos/cosmos-sdk/types"
10+
"github.com/cosmos/cosmos-sdk/runtime"
911
)
1012

1113
// SaveClass defines a method for creating a new nft class
12-
func (k Keeper) SaveClass(ctx sdk.Context, class nft.Class) error {
14+
func (k Keeper) SaveClass(ctx context.Context, class nft.Class) error {
1315
if k.HasClass(ctx, class.Id) {
1416
return errors.Wrap(nft.ErrClassExists, class.Id)
1517
}
1618
bz, err := k.cdc.Marshal(&class)
1719
if err != nil {
1820
return errors.Wrap(err, "Marshal nft.Class failed")
1921
}
20-
store := ctx.KVStore(k.storeKey)
21-
store.Set(classStoreKey(class.Id), bz)
22-
return nil
22+
store := k.storeService.OpenKVStore(ctx)
23+
return store.Set(classStoreKey(class.Id), bz)
2324
}
2425

2526
// UpdateClass defines a method for updating an exist nft class
26-
func (k Keeper) UpdateClass(ctx sdk.Context, class nft.Class) error {
27+
func (k Keeper) UpdateClass(ctx context.Context, class nft.Class) error {
2728
if !k.HasClass(ctx, class.Id) {
2829
return errors.Wrap(nft.ErrClassNotExists, class.Id)
2930
}
3031
bz, err := k.cdc.Marshal(&class)
3132
if err != nil {
3233
return errors.Wrap(err, "Marshal nft.Class failed")
3334
}
34-
store := ctx.KVStore(k.storeKey)
35-
store.Set(classStoreKey(class.Id), bz)
36-
return nil
35+
store := k.storeService.OpenKVStore(ctx)
36+
return store.Set(classStoreKey(class.Id), bz)
3737
}
3838

3939
// GetClass defines a method for returning the class information of the specified id
40-
func (k Keeper) GetClass(ctx sdk.Context, classID string) (nft.Class, bool) {
41-
store := ctx.KVStore(k.storeKey)
42-
bz := store.Get(classStoreKey(classID))
43-
40+
func (k Keeper) GetClass(ctx context.Context, classID string) (nft.Class, bool) {
41+
store := k.storeService.OpenKVStore(ctx)
4442
var class nft.Class
43+
44+
bz, err := store.Get(classStoreKey(classID))
45+
if err != nil {
46+
return class, false
47+
}
48+
4549
if len(bz) == 0 {
4650
return class, false
4751
}
@@ -50,9 +54,9 @@ func (k Keeper) GetClass(ctx sdk.Context, classID string) (nft.Class, bool) {
5054
}
5155

5256
// GetClasses defines a method for returning all classes information
53-
func (k Keeper) GetClasses(ctx sdk.Context) (classes []*nft.Class) {
54-
store := ctx.KVStore(k.storeKey)
55-
iterator := storetypes.KVStorePrefixIterator(store, ClassKey)
57+
func (k Keeper) GetClasses(ctx context.Context) (classes []*nft.Class) {
58+
store := k.storeService.OpenKVStore(ctx)
59+
iterator := storetypes.KVStorePrefixIterator(runtime.KVStoreAdapter(store), ClassKey)
5660
defer iterator.Close()
5761
for ; iterator.Valid(); iterator.Next() {
5862
var class nft.Class
@@ -63,7 +67,11 @@ func (k Keeper) GetClasses(ctx sdk.Context) (classes []*nft.Class) {
6367
}
6468

6569
// HasClass determines whether the specified classID exist
66-
func (k Keeper) HasClass(ctx sdk.Context, classID string) bool {
67-
store := ctx.KVStore(k.storeKey)
68-
return store.Has(classStoreKey(classID))
70+
func (k Keeper) HasClass(ctx context.Context, classID string) bool {
71+
store := k.storeService.OpenKVStore(ctx)
72+
has, err := store.Has(classStoreKey(classID))
73+
if err != nil {
74+
panic(err)
75+
}
76+
return has
6977
}

x/nft/keeper/grpc_query.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"cosmossdk.io/store/prefix"
77
"cosmossdk.io/x/nft"
88

9+
"github.com/cosmos/cosmos-sdk/runtime"
910
sdk "github.com/cosmos/cosmos-sdk/types"
1011
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
1112
"github.com/cosmos/cosmos-sdk/types/query"
@@ -174,8 +175,8 @@ func (k Keeper) Classes(goCtx context.Context, r *nft.QueryClassesRequest) (*nft
174175
}
175176

176177
ctx := sdk.UnwrapSDKContext(goCtx)
177-
store := ctx.KVStore(k.storeKey)
178-
classStore := prefix.NewStore(store, ClassKey)
178+
store := k.storeService.OpenKVStore(ctx)
179+
classStore := prefix.NewStore(runtime.KVStoreAdapter(store), ClassKey)
179180

180181
var classes []*nft.Class
181182
pageRes, err := query.Paginate(classStore, r.Pagination, func(_ []byte, value []byte) error {

x/nft/keeper/keeper.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package keeper
22

33
import (
4-
storetypes "cosmossdk.io/store/types"
4+
store "cosmossdk.io/core/store"
55
"cosmossdk.io/x/nft"
66

77
"github.com/cosmos/cosmos-sdk/codec"
88
)
99

1010
// Keeper of the nft store
1111
type Keeper struct {
12-
cdc codec.BinaryCodec
13-
storeKey storetypes.StoreKey
14-
bk nft.BankKeeper
12+
cdc codec.BinaryCodec
13+
storeService store.KVStoreService
14+
bk nft.BankKeeper
1515
}
1616

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

2626
return Keeper{
27-
cdc: cdc,
28-
storeKey: key,
29-
bk: bk,
27+
cdc: cdc,
28+
storeService: storeService,
29+
bk: bk,
3030
}
3131
}

x/nft/keeper/keeper_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
nfttestutil "cosmossdk.io/x/nft/testutil"
1616

1717
"github.com/cosmos/cosmos-sdk/baseapp"
18+
"github.com/cosmos/cosmos-sdk/runtime"
1819
"github.com/cosmos/cosmos-sdk/testutil"
1920
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
2021
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -50,6 +51,7 @@ func (s *TestSuite) SetupTest() {
5051
s.encCfg = moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{})
5152

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

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

62-
nftKeeper := keeper.NewKeeper(key, s.encCfg.Codec, accountKeeper, bankKeeper)
64+
nftKeeper := keeper.NewKeeper(storeService, s.encCfg.Codec, accountKeeper, bankKeeper)
6365
queryHelper := baseapp.NewQueryServerTestHelper(ctx, s.encCfg.InterfaceRegistry)
6466
nft.RegisterQueryServer(queryHelper, nftKeeper)
6567

0 commit comments

Comments
 (0)