diff --git a/x/gasfree/module_test.go b/x/gasfree/module_test.go index ade306e2..e72fbf46 100644 --- a/x/gasfree/module_test.go +++ b/x/gasfree/module_test.go @@ -1,7 +1,6 @@ package gasfree_test import ( - "encoding/json" "testing" "time" @@ -14,6 +13,7 @@ import ( "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/params" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -25,24 +25,24 @@ import ( "github.com/evmos/ethermint/crypto/ethsecp256k1" "github.com/evmos/ethermint/tests" - "github.com/evmos/ethermint/x/evm" "github.com/evmos/ethermint/x/evm/statedb" evmtypes "github.com/evmos/ethermint/x/evm/types" + feemarkettypes "github.com/evmos/ethermint/x/feemarket/types" althea "github.com/AltheaFoundation/althea-L1/app" - altheaconfig "github.com/AltheaFoundation/althea-L1/config" + altheacfg "github.com/AltheaFoundation/althea-L1/config" ) type GasfreeTestSuite struct { suite.Suite - ctx sdk.Context - handler sdk.Handler - app *althea.AltheaApp + ctx sdk.Context + app *althea.AltheaApp - signer keyring.Signer - ethSigner ethtypes.Signer - from common.Address + signer keyring.Signer + ethSigner ethtypes.Signer + from common.Address + mintFeeCollector bool } // / DoSetupTest setup test environment, it uses `require.TestingT` to support both `testing.T` and `testing.B`. @@ -52,83 +52,48 @@ func (suite *GasfreeTestSuite) DoSetupTest(t require.TestingT) { // 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 = Setup(checkTx, func(app *althea.AltheaApp, genesis simapp.GenesisState) simapp.GenesisState { - evmGenesis := evmtypes.DefaultGenesisState() - evmGenesis.Params.EvmDenom = altheaconfig.BaseDenom - evmGenesis.Params.AllowUnprotectedTxs = false - - genesis[evmtypes.ModuleName] = app.AppCodec().MustMarshalJSON(evmGenesis) - - coins := sdk.NewCoins(sdk.NewCoin(altheaconfig.BaseDenom, sdk.NewInt(100000000000000))) - genesisState := althea.ModuleBasics.DefaultGenesis(app.AppCodec()) - b32address := sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), priv.PubKey().Address().Bytes()) - balances := []banktypes.Balance{ - { - Address: b32address, - Coins: coins, - }, - { - Address: app.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName).String(), - Coins: coins, - }, - } - // Update total supply - bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, sdk.NewCoins(sdk.NewCoin(altheaconfig.BaseDenom, sdk.NewInt(200000000000000))), []banktypes.Metadata{}) - genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) - return genesis - }) - - coins := sdk.NewCoins(sdk.NewCoin(altheaconfig.BaseDenom, sdk.NewInt(100000000000000))) - genesisState := althea.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(altheaconfig.BaseDenom, sdk.NewInt(200000000000000))), []banktypes.Metadata{}) - genesisState[banktypes.ModuleName] = suite.app.AppCodec().MustMarshalJSON(bankGenesis) + // init app + suite.app = althea.NewSetup(checkTx, func(aa *althea.AltheaApp, gs simapp.GenesisState) simapp.GenesisState { + // setup feemarketGenesis params + feemarketGenesis := feemarkettypes.DefaultGenesisState() + feemarketGenesis.Params.EnableHeight = 1 + feemarketGenesis.Params.NoBaseFee = false + feemarketGenesis.Params.BaseFee = sdk.NewInt(1) + + gs[feemarkettypes.ModuleName] = aa.AppCodec().MustMarshalJSON(feemarketGenesis) + + if suite.mintFeeCollector { + // mint some coin to fee collector + coins := sdk.NewCoins(sdk.NewCoin(altheacfg.BaseDenom, sdk.NewInt(int64(params.TxGas)-1))) + balances := []banktypes.Balance{ + { + Address: suite.app.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName).String(), + Coins: coins, + }, + } + // update total supply + var bankGenesis banktypes.GenesisState + aa.AppCodec().MustUnmarshalJSON(gs[banktypes.ModuleName], &bankGenesis) + bankGenesis.Balances = append(bankGenesis.Balances, balances...) + bankGenesis.Supply = bankGenesis.Supply.Add(coins...) + gs[banktypes.ModuleName] = suite.app.AppCodec().MustMarshalJSON(&bankGenesis) - stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ") - require.NoError(t, err) + } - // Initialize the chain - suite.app.InitChain( - // nolint: exhaustruct - abci.RequestInitChain{ - ChainId: "althea_6633438-1", - Validators: []abci.ValidatorUpdate{}, - ConsensusParams: DefaultConsensusParams, - AppStateBytes: stateBytes, - }, - ) + return gs + }) - // nolint: exhaustruct suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{ Height: 1, - ChainID: "althea_6633438-1", + ChainID: "althea_7357-1", Time: time.Now().UTC(), - ProposerAddress: consAddress.Bytes(), - // nolint: exhaustruct + ProposerAddress: althea.ValidatorPubKey.Address().Bytes(), + Version: tmversion.Consensus{ Block: version.BlockProtocol, }, - // nolint: exhaustruct LastBlockId: tmproto.BlockID{ Hash: tmhash.Sum([]byte("block_id")), PartSetHeader: tmproto.PartSetHeader{ @@ -145,60 +110,6 @@ func (suite *GasfreeTestSuite) DoSetupTest(t require.TestingT) { LastResultsHash: tmhash.Sum([]byte("last_result")), }) - // queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry()) - // evmtypes.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()) - // // nolint: exhaustruct - // 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) -} - -// Setup initializes a new Althea app. A Nop logger is set in AltheaApp. -func Setup(isCheckTx bool, patchGenesis func(*althea.AltheaApp, althea.GenesisState) althea.GenesisState) *althea.AltheaApp { - db := dbm.NewMemDB() - app := althea.NewAltheaApp(tmlog.NewNopLogger(), db, nil, true, map[int64]bool{}, althea.DefaultNodeHome, 5, althea.MakeEncodingConfig(), simapp.EmptyAppOptions{}) - if !isCheckTx { - // init chain must be called to stop deliverState from being nil - genesisState := althea.NewDefaultGenesisState() - if patchGenesis != nil { - genesisState = patchGenesis(app, genesisState) - } - - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - if err != nil { - panic(err) - } - - // Initialize the chain - app.InitChain( - // nolint: exhaustruct - abci.RequestInitChain{ - ChainId: "althea_6633438-1", - Validators: []abci.ValidatorUpdate{}, - ConsensusParams: DefaultConsensusParams, - AppStateBytes: stateBytes, - }, - ) - } - - return app } // DefaultConsensusParams defines the default Tendermint consensus params used in