diff --git a/scripts/mockgen.sh b/scripts/mockgen.sh index b5b2fa7a9d..f7bf4cefc7 100755 --- a/scripts/mockgen.sh +++ b/scripts/mockgen.sh @@ -11,5 +11,6 @@ $mockgen_cmd -package mocks -destination tests/mocks/grpc_server.go github.com/g $mockgen_cmd -package mocks -destination tests/mocks/tendermint_tendermint_libs_log_DB.go github.com/tendermint/tendermint/libs/log Logger $mockgen_cmd -source=orm/model/ormtable/hooks.go -package ormmocks -destination orm/testing/ormmocks/hooks.go $mockgen_cmd -source=x/nft/expected_keepers.go -package testutil -destination x/nft/testutil/expected_keepers_mocks.go +$mockgen_cmd -source=x/feegrant/expected_keepers.go -package testutil -destination x/feegrant/testutil/expected_keepers_mocks.go $mockgen_cmd -source=x/mint/types/expected_keepers.go -package testutil -destination x/mint/testutil/expected_keepers_mocks.go $mockgen_cmd -source=x/params/proposal_handler_test.go -package testutil -destination x/params/testutil/staking_keeper_mock.go diff --git a/x/feegrant/basic_fee_test.go b/x/feegrant/basic_fee_test.go index d2795e82b7..a6c66c8ae9 100644 --- a/x/feegrant/basic_fee_test.go +++ b/x/feegrant/basic_fee_test.go @@ -8,33 +8,16 @@ import ( "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" "github.com/cosmos/cosmos-sdk/x/feegrant" - "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" - "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" - stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" ) func TestBasicFeeValidAllow(t *testing.T) { - var ( - interfaceRegistry codectypes.InterfaceRegistry - bankKeeper bankkeeper.Keeper - stakingKeeper *stakingkeeper.Keeper - feegrantKeeper keeper.Keeper - ) + key := sdk.NewKVStoreKey(feegrant.StoreKey) + testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test")) - app, err := simtestutil.Setup(testutil.AppConfig, - &feegrantKeeper, - &bankKeeper, - &stakingKeeper, - &interfaceRegistry, - ) - require.NoError(t, err) - - ctx := app.BaseApp.NewContext(false, tmproto.Header{Height: 1}) + ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Height: 1}) badTime := ctx.BlockTime().AddDate(0, 0, -1) allowace := &feegrant.BasicAllowance{ @@ -42,7 +25,7 @@ func TestBasicFeeValidAllow(t *testing.T) { } require.Error(t, allowace.ValidateBasic()) - ctx = app.BaseApp.NewContext(false, tmproto.Header{ + ctx = ctx.WithBlockHeader(tmproto.Header{ Time: time.Now(), }) eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 10)) @@ -150,7 +133,7 @@ func TestBasicFeeValidAllow(t *testing.T) { err := tc.allowance.ValidateBasic() require.NoError(t, err) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}).WithBlockTime(tc.blockTime) + ctx := testCtx.Ctx.WithBlockTime(tc.blockTime) // now try to deduct removed, err := tc.allowance.Accept(ctx, tc.fee, []sdk.Msg{}) diff --git a/x/feegrant/filtered_fee_test.go b/x/feegrant/filtered_fee_test.go index 4bda42a5b2..0f33cb2fd8 100644 --- a/x/feegrant/filtered_fee_test.go +++ b/x/feegrant/filtered_fee_test.go @@ -8,37 +8,20 @@ import ( "github.com/stretchr/testify/require" ocproto "github.com/tendermint/tendermint/proto/tendermint/types" - "cosmossdk.io/depinject" - "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/feegrant" - "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" - "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" - feegranttestutil "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" - stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + "github.com/cosmos/cosmos-sdk/x/feegrant/module" ) func TestFilteredFeeValidAllow(t *testing.T) { - var ( - interfaceRegistry codectypes.InterfaceRegistry - bankKeeper bankkeeper.Keeper - stakingKeeper *stakingkeeper.Keeper - feegrantKeeper keeper.Keeper - ) + key := sdk.NewKVStoreKey(feegrant.StoreKey) + testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test")) + encCfg := moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{}) - app, err := simtestutil.Setup(testutil.AppConfig, - &feegrantKeeper, - &bankKeeper, - &stakingKeeper, - &interfaceRegistry, - ) - require.NoError(t, err) - - ctx := app.BaseApp.NewContext(false, ocproto.Header{Time: time.Now()}) + ctx := testCtx.Ctx.WithBlockHeader(ocproto.Header{Time: time.Now()}) eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 10)) atom := sdk.NewCoins(sdk.NewInt64Coin("atom", 555)) @@ -155,7 +138,7 @@ func TestFilteredFeeValidAllow(t *testing.T) { err := tc.allowance.ValidateBasic() require.NoError(t, err) - ctx := app.BaseApp.NewContext(false, ocproto.Header{}).WithBlockTime(tc.blockTime) + ctx := testCtx.Ctx.WithBlockTime(tc.blockTime) // create grant var granter, grantee sdk.AccAddress @@ -187,14 +170,12 @@ func TestFilteredFeeValidAllow(t *testing.T) { require.NoError(t, err) // save the grant - var cdc codec.Codec - depinject.Inject(feegranttestutil.AppConfig, &cdc) - bz, err := cdc.Marshal(&newGrant) + bz, err := encCfg.Codec.Marshal(&newGrant) require.NoError(t, err) // load the grant var loadedGrant feegrant.Grant - err = cdc.Unmarshal(bz, &loadedGrant) + err = encCfg.Codec.Unmarshal(bz, &loadedGrant) require.NoError(t, err) newAllowance, err := loadedGrant.GetGrant() diff --git a/x/feegrant/grant_test.go b/x/feegrant/grant_test.go index b96e766446..2d945c6bb7 100644 --- a/x/feegrant/grant_test.go +++ b/x/feegrant/grant_test.go @@ -7,36 +7,19 @@ import ( "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" "github.com/cosmos/cosmos-sdk/x/feegrant" - "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" - "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" - stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + "github.com/cosmos/cosmos-sdk/x/feegrant/module" ) func TestGrant(t *testing.T) { - var ( - interfaceRegistry codectypes.InterfaceRegistry - bankKeeper bankkeeper.Keeper - stakingKeeper *stakingkeeper.Keeper - feegrantKeeper keeper.Keeper - cdc codec.Codec - ) + key := sdk.NewKVStoreKey(feegrant.StoreKey) + testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test")) + encCfg := moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{}) - app, err := simtestutil.Setup(testutil.AppConfig, - &feegrantKeeper, - &bankKeeper, - &stakingKeeper, - &interfaceRegistry, - &cdc, - ) - require.NoError(t, err) - - ctx := app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()}) + ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: time.Now()}) addr, err := sdk.AccAddressFromBech32("cosmos1qk93t4j0yyzgqgt6k5qf8deh8fq6smpn3ntu3x") require.NoError(t, err) @@ -109,10 +92,10 @@ func TestGrant(t *testing.T) { require.NoError(t, err) // if it is valid, let's try to serialize, deserialize, and make sure it matches - bz, err := cdc.Marshal(&grant) + bz, err := encCfg.Codec.Marshal(&grant) require.NoError(t, err) var loaded feegrant.Grant - err = cdc.Unmarshal(bz, &loaded) + err = encCfg.Codec.Unmarshal(bz, &loaded) require.NoError(t, err) err = loaded.ValidateBasic() diff --git a/x/feegrant/keeper/genesis_test.go b/x/feegrant/keeper/genesis_test.go index b7a17ade43..8db78b5f7c 100644 --- a/x/feegrant/keeper/genesis_test.go +++ b/x/feegrant/keeper/genesis_test.go @@ -3,20 +3,20 @@ package keeper_test import ( "testing" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + "github.com/cosmos/cosmos-sdk/testutil" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/feegrant" "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" - "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" - stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + "github.com/cosmos/cosmos-sdk/x/feegrant/module" + feegranttestutil "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" ) type GenesisTestSuite struct { @@ -26,24 +26,17 @@ type GenesisTestSuite struct { } func (suite *GenesisTestSuite) SetupTest() { - checkTx := false + key := sdk.NewKVStoreKey(feegrant.StoreKey) + testCtx := testutil.DefaultContextWithDB(suite.T(), key, sdk.NewTransientStoreKey("transient_test")) + encCfg := moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{}) - var ( - interfaceRegistry codectypes.InterfaceRegistry - bankKeeper bankkeeper.Keeper - stakingKeeper *stakingkeeper.Keeper - cdc codec.Codec - ) + ctrl := gomock.NewController(suite.T()) + accountKeeper := feegranttestutil.NewMockAccountKeeper(ctrl) + accountKeeper.EXPECT().GetAccount(gomock.Any(), granteeAddr).Return(authtypes.NewBaseAccountWithAddress(granteeAddr)).AnyTimes() - app, err := simtestutil.Setup(testutil.AppConfig, - &suite.feegrantKeeper, - &bankKeeper, - &stakingKeeper, - &interfaceRegistry, - &cdc, - ) - suite.Require().NoError(err) - suite.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 1}) + suite.feegrantKeeper = keeper.NewKeeper(encCfg.Codec, key, accountKeeper) + + suite.ctx = testCtx.Ctx } var ( diff --git a/x/feegrant/keeper/keeper.go b/x/feegrant/keeper/keeper.go index 3f198edd8a..302e5f1cd2 100644 --- a/x/feegrant/keeper/keeper.go +++ b/x/feegrant/keeper/keeper.go @@ -112,7 +112,6 @@ func (k Keeper) GrantAllowance(ctx sdk.Context, granter, grantee sdk.AccAddress, } store.Set(key, bz) - ctx.EventManager().EmitEvent( sdk.NewEvent( feegrant.EventTypeSetFeeGrant, @@ -222,7 +221,6 @@ func (k Keeper) IterateAllFeeAllowances(ctx sdk.Context, cb func(grant feegrant. if err := k.cdc.Unmarshal(bz, &feeGrant); err != nil { return err } - stop = cb(feeGrant) } diff --git a/x/feegrant/keeper/keeper_test.go b/x/feegrant/keeper/keeper_test.go index b1f4f478e7..16d10a532c 100644 --- a/x/feegrant/keeper/keeper_test.go +++ b/x/feegrant/keeper/keeper_test.go @@ -3,30 +3,29 @@ package keeper_test import ( "testing" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/testutil" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/feegrant" "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" - "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" - stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + "github.com/cosmos/cosmos-sdk/x/feegrant/module" + feegranttestutil "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" ) type KeeperTestSuite struct { suite.Suite - ctx sdk.Context - addrs []sdk.AccAddress - msgSrvr feegrant.MsgServer - atom sdk.Coins - feegrantKeeper keeper.Keeper - interfaceRegistry codectypes.InterfaceRegistry - bankKeeper bankkeeper.Keeper - stakingKeeper *stakingkeeper.Keeper + ctx sdk.Context + addrs []sdk.AccAddress + msgSrvr feegrant.MsgServer + atom sdk.Coins + feegrantKeeper keeper.Keeper + accountKeeper *feegranttestutil.MockAccountKeeper } func TestKeeperTestSuite(t *testing.T) { @@ -34,16 +33,21 @@ func TestKeeperTestSuite(t *testing.T) { } func (suite *KeeperTestSuite) SetupTest() { - app, err := simtestutil.Setup(testutil.AppConfig, - &suite.feegrantKeeper, - &suite.bankKeeper, - &suite.stakingKeeper, - &suite.interfaceRegistry, - ) - suite.Require().NoError(err) + suite.addrs = simtestutil.CreateIncrementalAccounts(4) + key := sdk.NewKVStoreKey(feegrant.StoreKey) + testCtx := testutil.DefaultContextWithDB(suite.T(), key, sdk.NewTransientStoreKey("transient_test")) + encCfg := moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{}) - suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: 1}) - suite.addrs = simtestutil.AddTestAddrsIncremental(suite.bankKeeper, suite.stakingKeeper, suite.ctx, 4, sdk.NewInt(30000000)) + // setup gomock and initialize some globally expected executions + ctrl := gomock.NewController(suite.T()) + suite.accountKeeper = feegranttestutil.NewMockAccountKeeper(ctrl) + suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), suite.addrs[0]).Return(authtypes.NewBaseAccountWithAddress(suite.addrs[0])).AnyTimes() + suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), suite.addrs[1]).Return(authtypes.NewBaseAccountWithAddress(suite.addrs[1])).AnyTimes() + suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), suite.addrs[2]).Return(authtypes.NewBaseAccountWithAddress(suite.addrs[2])).AnyTimes() + suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), suite.addrs[3]).Return(authtypes.NewBaseAccountWithAddress(suite.addrs[3])).AnyTimes() + + suite.feegrantKeeper = keeper.NewKeeper(encCfg.Codec, key, suite.accountKeeper) + suite.ctx = testCtx.Ctx suite.msgSrvr = keeper.NewMsgServerImpl(suite.feegrantKeeper) suite.atom = sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(555))) } @@ -146,6 +150,7 @@ func (suite *KeeperTestSuite) TestKeeperCrud() { } accAddr, err := sdk.AccAddressFromBech32("cosmos1rxr4mq58w3gtnx5tsc438mwjjafv3mja7k5pnu") 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) diff --git a/x/feegrant/module/abci_test.go b/x/feegrant/module/abci_test.go index db094a30bd..30a0a4cc39 100644 --- a/x/feegrant/module/abci_test.go +++ b/x/feegrant/module/abci_test.go @@ -4,51 +4,44 @@ import ( "testing" "github.com/cosmos/cosmos-sdk/baseapp" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/testutil" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/feegrant" "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" "github.com/cosmos/cosmos-sdk/x/feegrant/module" - "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" - stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + feegranttestutil "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" ) func TestFeegrantPruning(t *testing.T) { - var ( - interfaceRegistry codectypes.InterfaceRegistry - bankKeeper bankkeeper.Keeper - stakingKeeper *stakingkeeper.Keeper - feegrantKeeper keeper.Keeper - ) + key := sdk.NewKVStoreKey(feegrant.StoreKey) + testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test")) + encCfg := moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{}) - app, err := simtestutil.Setup(testutil.AppConfig, - &feegrantKeeper, - &bankKeeper, - &stakingKeeper, - &interfaceRegistry, - ) - require.NoError(t, err) - - ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - addrs := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 4, sdk.NewInt(1000)) + addrs := simtestutil.CreateIncrementalAccounts(4) granter1 := addrs[0] granter2 := addrs[1] granter3 := addrs[2] grantee := addrs[3] spendLimit := sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000))) - now := ctx.BlockTime() + now := testCtx.Ctx.BlockTime() oneDay := now.AddDate(0, 0, 1) - header := tmproto.Header{Height: app.LastBlockHeight() + 1} - app.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctrl := gomock.NewController(t) + accountKeeper := feegranttestutil.NewMockAccountKeeper(ctrl) + accountKeeper.EXPECT().GetAccount(gomock.Any(), grantee).Return(authtypes.NewBaseAccountWithAddress(grantee)).AnyTimes() + accountKeeper.EXPECT().GetAccount(gomock.Any(), granter1).Return(authtypes.NewBaseAccountWithAddress(granter1)).AnyTimes() + accountKeeper.EXPECT().GetAccount(gomock.Any(), granter2).Return(authtypes.NewBaseAccountWithAddress(granter2)).AnyTimes() + accountKeeper.EXPECT().GetAccount(gomock.Any(), granter3).Return(authtypes.NewBaseAccountWithAddress(granter3)).AnyTimes() + + feegrantKeeper := keeper.NewKeeper(encCfg.Codec, key, accountKeeper) feegrantKeeper.GrantAllowance( - ctx, + testCtx.Ctx, granter1, grantee, &feegrant.BasicAllowance{ @@ -56,7 +49,7 @@ func TestFeegrantPruning(t *testing.T) { }, ) feegrantKeeper.GrantAllowance( - ctx, + testCtx.Ctx, granter2, grantee, &feegrant.BasicAllowance{ @@ -64,7 +57,7 @@ func TestFeegrantPruning(t *testing.T) { }, ) feegrantKeeper.GrantAllowance( - ctx, + testCtx.Ctx, granter3, grantee, &feegrant.BasicAllowance{ @@ -72,23 +65,23 @@ func TestFeegrantPruning(t *testing.T) { }, ) - queryHelper := baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry) + queryHelper := baseapp.NewQueryServerTestHelper(testCtx.Ctx, encCfg.InterfaceRegistry) feegrant.RegisterQueryServer(queryHelper, feegrantKeeper) queryClient := feegrant.NewQueryClient(queryHelper) - module.EndBlocker(ctx, feegrantKeeper) + module.EndBlocker(testCtx.Ctx, feegrantKeeper) - res, err := queryClient.Allowances(ctx.Context(), &feegrant.QueryAllowancesRequest{ + res, err := queryClient.Allowances(testCtx.Ctx.Context(), &feegrant.QueryAllowancesRequest{ Grantee: grantee.String(), }) require.NoError(t, err) require.NotNil(t, res) require.Len(t, res.Allowances, 3) - ctx = ctx.WithBlockTime(now.AddDate(0, 0, 2)) - module.EndBlocker(ctx, feegrantKeeper) + testCtx.Ctx = testCtx.Ctx.WithBlockTime(now.AddDate(0, 0, 2)) + module.EndBlocker(testCtx.Ctx, feegrantKeeper) - res, err = queryClient.Allowances(ctx.Context(), &feegrant.QueryAllowancesRequest{ + res, err = queryClient.Allowances(testCtx.Ctx.Context(), &feegrant.QueryAllowancesRequest{ Grantee: grantee.String(), }) require.NoError(t, err) diff --git a/x/feegrant/periodic_fee_test.go b/x/feegrant/periodic_fee_test.go index d8b6052f02..1ac45b312a 100644 --- a/x/feegrant/periodic_fee_test.go +++ b/x/feegrant/periodic_fee_test.go @@ -8,36 +8,16 @@ import ( "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" "github.com/cosmos/cosmos-sdk/x/feegrant" - "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" - "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" - stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" ) func TestPeriodicFeeValidAllow(t *testing.T) { - var ( - interfaceRegistry codectypes.InterfaceRegistry - bankKeeper bankkeeper.Keeper - stakingKeeper *stakingkeeper.Keeper - feegrantKeeper keeper.Keeper - cdc codec.Codec - ) + key := sdk.NewKVStoreKey(feegrant.StoreKey) + testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test")) - app, err := simtestutil.Setup(testutil.AppConfig, - &feegrantKeeper, - &bankKeeper, - &stakingKeeper, - &interfaceRegistry, - &cdc, - ) - require.NoError(t, err) - - ctx := app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()}) + ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: time.Now()}) atom := sdk.NewCoins(sdk.NewInt64Coin("atom", 555)) smallAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 43)) @@ -212,7 +192,7 @@ func TestPeriodicFeeValidAllow(t *testing.T) { } require.NoError(t, err) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}).WithBlockTime(tc.blockTime) + ctx := testCtx.Ctx.WithBlockTime(tc.blockTime) // now try to deduct remove, err := tc.allow.Accept(ctx, tc.fee, []sdk.Msg{}) if !tc.accept { diff --git a/x/feegrant/simulation/decoder_test.go b/x/feegrant/simulation/decoder_test.go index 4652dd1146..055b99db86 100644 --- a/x/feegrant/simulation/decoder_test.go +++ b/x/feegrant/simulation/decoder_test.go @@ -19,7 +19,6 @@ import ( var ( granterPk = ed25519.GenPrivKey().PubKey() granterAddr = sdk.AccAddress(granterPk.Address()) - granteePk = ed25519.GenPrivKey().PubKey() granteeAddr = sdk.AccAddress(granterPk.Address()) ) diff --git a/x/feegrant/simulation/genesis_test.go b/x/feegrant/simulation/genesis_test.go index 39f22909e9..f15d1dd4b9 100644 --- a/x/feegrant/simulation/genesis_test.go +++ b/x/feegrant/simulation/genesis_test.go @@ -8,34 +8,24 @@ import ( "github.com/stretchr/testify/require" sdkmath "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/codec" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" - "github.com/cosmos/cosmos-sdk/types/module" + moduletypes "github.com/cosmos/cosmos-sdk/types/module" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/feegrant" - "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" + "github.com/cosmos/cosmos-sdk/x/feegrant/module" "github.com/cosmos/cosmos-sdk/x/feegrant/simulation" - "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" ) func TestRandomizedGenState(t *testing.T) { - var feegrantKeeper keeper.Keeper - var cdc codec.Codec - - _, err := simtestutil.Setup(testutil.AppConfig, - &feegrantKeeper, - &cdc, - ) - require.NoError(t, err) - + encCfg := moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{}) s := rand.NewSource(1) r := rand.New(s) accounts := simtypes.RandomAccounts(r, 3) - simState := module.SimulationState{ + simState := moduletypes.SimulationState{ AppParams: make(simtypes.AppParams), - Cdc: cdc, + Cdc: encCfg.Codec, Rand: r, NumBonded: 3, Accounts: accounts, diff --git a/x/feegrant/simulation/operations_test.go b/x/feegrant/simulation/operations_test.go index a3843e803a..2ca0dd3d0c 100644 --- a/x/feegrant/simulation/operations_test.go +++ b/x/feegrant/simulation/operations_test.go @@ -18,7 +18,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" "github.com/cosmos/cosmos-sdk/x/feegrant/simulation" "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" - stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" "github.com/stretchr/testify/suite" abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -33,7 +32,6 @@ type SimTestSuite struct { interfaceRegistry codectypes.InterfaceRegistry accountKeeper authkeeper.AccountKeeper bankKeeper bankkeeper.Keeper - stakingKeeper *stakingkeeper.Keeper cdc codec.Codec legacyAmino *codec.LegacyAmino } @@ -43,7 +41,6 @@ func (suite *SimTestSuite) SetupTest() { suite.app, err = simtestutil.Setup(testutil.AppConfig, &suite.feegrantKeeper, &suite.bankKeeper, - &suite.stakingKeeper, &suite.accountKeeper, &suite.interfaceRegistry, &suite.cdc, @@ -56,7 +53,6 @@ func (suite *SimTestSuite) SetupTest() { func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { accounts := simtypes.RandomAccounts(r, n) - initAmt := sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction) initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) @@ -152,7 +148,7 @@ func (suite *SimTestSuite) TestSimulateMsgRevokeAllowance() { // begin a new block app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: suite.app.LastBlockHeight() + 1, AppHash: suite.app.LastCommitID().Hash}}) - feeAmt := suite.stakingKeeper.TokensFromConsensusPower(ctx, 200000) + feeAmt := sdk.TokensFromConsensusPower(200000, sdk.DefaultPowerReduction) feeCoins := sdk.NewCoins(sdk.NewCoin("foo", feeAmt)) granter, grantee := accounts[0], accounts[1] diff --git a/x/feegrant/testutil/expected_keepers_mocks.go b/x/feegrant/testutil/expected_keepers_mocks.go new file mode 100644 index 0000000000..c00c4b0d71 --- /dev/null +++ b/x/feegrant/testutil/expected_keepers_mocks.go @@ -0,0 +1,155 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: x/feegrant/expected_keepers.go + +// Package testutil is a generated GoMock package. +package testutil + +import ( + reflect "reflect" + + types "github.com/cosmos/cosmos-sdk/types" + types0 "github.com/cosmos/cosmos-sdk/x/auth/types" + gomock "github.com/golang/mock/gomock" +) + +// MockAccountKeeper is a mock of AccountKeeper interface. +type MockAccountKeeper struct { + ctrl *gomock.Controller + recorder *MockAccountKeeperMockRecorder +} + +// MockAccountKeeperMockRecorder is the mock recorder for MockAccountKeeper. +type MockAccountKeeperMockRecorder struct { + mock *MockAccountKeeper +} + +// NewMockAccountKeeper creates a new mock instance. +func NewMockAccountKeeper(ctrl *gomock.Controller) *MockAccountKeeper { + mock := &MockAccountKeeper{ctrl: ctrl} + mock.recorder = &MockAccountKeeperMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockAccountKeeper) EXPECT() *MockAccountKeeperMockRecorder { + return m.recorder +} + +// GetAccount mocks base method. +func (m *MockAccountKeeper) GetAccount(ctx types.Context, addr types.AccAddress) types0.AccountI { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetAccount", ctx, addr) + ret0, _ := ret[0].(types0.AccountI) + return ret0 +} + +// GetAccount indicates an expected call of GetAccount. +func (mr *MockAccountKeeperMockRecorder) GetAccount(ctx, addr interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAccount", reflect.TypeOf((*MockAccountKeeper)(nil).GetAccount), ctx, addr) +} + +// GetModuleAccount mocks base method. +func (m *MockAccountKeeper) GetModuleAccount(ctx types.Context, moduleName string) types0.ModuleAccountI { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetModuleAccount", ctx, moduleName) + ret0, _ := ret[0].(types0.ModuleAccountI) + return ret0 +} + +// GetModuleAccount indicates an expected call of GetModuleAccount. +func (mr *MockAccountKeeperMockRecorder) GetModuleAccount(ctx, moduleName interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAccount", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAccount), ctx, moduleName) +} + +// GetModuleAddress mocks base method. +func (m *MockAccountKeeper) GetModuleAddress(moduleName string) types.AccAddress { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetModuleAddress", moduleName) + ret0, _ := ret[0].(types.AccAddress) + return ret0 +} + +// GetModuleAddress indicates an expected call of GetModuleAddress. +func (mr *MockAccountKeeperMockRecorder) GetModuleAddress(moduleName interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAddress", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAddress), moduleName) +} + +// NewAccountWithAddress mocks base method. +func (m *MockAccountKeeper) NewAccountWithAddress(ctx types.Context, addr types.AccAddress) types0.AccountI { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NewAccountWithAddress", ctx, addr) + ret0, _ := ret[0].(types0.AccountI) + return ret0 +} + +// NewAccountWithAddress indicates an expected call of NewAccountWithAddress. +func (mr *MockAccountKeeperMockRecorder) NewAccountWithAddress(ctx, addr interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewAccountWithAddress", reflect.TypeOf((*MockAccountKeeper)(nil).NewAccountWithAddress), ctx, addr) +} + +// SetAccount mocks base method. +func (m *MockAccountKeeper) SetAccount(ctx types.Context, acc types0.AccountI) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetAccount", ctx, acc) +} + +// SetAccount indicates an expected call of SetAccount. +func (mr *MockAccountKeeperMockRecorder) SetAccount(ctx, acc interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetAccount", reflect.TypeOf((*MockAccountKeeper)(nil).SetAccount), ctx, acc) +} + +// MockBankKeeper is a mock of BankKeeper interface. +type MockBankKeeper struct { + ctrl *gomock.Controller + recorder *MockBankKeeperMockRecorder +} + +// MockBankKeeperMockRecorder is the mock recorder for MockBankKeeper. +type MockBankKeeperMockRecorder struct { + mock *MockBankKeeper +} + +// NewMockBankKeeper creates a new mock instance. +func NewMockBankKeeper(ctrl *gomock.Controller) *MockBankKeeper { + mock := &MockBankKeeper{ctrl: ctrl} + mock.recorder = &MockBankKeeperMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockBankKeeper) EXPECT() *MockBankKeeperMockRecorder { + return m.recorder +} + +// SendCoinsFromAccountToModule mocks base method. +func (m *MockBankKeeper) SendCoinsFromAccountToModule(ctx types.Context, senderAddr types.AccAddress, recipientModule string, amt types.Coins) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SendCoinsFromAccountToModule", ctx, senderAddr, recipientModule, amt) + ret0, _ := ret[0].(error) + return ret0 +} + +// SendCoinsFromAccountToModule indicates an expected call of SendCoinsFromAccountToModule. +func (mr *MockBankKeeperMockRecorder) SendCoinsFromAccountToModule(ctx, senderAddr, recipientModule, amt interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendCoinsFromAccountToModule", reflect.TypeOf((*MockBankKeeper)(nil).SendCoinsFromAccountToModule), ctx, senderAddr, recipientModule, amt) +} + +// SpendableCoins mocks base method. +func (m *MockBankKeeper) SpendableCoins(ctx types.Context, addr types.AccAddress) types.Coins { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SpendableCoins", ctx, addr) + ret0, _ := ret[0].(types.Coins) + return ret0 +} + +// SpendableCoins indicates an expected call of SpendableCoins. +func (mr *MockBankKeeperMockRecorder) SpendableCoins(ctx, addr interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SpendableCoins", reflect.TypeOf((*MockBankKeeper)(nil).SpendableCoins), ctx, addr) +}