Skip to content

Commit

Permalink
remove it completely from feegrant
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica committed Sep 11, 2024
1 parent 7108d17 commit a1a96ed
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 199 deletions.
14 changes: 0 additions & 14 deletions x/feegrant/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,9 @@ package feegrant
import (
"context"

"cosmossdk.io/core/address"

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

// AccountKeeper defines the expected auth Account Keeper (noalias)
type AccountKeeper interface {
AddressCodec() address.Codec

GetModuleAddress(moduleName string) sdk.AccAddress
GetModuleAccount(ctx context.Context, moduleName string) sdk.ModuleAccountI

NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
SetAccount(ctx context.Context, acc sdk.AccountI)
}

// BankKeeper defines the expected supply Keeper (noalias)
type BankKeeper interface {
SpendableCoins(ctx context.Context, addr sdk.AccAddress) sdk.Coins
Expand Down
24 changes: 9 additions & 15 deletions x/feegrant/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"testing"

"github.com/golang/mock/gomock"
"gotest.tools/v3/assert"

coretesting "cosmossdk.io/core/testing"
Expand All @@ -13,9 +12,9 @@ import (
"cosmossdk.io/x/feegrant"
"cosmossdk.io/x/feegrant/keeper"
"cosmossdk.io/x/feegrant/module"
feegranttestutil "cosmossdk.io/x/feegrant/testutil"

"github.com/cosmos/cosmos-sdk/codec/address"
address "cosmossdk.io/core/address"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand All @@ -24,7 +23,6 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

var (
Expand All @@ -37,7 +35,7 @@ var (
type genesisFixture struct {
ctx sdk.Context
feegrantKeeper keeper.Keeper
accountKeeper *feegranttestutil.MockAccountKeeper
addrCdc address.Codec
}

func initFixture(t *testing.T) *genesisFixture {
Expand All @@ -46,22 +44,18 @@ func initFixture(t *testing.T) *genesisFixture {
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test"))
encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, module.AppModule{})

ctrl := gomock.NewController(t)
accountKeeper := feegranttestutil.NewMockAccountKeeper(ctrl)
accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()
addrCdc := addresscodec.NewBech32Codec(sdk.Bech32MainPrefix)

return &genesisFixture{
ctx: testCtx.Ctx,
feegrantKeeper: keeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()), encCfg.Codec, accountKeeper),
accountKeeper: accountKeeper,
feegrantKeeper: keeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()), encCfg.Codec, addrCdc),
addrCdc: addrCdc,
}
}

func TestImportExportGenesis(t *testing.T) {
f := initFixture(t)

f.accountKeeper.EXPECT().GetAccount(gomock.Any(), granteeAddr).Return(authtypes.NewBaseAccountWithAddress(granteeAddr)).AnyTimes()

coins := sdk.NewCoins(sdk.NewCoin("foo", math.NewInt(1_000)))
now := f.ctx.HeaderInfo().Time
oneYear := now.AddDate(1, 0, 0)
Expand All @@ -74,9 +68,9 @@ func TestImportExportGenesis(t *testing.T) {
genesis, err := f.feegrantKeeper.ExportGenesis(f.ctx)
assert.NilError(t, err)

granter, err := f.accountKeeper.AddressCodec().BytesToString(granterAddr.Bytes())
granter, err := f.addrCdc.BytesToString(granterAddr.Bytes())
assert.NilError(t, err)
grantee, err := f.accountKeeper.AddressCodec().BytesToString(granteeAddr.Bytes())
grantee, err := f.addrCdc.BytesToString(granteeAddr.Bytes())
assert.NilError(t, err)

// revoke fee allowance
Expand All @@ -98,7 +92,7 @@ func TestInitGenesis(t *testing.T) {
any, err := codectypes.NewAnyWithValue(&testdata.Dog{})
assert.NilError(t, err)

ac := address.NewBech32Codec("cosmos")
ac := addresscodec.NewBech32Codec("cosmos")

granter, err := ac.BytesToString(granterAddr.Bytes())
assert.NilError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions x/feegrant/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func (q Keeper) Allowance(ctx context.Context, req *feegrant.QueryAllowanceReque
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

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

granteeAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.Grantee)
granteeAddr, err := q.addrCdc.StringToBytes(req.Grantee)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func (q Keeper) Allowances(c context.Context, req *feegrant.QueryAllowancesReque
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

granteeAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.Grantee)
granteeAddr, err := q.addrCdc.StringToBytes(req.Grantee)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func (q Keeper) AllowancesByGranter(c context.Context, req *feegrant.QueryAllowa
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

granterAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.Granter)
granterAddr, err := q.addrCdc.StringToBytes(req.Granter)
if err != nil {
return nil, err
}
Expand Down
31 changes: 16 additions & 15 deletions x/feegrant/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"cosmossdk.io/collections"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
corecontext "cosmossdk.io/core/context"
"cosmossdk.io/core/event"
Expand All @@ -22,9 +23,9 @@ import (
type Keeper struct {
appmodule.Environment

cdc codec.BinaryCodec
authKeeper feegrant.AccountKeeper
Schema collections.Schema
cdc codec.BinaryCodec
addrCdc address.Codec
Schema collections.Schema
// FeeAllowance key: grantee+granter | value: Grant
FeeAllowance collections.Map[collections.Pair[sdk.AccAddress, sdk.AccAddress], feegrant.Grant]
// FeeAllowanceQueue key: expiration time+grantee+granter | value: bool
Expand All @@ -34,13 +35,13 @@ type Keeper struct {
var _ ante.FeegrantKeeper = &Keeper{}

// NewKeeper creates a feegrant Keeper
func NewKeeper(env appmodule.Environment, cdc codec.BinaryCodec, ak feegrant.AccountKeeper) Keeper {
func NewKeeper(env appmodule.Environment, cdc codec.BinaryCodec, addrCdc address.Codec) Keeper {
sb := collections.NewSchemaBuilder(env.KVStoreService)

return Keeper{
Environment: env,
cdc: cdc,
authKeeper: ak,
addrCdc: addrCdc,
FeeAllowance: collections.NewMap(
sb,
feegrant.FeeAllowanceKeyPrefix,
Expand Down Expand Up @@ -85,11 +86,11 @@ func (k Keeper) GrantAllowance(ctx context.Context, granter, grantee sdk.AccAddr
}
}

granterStr, err := k.authKeeper.AddressCodec().BytesToString(granter)
granterStr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeStr, err := k.authKeeper.AddressCodec().BytesToString(grantee)
granteeStr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
Expand Down Expand Up @@ -126,11 +127,11 @@ func (k Keeper) UpdateAllowance(ctx context.Context, granter, grantee sdk.AccAdd
return err
}

granterStr, err := k.authKeeper.AddressCodec().BytesToString(granter)
granterStr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeStr, err := k.authKeeper.AddressCodec().BytesToString(grantee)
granteeStr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
Expand Down Expand Up @@ -173,11 +174,11 @@ func (k Keeper) revokeAllowance(ctx context.Context, granter, grantee sdk.AccAdd
}
}

granterStr, err := k.authKeeper.AddressCodec().BytesToString(granter)
granterStr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeStr, err := k.authKeeper.AddressCodec().BytesToString(grantee)
granteeStr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
Expand Down Expand Up @@ -217,11 +218,11 @@ func (k Keeper) UseGrantedFees(ctx context.Context, granter, grantee sdk.AccAddr
return err
}

granterStr, err := k.authKeeper.AddressCodec().BytesToString(granter)
granterStr, err := k.addrCdc.BytesToString(granter)
if err != nil {
return err
}
granteeStr, err := k.authKeeper.AddressCodec().BytesToString(grantee)
granteeStr, err := k.addrCdc.BytesToString(grantee)
if err != nil {
return err
}
Expand Down Expand Up @@ -255,11 +256,11 @@ func (k *Keeper) emitUseGrantEvent(ctx context.Context, granter, grantee string)
// InitGenesis will initialize the keeper from a *previously validated* GenesisState
func (k Keeper) InitGenesis(ctx context.Context, data *feegrant.GenesisState) error {
for _, f := range data.Allowances {
granter, err := k.authKeeper.AddressCodec().StringToBytes(f.Granter)
granter, err := k.addrCdc.StringToBytes(f.Granter)
if err != nil {
return err
}
grantee, err := k.authKeeper.AddressCodec().StringToBytes(f.Grantee)
grantee, err := k.addrCdc.StringToBytes(f.Grantee)
if err != nil {
return err
}
Expand Down
11 changes: 1 addition & 10 deletions x/feegrant/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"cosmossdk.io/x/feegrant"
"cosmossdk.io/x/feegrant/keeper"
"cosmossdk.io/x/feegrant/module"
feegranttestutil "cosmossdk.io/x/feegrant/testutil"

codecaddress "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
Expand All @@ -22,7 +21,6 @@ import (
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

type KeeperTestSuite struct {
Expand All @@ -34,7 +32,6 @@ type KeeperTestSuite struct {
msgSrvr feegrant.MsgServer
atom sdk.Coins
feegrantKeeper keeper.Keeper
accountKeeper *feegranttestutil.MockAccountKeeper
}

func TestKeeperTestSuite(t *testing.T) {
Expand All @@ -49,20 +46,15 @@ func (suite *KeeperTestSuite) SetupTest() {

// setup gomock and initialize some globally expected executions
ctrl := gomock.NewController(suite.T())
suite.accountKeeper = feegranttestutil.NewMockAccountKeeper(ctrl)
for i := 0; i < len(suite.addrs); i++ {
suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), suite.addrs[i]).Return(authtypes.NewBaseAccountWithAddress(suite.addrs[i])).AnyTimes()
}

ac := codecaddress.NewBech32Codec("cosmos")
suite.accountKeeper.EXPECT().AddressCodec().Return(ac).AnyTimes()
for _, addr := range suite.addrs {
str, err := ac.BytesToString(addr)
suite.Require().NoError(err)
suite.encodedAddrs = append(suite.encodedAddrs, str)
}

suite.feegrantKeeper = keeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()), encCfg.Codec, suite.accountKeeper)
suite.feegrantKeeper = keeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()), encCfg.Codec, ac)
suite.ctx = testCtx.Ctx
suite.msgSrvr = keeper.NewMsgServerImpl(suite.feegrantKeeper)
suite.atom = sdk.NewCoins(sdk.NewCoin("atom", sdkmath.NewInt(555)))
Expand Down Expand Up @@ -181,7 +173,6 @@ func (suite *KeeperTestSuite) TestKeeperCrud() {
address := "cosmos1rxr4mq58w3gtnx5tsc438mwjjafv3mja7k5pnu"
accAddr, err := codecaddress.NewBech32Codec("cosmos").StringToBytes(address)
suite.Require().NoError(err)
suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), accAddr).Return(authtypes.NewBaseAccountWithAddress(accAddr)).AnyTimes()

// let's grant and revoke authorization to non existing account
err = suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[3], accAddr, basic2)
Expand Down
8 changes: 4 additions & 4 deletions x/feegrant/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func (k msgServer) GrantAllowance(ctx context.Context, msg *feegrant.MsgGrantAll
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "cannot self-grant fee authorization")
}

grantee, err := k.authKeeper.AddressCodec().StringToBytes(msg.Grantee)
grantee, err := k.addrCdc.StringToBytes(msg.Grantee)
if err != nil {
return nil, err
}

granter, err := k.authKeeper.AddressCodec().StringToBytes(msg.Granter)
granter, err := k.addrCdc.StringToBytes(msg.Granter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -68,12 +68,12 @@ func (k msgServer) RevokeAllowance(ctx context.Context, msg *feegrant.MsgRevokeA
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "addresses must be different")
}

grantee, err := k.authKeeper.AddressCodec().StringToBytes(msg.Grantee)
grantee, err := k.addrCdc.StringToBytes(msg.Grantee)
if err != nil {
return nil, err
}

granter, err := k.authKeeper.AddressCodec().StringToBytes(msg.Granter)
granter, err := k.addrCdc.StringToBytes(msg.Granter)
if err != nil {
return nil, err
}
Expand Down
16 changes: 0 additions & 16 deletions x/feegrant/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@ package keeper_test
import (
"time"

"github.com/golang/mock/gomock"

"cosmossdk.io/collections"
"cosmossdk.io/core/header"
"cosmossdk.io/x/feegrant"

codecaddress "github.com/cosmos/cosmos-sdk/codec/address"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

func (suite *KeeperTestSuite) TestGrantAllowance() {
ctx := suite.ctx.WithHeaderInfo(header.Info{Time: time.Now()})
oneYear := ctx.HeaderInfo().Time.AddDate(1, 0, 0)
yesterday := ctx.HeaderInfo().Time.AddDate(0, 0, -1)

addressCodec := codecaddress.NewBech32Codec("cosmos")

testCases := []struct {
name string
req func() *feegrant.MsgGrantAllowance
Expand Down Expand Up @@ -62,22 +56,12 @@ func (suite *KeeperTestSuite) TestGrantAllowance() {
name: "valid: grantee account doesn't exist",
req: func() *feegrant.MsgGrantAllowance {
grantee := "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5"
granteeAccAddr, err := addressCodec.StringToBytes(grantee)
suite.Require().NoError(err)
any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{
SpendLimit: suite.atom,
Expiration: &oneYear,
})
suite.Require().NoError(err)

suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), granteeAccAddr).Return(nil).AnyTimes()

acc := authtypes.NewBaseAccountWithAddress(granteeAccAddr)
add, err := addressCodec.StringToBytes(grantee)
suite.Require().NoError(err)

suite.accountKeeper.EXPECT().NewAccountWithAddress(gomock.Any(), add).Return(acc).AnyTimes()

suite.Require().NoError(err)
return &feegrant.MsgGrantAllowance{
Granter: suite.encodedAddrs[0],
Expand Down
Loading

0 comments on commit a1a96ed

Please sign in to comment.