Skip to content

Commit 91278f6

Browse files
refactor(x/authz)!: Use KVStoreService, context.Context and return errors instead of panic (#15962)
1 parent 26faee9 commit 91278f6

16 files changed

+161
-99
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
118118

119119
### API Breaking Changes
120120

121+
* (x/authz) [#15962](https://github.com/cosmos/cosmos-sdk/issues/15962) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`, methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context`. The `Authorization` interface's `Accept` method now takes a `context.Context` instead of a `sdk.Context`.
121122
* (x/distribution) [#15948](https://github.com/cosmos/cosmos-sdk/issues/15948) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey` and methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context`. Keeper methods also now return an `error`.
122123
* (x/bank) [#15891](https://github.com/cosmos/cosmos-sdk/issues/15891) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey` and methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context`. Also `FundAccount` and `FundModuleAccount` from the `testutil` package accept a `context.Context` instead of a `sdk.Context`, and it's position was moved to the first place.
123124
* (x/bank) [#15818](https://github.com/cosmos/cosmos-sdk/issues/15818) `BaseViewKeeper`'s `Logger` method now doesn't require a context. `NewBaseKeeper`, `NewBaseSendKeeper` and `NewBaseViewKeeper` now also require a `log.Logger` to be passed in.

UPGRADING.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ This is no longer the case, the assertion has been loosened to only require modu
7171
The following modules `NewKeeper` function now take a `KVStoreService` instead of a `StoreKey`:
7272

7373
* `x/auth`
74+
* `x/authz`
7475
* `x/bank`
7576
* `x/consensus`
7677
* `x/distribution`
@@ -94,10 +95,10 @@ The following modules `NewKeeper` function now also take a `log.Logger`:
9495

9596
The following modules' `Keeper` methods now take in a `context.Context` instead of `sdk.Context`. Any module that has an interfaces for them (like "expected keepers") will need to update and re-generate mocks if needed:
9697

98+
* `x/authz`
9799
* `x/bank`
98100
* `x/distribution`
99101

100-
101102
### depinject
102103

103104
For `depinject` users, now the logger must be supplied through the main `depinject.Inject` function instead of passing it to `appBuilder.Build`.

simapp/app.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func NewSimApp(
317317
stakingtypes.NewMultiStakingHooks(app.DistrKeeper.Hooks(), app.SlashingKeeper.Hooks()),
318318
)
319319

320-
app.AuthzKeeper = authzkeeper.NewKeeper(keys[authzkeeper.StoreKey], appCodec, app.MsgServiceRouter(), app.AccountKeeper)
320+
app.AuthzKeeper = authzkeeper.NewKeeper(runtime.NewKVStoreService(keys[authzkeeper.StoreKey]), appCodec, app.MsgServiceRouter(), app.AccountKeeper)
321321

322322
groupConfig := group.DefaultConfig()
323323
/*

x/authz/authorizations.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package authz
22

33
import (
4+
context "context"
5+
46
"github.com/cosmos/gogoproto/proto"
57

68
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -17,7 +19,7 @@ type Authorization interface {
1719

1820
// Accept determines whether this grant permits the provided sdk.Msg to be performed,
1921
// and if so provides an upgraded authorization instance.
20-
Accept(ctx sdk.Context, msg sdk.Msg) (AcceptResponse, error)
22+
Accept(ctx context.Context, msg sdk.Msg) (AcceptResponse, error)
2123

2224
// ValidateBasic does a simple validation check that
2325
// doesn't require access to any other information.

x/authz/generic_authorization.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package authz
22

33
import (
4+
context "context"
5+
46
sdk "github.com/cosmos/cosmos-sdk/types"
57
)
68

@@ -19,7 +21,7 @@ func (a GenericAuthorization) MsgTypeURL() string {
1921
}
2022

2123
// Accept implements Authorization.Accept.
22-
func (a GenericAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (AcceptResponse, error) {
24+
func (a GenericAuthorization) Accept(ctx context.Context, msg sdk.Msg) (AcceptResponse, error) {
2325
return AcceptResponse{Accept: true}, nil
2426
}
2527

x/authz/keeper/genesis_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/cosmos/cosmos-sdk/baseapp"
1616
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
17+
"github.com/cosmos/cosmos-sdk/runtime"
1718
"github.com/cosmos/cosmos-sdk/testutil"
1819
sdk "github.com/cosmos/cosmos-sdk/types"
1920
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
@@ -42,6 +43,7 @@ type GenesisTestSuite struct {
4243

4344
func (suite *GenesisTestSuite) SetupTest() {
4445
key := storetypes.NewKVStoreKey(keeper.StoreKey)
46+
storeService := runtime.NewKVStoreService(key)
4547
testCtx := testutil.DefaultContextWithDB(suite.T(), key, storetypes.NewTransientStoreKey("transient_test"))
4648
suite.ctx = testCtx.Ctx.WithBlockHeader(cmtproto.Header{Height: 1})
4749
suite.encCfg = moduletestutil.MakeTestEncodingConfig(authzmodule.AppModuleBasic{})
@@ -68,7 +70,7 @@ func (suite *GenesisTestSuite) SetupTest() {
6870
msr := suite.baseApp.MsgServiceRouter()
6971
msr.SetInterfaceRegistry(suite.encCfg.InterfaceRegistry)
7072

71-
suite.keeper = keeper.NewKeeper(key, suite.encCfg.Codec, msr, suite.accountKeeper)
73+
suite.keeper = keeper.NewKeeper(storeService, suite.encCfg.Codec, msr, suite.accountKeeper)
7274
}
7375

7476
func (suite *GenesisTestSuite) TestImportExportGenesis() {

x/authz/keeper/grpc_query.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"cosmossdk.io/store/prefix"
1212

1313
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
14-
sdk "github.com/cosmos/cosmos-sdk/types"
14+
"github.com/cosmos/cosmos-sdk/runtime"
1515
"github.com/cosmos/cosmos-sdk/types/query"
1616
"github.com/cosmos/cosmos-sdk/x/authz"
1717
)
@@ -20,7 +20,7 @@ var _ authz.QueryServer = Keeper{}
2020

2121
// Grants implements the Query/Grants gRPC method.
2222
// It returns grants for a granter-grantee pair. If msg type URL is set, it returns grants only for that msg type.
23-
func (k Keeper) Grants(c context.Context, req *authz.QueryGrantsRequest) (*authz.QueryGrantsResponse, error) {
23+
func (k Keeper) Grants(ctx context.Context, req *authz.QueryGrantsRequest) (*authz.QueryGrantsResponse, error) {
2424
if req == nil {
2525
return nil, status.Errorf(codes.InvalidArgument, "empty request")
2626
}
@@ -35,7 +35,6 @@ func (k Keeper) Grants(c context.Context, req *authz.QueryGrantsRequest) (*authz
3535
return nil, err
3636
}
3737

38-
ctx := sdk.UnwrapSDKContext(c)
3938
if req.MsgTypeUrl != "" {
4039
grant, found := k.getGrant(ctx, grantStoreKey(grantee, granter, req.MsgTypeUrl))
4140
if !found {
@@ -59,7 +58,7 @@ func (k Keeper) Grants(c context.Context, req *authz.QueryGrantsRequest) (*authz
5958
}, nil
6059
}
6160

62-
store := ctx.KVStore(k.storeKey)
61+
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
6362
key := grantStoreKey(grantee, granter, "")
6463
grantsStore := prefix.NewStore(store, key)
6564

@@ -91,7 +90,7 @@ func (k Keeper) Grants(c context.Context, req *authz.QueryGrantsRequest) (*authz
9190
}
9291

9392
// GranterGrants implements the Query/GranterGrants gRPC method.
94-
func (k Keeper) GranterGrants(c context.Context, req *authz.QueryGranterGrantsRequest) (*authz.QueryGranterGrantsResponse, error) {
93+
func (k Keeper) GranterGrants(ctx context.Context, req *authz.QueryGranterGrantsRequest) (*authz.QueryGranterGrantsResponse, error) {
9594
if req == nil {
9695
return nil, status.Errorf(codes.InvalidArgument, "empty request")
9796
}
@@ -101,8 +100,7 @@ func (k Keeper) GranterGrants(c context.Context, req *authz.QueryGranterGrantsRe
101100
return nil, err
102101
}
103102

104-
ctx := sdk.UnwrapSDKContext(c)
105-
store := ctx.KVStore(k.storeKey)
103+
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
106104
authzStore := prefix.NewStore(store, grantStoreKey(nil, granter, ""))
107105

108106
grants, pageRes, err := query.GenericFilteredPaginate(k.cdc, authzStore, req.Pagination, func(key []byte, auth *authz.Grant) (*authz.GrantAuthorization, error) {
@@ -137,7 +135,7 @@ func (k Keeper) GranterGrants(c context.Context, req *authz.QueryGranterGrantsRe
137135
}
138136

139137
// GranteeGrants implements the Query/GranteeGrants gRPC method.
140-
func (k Keeper) GranteeGrants(c context.Context, req *authz.QueryGranteeGrantsRequest) (*authz.QueryGranteeGrantsResponse, error) {
138+
func (k Keeper) GranteeGrants(ctx context.Context, req *authz.QueryGranteeGrantsRequest) (*authz.QueryGranteeGrantsResponse, error) {
141139
if req == nil {
142140
return nil, status.Errorf(codes.InvalidArgument, "empty request")
143141
}
@@ -147,8 +145,7 @@ func (k Keeper) GranteeGrants(c context.Context, req *authz.QueryGranteeGrantsRe
147145
return nil, err
148146
}
149147

150-
ctx := sdk.UnwrapSDKContext(c)
151-
store := prefix.NewStore(ctx.KVStore(k.storeKey), GrantKey)
148+
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), GrantKey)
152149

153150
authorizations, pageRes, err := query.GenericFilteredPaginate(k.cdc, store, req.Pagination, func(key []byte, auth *authz.Grant) (*authz.GrantAuthorization, error) {
154151
auth1, err := auth.GetAuthorization()

0 commit comments

Comments
 (0)