Skip to content

Commit

Permalink
Tests: Integration tests for erc20 module (#26)
Browse files Browse the repository at this point in the history
* update tests

* fix

* fix:tests

* Integration tests for erc20
  • Loading branch information
amityadav0 authored May 15, 2023
1 parent 6f517a5 commit 180177b
Show file tree
Hide file tree
Showing 18 changed files with 152 additions and 1,605 deletions.
2 changes: 1 addition & 1 deletion cmd/sidechaind/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func addTestnetFlagsToCmd(cmd *cobra.Command) {
cmd.Flags().Int(flagNumValidators, 4, "Number of validators to initialize the testnet with")
cmd.Flags().StringP(flagOutputDir, "o", "./.testnets", "Directory to store initialization data for the testnet")
cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")
cmd.Flags().String(sdkserver.FlagMinGasPrices, fmt.Sprintf("0.000006%s", cmdcfg.BaseDenom), "Minimum gas prices to accept for transactions; All fees in a tx must meet this minimum (e.g. 0.01photino,0.001stake)")
cmd.Flags().String(sdkserver.FlagMinGasPrices, fmt.Sprintf("0.000006%s", cmdcfg.BaseDenom), "Minimum gas prices to accept for transactions; All fees in a tx must meet this minimum (e.g. 0.01photino,0.001aside)")
cmd.Flags().String(flags.FlagKeyAlgorithm, string(hd.EthSecp256k1Type), "Key signing algorithm to generate keys for")
}

Expand Down
11 changes: 6 additions & 5 deletions testutil/fund.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package testutil

import (
minttypes "sidechain/x/mint/types"

sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
inflationtypes "github.com/evmos/evmos/v11/x/inflation/types"
)

// FundAccount is a utility function that funds an account by minting and
// sending the coins to the address. This should be used for testing purposes
// only!
func FundAccount(ctx sdk.Context, bankKeeper bankkeeper.Keeper, addr sdk.AccAddress, amounts sdk.Coins) error {
if err := bankKeeper.MintCoins(ctx, inflationtypes.ModuleName, amounts); err != nil {
if err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil {
return err
}

return bankKeeper.SendCoinsFromModuleToAccount(ctx, inflationtypes.ModuleName, addr, amounts)
return bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amounts)
}

// FundModuleAccount is a utility function that funds a module account by
// minting and sending the coins to the address. This should be used for testing
// purposes only!
func FundModuleAccount(ctx sdk.Context, bankKeeper bankkeeper.Keeper, recipientMod string, amounts sdk.Coins) error {
if err := bankKeeper.MintCoins(ctx, inflationtypes.ModuleName, amounts); err != nil {
if err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil {
return err
}

return bankKeeper.SendCoinsFromModuleToModule(ctx, inflationtypes.ModuleName, recipientMod, amounts)
return bankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, recipientMod, amounts)
}
9 changes: 5 additions & 4 deletions testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"

minttypes "sidechain/x/mint/types"

"github.com/cosmos/cosmos-sdk/server/api"
servergrpc "github.com/cosmos/cosmos-sdk/server/grpc"
srvtypes "github.com/cosmos/cosmos-sdk/server/types"
Expand All @@ -27,7 +29,6 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
inflationtypes "github.com/evmos/evmos/v11/x/inflation/types"

"github.com/evmos/ethermint/server"
evmtypes "github.com/evmos/ethermint/x/evm/types"
Expand Down Expand Up @@ -212,11 +213,11 @@ func initGenFiles(cfg Config, genAccounts []authtypes.GenesisAccount, genBalance
govGenState.DepositParams.MinDeposit[0].Denom = cfg.BondDenom
cfg.GenesisState[govtypes.ModuleName] = cfg.Codec.MustMarshalJSON(&govGenState)

var inflationGenState inflationtypes.GenesisState
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[inflationtypes.ModuleName], &inflationGenState)
var inflationGenState minttypes.GenesisState
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[minttypes.ModuleName], &inflationGenState)

inflationGenState.Params.MintDenom = cfg.BondDenom
cfg.GenesisState[inflationtypes.ModuleName] = cfg.Codec.MustMarshalJSON(&inflationGenState)
cfg.GenesisState[minttypes.ModuleName] = cfg.Codec.MustMarshalJSON(&inflationGenState)

var crisisGenState crisistypes.GenesisState
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[crisistypes.ModuleName], &crisisGenState)
Expand Down
2 changes: 1 addition & 1 deletion utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestEvmosCoinDenom(t *testing.T) {
}{
{
"valid denom - native coin",
"aevmos",
"aside",
false,
},
{
Expand Down
10 changes: 6 additions & 4 deletions x/devearn/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package cli

import (
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/ethereum/go-ethereum/common"
"strconv"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"sidechain/x/devearn/types"

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

func NewRegisterDevEarnProposalCmd() *cobra.Command {
Expand Down Expand Up @@ -78,7 +80,7 @@ func NewRegisterDevEarnProposalCmd() *cobra.Command {

cmd.Flags().String(cli.FlagTitle, "", "title of proposal")
cmd.Flags().String(cli.FlagDescription, "", "description of proposal")
cmd.Flags().String(cli.FlagDeposit, "1aevmos", "deposit of proposal")
cmd.Flags().String(cli.FlagDeposit, "1aside", "deposit of proposal")
if err := cmd.MarkFlagRequired(cli.FlagTitle); err != nil {
panic(err)
}
Expand Down Expand Up @@ -148,7 +150,7 @@ func NewCancelDevEarnProposalCmd() *cobra.Command {

cmd.Flags().String(cli.FlagTitle, "", "title of proposal")
cmd.Flags().String(cli.FlagDescription, "", "description of proposal")
cmd.Flags().String(cli.FlagDeposit, "1aevmos", "deposit of proposal")
cmd.Flags().String(cli.FlagDeposit, "1aside", "deposit of proposal")
if err := cmd.MarkFlagRequired(cli.FlagTitle); err != nil {
panic(err)
}
Expand Down
42 changes: 0 additions & 42 deletions x/epochs/keeper/setup_test.go

This file was deleted.

106 changes: 78 additions & 28 deletions x/epochs/keeper/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,88 @@
package keeper_test

import (
"testing"
"time"

"sidechain/app"
"sidechain/testutil"
"sidechain/x/epochs/types"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/cosmos/cosmos-sdk/baseapp"
abci "github.com/tendermint/tendermint/abci/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/crypto/tmhash"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
"github.com/tendermint/tendermint/version"

evm "github.com/evmos/ethermint/x/evm/types"

"sidechain/app"
"sidechain/x/epochs/types"
)

var denomMint = evm.DefaultEVMDenom

type KeeperTestSuite struct {
suite.Suite

ctx sdk.Context
app *app.Sidechain
queryClientEvm evm.QueryClient
queryClient types.QueryClient
consAddress sdk.ConsAddress
}

var s *KeeperTestSuite

func TestKeeperTestSuite(t *testing.T) {
s = new(KeeperTestSuite)
suite.Run(t, s)

// Run Ginkgo integration tests
RegisterFailHandler(Fail)
RunSpecs(t, "Keeper Suite")
}

func (suite *KeeperTestSuite) SetupTest() {
suite.DoSetupTest(suite.T())
}

// Test helpers
func (suite *KeeperTestSuite) DoSetupTest() {
func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {
checkTx := false

// init app
suite.app = app.Setup(checkTx, nil)

// setup context
header := testutil.NewHeader(
1, time.Now().UTC(), "sidechain_9001-1", suite.consAddress, nil, nil,
)
suite.ctx = suite.app.BaseApp.NewContext(checkTx, header)
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{
Height: 1,
ChainID: "sidechain_7071-1",
Time: time.Now().UTC(),
ProposerAddress: suite.consAddress.Bytes(),

Version: tmversion.Consensus{
Block: version.BlockProtocol,
},
LastBlockId: tmproto.BlockID{
Hash: tmhash.Sum([]byte("block_id")),
PartSetHeader: tmproto.PartSetHeader{
Total: 11,
Hash: tmhash.Sum([]byte("partset_header")),
},
},
AppHash: tmhash.Sum([]byte("app")),
DataHash: tmhash.Sum([]byte("data")),
EvidenceHash: tmhash.Sum([]byte("evidence")),
ValidatorsHash: tmhash.Sum([]byte("validators")),
NextValidatorsHash: tmhash.Sum([]byte("next_validators")),
ConsensusHash: tmhash.Sum([]byte("consensus")),
LastResultsHash: tmhash.Sum([]byte("last_result")),
})

// setup query helpers
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
Expand All @@ -45,28 +105,18 @@ func (suite *KeeperTestSuite) Commit() {
}

func (suite *KeeperTestSuite) CommitAfter(t time.Duration) {
var err error
suite.ctx, err = testutil.Commit(suite.ctx, suite.app, t, nil)
suite.Require().NoError(err)
_ = suite.app.Commit()
header := suite.ctx.BlockHeader()
header.Height += 1
header.Time = header.Time.Add(t)
suite.app.BeginBlock(abci.RequestBeginBlock{
Header: header,
})

// update ctx
suite.ctx = suite.app.BaseApp.NewContext(false, header)

queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
evm.RegisterQueryServer(queryHelper, suite.app.EvmKeeper)
suite.queryClientEvm = evm.NewQueryClient(queryHelper)
}

// func (suite *KeeperTestSuite) CommitAfter(t time.Duration) {
// _ = suite.app.Commit()
// header := suite.ctx.BlockHeader()
// header.Height += 1
// header.Time = header.Time.Add(t)
// suite.app.BeginBlock(abci.RequestBeginBlock{
// Header: header,
// })

// // update ctx
// suite.ctx = suite.app.BaseApp.NewContext(false, header)

// queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
// evm.RegisterQueryServer(queryHelper, suite.app.EvmKeeper)
// suite.queryClientEvm = evm.NewQueryClient(queryHelper)
// }
6 changes: 3 additions & 3 deletions x/erc20/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Where metadata.json contains (example):

cmd.Flags().String(cli.FlagTitle, "", "title of proposal")
cmd.Flags().String(cli.FlagDescription, "", "description of proposal")
cmd.Flags().String(cli.FlagDeposit, "1aevmos", "deposit of proposal")
cmd.Flags().String(cli.FlagDeposit, "1aside", "deposit of proposal")
if err := cmd.MarkFlagRequired(cli.FlagTitle); err != nil {
panic(err)
}
Expand Down Expand Up @@ -303,7 +303,7 @@ func NewRegisterERC20ProposalCmd() *cobra.Command {

cmd.Flags().String(cli.FlagTitle, "", "title of proposal")
cmd.Flags().String(cli.FlagDescription, "", "description of proposal")
cmd.Flags().String(cli.FlagDeposit, "1aevmos", "deposit of proposal")
cmd.Flags().String(cli.FlagDeposit, "1aside", "deposit of proposal")
if err := cmd.MarkFlagRequired(cli.FlagTitle); err != nil {
panic(err)
}
Expand Down Expand Up @@ -370,7 +370,7 @@ func NewToggleTokenConversionProposalCmd() *cobra.Command {

cmd.Flags().String(cli.FlagTitle, "", "title of proposal")
cmd.Flags().String(cli.FlagDescription, "", "description of proposal")
cmd.Flags().String(cli.FlagDeposit, "1aevmos", "deposit of proposal")
cmd.Flags().String(cli.FlagDeposit, "1aside", "deposit of proposal")
if err := cmd.MarkFlagRequired(cli.FlagTitle); err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion x/erc20/keeper/ibc_callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
// OnRecvPacket performs the ICS20 middleware receive callback for automatically
// converting an IBC Coin to their ERC20 representation.
// For the conversion to succeed, the IBC denomination must have previously been
// registered via governance. Note that the native staking denomination (e.g. "aevmos"),
// registered via governance. Note that the native staking denomination (e.g. "aside"),
// is excluded from the conversion.
//
// CONTRACT: This middleware MUST be executed transfer after the ICS20 OnRecvPacket
Expand Down
Loading

0 comments on commit 180177b

Please sign in to comment.