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

Problem: app utils triggers import cycle easily #477

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions app/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (

"github.com/ethereum/go-ethereum/core/types"
ethparams "github.com/ethereum/go-ethereum/params"
"github.com/evmos/ethermint/app"
"github.com/evmos/ethermint/app/ante"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
"github.com/evmos/ethermint/tests"
"github.com/evmos/ethermint/testutil"
evmtypes "github.com/evmos/ethermint/x/evm/types"

amino "github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -937,7 +937,7 @@ func (suite *AnteTestSuite) TestAnteHandler() {
suite.Run(tc.name, func() {
setup()

suite.ctx = suite.ctx.WithIsCheckTx(tc.checkTx).WithIsReCheckTx(tc.reCheckTx).WithConsensusParams(*app.DefaultConsensusParams)
suite.ctx = suite.ctx.WithIsCheckTx(tc.checkTx).WithIsReCheckTx(tc.reCheckTx).WithConsensusParams(*testutil.DefaultConsensusParams)
suite.app.EvmKeeper.RemoveParamsCache(suite.ctx)

// expConsumed := params.TxGasContractCreation + params.TxGas
Expand Down Expand Up @@ -1208,7 +1208,7 @@ func (suite *AnteTestSuite) TestAnteHandlerWithDynamicTxFee() {
suite.Require().NoError(acc.SetSequence(1))
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.ctx = suite.ctx.WithIsCheckTx(tc.checkTx).WithIsReCheckTx(tc.reCheckTx).WithConsensusParams(*app.DefaultConsensusParams)
suite.ctx = suite.ctx.WithIsCheckTx(tc.checkTx).WithIsReCheckTx(tc.reCheckTx).WithConsensusParams(*testutil.DefaultConsensusParams)
suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt((ethparams.InitialBaseFee+10)*100000), evmtypes.DefaultEVMDenom)
_, err := suite.anteHandler(suite.ctx, tc.txFn(), false)
if tc.expPass {
Expand Down Expand Up @@ -1337,7 +1337,7 @@ func (suite *AnteTestSuite) TestAnteHandlerWithParams() {
suite.Require().NoError(acc.SetSequence(1))
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

suite.ctx = suite.ctx.WithIsCheckTx(true).WithConsensusParams(*app.DefaultConsensusParams)
suite.ctx = suite.ctx.WithIsCheckTx(true).WithConsensusParams(*testutil.DefaultConsensusParams)
suite.app.EvmKeeper.SetBalance(suite.ctx, addr, big.NewInt((ethparams.InitialBaseFee+10)*100000), evmtypes.DefaultEVMDenom)
_, err := suite.anteHandler(suite.ctx, tc.txFn(), false)
if tc.expErr == nil {
Expand Down
7 changes: 4 additions & 3 deletions app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/evmos/ethermint/app"
"github.com/evmos/ethermint/ethereum/eip712"
"github.com/evmos/ethermint/testutil"
"github.com/evmos/ethermint/testutil/config"
utiltx "github.com/evmos/ethermint/testutil/tx"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -82,7 +83,7 @@ func (suite *AnteTestSuite) SetupTest() {
suite.Require().NoError(err)
suite.priv = priv

suite.app = app.Setup(checkTx, func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState {
suite.app = testutil.Setup(checkTx, func(app *app.EthermintApp, genesis app.GenesisState) app.GenesisState {
if suite.enableFeemarket {
// setup feemarketGenesis params
feemarketGenesis := feemarkettypes.DefaultGenesisState()
Expand Down Expand Up @@ -111,7 +112,7 @@ func (suite *AnteTestSuite) SetupTest() {
})
header := tmproto.Header{Height: 2, ChainID: testutil.TestnetChainID + "-1", Time: time.Now().UTC()}
suite.ctx = suite.app.BaseApp.NewUncachedContext(checkTx, header).
WithConsensusParams(*app.DefaultConsensusParams).
WithConsensusParams(*testutil.DefaultConsensusParams).
WithMinGasPrices(sdk.NewDecCoins(sdk.NewDecCoin(evmtypes.DefaultEVMDenom, sdkmath.OneInt()))).
WithBlockGasMeter(storetypes.NewGasMeter(1000000000000000000))
suite.app.EvmKeeper.WithChainID(suite.ctx)
Expand All @@ -123,7 +124,7 @@ func (suite *AnteTestSuite) SetupTest() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr)
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

encodingConfig := app.MakeConfigForTest()
encodingConfig := config.MakeConfigForTest(suite.app.BasicModuleManager)
// We're using TestMsg amino encoding in some tests, so register it here.
encodingConfig.Amino.RegisterConcrete(&testdata.TestMsg{}, "testdata.TestMsg", nil)
eip712.SetEncodingConfig(encodingConfig)
Expand Down
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ var (
_ servertypes.Application = (*EthermintApp)(nil)
)

type GenesisState map[string]json.RawMessage

// var _ server.Application (*EthermintApp)(nil)

// EthermintApp implements an extended ABCI application. It is an application
Expand Down
16 changes: 9 additions & 7 deletions app/app_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package app
package app_test

import (
"os"
"testing"

"github.com/evmos/ethermint/app"
"github.com/evmos/ethermint/testutil"
"github.com/stretchr/testify/require"

"cosmossdk.io/log"
Expand All @@ -15,18 +17,18 @@ import (

func TestEthermintAppExport(t *testing.T) {
db := dbm.NewMemDB()
app := SetupWithDB(false, nil, db)
app.Commit()
ethApp := testutil.SetupWithDB(false, nil, db)
ethApp.Commit()

// Making a new app object with the db, so that initchain hasn't been called
app2 := NewEthermintApp(
ethApp2 := app.NewEthermintApp(
log.NewLogger(os.Stdout),
db,
nil,
true,
simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome),
baseapp.SetChainID(ChainID),
simtestutil.NewAppOptionsWithFlagHome(app.DefaultNodeHome),
baseapp.SetChainID(testutil.ChainID),
)
_, err := app2.ExportAppStateAndValidators(false, []string{}, []string{})
_, err := ethApp2.ExportAppStateAndValidators(false, []string{}, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}
24 changes: 13 additions & 11 deletions app/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package app_test

import (
"encoding/json"
Expand All @@ -10,46 +10,48 @@ import (
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/baseapp"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/evmos/ethermint/app"
"github.com/evmos/ethermint/testutil"
)

func BenchmarkEthermintApp_ExportAppStateAndValidators(b *testing.B) {
db := dbm.NewMemDB()
app := NewEthermintApp(
app1 := app.NewEthermintApp(
log.NewLogger(io.Discard),
db,
nil,
true,
simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome),
baseapp.SetChainID(ChainID),
simtestutil.NewAppOptionsWithFlagHome(app.DefaultNodeHome),
baseapp.SetChainID(testutil.ChainID),
)

genesisState := NewTestGenesisState(app.AppCodec(), app.DefaultGenesis())
genesisState := testutil.NewTestGenesisState(app1.AppCodec(), app1.DefaultGenesis())
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
b.Fatal(err)
}

// Initialize the chain
app.InitChain(
app1.InitChain(
&abci.RequestInitChain{
ChainId: ChainID,
ChainId: testutil.ChainID,
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
},
)
app.Commit()
app1.Commit()

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
// Making a new app object with the db, so that initchain hasn't been called
app2 := NewEthermintApp(
app2 := app.NewEthermintApp(
log.NewLogger(io.Discard),
db,
nil,
true,
simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome),
baseapp.SetChainID(ChainID),
simtestutil.NewAppOptionsWithFlagHome(app.DefaultNodeHome),
baseapp.SetChainID(testutil.ChainID),
)
if _, err := app2.ExportAppStateAndValidators(false, []string{}, []string{}); err != nil {
b.Fatal(err)
Expand Down
83 changes: 43 additions & 40 deletions app/simulation_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package app_test

// TODO: COsmos SDK fix for the simulator issue for custom keys
import (
Expand Down Expand Up @@ -39,14 +39,17 @@ import (
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
"github.com/evmos/ethermint/app"
"github.com/evmos/ethermint/app/ante"
"github.com/evmos/ethermint/testutil"
)

func init() {
simcli.GetSimulatorFlags()
}

const (
appName = "ethermintd"
SimAppChainID = "simulation_777-1"
SimBlockMaxGas = 815000000
)
Expand All @@ -64,11 +67,11 @@ func fauxMerkleModeOpt(bapp *baseapp.BaseApp) {
}

// NewSimApp disable feemarket on native tx, otherwise the cosmos-sdk simulation tests will fail.
func NewSimApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*baseapp.BaseApp)) (*EthermintApp, error) {
func NewSimApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*baseapp.BaseApp)) (*app.EthermintApp, error) {
appOptions := make(simtestutil.AppOptionsMap, 0)
appOptions[flags.FlagHome] = DefaultNodeHome
appOptions[flags.FlagHome] = app.DefaultNodeHome
appOptions[server.FlagInvCheckPeriod] = simcli.FlagPeriodValue
app := NewEthermintApp(logger, db, nil, false, appOptions, baseAppOptions...)
app := app.NewEthermintApp(logger, db, nil, false, appOptions, baseAppOptions...)
// disable feemarket on native tx
anteHandler, err := ante.NewAnteHandler(ante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
Expand Down Expand Up @@ -123,8 +126,8 @@ func TestFullAppSimulation(t *testing.T) {
t,
os.Stdout,
app.BaseApp,
StateFn(app),
RandomAccounts, // Replace with own random account function if using keys other than secp256k1
testutil.StateFn(app),
testutil.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
Expand Down Expand Up @@ -156,25 +159,25 @@ func TestAppImportExport(t *testing.T) {
require.NoError(t, os.RemoveAll(dir))
}()

app, err := NewSimApp(logger, db, fauxMerkleModeOpt, baseapp.SetChainID(SimAppChainID))
simApp, err := NewSimApp(logger, db, fauxMerkleModeOpt, baseapp.SetChainID(SimAppChainID))
require.NoError(t, err)
require.Equal(t, appName, app.Name())
require.Equal(t, appName, simApp.Name())

// Run randomized simulation
_, simParams, simErr := simulation.SimulateFromSeed(
t,
os.Stdout,
app.BaseApp,
StateFn(app),
RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
simApp.BaseApp,
testutil.StateFn(simApp),
testutil.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(simApp, simApp.AppCodec(), config),
simApp.ModuleAccountAddrs(),
config,
app.AppCodec(),
simApp.AppCodec(),
)

// export state and simParams before the simulation error is checked
err = simtestutil.CheckExportSimulation(app, config, simParams)
err = simtestutil.CheckExportSimulation(simApp, config, simParams)
require.NoError(t, err)
require.NoError(t, simErr)

Expand All @@ -184,7 +187,7 @@ func TestAppImportExport(t *testing.T) {

fmt.Printf("exporting genesis...\n")

exported, err := app.ExportAppStateAndValidators(false, []string{}, []string{})
exported, err := simApp.ExportAppStateAndValidators(false, []string{}, []string{})
require.NoError(t, err)

fmt.Printf("importing genesis...\n")
Expand All @@ -202,7 +205,7 @@ func TestAppImportExport(t *testing.T) {
require.Equal(t, appName, newApp.Name())
require.NoError(t, err)

var genesisState GenesisState
var genesisState app.GenesisState
err = json.Unmarshal(exported.AppState, &genesisState)
require.NoError(t, err)

Expand All @@ -217,33 +220,33 @@ func TestAppImportExport(t *testing.T) {
}
}()

ctxA := app.NewContextLegacy(true, tmproto.Header{Height: app.LastBlockHeight(), ChainID: SimAppChainID})
ctxB := newApp.NewContextLegacy(true, tmproto.Header{Height: app.LastBlockHeight(), ChainID: SimAppChainID})
newApp.ModuleManager.InitGenesis(ctxB, app.AppCodec(), genesisState)
ctxA := simApp.NewContextLegacy(true, tmproto.Header{Height: simApp.LastBlockHeight(), ChainID: SimAppChainID})
ctxB := newApp.NewContextLegacy(true, tmproto.Header{Height: simApp.LastBlockHeight(), ChainID: SimAppChainID})
newApp.ModuleManager.InitGenesis(ctxB, simApp.AppCodec(), genesisState)
newApp.StoreConsensusParams(ctxB, exported.ConsensusParams)

fmt.Printf("comparing stores...\n")

storeKeysPrefixes := []storeKeysPrefixes{
{app.keys[authtypes.StoreKey], newApp.keys[authtypes.StoreKey], [][]byte{}},
{simApp.GetKey(authtypes.StoreKey), newApp.GetKey(authtypes.StoreKey), [][]byte{}},
{
app.keys[stakingtypes.StoreKey], newApp.keys[stakingtypes.StoreKey],
simApp.GetKey(stakingtypes.StoreKey), newApp.GetKey(stakingtypes.StoreKey),
[][]byte{
stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, stakingtypes.ValidatorQueueKey,
stakingtypes.HistoricalInfoKey, stakingtypes.UnbondingIDKey, stakingtypes.UnbondingIndexKey, stakingtypes.UnbondingTypeKey, stakingtypes.ValidatorUpdatesKey,
},
}, // ordering may change but it doesn't matter
{app.keys[slashingtypes.StoreKey], newApp.keys[slashingtypes.StoreKey], [][]byte{}},
{app.keys[minttypes.StoreKey], newApp.keys[minttypes.StoreKey], [][]byte{}},
{app.keys[distrtypes.StoreKey], newApp.keys[distrtypes.StoreKey], [][]byte{}},
{app.keys[banktypes.StoreKey], newApp.keys[banktypes.StoreKey], [][]byte{banktypes.BalancesPrefix}},
{app.keys[paramtypes.StoreKey], newApp.keys[paramtypes.StoreKey], [][]byte{}},
{app.keys[govtypes.StoreKey], newApp.keys[govtypes.StoreKey], [][]byte{}},
{app.keys[evidencetypes.StoreKey], newApp.keys[evidencetypes.StoreKey], [][]byte{}},
{app.keys[capabilitytypes.StoreKey], newApp.keys[capabilitytypes.StoreKey], [][]byte{}},
{app.keys[authzkeeper.StoreKey], newApp.keys[authzkeeper.StoreKey], [][]byte{authzkeeper.GrantKey, authzkeeper.GrantQueuePrefix}},
{app.keys[ibcexported.StoreKey], newApp.keys[ibcexported.StoreKey], [][]byte{}},
{app.keys[ibctransfertypes.StoreKey], newApp.keys[ibctransfertypes.StoreKey], [][]byte{}},
{simApp.GetKey(slashingtypes.StoreKey), newApp.GetKey(slashingtypes.StoreKey), [][]byte{}},
{simApp.GetKey(minttypes.StoreKey), newApp.GetKey(minttypes.StoreKey), [][]byte{}},
{simApp.GetKey(distrtypes.StoreKey), newApp.GetKey(distrtypes.StoreKey), [][]byte{}},
{simApp.GetKey(banktypes.StoreKey), newApp.GetKey(banktypes.StoreKey), [][]byte{banktypes.BalancesPrefix}},
{simApp.GetKey(paramtypes.StoreKey), newApp.GetKey(paramtypes.StoreKey), [][]byte{}},
{simApp.GetKey(govtypes.StoreKey), newApp.GetKey(govtypes.StoreKey), [][]byte{}},
{simApp.GetKey(evidencetypes.StoreKey), newApp.GetKey(evidencetypes.StoreKey), [][]byte{}},
{simApp.GetKey(capabilitytypes.StoreKey), newApp.GetKey(capabilitytypes.StoreKey), [][]byte{}},
{simApp.GetKey(authzkeeper.StoreKey), newApp.GetKey(authzkeeper.StoreKey), [][]byte{authzkeeper.GrantKey, authzkeeper.GrantQueuePrefix}},
{simApp.GetKey(ibcexported.StoreKey), newApp.GetKey(ibcexported.StoreKey), [][]byte{}},
{simApp.GetKey(ibctransfertypes.StoreKey), newApp.GetKey(ibctransfertypes.StoreKey), [][]byte{}},
}

for _, skp := range storeKeysPrefixes {
Expand All @@ -254,7 +257,7 @@ func TestAppImportExport(t *testing.T) {
require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare")

fmt.Printf("compared %d different key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B)
require.Equal(t, len(failedKVAs), 0, simtestutil.GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, failedKVAs, failedKVBs))
require.Equal(t, len(failedKVAs), 0, simtestutil.GetSimulationLog(skp.A.Name(), simApp.SimulationManager().StoreDecoders, failedKVAs, failedKVBs))
}
}

Expand Down Expand Up @@ -284,8 +287,8 @@ func TestAppSimulationAfterImport(t *testing.T) {
t,
os.Stdout,
app.BaseApp,
StateFn(app),
RandomAccounts, // Replace with own random account function if using keys other than secp256k1
testutil.StateFn(app),
testutil.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
Expand Down Expand Up @@ -334,8 +337,8 @@ func TestAppSimulationAfterImport(t *testing.T) {
t,
os.Stdout,
newApp.BaseApp,
StateFn(app),
RandomAccounts, // Replace with own random account function if using keys other than secp256k1
testutil.StateFn(app),
testutil.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(newApp, newApp.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
Expand Down Expand Up @@ -386,8 +389,8 @@ func TestAppStateDeterminism(t *testing.T) {
t,
os.Stdout,
app.BaseApp,
StateFn(app),
RandomAccounts, // Replace with own random account function if using keys other than secp256k1
testutil.StateFn(app),
testutil.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
Expand Down
Loading
Loading