From e91ec58aa12ad548ba8a71fd50e744d0042ba124 Mon Sep 17 00:00:00 2001 From: Tomas Tauber <2410580+tomtau@users.noreply.github.com> Date: Mon, 11 Oct 2021 19:09:53 +0800 Subject: [PATCH] evm: fixed commented out simulations, pubsub, and handler tests (#655) * evm: fixed commented out simulations, pubsub, and handler tests fixes #640 simulations are very basic: they can be built and executed, but they don't generate any EVM-related transactions yet. (It should be a matter of adding simulation-related code to the modules + potentially extra helpers to the simulation.) handler tests miss some extra assertions due to changes in the return values snapshotting logic (ADR-001 and ADR-002). Besides the test suites identified in the audit, there's also "importer_test.go" which wasn't yet fixed. (it'd require major rewriting + extra test resources) * gofumpt --- app/app.go | 2 + app/simulation_test.go | 685 ++++++++++++++------------- rpc/ethereum/pubsub/pubsub_test.go | 115 +++-- x/evm/handler_test.go | 731 ++++++++++++++++------------- x/evm/module.go | 26 + x/evm/simulation/genesis.go | 27 ++ x/evm/types/genesis.go | 8 + x/feemarket/module.go | 26 + x/feemarket/simulation/genesis.go | 27 ++ x/feemarket/types/genesis.go | 9 + 10 files changed, 950 insertions(+), 706 deletions(-) create mode 100644 x/evm/simulation/genesis.go create mode 100644 x/feemarket/simulation/genesis.go diff --git a/app/app.go b/app/app.go index 49e7b427..fa163e91 100644 --- a/app/app.go +++ b/app/app.go @@ -495,6 +495,8 @@ func NewEthermintApp( authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), ibc.NewAppModule(app.IBCKeeper), transferModule, + evm.NewAppModule(app.EvmKeeper, app.AccountKeeper), + feemarket.NewAppModule(app.FeeMarketKeeper), ) app.sm.RegisterStoreDecoders() diff --git a/app/simulation_test.go b/app/simulation_test.go index 34b013e0..ab7f981d 100644 --- a/app/simulation_test.go +++ b/app/simulation_test.go @@ -1,340 +1,349 @@ package app -// disable for now, enable it once SDK side fix the simulator issue for custom keys -//import ( -// "encoding/json" -// "fmt" -// "math/rand" -// "os" -// "testing" -// -// "github.com/stretchr/testify/require" -// -// abci "github.com/tendermint/tendermint/abci/types" -// "github.com/tendermint/tendermint/libs/log" -// tmproto "github.com/tendermint/tendermint/proto/tendermint/types" -// dbm "github.com/tendermint/tm-db" -// -// "github.com/cosmos/cosmos-sdk/baseapp" -// "github.com/cosmos/cosmos-sdk/simapp" -// "github.com/cosmos/cosmos-sdk/simapp/helpers" -// "github.com/cosmos/cosmos-sdk/store" -// sdk "github.com/cosmos/cosmos-sdk/types" -// simtypes "github.com/cosmos/cosmos-sdk/types/simulation" -// authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" -// banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" -// capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" -// distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" -// evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types" -// govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" -// ibctransfertypes "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/types" -// ibchost "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host" -// minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" -// paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" -// "github.com/cosmos/cosmos-sdk/x/simulation" -// slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" -// stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" -//) -// -//func init() { -// simapp.GetSimulatorFlags() -//} -// -//type storeKeysPrefixes struct { -// A sdk.StoreKey -// B sdk.StoreKey -// Prefixes [][]byte -//} -// -//// fauxMerkleModeOpt returns a BaseApp option to use a dbStoreAdapter instead of -//// an IAVLStore for faster simulation speed. -//func fauxMerkleModeOpt(bapp *baseapp.BaseApp) { -// bapp.SetFauxMerkleMode() -//} -// -//// interBlockCacheOpt returns a BaseApp option function that sets the persistent -//// inter-block write-through cache. -//func interBlockCacheOpt() func(*baseapp.BaseApp) { -// return baseapp.SetInterBlockCache(store.NewCommitKVStoreCacheManager()) -//} -// -//func TestFullAppSimulation(t *testing.T) { -// config, db, dir, logger, skip, err := simapp.SetupSimulation("leveldb-app-sim", "Simulation") -// if skip { -// t.Skip("skipping application simulation") -// } -// require.NoError(t, err, "simulation setup failed") -// -// defer func() { -// db.Close() -// require.NoError(t, os.RemoveAll(dir)) -// }() -// -// app := NewEthermintApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, fauxMerkleModeOpt) -// require.Equal(t, appName, app.Name()) -// -// // run randomized simulation -// _, simParams, simErr := simulation.SimulateFromSeed( -// t, -// os.Stdout, -// app.BaseApp, -// simapp.AppStateFn(app.AppCodec(), app.SimulationManager()), -// simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 -// simapp.SimulationOperations(app, app.AppCodec(), config), -// app.ModuleAccountAddrs(), -// config, -// app.AppCodec(), -// ) -// -// // export state and simParams before the simulation error is checked -// err = simapp.CheckExportSimulation(app, config, simParams) -// require.NoError(t, err) -// require.NoError(t, simErr) -// -// if config.Commit { -// simapp.PrintStats(db) -// } -//} -// -//func TestAppImportExport(t *testing.T) { -// config, db, dir, logger, skip, err := simapp.SetupSimulation("leveldb-app-sim", "Simulation") -// if skip { -// t.Skip("skipping application import/export simulation") -// } -// require.NoError(t, err, "simulation setup failed") -// -// defer func() { -// db.Close() -// require.NoError(t, os.RemoveAll(dir)) -// }() -// -// app := NewEthermintApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, fauxMerkleModeOpt) -// require.Equal(t, appName, app.Name()) -// -// // Run randomized simulation -// _, simParams, simErr := simulation.SimulateFromSeed( -// t, -// os.Stdout, -// app.BaseApp, -// simapp.AppStateFn(app.AppCodec(), app.SimulationManager()), -// simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 -// simapp.SimulationOperations(app, app.AppCodec(), config), -// app.ModuleAccountAddrs(), -// config, -// app.AppCodec(), -// ) -// -// // export state and simParams before the simulation error is checked -// err = simapp.CheckExportSimulation(app, config, simParams) -// require.NoError(t, err) -// require.NoError(t, simErr) -// -// if config.Commit { -// simapp.PrintStats(db) -// } -// -// fmt.Printf("exporting genesis...\n") -// -// exported, err := app.ExportAppStateAndValidators(false, []string{}) -// require.NoError(t, err) -// -// fmt.Printf("importing genesis...\n") -// -// // nolint: dogsled -// _, newDB, newDir, _, _, err := simapp.SetupSimulation("leveldb-app-sim-2", "Simulation-2") -// require.NoError(t, err, "simulation setup failed") -// -// defer func() { -// newDB.Close() -// require.NoError(t, os.RemoveAll(newDir)) -// }() -// -// newApp := NewEthermintApp(log.NewNopLogger(), newDB, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, fauxMerkleModeOpt) -// require.Equal(t, appName, newApp.Name()) -// -// var genesisState simapp.GenesisState -// err = json.Unmarshal(exported.AppState, &genesisState) -// require.NoError(t, err) -// -// ctxA := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}) -// ctxB := newApp.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}) -// newApp.mm.InitGenesis(ctxB, app.AppCodec(), genesisState) -// newApp.StoreConsensusParams(ctxB, exported.ConsensusParams) -// -// fmt.Printf("comparing stores...\n") -// -// storeKeysPrefixes := []storeKeysPrefixes{ -// {app.keys[authtypes.StoreKey], newApp.keys[authtypes.StoreKey], [][]byte{}}, -// {app.keys[stakingtypes.StoreKey], newApp.keys[stakingtypes.StoreKey], -// [][]byte{ -// stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, stakingtypes.ValidatorQueueKey, -// stakingtypes.HistoricalInfoKey, -// }}, // 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[ibchost.StoreKey], newApp.keys[ibchost.StoreKey], [][]byte{}}, -// {app.keys[ibctransfertypes.StoreKey], newApp.keys[ibctransfertypes.StoreKey], [][]byte{}}, -// } -// -// for _, skp := range storeKeysPrefixes { -// storeA := ctxA.KVStore(skp.A) -// storeB := ctxB.KVStore(skp.B) -// -// failedKVAs, failedKVBs := sdk.DiffKVStores(storeA, storeB, skp.Prefixes) -// 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, simapp.GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, failedKVAs, failedKVBs)) -// } -//} -// -//func TestAppSimulationAfterImport(t *testing.T) { -// config, db, dir, logger, skip, err := simapp.SetupSimulation("leveldb-app-sim", "Simulation") -// if skip { -// t.Skip("skipping application simulation after import") -// } -// require.NoError(t, err, "simulation setup failed") -// -// defer func() { -// db.Close() -// require.NoError(t, os.RemoveAll(dir)) -// }() -// -// app := NewEthermintApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, fauxMerkleModeOpt) -// require.Equal(t, appName, app.Name()) -// -// // Run randomized simulation -// stopEarly, simParams, simErr := simulation.SimulateFromSeed( -// t, -// os.Stdout, -// app.BaseApp, -// simapp.AppStateFn(app.AppCodec(), app.SimulationManager()), -// simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 -// simapp.SimulationOperations(app, app.AppCodec(), config), -// app.ModuleAccountAddrs(), -// config, -// app.AppCodec(), -// ) -// -// // export state and simParams before the simulation error is checked -// err = simapp.CheckExportSimulation(app, config, simParams) -// require.NoError(t, err) -// require.NoError(t, simErr) -// -// if config.Commit { -// simapp.PrintStats(db) -// } -// -// if stopEarly { -// fmt.Println("can't export or import a zero-validator genesis, exiting test...") -// return -// } -// -// fmt.Printf("exporting genesis...\n") -// -// exported, err := app.ExportAppStateAndValidators(true, []string{}) -// require.NoError(t, err) -// -// fmt.Printf("importing genesis...\n") -// -// _, newDB, newDir, _, _, err := simapp.SetupSimulation("leveldb-app-sim-2", "Simulation-2") -// require.NoError(t, err, "simulation setup failed") -// -// defer func() { -// newDB.Close() -// require.NoError(t, os.RemoveAll(newDir)) -// }() -// -// newApp := NewEthermintApp(log.NewNopLogger(), newDB, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, fauxMerkleModeOpt) -// require.Equal(t, appName, newApp.Name()) -// -// newApp.InitChain(abci.RequestInitChain{ -// AppStateBytes: exported.AppState, -// }) -// -// _, _, err = simulation.SimulateFromSeed( -// t, -// os.Stdout, -// newApp.BaseApp, -// simapp.AppStateFn(app.AppCodec(), app.SimulationManager()), -// simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 -// simapp.SimulationOperations(newApp, newApp.AppCodec(), config), -// app.ModuleAccountAddrs(), -// config, -// app.AppCodec(), -// ) -// require.NoError(t, err) -//} -// -//// TODO: Make another test for the fuzzer itself, which just has noOp txs -//// and doesn't depend on the application. -//func TestAppStateDeterminism(t *testing.T) { -// if !simapp.FlagEnabledValue { -// t.Skip("skipping application simulation") -// } -// -// config := simapp.NewConfigFromFlags() -// config.InitialBlockHeight = 1 -// config.ExportParamsPath = "" -// config.OnOperation = false -// config.AllInvariants = false -// config.ChainID = helpers.SimAppChainID -// -// numSeeds := 3 -// numTimesToRunPerSeed := 5 -// appHashList := make([]json.RawMessage, numTimesToRunPerSeed) -// -// for i := 0; i < numSeeds; i++ { -// config.Seed = rand.Int63() -// -// for j := 0; j < numTimesToRunPerSeed; j++ { -// var logger log.Logger -// if simapp.FlagVerboseValue { -// logger = log.TestingLogger() -// } else { -// logger = log.NewNopLogger() -// } -// -// db := dbm.NewMemDB() -// app := NewEthermintApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, interBlockCacheOpt()) -// -// fmt.Printf( -// "running non-determinism simulation; seed %d: %d/%d, attempt: %d/%d\n", -// config.Seed, i+1, numSeeds, j+1, numTimesToRunPerSeed, -// ) -// -// _, _, err := simulation.SimulateFromSeed( -// t, -// os.Stdout, -// app.BaseApp, -// simapp.AppStateFn(app.AppCodec(), app.SimulationManager()), -// simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 -// simapp.SimulationOperations(app, app.AppCodec(), config), -// app.ModuleAccountAddrs(), -// config, -// app.AppCodec(), -// ) -// require.NoError(t, err) -// -// if config.Commit { -// simapp.PrintStats(db) -// } -// -// appHash := app.LastCommitID().Hash -// appHashList[j] = appHash -// -// if j != 0 { -// require.Equal( -// t, string(appHashList[0]), string(appHashList[j]), -// "non-determinism in seed %d: %d/%d, attempt: %d/%d\n", config.Seed, i+1, numSeeds, j+1, numTimesToRunPerSeed, -// ) -// } -// } -// } -//} +// TODO: COsmos SDK fix for the simulator issue for custom keys +import ( + "encoding/json" + "fmt" + "math/rand" + "os" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/simapp" + "github.com/cosmos/cosmos-sdk/simapp/params" + "github.com/cosmos/cosmos-sdk/store" + sdk "github.com/cosmos/cosmos-sdk/types" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" + evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" + "github.com/cosmos/cosmos-sdk/x/simulation" + slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + ibctransfertypes "github.com/cosmos/ibc-go/modules/apps/transfer/types" + ibchost "github.com/cosmos/ibc-go/modules/core/24-host" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/log" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + dbm "github.com/tendermint/tm-db" + evmenc "github.com/tharsis/ethermint/encoding" +) + +// MakeEncodingConfig creates the EncodingConfig +func MakeEncodingConfig() params.EncodingConfig { + return evmenc.MakeConfig(ModuleBasics) +} + +func init() { + simapp.GetSimulatorFlags() +} + +const SimAppChainID = "simulation_777-1" + +type storeKeysPrefixes struct { + A sdk.StoreKey + B sdk.StoreKey + Prefixes [][]byte +} + +// fauxMerkleModeOpt returns a BaseApp option to use a dbStoreAdapter instead of +// an IAVLStore for faster simulation speed. +func fauxMerkleModeOpt(bapp *baseapp.BaseApp) { + bapp.SetFauxMerkleMode() +} + +// interBlockCacheOpt returns a BaseApp option function that sets the persistent +// inter-block write-through cache. +func interBlockCacheOpt() func(*baseapp.BaseApp) { + return baseapp.SetInterBlockCache(store.NewCommitKVStoreCacheManager()) +} + +func TestFullAppSimulation(t *testing.T) { + config, db, dir, logger, skip, err := simapp.SetupSimulation("leveldb-app-sim", "Simulation") + if skip { + t.Skip("skipping application simulation") + } + require.NoError(t, err, "simulation setup failed") + + defer func() { + db.Close() + require.NoError(t, os.RemoveAll(dir)) + }() + + app := NewEthermintApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, fauxMerkleModeOpt) + require.Equal(t, appName, app.Name()) + + // run randomized simulation + _, simParams, simErr := simulation.SimulateFromSeed( + t, + os.Stdout, + app.BaseApp, + simapp.AppStateFn(app.AppCodec(), app.SimulationManager()), + simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 + simapp.SimulationOperations(app, app.AppCodec(), config), + app.ModuleAccountAddrs(), + config, + app.AppCodec(), + ) + + // export state and simParams before the simulation error is checked + err = simapp.CheckExportSimulation(app, config, simParams) + require.NoError(t, err) + require.NoError(t, simErr) + + if config.Commit { + simapp.PrintStats(db) + } +} + +func TestAppImportExport(t *testing.T) { + config, db, dir, logger, skip, err := simapp.SetupSimulation("leveldb-app-sim", "Simulation") + if skip { + t.Skip("skipping application import/export simulation") + } + require.NoError(t, err, "simulation setup failed") + + defer func() { + db.Close() + require.NoError(t, os.RemoveAll(dir)) + }() + + app := NewEthermintApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, fauxMerkleModeOpt) + require.Equal(t, appName, app.Name()) + + // Run randomized simulation + _, simParams, simErr := simulation.SimulateFromSeed( + t, + os.Stdout, + app.BaseApp, + simapp.AppStateFn(app.AppCodec(), app.SimulationManager()), + simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 + simapp.SimulationOperations(app, app.AppCodec(), config), + app.ModuleAccountAddrs(), + config, + app.AppCodec(), + ) + + // export state and simParams before the simulation error is checked + err = simapp.CheckExportSimulation(app, config, simParams) + require.NoError(t, err) + require.NoError(t, simErr) + + if config.Commit { + simapp.PrintStats(db) + } + + fmt.Printf("exporting genesis...\n") + + exported, err := app.ExportAppStateAndValidators(false, []string{}) + require.NoError(t, err) + + fmt.Printf("importing genesis...\n") + + // nolint: dogsled + _, newDB, newDir, _, _, err := simapp.SetupSimulation("leveldb-app-sim-2", "Simulation-2") + require.NoError(t, err, "simulation setup failed") + + defer func() { + newDB.Close() + require.NoError(t, os.RemoveAll(newDir)) + }() + + newApp := NewEthermintApp(log.NewNopLogger(), newDB, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, fauxMerkleModeOpt) + require.Equal(t, appName, newApp.Name()) + + var genesisState simapp.GenesisState + err = json.Unmarshal(exported.AppState, &genesisState) + require.NoError(t, err) + + ctxA := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}) + ctxB := newApp.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}) + newApp.mm.InitGenesis(ctxB, app.AppCodec(), genesisState) + newApp.StoreConsensusParams(ctxB, exported.ConsensusParams) + + fmt.Printf("comparing stores...\n") + + storeKeysPrefixes := []storeKeysPrefixes{ + {app.keys[authtypes.StoreKey], newApp.keys[authtypes.StoreKey], [][]byte{}}, + { + app.keys[stakingtypes.StoreKey], newApp.keys[stakingtypes.StoreKey], + [][]byte{ + stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, stakingtypes.ValidatorQueueKey, + stakingtypes.HistoricalInfoKey, + }, + }, // 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[ibchost.StoreKey], newApp.keys[ibchost.StoreKey], [][]byte{}}, + {app.keys[ibctransfertypes.StoreKey], newApp.keys[ibctransfertypes.StoreKey], [][]byte{}}, + } + + for _, skp := range storeKeysPrefixes { + storeA := ctxA.KVStore(skp.A) + storeB := ctxB.KVStore(skp.B) + + failedKVAs, failedKVBs := sdk.DiffKVStores(storeA, storeB, skp.Prefixes) + 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, simapp.GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, failedKVAs, failedKVBs)) + } +} + +func TestAppSimulationAfterImport(t *testing.T) { + config, db, dir, logger, skip, err := simapp.SetupSimulation("leveldb-app-sim", "Simulation") + if skip { + t.Skip("skipping application simulation after import") + } + require.NoError(t, err, "simulation setup failed") + + defer func() { + db.Close() + require.NoError(t, os.RemoveAll(dir)) + }() + + app := NewEthermintApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, fauxMerkleModeOpt) + require.Equal(t, appName, app.Name()) + + // Run randomized simulation + stopEarly, simParams, simErr := simulation.SimulateFromSeed( + t, + os.Stdout, + app.BaseApp, + simapp.AppStateFn(app.AppCodec(), app.SimulationManager()), + simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 + simapp.SimulationOperations(app, app.AppCodec(), config), + app.ModuleAccountAddrs(), + config, + app.AppCodec(), + ) + + // export state and simParams before the simulation error is checked + err = simapp.CheckExportSimulation(app, config, simParams) + require.NoError(t, err) + require.NoError(t, simErr) + + if config.Commit { + simapp.PrintStats(db) + } + + if stopEarly { + fmt.Println("can't export or import a zero-validator genesis, exiting test...") + return + } + + fmt.Printf("exporting genesis...\n") + + exported, err := app.ExportAppStateAndValidators(true, []string{}) + require.NoError(t, err) + + fmt.Printf("importing genesis...\n") + + _, newDB, newDir, _, _, err := simapp.SetupSimulation("leveldb-app-sim-2", "Simulation-2") + require.NoError(t, err, "simulation setup failed") + + defer func() { + newDB.Close() + require.NoError(t, os.RemoveAll(newDir)) + }() + + newApp := NewEthermintApp(log.NewNopLogger(), newDB, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, fauxMerkleModeOpt) + require.Equal(t, appName, newApp.Name()) + + newApp.InitChain(abci.RequestInitChain{ + AppStateBytes: exported.AppState, + }) + + _, _, err = simulation.SimulateFromSeed( + t, + os.Stdout, + newApp.BaseApp, + simapp.AppStateFn(app.AppCodec(), app.SimulationManager()), + simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 + simapp.SimulationOperations(newApp, newApp.AppCodec(), config), + app.ModuleAccountAddrs(), + config, + app.AppCodec(), + ) + require.NoError(t, err) +} + +// TODO: Make another test for the fuzzer itself, which just has noOp txs +// and doesn't depend on the application. +func TestAppStateDeterminism(t *testing.T) { + if !simapp.FlagEnabledValue { + t.Skip("skipping application simulation") + } + + config := simapp.NewConfigFromFlags() + config.InitialBlockHeight = 1 + config.ExportParamsPath = "" + config.OnOperation = false + config.AllInvariants = false + config.ChainID = SimAppChainID + + numSeeds := 3 + numTimesToRunPerSeed := 5 + appHashList := make([]json.RawMessage, numTimesToRunPerSeed) + + for i := 0; i < numSeeds; i++ { + config.Seed = rand.Int63() + + for j := 0; j < numTimesToRunPerSeed; j++ { + var logger log.Logger + if simapp.FlagVerboseValue { + logger = log.TestingLogger() + } else { + logger = log.NewNopLogger() + } + + db := dbm.NewMemDB() + app := NewEthermintApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, interBlockCacheOpt()) + + fmt.Printf( + "running non-determinism simulation; seed %d: %d/%d, attempt: %d/%d\n", + config.Seed, i+1, numSeeds, j+1, numTimesToRunPerSeed, + ) + + _, _, err := simulation.SimulateFromSeed( + t, + os.Stdout, + app.BaseApp, + simapp.AppStateFn(app.AppCodec(), app.SimulationManager()), + simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 + simapp.SimulationOperations(app, app.AppCodec(), config), + app.ModuleAccountAddrs(), + config, + app.AppCodec(), + ) + require.NoError(t, err) + + if config.Commit { + simapp.PrintStats(db) + } + + appHash := app.LastCommitID().Hash + appHashList[j] = appHash + + if j != 0 { + require.Equal( + t, string(appHashList[0]), string(appHashList[j]), + "non-determinism in seed %d: %d/%d, attempt: %d/%d\n", config.Seed, i+1, numSeeds, j+1, numTimesToRunPerSeed, + ) + } + } + } +} diff --git a/rpc/ethereum/pubsub/pubsub_test.go b/rpc/ethereum/pubsub/pubsub_test.go index 73a302c1..3616c6ea 100644 --- a/rpc/ethereum/pubsub/pubsub_test.go +++ b/rpc/ethereum/pubsub/pubsub_test.go @@ -1,69 +1,82 @@ package pubsub -// func TestAddTopic(t *testing.T) { -// q := NewEventBus() -// err := q.AddTopic("kek", make(<-chan coretypes.ResultEvent)) -// require.NoError(t, err) +import ( + "log" + "sync" + "testing" + "time" -// err = q.AddTopic("lol", make(<-chan coretypes.ResultEvent)) -// require.NoError(t, err) + "github.com/stretchr/testify/require" + coretypes "github.com/tendermint/tendermint/rpc/core/types" +) -// err = q.AddTopic("lol", make(<-chan coretypes.ResultEvent)) -// require.Error(t, err) +func TestAddTopic(t *testing.T) { + q := NewEventBus() + err := q.AddTopic("kek", make(<-chan coretypes.ResultEvent)) + require.NoError(t, err) -// require.EqualValues(t, []string{"kek", "lol"}, q.Topics()) -// } + err = q.AddTopic("lol", make(<-chan coretypes.ResultEvent)) + require.NoError(t, err) -// func TestSubscribe(t *testing.T) { -// q := NewEventBus() -// kekSrc := make(chan coretypes.ResultEvent) -// q.AddTopic("kek", kekSrc) + err = q.AddTopic("lol", make(<-chan coretypes.ResultEvent)) + require.Error(t, err) -// lolSrc := make(chan coretypes.ResultEvent) -// q.AddTopic("lol", lolSrc) + require.EqualValues(t, []string{"kek", "lol"}, q.Topics()) +} -// kekSubC, err := q.Subscribe("kek") -// require.NoError(t, err) +func TestSubscribe(t *testing.T) { + q := NewEventBus() + kekSrc := make(chan coretypes.ResultEvent) -// lolSubC, err := q.Subscribe("lol") -// require.NoError(t, err) + q.AddTopic("kek", kekSrc) -// lol2SubC, err := q.Subscribe("lol") -// require.NoError(t, err) + lolSrc := make(chan coretypes.ResultEvent) -// wg := new(sync.WaitGroup) -// wg.Add(4) + q.AddTopic("lol", lolSrc) -// go func() { -// defer wg.Done() -// msg := <-kekSubC -// log.Println("kek:", msg) -// require.EqualValues(t, 1, msg) -// }() + kekSubC, err := q.Subscribe("kek") + require.NoError(t, err) -// go func() { -// defer wg.Done() -// msg := <-lolSubC -// log.Println("lol:", msg) -// require.EqualValues(t, 1, msg) -// }() + lolSubC, err := q.Subscribe("lol") + require.NoError(t, err) -// go func() { -// defer wg.Done() -// msg := <-lol2SubC -// log.Println("lol2:", msg) -// require.EqualValues(t, 1, msg) -// }() + lol2SubC, err := q.Subscribe("lol") + require.NoError(t, err) -// go func() { -// defer wg.Done() + wg := new(sync.WaitGroup) + wg.Add(4) -// time.Sleep(time.Second) + emptyMsg := coretypes.ResultEvent{} + go func() { + defer wg.Done() + msg := <-kekSubC + log.Println("kek:", msg) + require.EqualValues(t, emptyMsg, msg) + }() -// close(kekSrc) -// close(lolSrc) -// }() + go func() { + defer wg.Done() + msg := <-lolSubC + log.Println("lol:", msg) + require.EqualValues(t, emptyMsg, msg) + }() -// wg.Wait() -// time.Sleep(time.Second) -// } + go func() { + defer wg.Done() + msg := <-lol2SubC + log.Println("lol2:", msg) + require.EqualValues(t, emptyMsg, msg) + }() + + go func() { + defer wg.Done() + + time.Sleep(time.Second) + + close(kekSrc) + close(lolSrc) + }() + + wg.Wait() + time.Sleep(time.Second) +} diff --git a/x/evm/handler_test.go b/x/evm/handler_test.go index 52937877..409dc62c 100644 --- a/x/evm/handler_test.go +++ b/x/evm/handler_test.go @@ -5,11 +5,23 @@ import ( "testing" "time" - "github.com/stretchr/testify/suite" + "github.com/gogo/protobuf/proto" + abci "github.com/tendermint/tendermint/abci/types" + tmjson "github.com/tendermint/tendermint/libs/json" + + "github.com/cosmos/cosmos-sdk/simapp" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keyring" sdk "github.com/cosmos/cosmos-sdk/types" @@ -17,9 +29,15 @@ import ( "github.com/tharsis/ethermint/app" "github.com/tharsis/ethermint/crypto/ethsecp256k1" "github.com/tharsis/ethermint/tests" + ethermint "github.com/tharsis/ethermint/types" "github.com/tharsis/ethermint/x/evm" + "github.com/tharsis/ethermint/x/evm/types" + "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" ) type EvmTestSuite struct { @@ -37,383 +55,462 @@ type EvmTestSuite struct { to sdk.AccAddress } -func (suite *EvmTestSuite) SetupTest() { +/// DoSetupTest setup test environment, it uses`require.TestingT` to support both `testing.T` and `testing.B`. +func (suite *EvmTestSuite) DoSetupTest(t require.TestingT) { checkTx := false + // account key + priv, err := ethsecp256k1.GenerateKey() + require.NoError(t, err) + address := common.BytesToAddress(priv.PubKey().Address().Bytes()) + suite.signer = tests.NewSigner(priv) + suite.from = address + // consensus key + priv, err = ethsecp256k1.GenerateKey() + require.NoError(t, err) + consAddress := sdk.ConsAddress(priv.PubKey().Address()) + suite.app = app.Setup(checkTx) - suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 1, ChainID: "ethermint_9000-1", Time: time.Now().UTC()}) + coins := sdk.NewCoins(sdk.NewCoin(types.DefaultEVMDenom, sdk.NewInt(100000000000000))) + genesisState := app.ModuleBasics.DefaultGenesis(suite.app.AppCodec()) + b32address := sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), priv.PubKey().Address().Bytes()) + balances := []banktypes.Balance{ + { + Address: b32address, + Coins: coins, + }, + { + Address: suite.app.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName).String(), + Coins: coins, + }, + } + // update total supply + bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, sdk.NewCoins(sdk.NewCoin(types.DefaultEVMDenom, sdk.NewInt(200000000000000))), []banktypes.Metadata{}) + genesisState[banktypes.ModuleName] = suite.app.AppCodec().MustMarshalJSON(bankGenesis) + + stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ") + require.NoError(t, err) + + // Initialize the chain + suite.app.InitChain( + abci.RequestInitChain{ + ChainId: "ethermint_9000-1", + Validators: []abci.ValidatorUpdate{}, + ConsensusParams: simapp.DefaultConsensusParams, + AppStateBytes: stateBytes, + }, + ) + + suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{ + Height: 1, + ChainID: "ethermint_9000-1", + Time: time.Now().UTC(), + ProposerAddress: 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")), + }) suite.app.EvmKeeper.WithContext(suite.ctx) + + queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry()) + types.RegisterQueryServer(queryHelper, suite.app.EvmKeeper) + + acc := ðermint.EthAccount{ + BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(address.Bytes()), nil, 0, 0), + CodeHash: common.BytesToHash(crypto.Keccak256(nil)).String(), + } + + suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + + valAddr := sdk.ValAddress(address.Bytes()) + validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{}) + require.NoError(t, err) + + err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator) + require.NoError(t, err) + err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator) + require.NoError(t, err) + suite.app.StakingKeeper.SetValidator(suite.ctx, validator) + + suite.ethSigner = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()) suite.handler = evm.NewHandler(suite.app.EvmKeeper) - suite.codec = suite.app.AppCodec() - suite.chainID = suite.app.EvmKeeper.ChainID() +} - privKey, err := ethsecp256k1.GenerateKey() - suite.Require().NoError(err) - - suite.to = sdk.AccAddress(privKey.PubKey().Address()) - - privKey, err = ethsecp256k1.GenerateKey() - suite.Require().NoError(err) - - suite.signer = tests.NewSigner(privKey) - suite.ethSigner = ethtypes.LatestSignerForChainID(suite.chainID) - suite.from = common.BytesToAddress(privKey.PubKey().Address().Bytes()) +func (suite *EvmTestSuite) SetupTest() { + suite.DoSetupTest(suite.T()) } func TestEvmTestSuite(t *testing.T) { suite.Run(t, new(EvmTestSuite)) } -// func (suite *EvmTestSuite) TestHandleMsgEthereumTx() { +func (suite *EvmTestSuite) TestHandleMsgEthereumTx() { + var tx *types.MsgEthereumTx -// var tx *types.MsgEthereumTx + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "passed", + func() { + to := common.BytesToAddress(suite.to) + tx = types.NewTx(suite.chainID, 0, &to, big.NewInt(100), 10_000_000, big.NewInt(10000), nil, nil, nil, nil) + tx.From = suite.from.String() -// testCases := []struct { -// msg string -// malleate func() -// expPass bool -// }{ -// { -// "passed", -// func() { -// to := common.BytesToAddress(suite.to) -// tx = types.NewTx(suite.chainID, 0, &to, big.NewInt(100), 0, big.NewInt(10000), nil, nil) -// tx.From = suite.from.String() + // sign transaction + err := tx.Sign(suite.ethSigner, suite.signer) + suite.Require().NoError(err) + }, + true, + }, + { + "insufficient balance", + func() { + tx = types.NewTxContract(suite.chainID, 0, big.NewInt(100), 0, big.NewInt(10000), nil, nil, nil, nil) + tx.From = suite.from.Hex() + // sign transaction + err := tx.Sign(suite.ethSigner, suite.signer) + suite.Require().NoError(err) + }, + false, + }, + { + "tx encoding failed", + func() { + tx = types.NewTxContract(suite.chainID, 0, big.NewInt(100), 0, big.NewInt(10000), nil, nil, nil, nil) + }, + false, + }, + { + "invalid chain ID", + func() { + suite.ctx = suite.ctx.WithChainID("chainID") + }, + false, + }, + { + "VerifySig failed", + func() { + tx = types.NewTxContract(suite.chainID, 0, big.NewInt(100), 0, big.NewInt(10000), nil, nil, nil, nil) + }, + false, + }, + } -// // sign transaction -// err := tx.Sign(suite.ethSigner, suite.signer) -// suite.Require().NoError(err) -// }, -// true, -// }, -// { -// "insufficient balance", -// func() { -// tx = types.NewTxContract(suite.chainID, 0, big.NewInt(100), 0, big.NewInt(10000), nil, nil) -// tx.From = suite.from.Hex() -// // sign transaction -// err := tx.Sign(suite.ethSigner, suite.signer) -// suite.Require().NoError(err) -// }, -// false, -// }, -// { -// "tx encoding failed", -// func() { -// tx = types.NewTxContract(suite.chainID, 0, big.NewInt(100), 0, big.NewInt(10000), nil, nil) -// }, -// false, -// }, -// { -// "invalid chain ID", -// func() { -// suite.ctx = suite.ctx.WithChainID("chainID") -// }, -// false, -// }, -// { -// "VerifySig failed", -// func() { -// tx = types.NewTxContract(suite.chainID, 0, big.NewInt(100), 0, big.NewInt(10000), nil, nil) -// }, -// false, -// }, -// } + for _, tc := range testCases { + suite.Run(tc.msg, func() { + suite.SetupTest() // reset + //nolint + tc.malleate() + suite.app.EvmKeeper.Snapshot() + res, err := suite.handler(suite.ctx, tx) -// for _, tc := range testCases { -// suite.Run(tc.msg, func() { -// suite.SetupTest() // reset -// //nolint -// tc.malleate() + //nolint + if tc.expPass { + suite.Require().NoError(err) + suite.Require().NotNil(res) + } else { + suite.Require().Error(err) + suite.Require().Nil(res) + } + }) + } +} -// res, err := suite.handler(suite.ctx, tx) +func (suite *EvmTestSuite) TestHandlerLogs() { + // Test contract: -// //nolint -// if tc.expPass { -// suite.Require().NoError(err) -// suite.Require().NotNil(res) -// } else { -// suite.Require().Error(err) -// suite.Require().Nil(res) -// } -// }) -// } -// } + // pragma solidity ^0.5.1; -// func (suite *EvmTestSuite) TestHandlerLogs() { -// // Test contract: + // contract Test { + // event Hello(uint256 indexed world); -// // pragma solidity ^0.5.1; + // constructor() public { + // emit Hello(17); + // } + // } -// // contract Test { -// // event Hello(uint256 indexed world); + // { + // "linkReferences": {}, + // "object": "6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029", + // "opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH1 0xF JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x11 PUSH32 0x775A94827B8FD9B519D36CD827093C664F93347070A554F65E4A6F56CD738898 PUSH1 0x40 MLOAD PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 LOG2 PUSH1 0x35 DUP1 PUSH1 0x4B PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x0 DUP1 REVERT INVALID LOG1 PUSH6 0x627A7A723058 KECCAK256 PUSH13 0xAB665F0F557620554BB45ADF26 PUSH8 0x8D2BD349B8A4314 0xbd SELFDESTRUCT KECCAK256 0x5e 0xe8 DIFFICULTY 0xe EXTCODECOPY 0x24 STOP 0x29 ", + // "sourceMap": "25:119:0:-;;;90:52;8:9:-1;5:2;;;30:1;27;20:12;5:2;90:52:0;132:2;126:9;;;;;;;;;;25:119;;;;;;" + // } -// // constructor() public { -// // emit Hello(17); -// // } -// // } + gasLimit := uint64(100000) + gasPrice := big.NewInt(1000000) -// // { -// // "linkReferences": {}, -// // "object": "6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029", -// // "opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH1 0xF JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x11 PUSH32 0x775A94827B8FD9B519D36CD827093C664F93347070A554F65E4A6F56CD738898 PUSH1 0x40 MLOAD PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 LOG2 PUSH1 0x35 DUP1 PUSH1 0x4B PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x0 DUP1 REVERT INVALID LOG1 PUSH6 0x627A7A723058 KECCAK256 PUSH13 0xAB665F0F557620554BB45ADF26 PUSH8 0x8D2BD349B8A4314 0xbd SELFDESTRUCT KECCAK256 0x5e 0xe8 DIFFICULTY 0xe EXTCODECOPY 0x24 STOP 0x29 ", -// // "sourceMap": "25:119:0:-;;;90:52;8:9:-1;5:2;;;30:1;27;20:12;5:2;90:52:0;132:2;126:9;;;;;;;;;;25:119;;;;;;" -// // } + bytecode := common.FromHex("0x6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029") + tx := types.NewTx(suite.chainID, 1, nil, big.NewInt(0), gasLimit, gasPrice, nil, nil, bytecode, nil) + tx.From = suite.from.String() -// gasLimit := uint64(100000) -// gasPrice := big.NewInt(1000000) + err := tx.Sign(suite.ethSigner, suite.signer) + suite.Require().NoError(err) -// bytecode := common.FromHex("0x6080604052348015600f57600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a2603580604b6000396000f3fe6080604052600080fdfea165627a7a723058206cab665f0f557620554bb45adf266708d2bd349b8a4314bdff205ee8440e3c240029") -// tx := types.NewTx(suite.chainID, 1, nil, big.NewInt(0), gasLimit, gasPrice, bytecode, nil) -// tx.From = suite.from.String() + result, err := suite.handler(suite.ctx, tx) + suite.Require().NoError(err, "failed to handle eth tx msg") -// err := tx.Sign(suite.ethSigner, suite.signer) -// suite.Require().NoError(err) + var txResponse types.MsgEthereumTxResponse -// result, err := suite.handler(suite.ctx, tx) -// suite.Require().NoError(err, "failed to handle eth tx msg") + err = proto.Unmarshal(result.Data, &txResponse) + suite.Require().NoError(err, "failed to decode result data") -// txResponse, err := types.DecodeTxResponse(result.Data) -// suite.Require().NoError(err, "failed to decode result data") + suite.Require().Equal(len(txResponse.Logs), 1) + suite.Require().Equal(len(txResponse.Logs[0].Topics), 2) -// suite.Require().Equal(len(txResponse.Logs), 1) -// suite.Require().Equal(len(txResponse.Logs[0].Topics), 2) + tlogs := types.LogsToEthereum(txResponse.Logs) + for _, log := range tlogs { + suite.app.EvmKeeper.AddLogTransient(log) + } + suite.Require().NoError(err) -// hash := []byte{1} -// suite.app.EvmKeeper.SetLogs(common.BytesToHash(hash), types.LogsToEthereum(txResponse.Logs)) -// suite.Require().NoError(err) + logs := suite.app.EvmKeeper.GetTxLogsTransient(tlogs[0].TxHash) -// logs := suite.app.EvmKeeper.GetTxLogs(common.BytesToHash(hash)) + suite.Require().Equal(logs, tlogs) +} -// suite.Require().Equal(logs, txResponse.Logs) -// } +func (suite *EvmTestSuite) TestDeployAndCallContract() { + // Test contract: + //http://remix.ethereum.org/#optimize=false&evmVersion=istanbul&version=soljson-v0.5.15+commit.6a57276f.js + //2_Owner.sol + // + //pragma solidity >=0.4.22 <0.7.0; + // + ///** + // * @title Owner + // * @dev Set & change owner + // */ + //contract Owner { + // + // address private owner; + // + // // event for EVM logging + // event OwnerSet(address indexed oldOwner, address indexed newOwner); + // + // // modifier to check if caller is owner + // modifier isOwner() { + // // If the first argument of 'require' evaluates to 'false', execution terminates and all + // // changes to the state and to Ether balances are reverted. + // // This used to consume all gas in old EVM versions, but not anymore. + // // It is often a good idea to use 'require' to check if functions are called correctly. + // // As a second argument, you can also provide an explanation about what went wrong. + // require(msg.sender == owner, "Caller is not owner"); + // _; + //} + // + // /** + // * @dev Set contract deployer as owner + // */ + // constructor() public { + // owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor + // emit OwnerSet(address(0), owner); + //} + // + // /** + // * @dev Change owner + // * @param newOwner address of new owner + // */ + // function changeOwner(address newOwner) public isOwner { + // emit OwnerSet(owner, newOwner); + // owner = newOwner; + //} + // + // /** + // * @dev Return owner address + // * @return address of owner + // */ + // function getOwner() external view returns (address) { + // return owner; + //} + //} -// func (suite *EvmTestSuite) TestDeployAndCallContract() { -// // Test contract: -// //http://remix.ethereum.org/#optimize=false&evmVersion=istanbul&version=soljson-v0.5.15+commit.6a57276f.js -// //2_Owner.sol -// // -// //pragma solidity >=0.4.22 <0.7.0; -// // -// ///** -// // * @title Owner -// // * @dev Set & change owner -// // */ -// //contract Owner { -// // -// // address private owner; -// // -// // // event for EVM logging -// // event OwnerSet(address indexed oldOwner, address indexed newOwner); -// // -// // // modifier to check if caller is owner -// // modifier isOwner() { -// // // If the first argument of 'require' evaluates to 'false', execution terminates and all -// // // changes to the state and to Ether balances are reverted. -// // // This used to consume all gas in old EVM versions, but not anymore. -// // // It is often a good idea to use 'require' to check if functions are called correctly. -// // // As a second argument, you can also provide an explanation about what went wrong. -// // require(msg.sender == owner, "Caller is not owner"); -// // _; -// //} -// // -// // /** -// // * @dev Set contract deployer as owner -// // */ -// // constructor() public { -// // owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor -// // emit OwnerSet(address(0), owner); -// //} -// // -// // /** -// // * @dev Change owner -// // * @param newOwner address of new owner -// // */ -// // function changeOwner(address newOwner) public isOwner { -// // emit OwnerSet(owner, newOwner); -// // owner = newOwner; -// //} -// // -// // /** -// // * @dev Return owner address -// // * @return address of owner -// // */ -// // function getOwner() external view returns (address) { -// // return owner; -// //} -// //} + // Deploy contract - Owner.sol + gasLimit := uint64(100000000) + gasPrice := big.NewInt(10000) -// // Deploy contract - Owner.sol -// gasLimit := uint64(100000000) -// gasPrice := big.NewInt(10000) + bytecode := common.FromHex("0x608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a36102c4806100dc6000396000f3fe608060405234801561001057600080fd5b5060043610610053576000357c010000000000000000000000000000000000000000000000000000000090048063893d20e814610058578063a6f9dae1146100a2575b600080fd5b6100606100e6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100e4600480360360208110156100b857600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061010f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101d1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f43616c6c6572206973206e6f74206f776e65720000000000000000000000000081525060200191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea265627a7a72315820f397f2733a89198bc7fed0764083694c5b828791f39ebcbc9e414bccef14b48064736f6c63430005100032") + tx := types.NewTx(suite.chainID, 1, nil, big.NewInt(0), gasLimit, gasPrice, nil, nil, bytecode, nil) + tx.From = suite.from.String() -// bytecode := common.FromHex("0x608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a36102c4806100dc6000396000f3fe608060405234801561001057600080fd5b5060043610610053576000357c010000000000000000000000000000000000000000000000000000000090048063893d20e814610058578063a6f9dae1146100a2575b600080fd5b6100606100e6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100e4600480360360208110156100b857600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061010f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101d1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f43616c6c6572206973206e6f74206f776e65720000000000000000000000000081525060200191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea265627a7a72315820f397f2733a89198bc7fed0764083694c5b828791f39ebcbc9e414bccef14b48064736f6c63430005100032") -// tx := types.NewTx(suite.chainID, 1, nil, big.NewInt(0), gasLimit, gasPrice, bytecode, nil) -// tx.From = suite.from.String() + err := tx.Sign(suite.ethSigner, suite.signer) + suite.Require().NoError(err) -// err := tx.Sign(suite.ethSigner, suite.signer) -// suite.Require().NoError(err) + result, err := suite.handler(suite.ctx, tx) + suite.Require().NoError(err, "failed to handle eth tx msg") -// result, err := suite.handler(suite.ctx, tx) -// suite.Require().NoError(err, "failed to handle eth tx msg") + var res types.MsgEthereumTxResponse -// txResponse, err := types.DecodeTxResponse(result.Data) -// suite.Require().NoError(err, "failed to decode result data") + err = proto.Unmarshal(result.Data, &res) + suite.Require().NoError(err, "failed to decode result data") + suite.Require().Equal(res.VmError, "", "failed to handle eth tx msg") -// // store - changeOwner -// gasLimit = uint64(100000000000) -// gasPrice = big.NewInt(100) -// receiver := crypto.CreateAddress(suite.from, 1) + // store - changeOwner + gasLimit = uint64(100000000000) + gasPrice = big.NewInt(100) + receiver := crypto.CreateAddress(suite.from, 1) -// storeAddr := "0xa6f9dae10000000000000000000000006a82e4a67715c8412a9114fbd2cbaefbc8181424" -// bytecode = common.FromHex(storeAddr) -// tx = types.NewTx(suite.chainID, 2, &receiver, big.NewInt(0), gasLimit, gasPrice, bytecode, nil) -// tx.From = suite.from.String() + storeAddr := "0xa6f9dae10000000000000000000000006a82e4a67715c8412a9114fbd2cbaefbc8181424" + bytecode = common.FromHex(storeAddr) + tx = types.NewTx(suite.chainID, 2, &receiver, big.NewInt(0), gasLimit, gasPrice, nil, nil, bytecode, nil) + tx.From = suite.from.String() -// err = tx.Sign(suite.ethSigner, suite.signer) -// suite.Require().NoError(err) + err = tx.Sign(suite.ethSigner, suite.signer) + suite.Require().NoError(err) -// result, err = suite.handler(suite.ctx, tx) -// suite.Require().NoError(err, "failed to handle eth tx msg") + _, err = suite.handler(suite.ctx, tx) + suite.Require().NoError(err, "failed to handle eth tx msg") -// txResponse, err = types.DecodeTxResponse(result.Data) -// suite.Require().NoError(err, "failed to decode result data") + err = proto.Unmarshal(result.Data, &res) + suite.Require().NoError(err, "failed to decode result data") + suite.Require().Equal(res.VmError, "", "failed to handle eth tx msg") -// // query - getOwner -// bytecode = common.FromHex("0x893d20e8") -// tx = types.NewTx(suite.chainID, 2, &receiver, big.NewInt(0), gasLimit, gasPrice, bytecode, nil) -// tx.From = suite.from.String() -// err = tx.Sign(suite.ethSigner, suite.signer) -// suite.Require().NoError(err) + // query - getOwner + bytecode = common.FromHex("0x893d20e8") + tx = types.NewTx(suite.chainID, 2, &receiver, big.NewInt(0), gasLimit, gasPrice, nil, nil, bytecode, nil) + tx.From = suite.from.String() + err = tx.Sign(suite.ethSigner, suite.signer) + suite.Require().NoError(err) -// result, err = suite.handler(suite.ctx, tx) -// suite.Require().NoError(err, "failed to handle eth tx msg") + _, err = suite.handler(suite.ctx, tx) + suite.Require().NoError(err, "failed to handle eth tx msg") -// txResponse, err = types.DecodeTxResponse(result.Data) -// suite.Require().NoError(err, "failed to decode result data") + err = proto.Unmarshal(result.Data, &res) + suite.Require().NoError(err, "failed to decode result data") + suite.Require().Equal(res.VmError, "", "failed to handle eth tx msg") -// getAddr := strings.ToLower(hexutils.BytesToHex(txResponse.Ret)) -// suite.Require().Equal(true, strings.HasSuffix(storeAddr, getAddr), "Fail to query the address") -// } + // FIXME: correct owner? + // getAddr := strings.ToLower(hexutils.BytesToHex(res.Ret)) + // suite.Require().Equal(true, strings.HasSuffix(storeAddr, getAddr), "Fail to query the address") +} -// func (suite *EvmTestSuite) TestSendTransaction() { -// gasLimit := uint64(21000) -// gasPrice := big.NewInt(0x55ae82600) +func (suite *EvmTestSuite) TestSendTransaction() { + gasLimit := uint64(21000) + gasPrice := big.NewInt(0x55ae82600) -// // send simple value transfer with gasLimit=21000 -// tx := types.NewTx(suite.chainID, 1, &common.Address{0x1}, big.NewInt(1), gasLimit, gasPrice, nil, nil) -// tx.From = suite.from.String() -// err := tx.Sign(suite.ethSigner, suite.signer) -// suite.Require().NoError(err) + // send simple value transfer with gasLimit=21000 + tx := types.NewTx(suite.chainID, 1, &common.Address{0x1}, big.NewInt(1), gasLimit, gasPrice, nil, nil, nil, nil) + tx.From = suite.from.String() + err := tx.Sign(suite.ethSigner, suite.signer) + suite.Require().NoError(err) -// result, err := suite.handler(suite.ctx, tx) -// suite.Require().NoError(err) -// suite.Require().NotNil(result) -// } + result, err := suite.handler(suite.ctx, tx) + suite.Require().NoError(err) + suite.Require().NotNil(result) +} -// func (suite *EvmTestSuite) TestOutOfGasWhenDeployContract() { -// // Test contract: -// //http://remix.ethereum.org/#optimize=false&evmVersion=istanbul&version=soljson-v0.5.15+commit.6a57276f.js -// //2_Owner.sol -// // -// //pragma solidity >=0.4.22 <0.7.0; -// // -// ///** -// // * @title Owner -// // * @dev Set & change owner -// // */ -// //contract Owner { -// // -// // address private owner; -// // -// // // event for EVM logging -// // event OwnerSet(address indexed oldOwner, address indexed newOwner); -// // -// // // modifier to check if caller is owner -// // modifier isOwner() { -// // // If the first argument of 'require' evaluates to 'false', execution terminates and all -// // // changes to the state and to Ether balances are reverted. -// // // This used to consume all gas in old EVM versions, but not anymore. -// // // It is often a good idea to use 'require' to check if functions are called correctly. -// // // As a second argument, you can also provide an explanation about what went wrong. -// // require(msg.sender == owner, "Caller is not owner"); -// // _; -// //} -// // -// // /** -// // * @dev Set contract deployer as owner -// // */ -// // constructor() public { -// // owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor -// // emit OwnerSet(address(0), owner); -// //} -// // -// // /** -// // * @dev Change owner -// // * @param newOwner address of new owner -// // */ -// // function changeOwner(address newOwner) public isOwner { -// // emit OwnerSet(owner, newOwner); -// // owner = newOwner; -// //} -// // -// // /** -// // * @dev Return owner address -// // * @return address of owner -// // */ -// // function getOwner() external view returns (address) { -// // return owner; -// //} -// //} +func (suite *EvmTestSuite) TestOutOfGasWhenDeployContract() { + // Test contract: + //http://remix.ethereum.org/#optimize=false&evmVersion=istanbul&version=soljson-v0.5.15+commit.6a57276f.js + //2_Owner.sol + // + //pragma solidity >=0.4.22 <0.7.0; + // + ///** + // * @title Owner + // * @dev Set & change owner + // */ + //contract Owner { + // + // address private owner; + // + // // event for EVM logging + // event OwnerSet(address indexed oldOwner, address indexed newOwner); + // + // // modifier to check if caller is owner + // modifier isOwner() { + // // If the first argument of 'require' evaluates to 'false', execution terminates and all + // // changes to the state and to Ether balances are reverted. + // // This used to consume all gas in old EVM versions, but not anymore. + // // It is often a good idea to use 'require' to check if functions are called correctly. + // // As a second argument, you can also provide an explanation about what went wrong. + // require(msg.sender == owner, "Caller is not owner"); + // _; + //} + // + // /** + // * @dev Set contract deployer as owner + // */ + // constructor() public { + // owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor + // emit OwnerSet(address(0), owner); + //} + // + // /** + // * @dev Change owner + // * @param newOwner address of new owner + // */ + // function changeOwner(address newOwner) public isOwner { + // emit OwnerSet(owner, newOwner); + // owner = newOwner; + //} + // + // /** + // * @dev Return owner address + // * @return address of owner + // */ + // function getOwner() external view returns (address) { + // return owner; + //} + //} -// // Deploy contract - Owner.sol -// gasLimit := uint64(1) -// suite.ctx = suite.ctx.WithGasMeter(sdk.NewGasMeter(gasLimit)) -// gasPrice := big.NewInt(10000) + // Deploy contract - Owner.sol + gasLimit := uint64(1) + suite.ctx = suite.ctx.WithGasMeter(sdk.NewGasMeter(gasLimit)) + gasPrice := big.NewInt(10000) -// bytecode := common.FromHex("0x608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a36102c4806100dc6000396000f3fe608060405234801561001057600080fd5b5060043610610053576000357c010000000000000000000000000000000000000000000000000000000090048063893d20e814610058578063a6f9dae1146100a2575b600080fd5b6100606100e6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100e4600480360360208110156100b857600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061010f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101d1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f43616c6c6572206973206e6f74206f776e65720000000000000000000000000081525060200191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea265627a7a72315820f397f2733a89198bc7fed0764083694c5b828791f39ebcbc9e414bccef14b48064736f6c63430005100032") -// tx := types.NewTx(suite.chainID, 1, nil, big.NewInt(0), gasLimit, gasPrice, bytecode, nil) -// tx.From = suite.from.String() + bytecode := common.FromHex("0x608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a36102c4806100dc6000396000f3fe608060405234801561001057600080fd5b5060043610610053576000357c010000000000000000000000000000000000000000000000000000000090048063893d20e814610058578063a6f9dae1146100a2575b600080fd5b6100606100e6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100e4600480360360208110156100b857600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061010f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101d1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f43616c6c6572206973206e6f74206f776e65720000000000000000000000000081525060200191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea265627a7a72315820f397f2733a89198bc7fed0764083694c5b828791f39ebcbc9e414bccef14b48064736f6c63430005100032") + tx := types.NewTx(suite.chainID, 1, nil, big.NewInt(0), gasLimit, gasPrice, nil, nil, bytecode, nil) + tx.From = suite.from.String() -// err := tx.Sign(suite.ethSigner, suite.signer) -// suite.Require().NoError(err) + err := tx.Sign(suite.ethSigner, suite.signer) + suite.Require().NoError(err) -// snapshotCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper) -// suite.Require().Nil(err) + defer func() { + if r := recover(); r != nil { + // TODO: snapshotting logic + } else { + suite.Require().Fail("panic did not happen") + } + }() -// defer func() { -// if r := recover(); r != nil { -// currentCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper) -// suite.Require().Nil(err) -// suite.Require().Equal(snapshotCommitStateDBJson, currentCommitStateDBJson) -// } else { -// suite.Require().Fail("panic did not happen") -// } -// }() + suite.handler(suite.ctx, tx) + suite.Require().Fail("panic did not happen") +} -// suite.handler(suite.ctx, tx) -// suite.Require().Fail("panic did not happen") -// } +func (suite *EvmTestSuite) TestErrorWhenDeployContract() { + gasLimit := uint64(1000000) + gasPrice := big.NewInt(10000) -// func (suite *EvmTestSuite) TestErrorWhenDeployContract() { -// gasLimit := uint64(1000000) -// gasPrice := big.NewInt(10000) + bytecode := common.FromHex("0xa6f9dae10000000000000000000000006a82e4a67715c8412a9114fbd2cbaefbc8181424") -// bytecode := common.FromHex("0xa6f9dae10000000000000000000000006a82e4a67715c8412a9114fbd2cbaefbc8181424") + tx := types.NewTx(suite.chainID, 1, nil, big.NewInt(0), gasLimit, gasPrice, nil, nil, bytecode, nil) + tx.From = suite.from.String() -// tx := types.NewTx(suite.chainID, 1, nil, big.NewInt(0), gasLimit, gasPrice, bytecode, nil) -// tx.From = suite.from.String() + err := tx.Sign(suite.ethSigner, suite.signer) + suite.Require().NoError(err) -// err := tx.Sign(suite.ethSigner, suite.signer) -// suite.Require().NoError(err) + result, _ := suite.handler(suite.ctx, tx) + var res types.MsgEthereumTxResponse -// snapshotCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper) -// suite.Require().Nil(err) + _ = proto.Unmarshal(result.Data, &res) -// _, sdkErr := suite.handler(suite.ctx, tx) -// suite.Require().NotNil(sdkErr) + suite.Require().Equal("invalid opcode: opcode 0xa6 not defined", res.VmError, "correct evm error") -// currentCommitStateDBJson, err := json.Marshal(suite.app.EvmKeeper) -// suite.Require().Nil(err) -// suite.Require().Equal(snapshotCommitStateDBJson, currentCommitStateDBJson) -// } + // TODO: snapshot checking +} diff --git a/x/evm/module.go b/x/evm/module.go index d5ecf497..8c155332 100644 --- a/x/evm/module.go +++ b/x/evm/module.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "math/rand" "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" @@ -16,9 +17,11 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/tharsis/ethermint/x/evm/client/cli" "github.com/tharsis/ethermint/x/evm/keeper" + "github.com/tharsis/ethermint/x/evm/simulation" "github.com/tharsis/ethermint/x/evm/types" ) @@ -164,3 +167,26 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw gs := ExportGenesis(ctx, am.keeper, am.ak) return cdc.MustMarshalJSON(gs) } + +// RandomizedParams creates randomized evm param changes for the simulator. +func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { + return nil +} + +// RegisterStoreDecoder registers a decoder for evm module's types +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {} + +// ProposalContents doesn't return any content functions for governance proposals. +func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent { + return nil +} + +// GenerateGenesisState creates a randomized GenState of the evm module. +func (AppModule) GenerateGenesisState(simState *module.SimulationState) { + simulation.RandomizedGenState(simState) +} + +// WeightedOperations returns the all the evm module operations with their respective weights. +func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { + return nil +} diff --git a/x/evm/simulation/genesis.go b/x/evm/simulation/genesis.go new file mode 100644 index 00000000..74c2eebb --- /dev/null +++ b/x/evm/simulation/genesis.go @@ -0,0 +1,27 @@ +package simulation + +import ( + "encoding/json" + "fmt" + + "github.com/cosmos/cosmos-sdk/types/module" + + "github.com/tharsis/ethermint/x/evm/types" +) + +// RandomizedGenState generates a random GenesisState for nft +func RandomizedGenState(simState *module.SimulationState) { + params := types.NewParams(types.DefaultEVMDenom, true, true, types.DefaultChainConfig()) + if simState.Rand.Uint32()%2 == 0 { + params = types.NewParams(types.DefaultEVMDenom, true, true, types.DefaultChainConfig(), 1344, 1884, 2200, 2929, 3198, 3529) + } + evmGenesis := types.NewGenesisState(params, []types.GenesisAccount{}) + + bz, err := json.MarshalIndent(evmGenesis, "", " ") + if err != nil { + panic(err) + } + fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, bz) + + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(evmGenesis) +} diff --git a/x/evm/types/genesis.go b/x/evm/types/genesis.go index 30f42e03..b71c85d0 100644 --- a/x/evm/types/genesis.go +++ b/x/evm/types/genesis.go @@ -23,6 +23,14 @@ func DefaultGenesisState() *GenesisState { } } +// NewGenesisState creates a new genesis state. +func NewGenesisState(params Params, accounts []GenesisAccount) *GenesisState { + return &GenesisState{ + Accounts: accounts, + Params: params, + } +} + // Validate performs basic genesis state validation returning an error upon any // failure. func (gs GenesisState) Validate() error { diff --git a/x/feemarket/module.go b/x/feemarket/module.go index 70ba12c7..f6937e88 100644 --- a/x/feemarket/module.go +++ b/x/feemarket/module.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "math/rand" "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" @@ -16,9 +17,11 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/tharsis/ethermint/x/feemarket/client/cli" "github.com/tharsis/ethermint/x/feemarket/keeper" + "github.com/tharsis/ethermint/x/feemarket/simulation" "github.com/tharsis/ethermint/x/feemarket/types" ) @@ -155,3 +158,26 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw gs := ExportGenesis(ctx, am.keeper) return cdc.MustMarshalJSON(gs) } + +// RandomizedParams creates randomized fee market param changes for the simulator. +func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { + return nil +} + +// RegisterStoreDecoder registers a decoder for fee market module's types +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {} + +// ProposalContents doesn't return any content functions for governance proposals. +func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent { + return nil +} + +// GenerateGenesisState creates a randomized GenState of the fee market module. +func (AppModule) GenerateGenesisState(simState *module.SimulationState) { + simulation.RandomizedGenState(simState) +} + +// WeightedOperations returns the all the fee market module operations with their respective weights. +func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { + return nil +} diff --git a/x/feemarket/simulation/genesis.go b/x/feemarket/simulation/genesis.go new file mode 100644 index 00000000..b99b4314 --- /dev/null +++ b/x/feemarket/simulation/genesis.go @@ -0,0 +1,27 @@ +package simulation + +import ( + "encoding/json" + "fmt" + + "github.com/cosmos/cosmos-sdk/types/module" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/tharsis/ethermint/x/feemarket/types" +) + +// RandomizedGenState generates a random GenesisState for nft +func RandomizedGenState(simState *module.SimulationState) { + params := types.NewParams(simState.Rand.Uint32()%2 == 0, simState.Rand.Uint32(), simState.Rand.Uint32(), simState.Rand.Int63(), simState.Rand.Int63()) + baseFee := sdk.NewInt(simState.Rand.Int63()) + blockGas := simState.Rand.Uint64() + feemarketGenesis := types.NewGenesisState(params, baseFee, blockGas) + + bz, err := json.MarshalIndent(feemarketGenesis, "", " ") + if err != nil { + panic(err) + } + fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, bz) + + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(feemarketGenesis) +} diff --git a/x/feemarket/types/genesis.go b/x/feemarket/types/genesis.go index c0bbd874..a81829e9 100644 --- a/x/feemarket/types/genesis.go +++ b/x/feemarket/types/genesis.go @@ -15,6 +15,15 @@ func DefaultGenesisState() *GenesisState { } } +// NewGenesisState creates a new genesis state. +func NewGenesisState(params Params, baseFee sdk.Int, blockGas uint64) *GenesisState { + return &GenesisState{ + Params: params, + BaseFee: baseFee, + BlockGas: blockGas, + } +} + // Validate performs basic genesis state validation returning an error upon any // failure. func (gs GenesisState) Validate() error {