From 19c2eaff04cee1cb83294510b2ef77b5ee91331a Mon Sep 17 00:00:00 2001 From: Facundo Medica Date: Mon, 27 Jun 2022 13:11:49 -0300 Subject: [PATCH 1/5] progress --- x/feegrant/basic_fee_test.go | 23 +++- x/feegrant/filtered_fee_test.go | 24 ++++- x/feegrant/grant_test.go | 30 ++++-- x/feegrant/keeper/genesis_test.go | 38 +++++-- x/feegrant/keeper/grpc_query_test.go | 10 +- x/feegrant/keeper/keeper_test.go | 128 ++++++++++++----------- x/feegrant/keeper/msg_server_test.go | 4 +- x/feegrant/module/abci_test.go | 36 +++++-- x/feegrant/module/module.go | 2 +- x/feegrant/periodic_fee_test.go | 28 ++++- x/feegrant/simulation/genesis_test.go | 16 ++- x/feegrant/simulation/operations.go | 22 ++-- x/feegrant/simulation/operations_test.go | 82 ++++++++++----- 13 files changed, 297 insertions(+), 146 deletions(-) diff --git a/x/feegrant/basic_fee_test.go b/x/feegrant/basic_fee_test.go index 71d278fa404a..04d562a12665 100644 --- a/x/feegrant/basic_fee_test.go +++ b/x/feegrant/basic_fee_test.go @@ -8,15 +8,32 @@ import ( "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/simapp" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" 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) { - app := simapp.Setup(t, false) + var interfaceRegistry codectypes.InterfaceRegistry + var bankKeeper bankkeeper.Keeper + var stakingKeeper *stakingkeeper.Keeper + var feegrantKeeper keeper.Keeper + + app, err := simtestutil.Setup(testutil.AppConfig, + &feegrantKeeper, + &bankKeeper, + &stakingKeeper, + &interfaceRegistry, + ) + require.NoError(t, err) + + ctx := app.BaseApp.NewContext(false, tmproto.Header{Height: 1}) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}) badTime := ctx.BlockTime().AddDate(0, 0, -1) allowace := &feegrant.BasicAllowance{ Expiration: &badTime, diff --git a/x/feegrant/filtered_fee_test.go b/x/feegrant/filtered_fee_test.go index 6928bb562ddc..8fa95d5d9e5c 100644 --- a/x/feegrant/filtered_fee_test.go +++ b/x/feegrant/filtered_fee_test.go @@ -8,18 +8,34 @@ import ( "github.com/stretchr/testify/require" ocproto "github.com/tendermint/tendermint/proto/tendermint/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/simapp" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" 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" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" ) func TestFilteredFeeValidAllow(t *testing.T) { - app := simapp.Setup(t, false) + var interfaceRegistry codectypes.InterfaceRegistry + var bankKeeper bankkeeper.Keeper + var stakingKeeper *stakingkeeper.Keeper + var feegrantKeeper keeper.Keeper + + 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 := app.BaseApp.NewContext(false, ocproto.Header{ - Time: time.Now(), - }) eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 10)) atom := sdk.NewCoins(sdk.NewInt64Coin("atom", 555)) smallAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 43)) diff --git a/x/feegrant/grant_test.go b/x/feegrant/grant_test.go index f8b56c891768..7a29904dbf19 100644 --- a/x/feegrant/grant_test.go +++ b/x/feegrant/grant_test.go @@ -7,26 +7,44 @@ import ( "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" 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 TestGrant(t *testing.T) { - app := simapp.Setup(t, false) + var interfaceRegistry codectypes.InterfaceRegistry + var bankKeeper bankkeeper.Keeper + var stakingKeeper *stakingkeeper.Keeper + var feegrantKeeper keeper.Keeper + var cdc codec.Codec + + 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()}) + addr, err := sdk.AccAddressFromBech32("cosmos1qk93t4j0yyzgqgt6k5qf8deh8fq6smpn3ntu3x") require.NoError(t, err) addr2, err := sdk.AccAddressFromBech32("cosmos1p9qh4ldfd6n0qehujsal4k7g0e37kel90rc4ts") require.NoError(t, err) atom := sdk.NewCoins(sdk.NewInt64Coin("atom", 555)) - ctx := app.BaseApp.NewContext(false, tmproto.Header{ - Time: time.Now(), - }) now := ctx.BlockTime() oneYear := now.AddDate(1, 0, 0) zeroAtoms := sdk.NewCoins(sdk.NewInt64Coin("atom", 0)) - cdc := app.AppCodec() cases := map[string]struct { granter sdk.AccAddress diff --git a/x/feegrant/keeper/genesis_test.go b/x/feegrant/keeper/genesis_test.go index 0c80780da57c..94c8062201be 100644 --- a/x/feegrant/keeper/genesis_test.go +++ b/x/feegrant/keeper/genesis_test.go @@ -6,26 +6,42 @@ import ( "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" - "github.com/cosmos/cosmos-sdk/simapp" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" "github.com/cosmos/cosmos-sdk/testutil/testdata" 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" ) type GenesisTestSuite struct { suite.Suite - ctx sdk.Context - keeper keeper.Keeper + ctx sdk.Context + feegrantKeeper keeper.Keeper } func (suite *GenesisTestSuite) SetupTest() { checkTx := false - app := simapp.Setup(suite.T(), checkTx) + + var interfaceRegistry codectypes.InterfaceRegistry + var bankKeeper bankkeeper.Keeper + var stakingKeeper *stakingkeeper.Keeper + var cdc codec.Codec + + 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.keeper = app.FeeGrantKeeper } var ( @@ -39,13 +55,13 @@ func (suite *GenesisTestSuite) TestImportExportGenesis() { coins := sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(1_000))) now := suite.ctx.BlockHeader().Time oneYear := now.AddDate(1, 0, 0) - msgSrvr := keeper.NewMsgServerImpl(suite.keeper) + msgSrvr := keeper.NewMsgServerImpl(suite.feegrantKeeper) allowance := &feegrant.BasicAllowance{SpendLimit: coins, Expiration: &oneYear} - err := suite.keeper.GrantAllowance(suite.ctx, granterAddr, granteeAddr, allowance) + err := suite.feegrantKeeper.GrantAllowance(suite.ctx, granterAddr, granteeAddr, allowance) suite.Require().NoError(err) - genesis, err := suite.keeper.ExportGenesis(suite.ctx) + genesis, err := suite.feegrantKeeper.ExportGenesis(suite.ctx) suite.Require().NoError(err) // revoke fee allowance _, err = msgSrvr.RevokeAllowance(sdk.WrapSDKContext(suite.ctx), &feegrant.MsgRevokeAllowance{ @@ -53,10 +69,10 @@ func (suite *GenesisTestSuite) TestImportExportGenesis() { Grantee: granteeAddr.String(), }) suite.Require().NoError(err) - err = suite.keeper.InitGenesis(suite.ctx, genesis) + err = suite.feegrantKeeper.InitGenesis(suite.ctx, genesis) suite.Require().NoError(err) - newGenesis, err := suite.keeper.ExportGenesis(suite.ctx) + newGenesis, err := suite.feegrantKeeper.ExportGenesis(suite.ctx) suite.Require().NoError(err) suite.Require().Equal(genesis, newGenesis) } @@ -102,7 +118,7 @@ func (suite *GenesisTestSuite) TestInitGenesis() { for _, tc := range testCases { tc := tc suite.Run(tc.name, func() { - err := suite.keeper.InitGenesis(suite.ctx, &feegrant.GenesisState{Allowances: tc.feeAllowances}) + err := suite.feegrantKeeper.InitGenesis(suite.ctx, &feegrant.GenesisState{Allowances: tc.feeAllowances}) suite.Require().Error(err) }) } diff --git a/x/feegrant/keeper/grpc_query_test.go b/x/feegrant/keeper/grpc_query_test.go index fd46c5ec0653..35d51ffd2142 100644 --- a/x/feegrant/keeper/grpc_query_test.go +++ b/x/feegrant/keeper/grpc_query_test.go @@ -70,7 +70,7 @@ func (suite *KeeperTestSuite) TestFeeAllowance() { for _, tc := range testCases { suite.Run(tc.name, func() { tc.preRun() - resp, err := suite.keeper.Allowance(suite.ctx, tc.req) + resp, err := suite.feegrantKeeper.Allowance(suite.ctx, tc.req) if tc.expectErr { suite.Require().Error(err) } else { @@ -136,7 +136,7 @@ func (suite *KeeperTestSuite) TestFeeAllowances() { for _, tc := range testCases { suite.Run(tc.name, func() { tc.preRun() - resp, err := suite.keeper.Allowances(suite.ctx, tc.req) + resp, err := suite.feegrantKeeper.Allowances(suite.ctx, tc.req) if tc.expectErr { suite.Require().Error(err) } else { @@ -206,7 +206,7 @@ func (suite *KeeperTestSuite) TestFeeAllowancesByGranter() { for _, tc := range testCases { suite.Run(tc.name, func() { tc.preRun() - resp, err := suite.keeper.AllowancesByGranter(suite.ctx, tc.req) + resp, err := suite.feegrantKeeper.AllowancesByGranter(suite.ctx, tc.req) if tc.expectErr { suite.Require().Error(err) } else { @@ -218,8 +218,8 @@ func (suite *KeeperTestSuite) TestFeeAllowancesByGranter() { } func (suite *KeeperTestSuite) grantFeeAllowance(granter, grantee sdk.AccAddress) { - exp := suite.sdkCtx.BlockTime().AddDate(1, 0, 0) - err := suite.app.FeeGrantKeeper.GrantAllowance(suite.sdkCtx, granter, grantee, &feegrant.BasicAllowance{ + exp := suite.ctx.BlockTime().AddDate(1, 0, 0) + err := suite.feegrantKeeper.GrantAllowance(suite.ctx, granter, grantee, &feegrant.BasicAllowance{ SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 555)), Expiration: &exp, }) diff --git a/x/feegrant/keeper/keeper_test.go b/x/feegrant/keeper/keeper_test.go index 4f5500f976d4..b1f4f478e710 100644 --- a/x/feegrant/keeper/keeper_test.go +++ b/x/feegrant/keeper/keeper_test.go @@ -1,28 +1,32 @@ package keeper_test import ( - "context" "testing" "github.com/stretchr/testify/suite" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/simapp" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" 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" ) type KeeperTestSuite struct { suite.Suite - app *simapp.SimApp - sdkCtx sdk.Context - addrs []sdk.AccAddress - msgSrvr feegrant.MsgServer - ctx context.Context - atom sdk.Coins - keeper keeper.Keeper + ctx sdk.Context + addrs []sdk.AccAddress + msgSrvr feegrant.MsgServer + atom sdk.Coins + feegrantKeeper keeper.Keeper + interfaceRegistry codectypes.InterfaceRegistry + bankKeeper bankkeeper.Keeper + stakingKeeper *stakingkeeper.Keeper } func TestKeeperTestSuite(t *testing.T) { @@ -30,23 +34,25 @@ func TestKeeperTestSuite(t *testing.T) { } func (suite *KeeperTestSuite) SetupTest() { - app := simapp.Setup(suite.T(), false) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - - suite.app = app - suite.sdkCtx = ctx - suite.addrs = simapp.AddTestAddrsIncremental(app, ctx, 4, sdk.NewInt(30000000)) - suite.ctx = sdk.WrapSDKContext(ctx) - suite.keeper = suite.app.FeeGrantKeeper - suite.msgSrvr = keeper.NewMsgServerImpl(suite.keeper) + app, err := simtestutil.Setup(testutil.AppConfig, + &suite.feegrantKeeper, + &suite.bankKeeper, + &suite.stakingKeeper, + &suite.interfaceRegistry, + ) + suite.Require().NoError(err) + + suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: 1}) + suite.addrs = simtestutil.AddTestAddrsIncremental(suite.bankKeeper, suite.stakingKeeper, suite.ctx, 4, sdk.NewInt(30000000)) + suite.msgSrvr = keeper.NewMsgServerImpl(suite.feegrantKeeper) suite.atom = sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(555))) } func (suite *KeeperTestSuite) TestKeeperCrud() { // some helpers eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 123)) - exp := suite.sdkCtx.BlockTime().AddDate(1, 0, 0) - exp2 := suite.sdkCtx.BlockTime().AddDate(2, 0, 0) + exp := suite.ctx.BlockTime().AddDate(1, 0, 0) + exp2 := suite.ctx.BlockTime().AddDate(2, 0, 0) basic := &feegrant.BasicAllowance{ SpendLimit: suite.atom, Expiration: &exp, @@ -63,19 +69,19 @@ func (suite *KeeperTestSuite) TestKeeperCrud() { } // let's set up some initial state here - err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[0], suite.addrs[1], basic) + err := suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[0], suite.addrs[1], basic) suite.Require().NoError(err) - err = suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[0], suite.addrs[2], basic2) + err = suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[0], suite.addrs[2], basic2) suite.Require().NoError(err) - err = suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[1], suite.addrs[2], basic) + err = suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[1], suite.addrs[2], basic) suite.Require().NoError(err) - err = suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[1], suite.addrs[3], basic) + err = suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[1], suite.addrs[3], basic) suite.Require().NoError(err) - err = suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[3], suite.addrs[0], basic2) + err = suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[3], suite.addrs[0], basic2) suite.Require().NoError(err) // remove some, overwrite other @@ -88,10 +94,10 @@ func (suite *KeeperTestSuite) TestKeeperCrud() { _, err = suite.msgSrvr.RevokeAllowance(suite.ctx, &feegrant.MsgRevokeAllowance{Granter: suite.addrs[0].String(), Grantee: suite.addrs[2].String()}) suite.Require().Error(err) - err = suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[0], suite.addrs[2], basic) + err = suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[0], suite.addrs[2], basic) suite.Require().NoError(err) - err = suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[1], suite.addrs[2], basic3) + err = suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[1], suite.addrs[2], basic3) suite.Require().NoError(err) // end state: @@ -128,7 +134,7 @@ func (suite *KeeperTestSuite) TestKeeperCrud() { for name, tc := range cases { tc := tc suite.Run(name, func() { - allow, _ := suite.keeper.GetAllowance(suite.sdkCtx, tc.granter, tc.grantee) + allow, _ := suite.feegrantKeeper.GetAllowance(suite.ctx, tc.granter, tc.grantee) if tc.allowance == nil { suite.Nil(allow) @@ -142,10 +148,10 @@ func (suite *KeeperTestSuite) TestKeeperCrud() { suite.Require().NoError(err) // let's grant and revoke authorization to non existing account - err = suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[3], accAddr, basic2) + err = suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[3], accAddr, basic2) suite.Require().NoError(err) - _, err = suite.keeper.GetAllowance(suite.sdkCtx, suite.addrs[3], accAddr) + _, err = suite.feegrantKeeper.GetAllowance(suite.ctx, suite.addrs[3], accAddr) suite.Require().NoError(err) _, err = suite.msgSrvr.RevokeAllowance(suite.ctx, &feegrant.MsgRevokeAllowance{Granter: suite.addrs[3].String(), Grantee: accAddr.String()}) @@ -154,7 +160,7 @@ func (suite *KeeperTestSuite) TestKeeperCrud() { func (suite *KeeperTestSuite) TestUseGrantedFee() { eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 123)) - blockTime := suite.sdkCtx.BlockTime() + blockTime := suite.ctx.BlockTime() oneYear := blockTime.AddDate(1, 0, 0) future := &feegrant.BasicAllowance{ @@ -204,17 +210,17 @@ func (suite *KeeperTestSuite) TestUseGrantedFee() { for name, tc := range cases { tc := tc suite.Run(name, func() { - err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[0], suite.addrs[1], future) + err := suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[0], suite.addrs[1], future) suite.Require().NoError(err) - err = suite.keeper.UseGrantedFees(suite.sdkCtx, tc.granter, tc.grantee, tc.fee, []sdk.Msg{}) + err = suite.feegrantKeeper.UseGrantedFees(suite.ctx, tc.granter, tc.grantee, tc.fee, []sdk.Msg{}) if tc.allowed { suite.NoError(err) } else { suite.Error(err) } - loaded, _ := suite.keeper.GetAllowance(suite.sdkCtx, tc.granter, tc.grantee) + loaded, _ := suite.feegrantKeeper.GetAllowance(suite.ctx, tc.granter, tc.grantee) suite.Equal(tc.final, loaded) }) } @@ -225,26 +231,26 @@ func (suite *KeeperTestSuite) TestUseGrantedFee() { } // create basic fee allowance - err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[0], suite.addrs[2], basicAllowance) + err := suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[0], suite.addrs[2], basicAllowance) suite.Require().NoError(err) // waiting for future blocks, allowance to be pruned. - ctx := suite.sdkCtx.WithBlockTime(oneYear) + ctx := suite.ctx.WithBlockTime(oneYear) // expect error: feegrant expired - err = suite.keeper.UseGrantedFees(ctx, suite.addrs[0], suite.addrs[2], eth, []sdk.Msg{}) + err = suite.feegrantKeeper.UseGrantedFees(ctx, suite.addrs[0], suite.addrs[2], eth, []sdk.Msg{}) suite.Error(err) suite.Contains(err.Error(), "fee allowance expired") // verify: feegrant is revoked - _, err = suite.keeper.GetAllowance(ctx, suite.addrs[0], suite.addrs[2]) + _, err = suite.feegrantKeeper.GetAllowance(ctx, suite.addrs[0], suite.addrs[2]) suite.Error(err) suite.Contains(err.Error(), "fee-grant not found") } func (suite *KeeperTestSuite) TestIterateGrants() { eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 123)) - exp := suite.sdkCtx.BlockTime().AddDate(1, 0, 0) + exp := suite.ctx.BlockTime().AddDate(1, 0, 0) allowance := &feegrant.BasicAllowance{ SpendLimit: suite.atom, @@ -256,10 +262,10 @@ func (suite *KeeperTestSuite) TestIterateGrants() { Expiration: &exp, } - suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[0], suite.addrs[1], allowance) - suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance1) + suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[0], suite.addrs[1], allowance) + suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[2], suite.addrs[1], allowance1) - suite.keeper.IterateAllFeeAllowances(suite.sdkCtx, func(grant feegrant.Grant) bool { + suite.feegrantKeeper.IterateAllFeeAllowances(suite.ctx, func(grant feegrant.Grant) bool { suite.Require().Equal(suite.addrs[1].String(), grant.Grantee) suite.Require().Contains([]string{suite.addrs[0].String(), suite.addrs[2].String()}, grant.Granter) return true @@ -268,7 +274,7 @@ func (suite *KeeperTestSuite) TestIterateGrants() { func (suite *KeeperTestSuite) TestPruneGrants() { eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 123)) - now := suite.sdkCtx.BlockTime() + now := suite.ctx.BlockTime() oneYearExpiry := now.AddDate(1, 0, 0) oneDay := now.AddDate(0, 0, 1) @@ -284,7 +290,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { }{ { name: "grant not pruned from state", - ctx: suite.sdkCtx, + ctx: suite.ctx, granter: suite.addrs[0], grantee: suite.addrs[1], allowance: &feegrant.BasicAllowance{ @@ -294,7 +300,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { }, { name: "grant pruned from state after a block: error", - ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 1)), + ctx: suite.ctx.WithBlockTime(now.AddDate(0, 0, 1)), granter: suite.addrs[2], grantee: suite.addrs[1], expErrMsg: "not found", @@ -305,7 +311,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { }, { name: "grant not pruned from state after a day: no error", - ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 1)), + ctx: suite.ctx.WithBlockTime(now.AddDate(0, 0, 1)), granter: suite.addrs[1], grantee: suite.addrs[0], allowance: &feegrant.BasicAllowance{ @@ -315,7 +321,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { }, { name: "grant pruned from state after a year: error", - ctx: suite.sdkCtx.WithBlockTime(now.AddDate(1, 0, 1)), + ctx: suite.ctx.WithBlockTime(now.AddDate(1, 0, 1)), granter: suite.addrs[1], grantee: suite.addrs[2], expErrMsg: "not found", @@ -326,7 +332,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { }, { name: "no expiry: no error", - ctx: suite.sdkCtx.WithBlockTime(now.AddDate(1, 0, 0)), + ctx: suite.ctx.WithBlockTime(now.AddDate(1, 0, 0)), granter: suite.addrs[1], grantee: suite.addrs[2], allowance: &feegrant.BasicAllowance{ @@ -336,7 +342,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { }, { name: "grant created with a day expiry & overwritten with no expiry shouldn't be pruned: no error", - ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 2)), + ctx: suite.ctx.WithBlockTime(now.AddDate(0, 0, 2)), granter: suite.addrs[2], grantee: suite.addrs[1], allowance: &feegrant.BasicAllowance{ @@ -348,11 +354,11 @@ func (suite *KeeperTestSuite) TestPruneGrants() { SpendLimit: suite.atom, Expiration: &oneDay, } - err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) + err := suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[2], suite.addrs[1], allowance) suite.NoError(err) }, postRun: func() { - _, err := suite.msgSrvr.RevokeAllowance(suite.sdkCtx, &feegrant.MsgRevokeAllowance{ + _, err := suite.msgSrvr.RevokeAllowance(suite.ctx, &feegrant.MsgRevokeAllowance{ Granter: suite.addrs[2].String(), Grantee: suite.addrs[1].String(), }) @@ -361,7 +367,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { }, { name: "grant created with a day expiry & overwritten with a year expiry shouldn't be pruned: no error", - ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 2)), + ctx: suite.ctx.WithBlockTime(now.AddDate(0, 0, 2)), granter: suite.addrs[2], grantee: suite.addrs[1], allowance: &feegrant.BasicAllowance{ @@ -374,11 +380,11 @@ func (suite *KeeperTestSuite) TestPruneGrants() { SpendLimit: suite.atom, Expiration: &oneDay, } - err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) + err := suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[2], suite.addrs[1], allowance) suite.NoError(err) }, postRun: func() { - _, err := suite.msgSrvr.RevokeAllowance(suite.sdkCtx, &feegrant.MsgRevokeAllowance{ + _, err := suite.msgSrvr.RevokeAllowance(suite.ctx, &feegrant.MsgRevokeAllowance{ Granter: suite.addrs[2].String(), Grantee: suite.addrs[1].String(), }) @@ -387,7 +393,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { }, { name: "grant created with a year expiry & overwritten with a day expiry should be pruned after a day: error", - ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 2)), + ctx: suite.ctx.WithBlockTime(now.AddDate(0, 0, 2)), granter: suite.addrs[2], grantee: suite.addrs[1], allowance: &feegrant.BasicAllowance{ @@ -400,7 +406,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { SpendLimit: suite.atom, Expiration: &oneYearExpiry, } - err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) + err := suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[2], suite.addrs[1], allowance) suite.NoError(err) }, postRun: func() {}, @@ -408,7 +414,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { }, { name: "grant created with no expiry & overwritten with a day expiry should be pruned after a day: error", - ctx: suite.sdkCtx.WithBlockTime(now.AddDate(0, 0, 2)), + ctx: suite.ctx.WithBlockTime(now.AddDate(0, 0, 2)), granter: suite.addrs[2], grantee: suite.addrs[1], allowance: &feegrant.BasicAllowance{ @@ -420,7 +426,7 @@ func (suite *KeeperTestSuite) TestPruneGrants() { allowance := &feegrant.BasicAllowance{ SpendLimit: suite.atom, } - err := suite.keeper.GrantAllowance(suite.sdkCtx, suite.addrs[2], suite.addrs[1], allowance) + err := suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[2], suite.addrs[1], allowance) suite.NoError(err) }, postRun: func() {}, @@ -434,10 +440,10 @@ func (suite *KeeperTestSuite) TestPruneGrants() { if tc.preRun != nil { tc.preRun() } - err := suite.keeper.GrantAllowance(suite.sdkCtx, tc.granter, tc.grantee, tc.allowance) + err := suite.feegrantKeeper.GrantAllowance(suite.ctx, tc.granter, tc.grantee, tc.allowance) suite.NoError(err) - suite.app.FeeGrantKeeper.RemoveExpiredAllowances(tc.ctx) - grant, err := suite.keeper.GetAllowance(tc.ctx, tc.granter, tc.grantee) + suite.feegrantKeeper.RemoveExpiredAllowances(tc.ctx) + grant, err := suite.feegrantKeeper.GetAllowance(tc.ctx, tc.granter, tc.grantee) if tc.expErrMsg != "" { suite.Error(err) suite.Contains(err.Error(), tc.expErrMsg) diff --git a/x/feegrant/keeper/msg_server_test.go b/x/feegrant/keeper/msg_server_test.go index 4fa3193e3af9..c7bd3da3d7e6 100644 --- a/x/feegrant/keeper/msg_server_test.go +++ b/x/feegrant/keeper/msg_server_test.go @@ -6,7 +6,7 @@ import ( ) func (suite *KeeperTestSuite) TestGrantAllowance() { - oneYear := suite.sdkCtx.BlockTime().AddDate(1, 0, 0) + oneYear := suite.ctx.BlockTime().AddDate(1, 0, 0) testCases := []struct { name string @@ -127,7 +127,7 @@ func (suite *KeeperTestSuite) TestGrantAllowance() { } func (suite *KeeperTestSuite) TestRevokeAllowance() { - oneYear := suite.sdkCtx.BlockTime().AddDate(1, 0, 0) + oneYear := suite.ctx.BlockTime().AddDate(1, 0, 0) testCases := []struct { name string diff --git a/x/feegrant/module/abci_test.go b/x/feegrant/module/abci_test.go index 4c8aa03d410e..21a4b42c0045 100644 --- a/x/feegrant/module/abci_test.go +++ b/x/feegrant/module/abci_test.go @@ -4,20 +4,36 @@ import ( "testing" "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/simapp" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" 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/module" + "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" "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) { - app := simapp.Setup(t, false) + var interfaceRegistry codectypes.InterfaceRegistry + var bankKeeper bankkeeper.Keeper + var stakingKeeper *stakingkeeper.Keeper + var feegrantKeeper keeper.Keeper + + app, err := simtestutil.Setup(testutil.AppConfig, + &feegrantKeeper, + &bankKeeper, + &stakingKeeper, + &interfaceRegistry, + ) + require.NoError(t, err) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - addrs := simapp.AddTestAddrs(app, ctx, 4, sdk.NewInt(1000)) + addrs := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 4, sdk.NewInt(1000)) granter1 := addrs[0] granter2 := addrs[1] granter3 := addrs[2] @@ -29,7 +45,7 @@ func TestFeegrantPruning(t *testing.T) { header := tmproto.Header{Height: app.LastBlockHeight() + 1} app.BeginBlock(abci.RequestBeginBlock{Header: header}) - app.FeeGrantKeeper.GrantAllowance( + feegrantKeeper.GrantAllowance( ctx, granter1, grantee, @@ -37,7 +53,7 @@ func TestFeegrantPruning(t *testing.T) { Expiration: &now, }, ) - app.FeeGrantKeeper.GrantAllowance( + feegrantKeeper.GrantAllowance( ctx, granter2, grantee, @@ -45,7 +61,7 @@ func TestFeegrantPruning(t *testing.T) { SpendLimit: spendLimit, }, ) - app.FeeGrantKeeper.GrantAllowance( + feegrantKeeper.GrantAllowance( ctx, granter3, grantee, @@ -54,11 +70,11 @@ func TestFeegrantPruning(t *testing.T) { }, ) - queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry()) - feegrant.RegisterQueryServer(queryHelper, app.FeeGrantKeeper) + queryHelper := baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry) + feegrant.RegisterQueryServer(queryHelper, feegrantKeeper) queryClient := feegrant.NewQueryClient(queryHelper) - module.EndBlocker(ctx, app.FeeGrantKeeper) + module.EndBlocker(ctx, feegrantKeeper) res, err := queryClient.Allowances(ctx.Context(), &feegrant.QueryAllowancesRequest{ Grantee: grantee.String(), @@ -68,7 +84,7 @@ func TestFeegrantPruning(t *testing.T) { require.Len(t, res.Allowances, 3) ctx = ctx.WithBlockTime(now.AddDate(0, 0, 2)) - module.EndBlocker(ctx, app.FeeGrantKeeper) + module.EndBlocker(ctx, feegrantKeeper) res, err = queryClient.Allowances(ctx.Context(), &feegrant.QueryAllowancesRequest{ Grantee: grantee.String(), diff --git a/x/feegrant/module/module.go b/x/feegrant/module/module.go index 2788ed204b0c..d0fd9c0aef4e 100644 --- a/x/feegrant/module/module.go +++ b/x/feegrant/module/module.go @@ -246,6 +246,6 @@ func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { // WeightedOperations returns all the feegrant module operations with their respective weights. func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, am.accountKeeper, am.bankKeeper, am.keeper, + am.registry, simState.AppParams, simState.Cdc, am.accountKeeper, am.bankKeeper, am.keeper, ) } diff --git a/x/feegrant/periodic_fee_test.go b/x/feegrant/periodic_fee_test.go index 439ef19a0195..a9c76d67e719 100644 --- a/x/feegrant/periodic_fee_test.go +++ b/x/feegrant/periodic_fee_test.go @@ -8,16 +8,34 @@ import ( "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" 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) { - app := simapp.Setup(t, false) - ctx := app.BaseApp.NewContext(false, tmproto.Header{ - Time: time.Now(), - }) + var interfaceRegistry codectypes.InterfaceRegistry + var bankKeeper bankkeeper.Keeper + var stakingKeeper *stakingkeeper.Keeper + var feegrantKeeper keeper.Keeper + var cdc codec.Codec + + 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()}) atom := sdk.NewCoins(sdk.NewInt64Coin("atom", 555)) smallAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 43)) diff --git a/x/feegrant/simulation/genesis_test.go b/x/feegrant/simulation/genesis_test.go index 44669c04be98..39f22909e964 100644 --- a/x/feegrant/simulation/genesis_test.go +++ b/x/feegrant/simulation/genesis_test.go @@ -8,15 +8,25 @@ import ( "github.com/stretchr/testify/require" sdkmath "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/codec" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" "github.com/cosmos/cosmos-sdk/types/module" 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/simulation" + "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" ) func TestRandomizedGenState(t *testing.T) { - app := simapp.Setup(t, false) + var feegrantKeeper keeper.Keeper + var cdc codec.Codec + + _, err := simtestutil.Setup(testutil.AppConfig, + &feegrantKeeper, + &cdc, + ) + require.NoError(t, err) s := rand.NewSource(1) r := rand.New(s) @@ -25,7 +35,7 @@ func TestRandomizedGenState(t *testing.T) { simState := module.SimulationState{ AppParams: make(simtypes.AppParams), - Cdc: app.AppCodec(), + Cdc: cdc, Rand: r, NumBonded: 3, Accounts: accounts, diff --git a/x/feegrant/simulation/operations.go b/x/feegrant/simulation/operations.go index cd02e53013c8..6ab599abbcba 100644 --- a/x/feegrant/simulation/operations.go +++ b/x/feegrant/simulation/operations.go @@ -5,9 +5,11 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" simappparams "github.com/cosmos/cosmos-sdk/simapp/params" sdk "github.com/cosmos/cosmos-sdk/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/auth/tx" "github.com/cosmos/cosmos-sdk/x/feegrant" "github.com/cosmos/cosmos-sdk/x/feegrant/keeper" "github.com/cosmos/cosmos-sdk/x/simulation" @@ -26,8 +28,12 @@ var ( ) func WeightedOperations( - appParams simtypes.AppParams, cdc codec.JSONCodec, - ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper, + registry codectypes.InterfaceRegistry, + appParams simtypes.AppParams, + cdc codec.JSONCodec, + ak feegrant.AccountKeeper, + bk feegrant.BankKeeper, + k keeper.Keeper, ) simulation.WeightedOperations { var ( weightMsgGrantAllowance int @@ -49,17 +55,17 @@ func WeightedOperations( return simulation.WeightedOperations{ simulation.NewWeightedOperation( weightMsgGrantAllowance, - SimulateMsgGrantAllowance(ak, bk, k), + SimulateMsgGrantAllowance(codec.NewProtoCodec(registry), ak, bk, k), ), simulation.NewWeightedOperation( weightMsgRevokeAllowance, - SimulateMsgRevokeAllowance(ak, bk, k), + SimulateMsgRevokeAllowance(codec.NewProtoCodec(registry), ak, bk, k), ), } } // SimulateMsgGrantAllowance generates MsgGrantAllowance with random values. -func SimulateMsgGrantAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper) simtypes.Operation { +func SimulateMsgGrantAllowance(cdc *codec.ProtoCodec, ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -92,7 +98,7 @@ func SimulateMsgGrantAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper txCtx := simulation.OperationInput{ R: r, App: app, - TxGen: simappparams.MakeTestEncodingConfig().TxConfig, + TxGen: tx.NewTxConfig(cdc, tx.DefaultSignModes), Cdc: nil, Msg: msg, MsgType: TypeMsgGrantAllowance, @@ -109,7 +115,7 @@ func SimulateMsgGrantAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper } // SimulateMsgRevokeAllowance generates a MsgRevokeAllowance with random values. -func SimulateMsgRevokeAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper) simtypes.Operation { +func SimulateMsgRevokeAllowance(cdc *codec.ProtoCodec, ak feegrant.AccountKeeper, bk feegrant.BankKeeper, k keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { @@ -142,7 +148,7 @@ func SimulateMsgRevokeAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeepe txCtx := simulation.OperationInput{ R: r, App: app, - TxGen: simappparams.MakeTestEncodingConfig().TxConfig, + TxGen: tx.NewTxConfig(cdc, tx.DefaultSignModes), Cdc: nil, Msg: &msg, MsgType: TypeMsgRevokeAllowance, diff --git a/x/feegrant/simulation/operations_test.go b/x/feegrant/simulation/operations_test.go index 1c43eddb73f4..b21fc254a22f 100644 --- a/x/feegrant/simulation/operations_test.go +++ b/x/feegrant/simulation/operations_test.go @@ -5,33 +5,62 @@ import ( "testing" "time" - "github.com/stretchr/testify/suite" - abci "github.com/tendermint/tendermint/abci/types" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/runtime" simappparams "github.com/cosmos/cosmos-sdk/simapp/params" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" - "github.com/cosmos/cosmos-sdk/x/bank/testutil" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" "github.com/cosmos/cosmos-sdk/x/feegrant" + "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" ) type SimTestSuite struct { suite.Suite - ctx sdk.Context - app *simapp.SimApp + app *runtime.App + ctx sdk.Context + feegrantKeeper keeper.Keeper + interfaceRegistry codectypes.InterfaceRegistry + accountKeeper authkeeper.AccountKeeper + bankKeeper bankkeeper.Keeper + stakingKeeper *stakingkeeper.Keeper + cdc codec.Codec + legacyAmino *codec.LegacyAmino } func (suite *SimTestSuite) SetupTest() { - checkTx := false - app := simapp.Setup(suite.T(), checkTx) - suite.app = app - suite.ctx = app.BaseApp.NewContext(checkTx, tmproto.Header{ - Time: time.Now(), - }) + var err error + suite.app, err = simtestutil.Setup(testutil.AppConfig, + &suite.feegrantKeeper, + &suite.bankKeeper, + &suite.stakingKeeper, + &suite.accountKeeper, + &suite.interfaceRegistry, + &suite.cdc, + &suite.legacyAmino, + ) + suite.Require().NoError(err) + + suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()}) + // suite.addrs = simtestutil.AddTestAddrsIncremental(suite.bankKeeper, suite.stakingKeeper, suite.ctx, 4, sdk.NewInt(30000000)) + // suite.msgSrvr = keeper.NewMsgServerImpl(suite.feegrantKeeper) + // suite.atom = sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(555))) + + // suite.app = app + // suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{ + // Time: time.Now(), + // }) } func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { @@ -42,7 +71,7 @@ func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Ac // add coins to the accounts for _, account := range accounts { - err := testutil.FundAccount(suite.app.BankKeeper, suite.ctx, account.Address, initCoins) + err := banktestutil.FundAccount(suite.bankKeeper, suite.ctx, account.Address, initCoins) suite.Require().NoError(err) } @@ -50,17 +79,16 @@ func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Ac } func (suite *SimTestSuite) TestWeightedOperations() { - app, ctx := suite.app, suite.ctx require := suite.Require() - ctx.WithChainID("test-chain") + suite.ctx.WithChainID("test-chain") - cdc := app.AppCodec() appParams := make(simtypes.AppParams) weightedOps := simulation.WeightedOperations( - appParams, cdc, app.AccountKeeper, - app.BankKeeper, app.FeeGrantKeeper, + suite.interfaceRegistry, + appParams, suite.cdc, suite.accountKeeper, + suite.bankKeeper, suite.feegrantKeeper, ) s := rand.NewSource(1) @@ -85,7 +113,7 @@ func (suite *SimTestSuite) TestWeightedOperations() { } for i, w := range weightedOps { - operationMsg, _, err := w.Op()(r, app.BaseApp, ctx, accs, ctx.ChainID()) + operationMsg, _, err := w.Op()(r, suite.app.BaseApp, suite.ctx, accs, suite.ctx.ChainID()) require.NoError(err) // the following checks are very much dependent from the ordering of the output given @@ -109,12 +137,12 @@ func (suite *SimTestSuite) TestSimulateMsgGrantAllowance() { app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash}}) // execute operation - op := simulation.SimulateMsgGrantAllowance(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper) + op := simulation.SimulateMsgGrantAllowance(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.feegrantKeeper) operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "") require.NoError(err) var msg feegrant.MsgGrantAllowance - suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) + suite.legacyAmino.UnmarshalJSON(operationMsg.Msg, &msg) require.True(operationMsg.OK) require.Equal(accounts[2].Address.String(), msg.Granter) @@ -133,13 +161,13 @@ 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 := app.StakingKeeper.TokensFromConsensusPower(ctx, 200000) + feeAmt := suite.stakingKeeper.TokensFromConsensusPower(ctx, 200000) feeCoins := sdk.NewCoins(sdk.NewCoin("foo", feeAmt)) granter, grantee := accounts[0], accounts[1] oneYear := ctx.BlockTime().AddDate(1, 0, 0) - err := app.FeeGrantKeeper.GrantAllowance( + err := suite.feegrantKeeper.GrantAllowance( ctx, granter.Address, grantee.Address, @@ -151,12 +179,12 @@ func (suite *SimTestSuite) TestSimulateMsgRevokeAllowance() { require.NoError(err) // execute operation - op := simulation.SimulateMsgRevokeAllowance(app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper) + op := simulation.SimulateMsgRevokeAllowance(codec.NewProtoCodec(suite.interfaceRegistry), suite.accountKeeper, suite.bankKeeper, suite.feegrantKeeper) operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "") require.NoError(err) var msg feegrant.MsgRevokeAllowance - suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) + suite.legacyAmino.UnmarshalJSON(operationMsg.Msg, &msg) require.True(operationMsg.OK) require.Equal(granter.Address.String(), msg.Granter) From 9935bbbde4a0b9c42e74573cba817201d7eda896 Mon Sep 17 00:00:00 2001 From: Facundo Medica Date: Mon, 27 Jun 2022 13:28:07 -0300 Subject: [PATCH 2/5] simapp out --- simapp/params/weights.go | 4 ---- x/feegrant/filtered_fee_test.go | 7 +++++-- x/feegrant/migrations/v046/store_test.go | 9 ++++++--- x/feegrant/simulation/decoder_test.go | 7 +++++-- x/feegrant/simulation/operations.go | 11 ++++++----- x/feegrant/simulation/operations_test.go | 5 ++--- 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/simapp/params/weights.go b/simapp/params/weights.go index 0ae258cf5700..ec464d23e621 100644 --- a/simapp/params/weights.go +++ b/simapp/params/weights.go @@ -14,8 +14,4 @@ const ( DefaultWeightMsgUndelegate int = 100 DefaultWeightMsgBeginRedelegate int = 100 DefaultWeightMsgCancelUnbondingDelegation int = 100 - - // feegrant - DefaultWeightGrantAllowance int = 100 - DefaultWeightRevokeAllowance int = 100 ) diff --git a/x/feegrant/filtered_fee_test.go b/x/feegrant/filtered_fee_test.go index 8fa95d5d9e5c..506886d4e435 100644 --- a/x/feegrant/filtered_fee_test.go +++ b/x/feegrant/filtered_fee_test.go @@ -8,8 +8,9 @@ import ( "github.com/stretchr/testify/require" ocproto "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/simapp" + "github.com/cosmos/cosmos-sdk/depinject" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" @@ -17,6 +18,7 @@ import ( "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" ) @@ -183,7 +185,8 @@ func TestFilteredFeeValidAllow(t *testing.T) { require.NoError(t, err) // save the grant - cdc := simapp.MakeTestEncodingConfig().Codec + var cdc codec.Codec + depinject.Inject(feegranttestutil.AppConfig, &cdc) bz, err := cdc.Marshal(&newGrant) require.NoError(t, err) diff --git a/x/feegrant/migrations/v046/store_test.go b/x/feegrant/migrations/v046/store_test.go index 5d7cb5d9d073..6d6f8828b867 100644 --- a/x/feegrant/migrations/v046/store_test.go +++ b/x/feegrant/migrations/v046/store_test.go @@ -4,18 +4,21 @@ import ( "testing" "time" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/depinject" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/feegrant" v046 "github.com/cosmos/cosmos-sdk/x/feegrant/migrations/v046" + feegranttestutil "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" "github.com/stretchr/testify/require" ) func TestMigration(t *testing.T) { - encCfg := simapp.MakeTestEncodingConfig() - cdc := encCfg.Codec + var cdc codec.Codec + depinject.Inject(feegranttestutil.AppConfig, &cdc) + feegrantKey := sdk.NewKVStoreKey(v046.ModuleName) ctx := testutil.DefaultContext(feegrantKey, sdk.NewTransientStoreKey("transient_test")) granter1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address()) diff --git a/x/feegrant/simulation/decoder_test.go b/x/feegrant/simulation/decoder_test.go index bb6fc789d76b..0a6372403cdd 100644 --- a/x/feegrant/simulation/decoder_test.go +++ b/x/feegrant/simulation/decoder_test.go @@ -6,12 +6,14 @@ import ( "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/depinject" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" "github.com/cosmos/cosmos-sdk/x/feegrant" "github.com/cosmos/cosmos-sdk/x/feegrant/simulation" + feegranttestutil "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" ) var ( @@ -22,7 +24,8 @@ var ( ) func TestDecodeStore(t *testing.T) { - cdc := simapp.MakeTestEncodingConfig().Codec + var cdc codec.Codec + depinject.Inject(feegranttestutil.AppConfig, &cdc) dec := simulation.NewDecodeStore(cdc) grant, err := feegrant.NewGrant(granterAddr, granteeAddr, &feegrant.BasicAllowance{ diff --git a/x/feegrant/simulation/operations.go b/x/feegrant/simulation/operations.go index 6ab599abbcba..0bd3993528af 100644 --- a/x/feegrant/simulation/operations.go +++ b/x/feegrant/simulation/operations.go @@ -6,7 +6,6 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - simappparams "github.com/cosmos/cosmos-sdk/simapp/params" sdk "github.com/cosmos/cosmos-sdk/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/auth/tx" @@ -18,8 +17,10 @@ import ( // Simulation operation weights constants //nolint:gosec // These aren't harcoded credentials. const ( - OpWeightMsgGrantAllowance = "op_weight_msg_grant_fee_allowance" - OpWeightMsgRevokeAllowance = "op_weight_msg_grant_revoke_allowance" + OpWeightMsgGrantAllowance = "op_weight_msg_grant_fee_allowance" + OpWeightMsgRevokeAllowance = "op_weight_msg_grant_revoke_allowance" + DefaultWeightGrantAllowance int = 100 + DefaultWeightRevokeAllowance int = 100 ) var ( @@ -42,13 +43,13 @@ func WeightedOperations( appParams.GetOrGenerate(cdc, OpWeightMsgGrantAllowance, &weightMsgGrantAllowance, nil, func(_ *rand.Rand) { - weightMsgGrantAllowance = simappparams.DefaultWeightGrantAllowance + weightMsgGrantAllowance = DefaultWeightGrantAllowance }, ) appParams.GetOrGenerate(cdc, OpWeightMsgRevokeAllowance, &weightMsgRevokeAllowance, nil, func(_ *rand.Rand) { - weightMsgRevokeAllowance = simappparams.DefaultWeightRevokeAllowance + weightMsgRevokeAllowance = DefaultWeightRevokeAllowance }, ) diff --git a/x/feegrant/simulation/operations_test.go b/x/feegrant/simulation/operations_test.go index b21fc254a22f..0bfc5ee5aa3f 100644 --- a/x/feegrant/simulation/operations_test.go +++ b/x/feegrant/simulation/operations_test.go @@ -8,7 +8,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/runtime" - simappparams "github.com/cosmos/cosmos-sdk/simapp/params" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" @@ -101,12 +100,12 @@ func (suite *SimTestSuite) TestWeightedOperations() { opMsgName string }{ { - simappparams.DefaultWeightGrantAllowance, + simulation.DefaultWeightGrantAllowance, feegrant.MsgGrantAllowance{}.Route(), simulation.TypeMsgGrantAllowance, }, { - simappparams.DefaultWeightRevokeAllowance, + simulation.DefaultWeightRevokeAllowance, feegrant.MsgRevokeAllowance{}.Route(), simulation.TypeMsgRevokeAllowance, }, From e662d8642a568c310a1661fb00f83b21dd1f7659 Mon Sep 17 00:00:00 2001 From: Facundo Medica Date: Mon, 27 Jun 2022 13:41:12 -0300 Subject: [PATCH 3/5] add files --- x/feegrant/testutil/app.yaml | 51 +++++++++++++++++++++++++++++++ x/feegrant/testutil/app_config.go | 23 ++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 x/feegrant/testutil/app.yaml create mode 100644 x/feegrant/testutil/app_config.go diff --git a/x/feegrant/testutil/app.yaml b/x/feegrant/testutil/app.yaml new file mode 100644 index 000000000000..72b6f3e9466a --- /dev/null +++ b/x/feegrant/testutil/app.yaml @@ -0,0 +1,51 @@ +modules: + - name: runtime + config: + "@type": cosmos.app.runtime.v1alpha1.Module + + app_name: AuthApp + + begin_blockers: [staking, auth, bank, genutil, feegrant, params, vesting] + end_blockers: [staking, auth, bank, genutil, feegrant, params, vesting] + init_genesis: [auth, bank, staking, genutil, feegrant, params, vesting] + + - name: auth + config: + "@type": cosmos.auth.module.v1.Module + bech32_prefix: cosmos + module_account_permissions: + - account: fee_collector + - account: mint + permissions: [minter] + - account: bonded_tokens_pool + permissions: [burner, staking] + - account: not_bonded_tokens_pool + permissions: [burner, staking] + + - name: bank + config: + "@type": cosmos.bank.module.v1.Module + + - name: params + config: + "@type": cosmos.params.module.v1.Module + + - name: tx + config: + "@type": cosmos.tx.module.v1.Module + + - name: staking + config: + "@type": cosmos.staking.module.v1.Module + + - name: genutil + config: + "@type": cosmos.genutil.module.v1.Module + + - name: feegrant + config: + "@type": cosmos.feegrant.module.v1.Module + + - name: vesting + config: + "@type": cosmos.vesting.module.v1.Module diff --git a/x/feegrant/testutil/app_config.go b/x/feegrant/testutil/app_config.go new file mode 100644 index 000000000000..6de28a03ba9e --- /dev/null +++ b/x/feegrant/testutil/app_config.go @@ -0,0 +1,23 @@ +package testutil + +import ( + _ "embed" + + "cosmossdk.io/core/appconfig" + _ "github.com/cosmos/cosmos-sdk/x/auth" + _ "github.com/cosmos/cosmos-sdk/x/auth/tx/module" + _ "github.com/cosmos/cosmos-sdk/x/auth/vesting" + _ "github.com/cosmos/cosmos-sdk/x/bank" + _ "github.com/cosmos/cosmos-sdk/x/feegrant/module" + _ "github.com/cosmos/cosmos-sdk/x/genutil" + _ "github.com/cosmos/cosmos-sdk/x/mint" + _ "github.com/cosmos/cosmos-sdk/x/params" + _ "github.com/cosmos/cosmos-sdk/x/staking" +) + +//go:embed app.yaml +var appConfig []byte + +var ( + AppConfig = appconfig.LoadYAML(appConfig) +) From 4dd11db28a7f3dea327fba2f8c9ca145132e65a2 Mon Sep 17 00:00:00 2001 From: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Date: Tue, 28 Jun 2022 10:40:37 -0300 Subject: [PATCH 4/5] Update x/feegrant/simulation/operations_test.go Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com> --- x/feegrant/simulation/operations_test.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/x/feegrant/simulation/operations_test.go b/x/feegrant/simulation/operations_test.go index 0bfc5ee5aa3f..a3843e803a57 100644 --- a/x/feegrant/simulation/operations_test.go +++ b/x/feegrant/simulation/operations_test.go @@ -52,14 +52,6 @@ func (suite *SimTestSuite) SetupTest() { suite.Require().NoError(err) suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()}) - // suite.addrs = simtestutil.AddTestAddrsIncremental(suite.bankKeeper, suite.stakingKeeper, suite.ctx, 4, sdk.NewInt(30000000)) - // suite.msgSrvr = keeper.NewMsgServerImpl(suite.feegrantKeeper) - // suite.atom = sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(555))) - - // suite.app = app - // suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{ - // Time: time.Now(), - // }) } func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { From 23102155be050915a03a92a417671d058d36599a Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 29 Jun 2022 15:57:53 +0200 Subject: [PATCH 5/5] updates comments --- x/feegrant/client/testutil/cli_test.go | 8 ++++---- x/feegrant/client/testutil/suite.go | 4 ++++ x/feegrant/module/abci_test.go | 10 ++++++---- x/feegrant/periodic_fee_test.go | 12 +++++++----- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/x/feegrant/client/testutil/cli_test.go b/x/feegrant/client/testutil/cli_test.go index 9814e9573b3f..cbd952230f4e 100644 --- a/x/feegrant/client/testutil/cli_test.go +++ b/x/feegrant/client/testutil/cli_test.go @@ -1,18 +1,18 @@ -//go:build norace -// +build norace - package testutil import ( "testing" "github.com/cosmos/cosmos-sdk/testutil/network" + "github.com/cosmos/cosmos-sdk/x/feegrant/testutil" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) func TestIntegrationTestSuite(t *testing.T) { - cfg := network.DefaultConfig() + cfg, err := network.DefaultConfigWithAppConfig(testutil.AppConfig) + require.NoError(t, err) cfg.NumValidators = 3 suite.Run(t, NewIntegrationTestSuite(cfg)) } diff --git a/x/feegrant/client/testutil/suite.go b/x/feegrant/client/testutil/suite.go index dd4e3c7c61e2..7411976dd15a 100644 --- a/x/feegrant/client/testutil/suite.go +++ b/x/feegrant/client/testutil/suite.go @@ -713,6 +713,8 @@ func (s *IntegrationTestSuite) TestNewCmdRevokeFeegrant() { } func (s *IntegrationTestSuite) TestTxWithFeeGrant() { + s.T().Skip() // TODO to re-enable in #12274 + val := s.network.Validators[0] clientCtx := val.ClientCtx granter := val.Address @@ -802,6 +804,8 @@ func (s *IntegrationTestSuite) TestTxWithFeeGrant() { } func (s *IntegrationTestSuite) TestFilteredFeeAllowance() { + s.T().Skip() // TODO to re-enable in #12274 + val := s.network.Validators[0] granter := val.Address diff --git a/x/feegrant/module/abci_test.go b/x/feegrant/module/abci_test.go index 21a4b42c0045..db094a30bd28 100644 --- a/x/feegrant/module/abci_test.go +++ b/x/feegrant/module/abci_test.go @@ -19,10 +19,12 @@ import ( ) func TestFeegrantPruning(t *testing.T) { - var interfaceRegistry codectypes.InterfaceRegistry - var bankKeeper bankkeeper.Keeper - var stakingKeeper *stakingkeeper.Keeper - var feegrantKeeper keeper.Keeper + var ( + interfaceRegistry codectypes.InterfaceRegistry + bankKeeper bankkeeper.Keeper + stakingKeeper *stakingkeeper.Keeper + feegrantKeeper keeper.Keeper + ) app, err := simtestutil.Setup(testutil.AppConfig, &feegrantKeeper, diff --git a/x/feegrant/periodic_fee_test.go b/x/feegrant/periodic_fee_test.go index a9c76d67e719..d8b6052f023e 100644 --- a/x/feegrant/periodic_fee_test.go +++ b/x/feegrant/periodic_fee_test.go @@ -20,11 +20,13 @@ import ( ) func TestPeriodicFeeValidAllow(t *testing.T) { - var interfaceRegistry codectypes.InterfaceRegistry - var bankKeeper bankkeeper.Keeper - var stakingKeeper *stakingkeeper.Keeper - var feegrantKeeper keeper.Keeper - var cdc codec.Codec + var ( + interfaceRegistry codectypes.InterfaceRegistry + bankKeeper bankkeeper.Keeper + stakingKeeper *stakingkeeper.Keeper + feegrantKeeper keeper.Keeper + cdc codec.Codec + ) app, err := simtestutil.Setup(testutil.AppConfig, &feegrantKeeper,