Skip to content

Commit

Permalink
chore(x/authz)!: Remove account keeper dependency (#21632)
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica authored Oct 1, 2024
1 parent c44683d commit 8763d8d
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 202 deletions.
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func NewSimApp(
app.CircuitKeeper = circuitkeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[circuittypes.StoreKey]), logger.With(log.ModuleKey, "x/circuit")), appCodec, govModuleAddr, app.AuthKeeper.AddressCodec())
app.BaseApp.SetCircuitBreaker(&app.CircuitKeeper)

app.AuthzKeeper = authzkeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[authzkeeper.StoreKey]), logger.With(log.ModuleKey, "x/authz"), runtime.EnvWithMsgRouterService(app.MsgServiceRouter()), runtime.EnvWithQueryRouterService(app.GRPCQueryRouter())), appCodec, app.AuthKeeper)
app.AuthzKeeper = authzkeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[authzkeeper.StoreKey]), logger.With(log.ModuleKey, "x/authz"), runtime.EnvWithMsgRouterService(app.MsgServiceRouter()), runtime.EnvWithQueryRouterService(app.GRPCQueryRouter())), appCodec, signingCtx.AddressCodec())

groupConfig := group.DefaultConfig()
/*
Expand Down
1 change: 1 addition & 0 deletions x/authz/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* [#21632](https://github.com/cosmos/cosmos-sdk/pull/21632) `NewKeeper` now takes `address.Codec` instead of `authKeeper`.
* [#21044](https://github.com/cosmos/cosmos-sdk/pull/21044) `k.DispatchActions` returns a slice of byte slices of proto marshaled anys instead of a slice of byte slices of `sdk.Result.Data`.
* [#20502](https://github.com/cosmos/cosmos-sdk/pull/20502) `Accept` on the `Authorization` interface now expects the authz environment in the `context.Context`. This is already done when `Accept` is called by `k.DispatchActions`, but should be done manually if `Accept` is called directly.
* [#19783](https://github.com/cosmos/cosmos-sdk/pull/19783) Removes the use of Accounts String() method
Expand Down
9 changes: 0 additions & 9 deletions x/authz/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@ package authz
import (
"context"

"cosmossdk.io/core/address"

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

// AccountKeeper defines the expected account keeper (noalias)
type AccountKeeper interface {
AddressCodec() address.Codec
GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
}

// BankKeeper defines the expected interface needed to retrieve account balances.
type BankKeeper interface {
SpendableCoins(ctx context.Context, addr sdk.AccAddress) sdk.Coins
Expand Down
8 changes: 4 additions & 4 deletions x/authz/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func (k Keeper) InitGenesis(ctx context.Context, data *authz.GenesisState) error
continue
}

grantee, err := k.authKeeper.AddressCodec().StringToBytes(entry.Grantee)
grantee, err := k.addrCdc.StringToBytes(entry.Grantee)
if err != nil {
return err
}
granter, err := k.authKeeper.AddressCodec().StringToBytes(entry.Granter)
granter, err := k.addrCdc.StringToBytes(entry.Granter)
if err != nil {
return err
}
Expand All @@ -44,11 +44,11 @@ func (k Keeper) InitGenesis(ctx context.Context, data *authz.GenesisState) error
func (k Keeper) ExportGenesis(ctx context.Context) (*authz.GenesisState, error) {
var entries []authz.GrantAuthorization
err := k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) (bool, error) {
granterAddr, err := k.authKeeper.AddressCodec().BytesToString(granter)
granterAddr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return false, err
}
granteeAddr, err := k.authKeeper.AddressCodec().BytesToString(grantee)
granteeAddr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return false, err
}
Expand Down
21 changes: 7 additions & 14 deletions x/authz/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

"cosmossdk.io/core/header"
Expand All @@ -14,11 +13,10 @@ import (
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/authz/keeper"
authzmodule "cosmossdk.io/x/authz/module"
authztestutil "cosmossdk.io/x/authz/testutil"
bank "cosmossdk.io/x/bank/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec/address"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/runtime"
Expand All @@ -37,11 +35,10 @@ var (
type GenesisTestSuite struct {
suite.Suite

ctx sdk.Context
keeper keeper.Keeper
baseApp *baseapp.BaseApp
accountKeeper *authztestutil.MockAccountKeeper
encCfg moduletestutil.TestEncodingConfig
ctx sdk.Context
keeper keeper.Keeper
baseApp *baseapp.BaseApp
encCfg moduletestutil.TestEncodingConfig
}

func (suite *GenesisTestSuite) SetupTest() {
Expand All @@ -52,11 +49,6 @@ func (suite *GenesisTestSuite) SetupTest() {

suite.encCfg = moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, authzmodule.AppModule{})

// gomock initializations
ctrl := gomock.NewController(suite.T())
suite.accountKeeper = authztestutil.NewMockAccountKeeper(ctrl)
suite.accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()

suite.baseApp = baseapp.NewBaseApp(
"authz",
log.NewNopLogger(),
Expand All @@ -71,7 +63,8 @@ func (suite *GenesisTestSuite) SetupTest() {
msr.SetInterfaceRegistry(suite.encCfg.InterfaceRegistry)
env := runtime.NewEnvironment(storeService, coretesting.NewNopLogger(), runtime.EnvWithMsgRouterService(msr))

suite.keeper = keeper.NewKeeper(env, suite.encCfg.Codec, suite.accountKeeper)
addrCdc := addresscodec.NewBech32Codec("cosmos")
suite.keeper = keeper.NewKeeper(env, suite.encCfg.Codec, addrCdc)
}

func (suite *GenesisTestSuite) TestImportExportGenesis() {
Expand Down
12 changes: 6 additions & 6 deletions x/authz/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func (k Keeper) Grants(ctx context.Context, req *authz.QueryGrantsRequest) (*aut
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

granter, err := k.authKeeper.AddressCodec().StringToBytes(req.Granter)
granter, err := k.addrCdc.StringToBytes(req.Granter)
if err != nil {
return nil, err
}

grantee, err := k.authKeeper.AddressCodec().StringToBytes(req.Grantee)
grantee, err := k.addrCdc.StringToBytes(req.Grantee)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func (k Keeper) GranterGrants(ctx context.Context, req *authz.QueryGranterGrants
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

granter, err := k.authKeeper.AddressCodec().StringToBytes(req.Granter)
granter, err := k.addrCdc.StringToBytes(req.Granter)
if err != nil {
return nil, err
}
Expand All @@ -116,7 +116,7 @@ func (k Keeper) GranterGrants(ctx context.Context, req *authz.QueryGranterGrants

grantee := firstAddressFromGrantStoreKey(key)

granteeAddr, err := k.authKeeper.AddressCodec().BytesToString(grantee)
granteeAddr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func (k Keeper) GranteeGrants(ctx context.Context, req *authz.QueryGranteeGrants
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

grantee, err := k.authKeeper.AddressCodec().StringToBytes(req.Grantee)
grantee, err := k.addrCdc.StringToBytes(req.Grantee)
if err != nil {
return nil, err
}
Expand All @@ -169,7 +169,7 @@ func (k Keeper) GranteeGrants(ctx context.Context, req *authz.QueryGranteeGrants
return nil, status.Error(codes.Internal, err.Error())
}

granterAddr, err := k.authKeeper.AddressCodec().BytesToString(granter)
granterAddr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions x/authz/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func (suite *TestSuite) TestGRPCQueryAuthorization() {
expAuthorization authz.Authorization
)

addr0, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[0])
addr0, err := suite.addrCdc.BytesToString(addrs[0])
suite.Require().NoError(err)
addr1, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[1])
addr1, err := suite.addrCdc.BytesToString(addrs[1])
suite.Require().NoError(err)

testCases := []struct {
Expand Down Expand Up @@ -137,7 +137,7 @@ func (suite *TestSuite) TestGRPCQueryGranterGrants() {
require := suite.Require()
queryClient, addrs := suite.queryClient, suite.addrs

addr0, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[0])
addr0, err := suite.addrCdc.BytesToString(addrs[0])
suite.Require().NoError(err)

testCases := []struct {
Expand Down Expand Up @@ -209,9 +209,9 @@ func (suite *TestSuite) TestGRPCQueryGranteeGrants() {
require := suite.Require()
queryClient, addrs := suite.queryClient, suite.addrs

addr0, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[0])
addr0, err := suite.addrCdc.BytesToString(addrs[0])
suite.Require().NoError(err)
addr2, err := suite.accountKeeper.AddressCodec().BytesToString(addrs[2])
addr2, err := suite.addrCdc.BytesToString(addrs[2])
suite.Require().NoError(err)

testCases := []struct {
Expand Down Expand Up @@ -299,7 +299,7 @@ func (suite *TestSuite) createSendAuthorization(grantee, granter sdk.AccAddress)
func (suite *TestSuite) createSendAuthorizationWithAllowList(grantee, granter sdk.AccAddress) authz.Authorization {
exp := suite.ctx.HeaderInfo().Time.Add(time.Hour)
newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100))
addr, err := suite.accountKeeper.AddressCodec().BytesToString(suite.addrs[5])
addr, err := suite.addrCdc.BytesToString(suite.addrs[5])
suite.Require().NoError(err)
authorization := &banktypes.SendAuthorization{SpendLimit: newCoins, AllowList: []string{addr}}
err = suite.authzKeeper.SaveGrant(suite.ctx, grantee, granter, authorization, &exp)
Expand Down
23 changes: 12 additions & 11 deletions x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
gogoproto "github.com/cosmos/gogoproto/proto"
gogoprotoany "github.com/cosmos/gogoproto/types/any"

"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
corecontext "cosmossdk.io/core/context"
errorsmod "cosmossdk.io/errors"
Expand All @@ -30,16 +31,16 @@ const gasCostPerIteration = uint64(20)
type Keeper struct {
appmodule.Environment

cdc codec.Codec
authKeeper authz.AccountKeeper
cdc codec.Codec
addrCdc address.Codec
}

// NewKeeper constructs a message authorization Keeper
func NewKeeper(env appmodule.Environment, cdc codec.Codec, ak authz.AccountKeeper) Keeper {
func NewKeeper(env appmodule.Environment, cdc codec.Codec, addrCdc address.Codec) Keeper {
return Keeper{
Environment: env,
cdc: cdc,
authKeeper: ak,
addrCdc: addrCdc,
}
}

Expand Down Expand Up @@ -206,11 +207,11 @@ func (k Keeper) SaveGrant(ctx context.Context, grantee, granter sdk.AccAddress,
return err
}

granterAddr, err := k.authKeeper.AddressCodec().BytesToString(granter)
granterAddr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeAddr, err := k.authKeeper.AddressCodec().BytesToString(grantee)
granteeAddr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
Expand All @@ -229,12 +230,12 @@ func (k Keeper) DeleteGrant(ctx context.Context, grantee, granter sdk.AccAddress
skey := grantStoreKey(grantee, granter, msgType)
grant, found := k.getGrant(ctx, skey)
if !found {
granterAddr, err := k.authKeeper.AddressCodec().BytesToString(granter)
granterAddr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return errorsmod.Wrapf(authz.ErrNoAuthorizationFound,
"could not convert granter address to string")
}
granteeAddr, err := k.authKeeper.AddressCodec().BytesToString(grantee)
granteeAddr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return errorsmod.Wrapf(authz.ErrNoAuthorizationFound,
"could not convert grantee address to string")
Expand All @@ -255,11 +256,11 @@ func (k Keeper) DeleteGrant(ctx context.Context, grantee, granter sdk.AccAddress
return err
}

granterAddr, err := k.authKeeper.AddressCodec().BytesToString(granter)
granterAddr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeAddr, err := k.authKeeper.AddressCodec().BytesToString(grantee)
granteeAddr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
Expand Down Expand Up @@ -291,7 +292,7 @@ func (k Keeper) DeleteAllGrants(ctx context.Context, granter sdk.AccAddress) err
}
}

grantAddr, err := k.authKeeper.AddressCodec().BytesToString(granter)
grantAddr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 8763d8d

Please sign in to comment.