Skip to content

Commit 3ada275

Browse files
testinginprodunknown unknown
andauthored
refactor(auth): decouple auth Keeper API from Msg and Query server implementation (#15985)
Co-authored-by: unknown unknown <unknown@unknown>
1 parent 7c59ead commit 3ada275

7 files changed

Lines changed: 47 additions & 40 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
116116

117117
### API Breaking Changes
118118

119+
* (x/auth) [#15985](https://github.com/cosmos/cosmos-sdk/pull/15985) The `AccountKeeper` does not expose the `QueryServer` and `MsgServer` APIs anymore.
119120
* (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`.
120121
* (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`.
121122
* (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.

x/auth/keeper/deterministic_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (suite *DeterministicTestSuite) SetupTest() {
7474
)
7575

7676
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.encCfg.InterfaceRegistry)
77-
types.RegisterQueryServer(queryHelper, suite.accountKeeper)
77+
types.RegisterQueryServer(queryHelper, keeper.NewQueryServer(suite.accountKeeper))
7878
suite.queryClient = types.NewQueryClient(queryHelper)
7979

8080
suite.key = key
@@ -239,7 +239,7 @@ func (suite *DeterministicTestSuite) TestGRPCQueryAccountInfo() {
239239

240240
func (suite *DeterministicTestSuite) createAndReturnQueryClient(ak keeper.AccountKeeper) types.QueryClient {
241241
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.encCfg.InterfaceRegistry)
242-
types.RegisterQueryServer(queryHelper, ak)
242+
types.RegisterQueryServer(queryHelper, keeper.NewQueryServer(ak))
243243
return types.NewQueryClient(queryHelper)
244244
}
245245

x/auth/keeper/grpc_query.go

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ import (
1919
"github.com/cosmos/cosmos-sdk/x/auth/types"
2020
)
2121

22-
var _ types.QueryServer = AccountKeeper{}
22+
var _ types.QueryServer = queryServer{}
2323

24-
func (ak AccountKeeper) AccountAddressByID(c context.Context, req *types.QueryAccountAddressByIDRequest) (*types.QueryAccountAddressByIDResponse, error) {
24+
func NewQueryServer(k AccountKeeper) types.QueryServer {
25+
return queryServer{k: k}
26+
}
27+
28+
type queryServer struct{ k AccountKeeper }
29+
30+
func (s queryServer) AccountAddressByID(c context.Context, req *types.QueryAccountAddressByIDRequest) (*types.QueryAccountAddressByIDResponse, error) {
2531
if req == nil {
2632
return nil, status.Errorf(codes.InvalidArgument, "empty request")
2733
}
@@ -33,25 +39,25 @@ func (ak AccountKeeper) AccountAddressByID(c context.Context, req *types.QueryAc
3339
accID := req.AccountId
3440

3541
ctx := sdk.UnwrapSDKContext(c)
36-
address := ak.GetAccountAddressByID(ctx, accID)
42+
address := s.k.GetAccountAddressByID(ctx, accID)
3743
if len(address) == 0 {
3844
return nil, status.Errorf(codes.NotFound, "account address not found with account number %d", req.Id)
3945
}
4046

4147
return &types.QueryAccountAddressByIDResponse{AccountAddress: address}, nil
4248
}
4349

44-
func (ak AccountKeeper) Accounts(ctx context.Context, req *types.QueryAccountsRequest) (*types.QueryAccountsResponse, error) {
50+
func (s queryServer) Accounts(ctx context.Context, req *types.QueryAccountsRequest) (*types.QueryAccountsResponse, error) {
4551
if req == nil {
4652
return nil, status.Error(codes.InvalidArgument, "empty request")
4753
}
4854

49-
store := ak.storeService.OpenKVStore(ctx)
55+
store := s.k.storeService.OpenKVStore(ctx)
5056
accountsStore := prefix.NewStore(runtime.KVStoreAdapter(store), types.AddressStoreKeyPrefix)
5157

5258
var accounts []*codectypes.Any
5359
pageRes, err := query.Paginate(accountsStore, req.Pagination, func(key, value []byte) error {
54-
account := ak.decodeAccount(value)
60+
account := s.k.decodeAccount(value)
5561
any, err := codectypes.NewAnyWithValue(account)
5662
if err != nil {
5763
return err
@@ -68,7 +74,7 @@ func (ak AccountKeeper) Accounts(ctx context.Context, req *types.QueryAccountsRe
6874
}
6975

7076
// Account returns account details based on address
71-
func (ak AccountKeeper) Account(c context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) {
77+
func (s queryServer) Account(c context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) {
7278
if req == nil {
7379
return nil, status.Errorf(codes.InvalidArgument, "empty request")
7480
}
@@ -78,11 +84,11 @@ func (ak AccountKeeper) Account(c context.Context, req *types.QueryAccountReques
7884
}
7985

8086
ctx := sdk.UnwrapSDKContext(c)
81-
addr, err := ak.StringToBytes(req.Address)
87+
addr, err := s.k.StringToBytes(req.Address)
8288
if err != nil {
8389
return nil, err
8490
}
85-
account := ak.GetAccount(ctx, addr)
91+
account := s.k.GetAccount(ctx, addr)
8692
if account == nil {
8793
return nil, status.Errorf(codes.NotFound, "account %s not found", req.Address)
8894
}
@@ -96,35 +102,35 @@ func (ak AccountKeeper) Account(c context.Context, req *types.QueryAccountReques
96102
}
97103

98104
// Params returns parameters of auth module
99-
func (ak AccountKeeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
105+
func (s queryServer) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
100106
if req == nil {
101107
return nil, status.Error(codes.InvalidArgument, "empty request")
102108
}
103109
ctx := sdk.UnwrapSDKContext(c)
104-
params := ak.GetParams(ctx)
110+
params := s.k.GetParams(ctx)
105111

106112
return &types.QueryParamsResponse{Params: params}, nil
107113
}
108114

109115
// ModuleAccounts returns all the existing Module Accounts
110-
func (ak AccountKeeper) ModuleAccounts(c context.Context, req *types.QueryModuleAccountsRequest) (*types.QueryModuleAccountsResponse, error) {
116+
func (s queryServer) ModuleAccounts(c context.Context, req *types.QueryModuleAccountsRequest) (*types.QueryModuleAccountsResponse, error) {
111117
if req == nil {
112118
return nil, status.Error(codes.InvalidArgument, "empty request")
113119
}
114120

115121
ctx := sdk.UnwrapSDKContext(c)
116122

117123
// For deterministic output, sort the permAddrs by module name.
118-
sortedPermAddrs := make([]string, 0, len(ak.permAddrs))
119-
for moduleName := range ak.permAddrs {
124+
sortedPermAddrs := make([]string, 0, len(s.k.permAddrs))
125+
for moduleName := range s.k.permAddrs {
120126
sortedPermAddrs = append(sortedPermAddrs, moduleName)
121127
}
122128
sort.Strings(sortedPermAddrs)
123129

124-
modAccounts := make([]*codectypes.Any, 0, len(ak.permAddrs))
130+
modAccounts := make([]*codectypes.Any, 0, len(s.k.permAddrs))
125131

126132
for _, moduleName := range sortedPermAddrs {
127-
account := ak.GetModuleAccount(ctx, moduleName)
133+
account := s.k.GetModuleAccount(ctx, moduleName)
128134
if account == nil {
129135
return nil, status.Errorf(codes.NotFound, "account %s not found", moduleName)
130136
}
@@ -139,7 +145,7 @@ func (ak AccountKeeper) ModuleAccounts(c context.Context, req *types.QueryModule
139145
}
140146

141147
// ModuleAccountByName returns module account by module name
142-
func (ak AccountKeeper) ModuleAccountByName(c context.Context, req *types.QueryModuleAccountByNameRequest) (*types.QueryModuleAccountByNameResponse, error) {
148+
func (s queryServer) ModuleAccountByName(c context.Context, req *types.QueryModuleAccountByNameRequest) (*types.QueryModuleAccountByNameResponse, error) {
143149
if req == nil {
144150
return nil, status.Errorf(codes.InvalidArgument, "empty request")
145151
}
@@ -151,7 +157,7 @@ func (ak AccountKeeper) ModuleAccountByName(c context.Context, req *types.QueryM
151157
ctx := sdk.UnwrapSDKContext(c)
152158
moduleName := req.Name
153159

154-
account := ak.GetModuleAccount(ctx, moduleName)
160+
account := s.k.GetModuleAccount(ctx, moduleName)
155161
if account == nil {
156162
return nil, status.Errorf(codes.NotFound, "account %s not found", moduleName)
157163
}
@@ -164,8 +170,8 @@ func (ak AccountKeeper) ModuleAccountByName(c context.Context, req *types.QueryM
164170
}
165171

166172
// Bech32Prefix returns the keeper internally stored bech32 prefix.
167-
func (ak AccountKeeper) Bech32Prefix(ctx context.Context, req *types.Bech32PrefixRequest) (*types.Bech32PrefixResponse, error) {
168-
bech32Prefix, err := ak.getBech32Prefix()
173+
func (s queryServer) Bech32Prefix(ctx context.Context, req *types.Bech32PrefixRequest) (*types.Bech32PrefixResponse, error) {
174+
bech32Prefix, err := s.k.getBech32Prefix()
169175
if err != nil {
170176
return nil, err
171177
}
@@ -175,7 +181,7 @@ func (ak AccountKeeper) Bech32Prefix(ctx context.Context, req *types.Bech32Prefi
175181

176182
// AddressBytesToString converts an address from bytes to string, using the
177183
// keeper's bech32 prefix.
178-
func (ak AccountKeeper) AddressBytesToString(ctx context.Context, req *types.AddressBytesToStringRequest) (*types.AddressBytesToStringResponse, error) {
184+
func (s queryServer) AddressBytesToString(ctx context.Context, req *types.AddressBytesToStringRequest) (*types.AddressBytesToStringResponse, error) {
179185
if req == nil {
180186
return nil, status.Error(codes.InvalidArgument, "empty request")
181187
}
@@ -184,7 +190,7 @@ func (ak AccountKeeper) AddressBytesToString(ctx context.Context, req *types.Add
184190
return nil, errors.New("empty address bytes is not allowed")
185191
}
186192

187-
text, err := ak.BytesToString(req.AddressBytes)
193+
text, err := s.k.BytesToString(req.AddressBytes)
188194
if err != nil {
189195
return nil, err
190196
}
@@ -194,7 +200,7 @@ func (ak AccountKeeper) AddressBytesToString(ctx context.Context, req *types.Add
194200

195201
// AddressStringToBytes converts an address from string to bytes, using the
196202
// keeper's bech32 prefix.
197-
func (ak AccountKeeper) AddressStringToBytes(ctx context.Context, req *types.AddressStringToBytesRequest) (*types.AddressStringToBytesResponse, error) {
203+
func (s queryServer) AddressStringToBytes(ctx context.Context, req *types.AddressStringToBytesRequest) (*types.AddressStringToBytesResponse, error) {
198204
if req == nil {
199205
return nil, status.Error(codes.InvalidArgument, "empty request")
200206
}
@@ -203,7 +209,7 @@ func (ak AccountKeeper) AddressStringToBytes(ctx context.Context, req *types.Add
203209
return nil, errors.New("empty address string is not allowed")
204210
}
205211

206-
bz, err := ak.StringToBytes(req.AddressString)
212+
bz, err := s.k.StringToBytes(req.AddressString)
207213
if err != nil {
208214
return nil, err
209215
}
@@ -212,7 +218,7 @@ func (ak AccountKeeper) AddressStringToBytes(ctx context.Context, req *types.Add
212218
}
213219

214220
// AccountInfo implements the AccountInfo query.
215-
func (ak AccountKeeper) AccountInfo(goCtx context.Context, req *types.QueryAccountInfoRequest) (*types.QueryAccountInfoResponse, error) {
221+
func (s queryServer) AccountInfo(goCtx context.Context, req *types.QueryAccountInfoRequest) (*types.QueryAccountInfoResponse, error) {
216222
if req == nil {
217223
return nil, status.Errorf(codes.InvalidArgument, "empty request")
218224
}
@@ -222,12 +228,12 @@ func (ak AccountKeeper) AccountInfo(goCtx context.Context, req *types.QueryAccou
222228
}
223229

224230
ctx := sdk.UnwrapSDKContext(goCtx)
225-
addr, err := ak.StringToBytes(req.Address)
231+
addr, err := s.k.StringToBytes(req.Address)
226232
if err != nil {
227233
return nil, err
228234
}
229235

230-
account := ak.GetAccount(ctx, addr)
236+
account := s.k.GetAccount(ctx, addr)
231237
if account == nil {
232238
return nil, status.Errorf(codes.NotFound, "account %s not found", req.Address)
233239
}

x/auth/keeper/keeper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type AccountKeeper struct {
7676
authority string
7777

7878
// State
79-
ParamsState collections.Item[types.Params] // NOTE: name is this because it conflicts with the Params gRPC method impl
79+
Params collections.Item[types.Params]
8080
AccountNumber collections.Sequence
8181
}
8282

@@ -107,7 +107,7 @@ func NewAccountKeeper(
107107
cdc: cdc,
108108
permAddrs: permAddrs,
109109
authority: authority,
110-
ParamsState: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
110+
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
111111
AccountNumber: collections.NewSequence(sb, types.GlobalAccountNumberKey, "account_number"),
112112
}
113113
}
@@ -265,12 +265,12 @@ func (ak AccountKeeper) getBech32Prefix() (string, error) {
265265
// SetParams sets the auth module's parameters.
266266
// CONTRACT: This method performs no validation of the parameters.
267267
func (ak AccountKeeper) SetParams(ctx context.Context, params types.Params) error {
268-
return ak.ParamsState.Set(ctx, params)
268+
return ak.Params.Set(ctx, params)
269269
}
270270

271271
// GetParams gets the auth module's parameters.
272272
func (ak AccountKeeper) GetParams(ctx context.Context) (params types.Params) {
273-
params, err := ak.ParamsState.Get(ctx)
273+
params, err := ak.Params.Get(ctx)
274274
if err != nil && !errors.Is(err, collections.ErrNotFound) {
275275
panic(err)
276276
}

x/auth/keeper/keeper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (suite *KeeperTestSuite) SetupTest() {
7171
)
7272
suite.msgServer = keeper.NewMsgServerImpl(suite.accountKeeper)
7373
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.encCfg.InterfaceRegistry)
74-
types.RegisterQueryServer(queryHelper, suite.accountKeeper)
74+
types.RegisterQueryServer(queryHelper, keeper.NewQueryServer(suite.accountKeeper))
7575
suite.queryClient = types.NewQueryClient(queryHelper)
7676
}
7777

x/auth/keeper/msg_server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ import (
1313
var _ types.MsgServer = msgServer{}
1414

1515
type msgServer struct {
16-
AccountKeeper
16+
ak AccountKeeper
1717
}
1818

1919
// NewMsgServerImpl returns an implementation of the x/auth MsgServer interface.
2020
func NewMsgServerImpl(ak AccountKeeper) types.MsgServer {
2121
return &msgServer{
22-
AccountKeeper: ak,
22+
ak: ak,
2323
}
2424
}
2525

2626
func (ms msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
27-
if ms.authority != msg.Authority {
28-
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.authority, msg.Authority)
27+
if ms.ak.authority != msg.Authority {
28+
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.ak.authority, msg.Authority)
2929
}
3030

3131
if err := msg.Params.Validate(); err != nil {
3232
return nil, err
3333
}
3434

3535
ctx := sdk.UnwrapSDKContext(goCtx)
36-
if err := ms.SetParams(ctx, msg.Params); err != nil {
36+
if err := ms.ak.SetParams(ctx, msg.Params); err != nil {
3737
return nil, err
3838
}
3939

x/auth/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (AppModule) Name() string {
133133
// module-specific GRPC queries.
134134
func (am AppModule) RegisterServices(cfg module.Configurator) {
135135
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.accountKeeper))
136-
types.RegisterQueryServer(cfg.QueryServer(), am.accountKeeper)
136+
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.accountKeeper))
137137

138138
m := keeper.NewMigrator(am.accountKeeper, cfg.QueryServer(), am.legacySubspace)
139139
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {

0 commit comments

Comments
 (0)