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 #12388

Merged
merged 12 commits into from
Jun 30, 2022
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
)
25 changes: 22 additions & 3 deletions x/feegrant/basic_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,34 @@ 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
bankKeeper bankkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
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
5 changes: 4 additions & 1 deletion x/feegrant/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ 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))
}
4 changes: 4 additions & 0 deletions x/feegrant/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 27 additions & 6 deletions x/feegrant/filtered_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,38 @@ 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
bankKeeper bankkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
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 +187,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
32 changes: 26 additions & 6 deletions x/feegrant/grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,46 @@ 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
bankKeeper bankkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
feegrantKeeper keeper.Keeper
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
40 changes: 29 additions & 11 deletions x/feegrant/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,44 @@ 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
bankKeeper bankkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
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 +57,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 +120,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