refactor(auth): decouple auth Keeper API from Msg and Query server implementation (#15985)

Co-authored-by: unknown unknown <unknown@unknown>
This commit is contained in:
testinginprod 2023-05-01 16:30:20 +02:00 committed by GitHub
parent 7c59eade9a
commit 3ada275c32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 40 deletions

View File

@ -116,6 +116,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### API Breaking Changes
* (x/auth) [#15985](https://github.com/cosmos/cosmos-sdk/pull/15985) The `AccountKeeper` does not expose the `QueryServer` and `MsgServer` APIs anymore.
* (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`.
* (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`.
* (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.

View File

@ -74,7 +74,7 @@ func (suite *DeterministicTestSuite) SetupTest() {
)
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.encCfg.InterfaceRegistry)
types.RegisterQueryServer(queryHelper, suite.accountKeeper)
types.RegisterQueryServer(queryHelper, keeper.NewQueryServer(suite.accountKeeper))
suite.queryClient = types.NewQueryClient(queryHelper)
suite.key = key
@ -239,7 +239,7 @@ func (suite *DeterministicTestSuite) TestGRPCQueryAccountInfo() {
func (suite *DeterministicTestSuite) createAndReturnQueryClient(ak keeper.AccountKeeper) types.QueryClient {
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.encCfg.InterfaceRegistry)
types.RegisterQueryServer(queryHelper, ak)
types.RegisterQueryServer(queryHelper, keeper.NewQueryServer(ak))
return types.NewQueryClient(queryHelper)
}

View File

@ -19,9 +19,15 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
var _ types.QueryServer = AccountKeeper{}
var _ types.QueryServer = queryServer{}
func (ak AccountKeeper) AccountAddressByID(c context.Context, req *types.QueryAccountAddressByIDRequest) (*types.QueryAccountAddressByIDResponse, error) {
func NewQueryServer(k AccountKeeper) types.QueryServer {
return queryServer{k: k}
}
type queryServer struct{ k AccountKeeper }
func (s queryServer) AccountAddressByID(c context.Context, req *types.QueryAccountAddressByIDRequest) (*types.QueryAccountAddressByIDResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
@ -33,7 +39,7 @@ func (ak AccountKeeper) AccountAddressByID(c context.Context, req *types.QueryAc
accID := req.AccountId
ctx := sdk.UnwrapSDKContext(c)
address := ak.GetAccountAddressByID(ctx, accID)
address := s.k.GetAccountAddressByID(ctx, accID)
if len(address) == 0 {
return nil, status.Errorf(codes.NotFound, "account address not found with account number %d", req.Id)
}
@ -41,17 +47,17 @@ func (ak AccountKeeper) AccountAddressByID(c context.Context, req *types.QueryAc
return &types.QueryAccountAddressByIDResponse{AccountAddress: address}, nil
}
func (ak AccountKeeper) Accounts(ctx context.Context, req *types.QueryAccountsRequest) (*types.QueryAccountsResponse, error) {
func (s queryServer) Accounts(ctx context.Context, req *types.QueryAccountsRequest) (*types.QueryAccountsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
store := ak.storeService.OpenKVStore(ctx)
store := s.k.storeService.OpenKVStore(ctx)
accountsStore := prefix.NewStore(runtime.KVStoreAdapter(store), types.AddressStoreKeyPrefix)
var accounts []*codectypes.Any
pageRes, err := query.Paginate(accountsStore, req.Pagination, func(key, value []byte) error {
account := ak.decodeAccount(value)
account := s.k.decodeAccount(value)
any, err := codectypes.NewAnyWithValue(account)
if err != nil {
return err
@ -68,7 +74,7 @@ func (ak AccountKeeper) Accounts(ctx context.Context, req *types.QueryAccountsRe
}
// Account returns account details based on address
func (ak AccountKeeper) Account(c context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) {
func (s queryServer) Account(c context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
@ -78,11 +84,11 @@ func (ak AccountKeeper) Account(c context.Context, req *types.QueryAccountReques
}
ctx := sdk.UnwrapSDKContext(c)
addr, err := ak.StringToBytes(req.Address)
addr, err := s.k.StringToBytes(req.Address)
if err != nil {
return nil, err
}
account := ak.GetAccount(ctx, addr)
account := s.k.GetAccount(ctx, addr)
if account == nil {
return nil, status.Errorf(codes.NotFound, "account %s not found", req.Address)
}
@ -96,18 +102,18 @@ func (ak AccountKeeper) Account(c context.Context, req *types.QueryAccountReques
}
// Params returns parameters of auth module
func (ak AccountKeeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
func (s queryServer) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
params := ak.GetParams(ctx)
params := s.k.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}
// ModuleAccounts returns all the existing Module Accounts
func (ak AccountKeeper) ModuleAccounts(c context.Context, req *types.QueryModuleAccountsRequest) (*types.QueryModuleAccountsResponse, error) {
func (s queryServer) ModuleAccounts(c context.Context, req *types.QueryModuleAccountsRequest) (*types.QueryModuleAccountsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
@ -115,16 +121,16 @@ func (ak AccountKeeper) ModuleAccounts(c context.Context, req *types.QueryModule
ctx := sdk.UnwrapSDKContext(c)
// For deterministic output, sort the permAddrs by module name.
sortedPermAddrs := make([]string, 0, len(ak.permAddrs))
for moduleName := range ak.permAddrs {
sortedPermAddrs := make([]string, 0, len(s.k.permAddrs))
for moduleName := range s.k.permAddrs {
sortedPermAddrs = append(sortedPermAddrs, moduleName)
}
sort.Strings(sortedPermAddrs)
modAccounts := make([]*codectypes.Any, 0, len(ak.permAddrs))
modAccounts := make([]*codectypes.Any, 0, len(s.k.permAddrs))
for _, moduleName := range sortedPermAddrs {
account := ak.GetModuleAccount(ctx, moduleName)
account := s.k.GetModuleAccount(ctx, moduleName)
if account == nil {
return nil, status.Errorf(codes.NotFound, "account %s not found", moduleName)
}
@ -139,7 +145,7 @@ func (ak AccountKeeper) ModuleAccounts(c context.Context, req *types.QueryModule
}
// ModuleAccountByName returns module account by module name
func (ak AccountKeeper) ModuleAccountByName(c context.Context, req *types.QueryModuleAccountByNameRequest) (*types.QueryModuleAccountByNameResponse, error) {
func (s queryServer) ModuleAccountByName(c context.Context, req *types.QueryModuleAccountByNameRequest) (*types.QueryModuleAccountByNameResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
@ -151,7 +157,7 @@ func (ak AccountKeeper) ModuleAccountByName(c context.Context, req *types.QueryM
ctx := sdk.UnwrapSDKContext(c)
moduleName := req.Name
account := ak.GetModuleAccount(ctx, moduleName)
account := s.k.GetModuleAccount(ctx, moduleName)
if account == nil {
return nil, status.Errorf(codes.NotFound, "account %s not found", moduleName)
}
@ -164,8 +170,8 @@ func (ak AccountKeeper) ModuleAccountByName(c context.Context, req *types.QueryM
}
// Bech32Prefix returns the keeper internally stored bech32 prefix.
func (ak AccountKeeper) Bech32Prefix(ctx context.Context, req *types.Bech32PrefixRequest) (*types.Bech32PrefixResponse, error) {
bech32Prefix, err := ak.getBech32Prefix()
func (s queryServer) Bech32Prefix(ctx context.Context, req *types.Bech32PrefixRequest) (*types.Bech32PrefixResponse, error) {
bech32Prefix, err := s.k.getBech32Prefix()
if err != nil {
return nil, err
}
@ -175,7 +181,7 @@ func (ak AccountKeeper) Bech32Prefix(ctx context.Context, req *types.Bech32Prefi
// AddressBytesToString converts an address from bytes to string, using the
// keeper's bech32 prefix.
func (ak AccountKeeper) AddressBytesToString(ctx context.Context, req *types.AddressBytesToStringRequest) (*types.AddressBytesToStringResponse, error) {
func (s queryServer) AddressBytesToString(ctx context.Context, req *types.AddressBytesToStringRequest) (*types.AddressBytesToStringResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
@ -184,7 +190,7 @@ func (ak AccountKeeper) AddressBytesToString(ctx context.Context, req *types.Add
return nil, errors.New("empty address bytes is not allowed")
}
text, err := ak.BytesToString(req.AddressBytes)
text, err := s.k.BytesToString(req.AddressBytes)
if err != nil {
return nil, err
}
@ -194,7 +200,7 @@ func (ak AccountKeeper) AddressBytesToString(ctx context.Context, req *types.Add
// AddressStringToBytes converts an address from string to bytes, using the
// keeper's bech32 prefix.
func (ak AccountKeeper) AddressStringToBytes(ctx context.Context, req *types.AddressStringToBytesRequest) (*types.AddressStringToBytesResponse, error) {
func (s queryServer) AddressStringToBytes(ctx context.Context, req *types.AddressStringToBytesRequest) (*types.AddressStringToBytesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
@ -203,7 +209,7 @@ func (ak AccountKeeper) AddressStringToBytes(ctx context.Context, req *types.Add
return nil, errors.New("empty address string is not allowed")
}
bz, err := ak.StringToBytes(req.AddressString)
bz, err := s.k.StringToBytes(req.AddressString)
if err != nil {
return nil, err
}
@ -212,7 +218,7 @@ func (ak AccountKeeper) AddressStringToBytes(ctx context.Context, req *types.Add
}
// AccountInfo implements the AccountInfo query.
func (ak AccountKeeper) AccountInfo(goCtx context.Context, req *types.QueryAccountInfoRequest) (*types.QueryAccountInfoResponse, error) {
func (s queryServer) AccountInfo(goCtx context.Context, req *types.QueryAccountInfoRequest) (*types.QueryAccountInfoResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
@ -222,12 +228,12 @@ func (ak AccountKeeper) AccountInfo(goCtx context.Context, req *types.QueryAccou
}
ctx := sdk.UnwrapSDKContext(goCtx)
addr, err := ak.StringToBytes(req.Address)
addr, err := s.k.StringToBytes(req.Address)
if err != nil {
return nil, err
}
account := ak.GetAccount(ctx, addr)
account := s.k.GetAccount(ctx, addr)
if account == nil {
return nil, status.Errorf(codes.NotFound, "account %s not found", req.Address)
}

View File

@ -76,7 +76,7 @@ type AccountKeeper struct {
authority string
// State
ParamsState collections.Item[types.Params] // NOTE: name is this because it conflicts with the Params gRPC method impl
Params collections.Item[types.Params]
AccountNumber collections.Sequence
}
@ -107,7 +107,7 @@ func NewAccountKeeper(
cdc: cdc,
permAddrs: permAddrs,
authority: authority,
ParamsState: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
AccountNumber: collections.NewSequence(sb, types.GlobalAccountNumberKey, "account_number"),
}
}
@ -265,12 +265,12 @@ func (ak AccountKeeper) getBech32Prefix() (string, error) {
// SetParams sets the auth module's parameters.
// CONTRACT: This method performs no validation of the parameters.
func (ak AccountKeeper) SetParams(ctx context.Context, params types.Params) error {
return ak.ParamsState.Set(ctx, params)
return ak.Params.Set(ctx, params)
}
// GetParams gets the auth module's parameters.
func (ak AccountKeeper) GetParams(ctx context.Context) (params types.Params) {
params, err := ak.ParamsState.Get(ctx)
params, err := ak.Params.Get(ctx)
if err != nil && !errors.Is(err, collections.ErrNotFound) {
panic(err)
}

View File

@ -71,7 +71,7 @@ func (suite *KeeperTestSuite) SetupTest() {
)
suite.msgServer = keeper.NewMsgServerImpl(suite.accountKeeper)
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.encCfg.InterfaceRegistry)
types.RegisterQueryServer(queryHelper, suite.accountKeeper)
types.RegisterQueryServer(queryHelper, keeper.NewQueryServer(suite.accountKeeper))
suite.queryClient = types.NewQueryClient(queryHelper)
}

View File

@ -13,19 +13,19 @@ import (
var _ types.MsgServer = msgServer{}
type msgServer struct {
AccountKeeper
ak AccountKeeper
}
// NewMsgServerImpl returns an implementation of the x/auth MsgServer interface.
func NewMsgServerImpl(ak AccountKeeper) types.MsgServer {
return &msgServer{
AccountKeeper: ak,
ak: ak,
}
}
func (ms msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if ms.authority != msg.Authority {
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.authority, msg.Authority)
if ms.ak.authority != msg.Authority {
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.ak.authority, msg.Authority)
}
if err := msg.Params.Validate(); err != nil {
@ -33,7 +33,7 @@ func (ms msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdatePara
}
ctx := sdk.UnwrapSDKContext(goCtx)
if err := ms.SetParams(ctx, msg.Params); err != nil {
if err := ms.ak.SetParams(ctx, msg.Params); err != nil {
return nil, err
}

View File

@ -133,7 +133,7 @@ func (AppModule) Name() string {
// module-specific GRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.accountKeeper))
types.RegisterQueryServer(cfg.QueryServer(), am.accountKeeper)
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.accountKeeper))
m := keeper.NewMigrator(am.accountKeeper, cfg.QueryServer(), am.legacySubspace)
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {