Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: decouple x/feegrant and simapp #12366

Merged
merged 8 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions simapp/params/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ const (
DefaultWeightMsgUndelegate int = 100
DefaultWeightMsgBeginRedelegate int = 100
DefaultWeightMsgCancelUnbondingDelegation int = 100

// feegrant
DefaultWeightGrantAllowance int = 100
DefaultWeightRevokeAllowance int = 100
)
23 changes: 20 additions & 3 deletions x/feegrant/basic_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 25 additions & 6 deletions x/feegrant/filtered_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,36 @@ import (
"github.com/stretchr/testify/require"
ocproto "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/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"
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"
)

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))
Expand Down Expand Up @@ -167,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)

Expand Down
30 changes: 24 additions & 6 deletions x/feegrant/grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 27 additions & 11 deletions x/feegrant/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -39,24 +55,24 @@ 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{
Granter: granterAddr.String(),
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)
}
Expand Down Expand Up @@ -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)
})
}
Expand Down
10 changes: 5 additions & 5 deletions x/feegrant/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
})
Expand Down
Loading