Skip to content

Commit

Permalink
re-do
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica committed Sep 26, 2024
1 parent 3856e77 commit fe95ddf
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 122 deletions.
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
4 changes: 3 additions & 1 deletion x/authz/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"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 Down Expand Up @@ -71,7 +72,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
48 changes: 23 additions & 25 deletions x/authz/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

"cosmossdk.io/core/address"
"cosmossdk.io/core/header"
coretesting "cosmossdk.io/core/testing"
"cosmossdk.io/log"
Expand All @@ -20,7 +21,7 @@ import (
banktypes "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/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
Expand All @@ -39,15 +40,15 @@ var (
type TestSuite struct {
suite.Suite

ctx sdk.Context
addrs []sdk.AccAddress
authzKeeper authzkeeper.Keeper
accountKeeper *authztestutil.MockAccountKeeper
bankKeeper *authztestutil.MockBankKeeper
baseApp *baseapp.BaseApp
encCfg moduletestutil.TestEncodingConfig
queryClient authz.QueryClient
msgSrvr authz.MsgServer
ctx sdk.Context
addrs []sdk.AccAddress
authzKeeper authzkeeper.Keeper
bankKeeper *authztestutil.MockBankKeeper
baseApp *baseapp.BaseApp
encCfg moduletestutil.TestEncodingConfig
queryClient authz.QueryClient
msgSrvr authz.MsgServer
addrCdc address.Codec
}

func (s *TestSuite) SetupTest() {
Expand All @@ -70,16 +71,13 @@ func (s *TestSuite) SetupTest() {

// gomock initializations
ctrl := gomock.NewController(s.T())
s.accountKeeper = authztestutil.NewMockAccountKeeper(ctrl)

s.accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()

s.bankKeeper = authztestutil.NewMockBankKeeper(ctrl)
banktypes.RegisterInterfaces(s.encCfg.InterfaceRegistry)
banktypes.RegisterMsgServer(s.baseApp.MsgServiceRouter(), s.bankKeeper)

env := runtime.NewEnvironment(storeService, coretesting.NewNopLogger(), runtime.EnvWithQueryRouterService(s.baseApp.GRPCQueryRouter()), runtime.EnvWithMsgRouterService(s.baseApp.MsgServiceRouter()))
s.authzKeeper = authzkeeper.NewKeeper(env, s.encCfg.Codec, s.accountKeeper)
s.addrCdc = addresscodec.NewBech32Codec("cosmos")
s.authzKeeper = authzkeeper.NewKeeper(env, s.encCfg.Codec, s.addrCdc)

queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, s.encCfg.InterfaceRegistry)
authz.RegisterQueryServer(queryHelper, s.authzKeeper)
Expand Down Expand Up @@ -183,7 +181,7 @@ func (s *TestSuite) TestKeeperIter() {
granteeAddr := addrs[1]
granter2Addr := addrs[2]
e := ctx.HeaderInfo().Time.AddDate(1, 0, 0)
sendAuthz := banktypes.NewSendAuthorization(coins100, nil, s.accountKeeper.AddressCodec())
sendAuthz := banktypes.NewSendAuthorization(coins100, nil, s.addrCdc)

err := s.authzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, sendAuthz, &e)
s.Require().NoError(err)
Expand All @@ -207,7 +205,7 @@ func (s *TestSuite) TestKeeperGranterGrantsIter() {
grantee2Addr := addrs[3]
grantee3Addr := addrs[4]
e := ctx.HeaderInfo().Time.AddDate(1, 0, 0)
sendAuthz := banktypes.NewSendAuthorization(coins100, nil, s.accountKeeper.AddressCodec())
sendAuthz := banktypes.NewSendAuthorization(coins100, nil, s.addrCdc)

err := s.authzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, sendAuthz, &e)
s.Require().NoError(err)
Expand Down Expand Up @@ -238,13 +236,13 @@ func (s *TestSuite) TestDispatchAction() {

granterAddr := addrs[0]
granteeAddr := addrs[1]
granterStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[0])
granterStrAddr, err := s.addrCdc.BytesToString(addrs[0])
s.Require().NoError(err)
granteeStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[1])
granteeStrAddr, err := s.addrCdc.BytesToString(addrs[1])
s.Require().NoError(err)
recipientStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[2])
recipientStrAddr, err := s.addrCdc.BytesToString(addrs[2])
s.Require().NoError(err)
a := banktypes.NewSendAuthorization(coins100, nil, s.accountKeeper.AddressCodec())
a := banktypes.NewSendAuthorization(coins100, nil, s.addrCdc)

testCases := []struct {
name string
Expand Down Expand Up @@ -402,11 +400,11 @@ func (s *TestSuite) TestDispatchedEvents() {
addrs := s.addrs
granterAddr := addrs[0]
granteeAddr := addrs[1]
granterStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[0])
granterStrAddr, err := s.addrCdc.BytesToString(addrs[0])
s.Require().NoError(err)
granteeStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[1])
granteeStrAddr, err := s.addrCdc.BytesToString(addrs[1])
s.Require().NoError(err)
recipientStrAddr, err := s.accountKeeper.AddressCodec().BytesToString(addrs[2])
recipientStrAddr, err := s.addrCdc.BytesToString(addrs[2])
s.Require().NoError(err)
expiration := s.ctx.HeaderInfo().Time.Add(1 * time.Second) // must be in the future

Expand Down Expand Up @@ -505,7 +503,7 @@ func (s *TestSuite) TestGetAuthorization() {

genAuthMulti := authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgMultiSend{}))
genAuthSend := authz.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgSend{}))
sendAuth := banktypes.NewSendAuthorization(coins10, nil, s.accountKeeper.AddressCodec())
sendAuth := banktypes.NewSendAuthorization(coins10, nil, s.addrCdc)

start := s.ctx.HeaderInfo().Time
expired := start.Add(time.Duration(1) * time.Second)
Expand Down
Loading

0 comments on commit fe95ddf

Please sign in to comment.