2020-04-01 18:49:21 +00:00
|
|
|
package keeper_test
|
2019-09-26 15:54:23 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-30 03:14:58 +00:00
|
|
|
_ "embed"
|
|
|
|
"encoding/json"
|
2021-12-28 07:59:28 +00:00
|
|
|
"math"
|
2021-08-30 03:14:58 +00:00
|
|
|
"math/big"
|
2019-09-26 15:54:23 +00:00
|
|
|
"testing"
|
2020-04-01 18:49:21 +00:00
|
|
|
"time"
|
|
|
|
|
2022-07-18 20:21:04 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
2022-07-28 13:43:49 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2021-08-30 03:14:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-01 18:49:21 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2021-06-29 10:54:29 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2021-07-23 14:24:36 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2021-10-20 19:09:06 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2019-09-26 15:54:23 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2021-10-20 19:09:06 +00:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2021-06-22 10:14:40 +00:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2022-06-19 09:43:41 +00:00
|
|
|
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
|
2021-10-20 19:09:06 +00:00
|
|
|
tmjson "github.com/tendermint/tendermint/libs/json"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/app"
|
|
|
|
"github.com/evmos/ethermint/crypto/ethsecp256k1"
|
|
|
|
"github.com/evmos/ethermint/encoding"
|
|
|
|
"github.com/evmos/ethermint/server/config"
|
|
|
|
"github.com/evmos/ethermint/tests"
|
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
|
|
|
"github.com/evmos/ethermint/x/evm/statedb"
|
|
|
|
"github.com/evmos/ethermint/x/evm/types"
|
2022-07-18 20:21:04 +00:00
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-08-30 03:14:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-06-29 10:54:29 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-08-30 03:14:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-10-20 19:09:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-07-23 14:24:36 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2021-08-31 15:58:46 +00:00
|
|
|
"github.com/tendermint/tendermint/crypto/tmhash"
|
2021-04-17 10:00:07 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2021-08-31 15:58:46 +00:00
|
|
|
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
|
|
|
|
"github.com/tendermint/tendermint/version"
|
2019-09-26 15:54:23 +00:00
|
|
|
)
|
|
|
|
|
2022-07-28 13:43:49 +00:00
|
|
|
var testTokens = sdkmath.NewIntWithDecimal(1000, 18)
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
type KeeperTestSuite struct {
|
|
|
|
suite.Suite
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
ctx sdk.Context
|
|
|
|
app *app.EthermintApp
|
|
|
|
queryClient types.QueryClient
|
2021-09-03 18:06:36 +00:00
|
|
|
address common.Address
|
2021-06-22 10:14:40 +00:00
|
|
|
consAddress sdk.ConsAddress
|
2021-06-29 10:54:29 +00:00
|
|
|
|
|
|
|
// for generate test tx
|
|
|
|
clientCtx client.Context
|
|
|
|
ethSigner ethtypes.Signer
|
2021-07-23 14:24:36 +00:00
|
|
|
|
|
|
|
appCodec codec.Codec
|
|
|
|
signer keyring.Signer
|
2021-10-19 08:49:29 +00:00
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
enableFeemarket bool
|
|
|
|
enableLondonHF bool
|
2021-10-20 19:09:06 +00:00
|
|
|
mintFeeCollector bool
|
2022-07-18 20:21:04 +00:00
|
|
|
denom string
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2022-07-18 20:21:04 +00:00
|
|
|
var s *KeeperTestSuite
|
|
|
|
|
|
|
|
func TestKeeperTestSuite(t *testing.T) {
|
|
|
|
s = new(KeeperTestSuite)
|
|
|
|
s.enableFeemarket = false
|
|
|
|
s.enableLondonHF = true
|
|
|
|
suite.Run(t, s)
|
|
|
|
|
|
|
|
// Run Ginkgo integration tests
|
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
RunSpecs(t, "Keeper Suite")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) SetupTest() {
|
2020-04-01 18:49:21 +00:00
|
|
|
checkTx := false
|
2022-07-18 20:21:04 +00:00
|
|
|
suite.app = app.Setup(checkTx, nil)
|
|
|
|
suite.SetupApp(checkTx)
|
|
|
|
}
|
2019-09-26 15:54:23 +00:00
|
|
|
|
2022-08-26 10:30:55 +00:00
|
|
|
// SetupApp setup test environment, it uses`require.TestingT` to support both `testing.T` and `testing.B`.
|
2022-07-18 20:21:04 +00:00
|
|
|
func (suite *KeeperTestSuite) SetupApp(checkTx bool) {
|
|
|
|
t := suite.T()
|
2022-01-07 13:04:16 +00:00
|
|
|
// account key, use a constant account to keep unit test deterministic.
|
|
|
|
ecdsaPriv, err := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-07 13:04:16 +00:00
|
|
|
priv := ðsecp256k1.PrivKey{
|
|
|
|
Key: crypto.FromECDSA(ecdsaPriv),
|
|
|
|
}
|
2021-09-03 18:06:36 +00:00
|
|
|
suite.address = common.BytesToAddress(priv.PubKey().Address().Bytes())
|
2021-07-23 14:24:36 +00:00
|
|
|
suite.signer = tests.NewSigner(priv)
|
|
|
|
|
|
|
|
// consensus key
|
|
|
|
priv, err = ethsecp256k1.GenerateKey()
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-23 14:24:36 +00:00
|
|
|
suite.consAddress = sdk.ConsAddress(priv.PubKey().Address())
|
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
suite.app = app.Setup(checkTx, func(app *app.EthermintApp, genesis simapp.GenesisState) simapp.GenesisState {
|
2022-01-26 10:36:07 +00:00
|
|
|
feemarketGenesis := feemarkettypes.DefaultGenesisState()
|
2021-12-28 07:59:28 +00:00
|
|
|
if suite.enableFeemarket {
|
|
|
|
feemarketGenesis.Params.EnableHeight = 1
|
|
|
|
feemarketGenesis.Params.NoBaseFee = false
|
2022-01-26 10:36:07 +00:00
|
|
|
} else {
|
|
|
|
feemarketGenesis.Params.NoBaseFee = true
|
2021-12-28 07:59:28 +00:00
|
|
|
}
|
2022-01-26 10:36:07 +00:00
|
|
|
genesis[feemarkettypes.ModuleName] = app.AppCodec().MustMarshalJSON(feemarketGenesis)
|
2021-12-28 07:59:28 +00:00
|
|
|
if !suite.enableLondonHF {
|
|
|
|
evmGenesis := types.DefaultGenesisState()
|
2022-07-28 13:43:49 +00:00
|
|
|
maxInt := sdkmath.NewInt(math.MaxInt64)
|
2021-12-28 07:59:28 +00:00
|
|
|
evmGenesis.Params.ChainConfig.LondonBlock = &maxInt
|
2022-02-14 23:08:41 +00:00
|
|
|
evmGenesis.Params.ChainConfig.ArrowGlacierBlock = &maxInt
|
2022-07-08 10:58:04 +00:00
|
|
|
evmGenesis.Params.ChainConfig.GrayGlacierBlock = &maxInt
|
|
|
|
evmGenesis.Params.ChainConfig.MergeNetsplitBlock = &maxInt
|
2021-12-28 07:59:28 +00:00
|
|
|
genesis[types.ModuleName] = app.AppCodec().MustMarshalJSON(evmGenesis)
|
|
|
|
}
|
|
|
|
return genesis
|
|
|
|
})
|
2021-10-19 08:49:29 +00:00
|
|
|
|
2021-10-20 19:09:06 +00:00
|
|
|
if suite.mintFeeCollector {
|
|
|
|
// mint some coin to fee collector
|
2022-07-28 13:43:49 +00:00
|
|
|
coins := sdk.NewCoins(sdk.NewCoin(types.DefaultEVMDenom, sdkmath.NewInt(int64(params.TxGas)-1)))
|
2022-07-11 12:42:59 +00:00
|
|
|
genesisState := app.NewTestGenesisState(suite.app.AppCodec())
|
2021-10-20 19:09:06 +00:00
|
|
|
balances := []banktypes.Balance{
|
|
|
|
{
|
|
|
|
Address: suite.app.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName).String(),
|
|
|
|
Coins: coins,
|
|
|
|
},
|
|
|
|
}
|
2022-07-11 12:42:59 +00:00
|
|
|
var bankGenesis banktypes.GenesisState
|
|
|
|
suite.app.AppCodec().MustUnmarshalJSON(genesisState[banktypes.ModuleName], &bankGenesis)
|
|
|
|
// Update balances and total supply
|
|
|
|
bankGenesis.Balances = append(bankGenesis.Balances, balances...)
|
|
|
|
bankGenesis.Supply = bankGenesis.Supply.Add(coins...)
|
|
|
|
genesisState[banktypes.ModuleName] = suite.app.AppCodec().MustMarshalJSON(&bankGenesis)
|
2021-10-20 19:09:06 +00:00
|
|
|
|
|
|
|
// we marshal the genesisState of all module to a byte array
|
|
|
|
stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-17 11:58:52 +00:00
|
|
|
// Initialize the chain
|
2021-10-20 19:09:06 +00:00
|
|
|
suite.app.InitChain(
|
|
|
|
abci.RequestInitChain{
|
|
|
|
ChainId: "ethermint_9000-1",
|
|
|
|
Validators: []abci.ValidatorUpdate{},
|
2022-06-08 13:21:51 +00:00
|
|
|
ConsensusParams: app.DefaultConsensusParams,
|
2021-10-20 19:09:06 +00:00
|
|
|
AppStateBytes: stateBytes,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-07-23 14:24:36 +00:00
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{
|
|
|
|
Height: 1,
|
2021-08-17 14:11:26 +00:00
|
|
|
ChainID: "ethermint_9000-1",
|
2021-07-23 14:24:36 +00:00
|
|
|
Time: time.Now().UTC(),
|
|
|
|
ProposerAddress: suite.consAddress.Bytes(),
|
2021-08-31 15:58:46 +00:00
|
|
|
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")),
|
2021-07-23 14:24:36 +00:00
|
|
|
})
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
|
|
|
|
types.RegisterQueryServer(queryHelper, suite.app.EvmKeeper)
|
|
|
|
suite.queryClient = types.NewQueryClient(queryHelper)
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
acc := ðermint.EthAccount{
|
2021-04-17 10:00:07 +00:00
|
|
|
BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(suite.address.Bytes()), nil, 0, 0),
|
2021-09-03 18:06:36 +00:00
|
|
|
CodeHash: common.BytesToHash(crypto.Keccak256(nil)).String(),
|
2020-08-23 21:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
|
2021-06-22 10:14:40 +00:00
|
|
|
|
|
|
|
valAddr := sdk.ValAddress(suite.address.Bytes())
|
|
|
|
validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{})
|
2022-10-21 16:59:03 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-23 14:24:36 +00:00
|
|
|
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-23 14:24:36 +00:00
|
|
|
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(t, err)
|
2021-06-22 10:14:40 +00:00
|
|
|
suite.app.StakingKeeper.SetValidator(suite.ctx, validator)
|
2021-06-29 10:54:29 +00:00
|
|
|
|
|
|
|
encodingConfig := encoding.MakeConfig(app.ModuleBasics)
|
|
|
|
suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig)
|
|
|
|
suite.ethSigner = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
|
2022-07-28 13:43:49 +00:00
|
|
|
suite.appCodec = encodingConfig.Codec
|
2022-07-18 20:21:04 +00:00
|
|
|
suite.denom = evmtypes.DefaultEVMDenom
|
2021-08-30 03:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) EvmDenom() string {
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
rsp, _ := suite.queryClient.Params(ctx, &types.QueryParamsRequest{})
|
|
|
|
return rsp.Params.EvmDenom
|
2021-07-23 14:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit and begin new block
|
|
|
|
func (suite *KeeperTestSuite) Commit() {
|
2021-08-31 15:58:46 +00:00
|
|
|
_ = suite.app.Commit()
|
2021-07-23 14:24:36 +00:00
|
|
|
header := suite.ctx.BlockHeader()
|
|
|
|
header.Height += 1
|
|
|
|
suite.app.BeginBlock(abci.RequestBeginBlock{
|
|
|
|
Header: header,
|
|
|
|
})
|
|
|
|
|
|
|
|
// update ctx
|
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(false, header)
|
|
|
|
|
|
|
|
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
|
|
|
|
types.RegisterQueryServer(queryHelper, suite.app.EvmKeeper)
|
|
|
|
suite.queryClient = types.NewQueryClient(queryHelper)
|
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
func (suite *KeeperTestSuite) StateDB() *statedb.StateDB {
|
|
|
|
return statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash().Bytes())))
|
|
|
|
}
|
|
|
|
|
2021-08-30 03:14:58 +00:00
|
|
|
// DeployTestContract deploy a test erc20 contract and returns the contract address
|
|
|
|
func (suite *KeeperTestSuite) DeployTestContract(t require.TestingT, owner common.Address, supply *big.Int) common.Address {
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
chainID := suite.app.EvmKeeper.ChainID()
|
|
|
|
|
2021-10-20 19:00:17 +00:00
|
|
|
ctorArgs, err := types.ERC20Contract.ABI.Pack("", owner, supply)
|
2021-08-30 03:14:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
nonce := suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address)
|
2021-12-28 07:55:40 +00:00
|
|
|
|
2021-10-20 19:00:17 +00:00
|
|
|
data := append(types.ERC20Contract.Bin, ctorArgs...)
|
2021-10-05 15:38:20 +00:00
|
|
|
args, err := json.Marshal(&types.TransactionArgs{
|
2022-01-04 10:55:30 +00:00
|
|
|
From: &suite.address,
|
|
|
|
Data: (*hexutil.Bytes)(&data),
|
2021-08-30 03:14:58 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
res, err := suite.queryClient.EstimateGas(ctx, &types.EthCallRequest{
|
2022-10-21 23:58:29 +00:00
|
|
|
Args: args,
|
|
|
|
GasCap: uint64(config.DefaultGasCap),
|
|
|
|
ProposerAddress: suite.ctx.BlockHeader().ProposerAddress,
|
2021-08-30 03:14:58 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-19 08:49:29 +00:00
|
|
|
var erc20DeployTx *types.MsgEthereumTx
|
2021-12-28 07:59:28 +00:00
|
|
|
if suite.enableFeemarket {
|
2021-10-19 08:49:29 +00:00
|
|
|
erc20DeployTx = types.NewTxContract(
|
|
|
|
chainID,
|
|
|
|
nonce,
|
|
|
|
nil, // amount
|
|
|
|
res.Gas, // gasLimit
|
|
|
|
nil, // gasPrice
|
|
|
|
suite.app.FeeMarketKeeper.GetBaseFee(suite.ctx),
|
|
|
|
big.NewInt(1),
|
|
|
|
data, // input
|
|
|
|
ðtypes.AccessList{}, // accesses
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
erc20DeployTx = types.NewTxContract(
|
|
|
|
chainID,
|
|
|
|
nonce,
|
|
|
|
nil, // amount
|
|
|
|
res.Gas, // gasLimit
|
|
|
|
nil, // gasPrice
|
|
|
|
nil, nil,
|
|
|
|
data, // input
|
|
|
|
nil, // accesses
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-30 03:14:58 +00:00
|
|
|
erc20DeployTx.From = suite.address.Hex()
|
|
|
|
err = erc20DeployTx.Sign(ethtypes.LatestSignerForChainID(chainID), suite.signer)
|
|
|
|
require.NoError(t, err)
|
|
|
|
rsp, err := suite.app.EvmKeeper.EthereumTx(ctx, erc20DeployTx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, rsp.VmError)
|
|
|
|
return crypto.CreateAddress(suite.address, nonce)
|
|
|
|
}
|
|
|
|
|
2021-09-05 11:03:06 +00:00
|
|
|
func (suite *KeeperTestSuite) TransferERC20Token(t require.TestingT, contractAddr, from, to common.Address, amount *big.Int) *types.MsgEthereumTx {
|
2021-09-04 20:33:06 +00:00
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
chainID := suite.app.EvmKeeper.ChainID()
|
|
|
|
|
2021-10-20 19:00:17 +00:00
|
|
|
transferData, err := types.ERC20Contract.ABI.Pack("transfer", to, amount)
|
2021-09-04 20:33:06 +00:00
|
|
|
require.NoError(t, err)
|
2021-10-05 15:38:20 +00:00
|
|
|
args, err := json.Marshal(&types.TransactionArgs{To: &contractAddr, From: &from, Data: (*hexutil.Bytes)(&transferData)})
|
2021-09-04 20:33:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
res, err := suite.queryClient.EstimateGas(ctx, &types.EthCallRequest{
|
2022-10-21 23:58:29 +00:00
|
|
|
Args: args,
|
|
|
|
GasCap: 25_000_000,
|
|
|
|
ProposerAddress: suite.ctx.BlockHeader().ProposerAddress,
|
2021-09-04 20:33:06 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
nonce := suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address)
|
2021-10-19 08:49:29 +00:00
|
|
|
|
|
|
|
var ercTransferTx *types.MsgEthereumTx
|
2021-12-28 07:59:28 +00:00
|
|
|
if suite.enableFeemarket {
|
2021-10-19 08:49:29 +00:00
|
|
|
ercTransferTx = types.NewTx(
|
|
|
|
chainID,
|
|
|
|
nonce,
|
|
|
|
&contractAddr,
|
|
|
|
nil,
|
|
|
|
res.Gas,
|
|
|
|
nil,
|
|
|
|
suite.app.FeeMarketKeeper.GetBaseFee(suite.ctx),
|
|
|
|
big.NewInt(1),
|
|
|
|
transferData,
|
|
|
|
ðtypes.AccessList{}, // accesses
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
ercTransferTx = types.NewTx(
|
|
|
|
chainID,
|
|
|
|
nonce,
|
|
|
|
&contractAddr,
|
|
|
|
nil,
|
|
|
|
res.Gas,
|
|
|
|
nil,
|
|
|
|
nil, nil,
|
|
|
|
transferData,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
ercTransferTx.From = suite.address.Hex()
|
|
|
|
err = ercTransferTx.Sign(ethtypes.LatestSignerForChainID(chainID), suite.signer)
|
|
|
|
require.NoError(t, err)
|
|
|
|
rsp, err := suite.app.EvmKeeper.EthereumTx(ctx, ercTransferTx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, rsp.VmError)
|
|
|
|
return ercTransferTx
|
|
|
|
}
|
|
|
|
|
2021-11-08 13:04:49 +00:00
|
|
|
// DeployTestMessageCall deploy a test erc20 contract and returns the contract address
|
|
|
|
func (suite *KeeperTestSuite) DeployTestMessageCall(t require.TestingT) common.Address {
|
|
|
|
ctx := sdk.WrapSDKContext(suite.ctx)
|
|
|
|
chainID := suite.app.EvmKeeper.ChainID()
|
|
|
|
|
|
|
|
data := types.TestMessageCall.Bin
|
|
|
|
args, err := json.Marshal(&types.TransactionArgs{
|
|
|
|
From: &suite.address,
|
|
|
|
Data: (*hexutil.Bytes)(&data),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err := suite.queryClient.EstimateGas(ctx, &types.EthCallRequest{
|
2022-10-21 23:58:29 +00:00
|
|
|
Args: args,
|
|
|
|
GasCap: uint64(config.DefaultGasCap),
|
|
|
|
ProposerAddress: suite.ctx.BlockHeader().ProposerAddress,
|
2021-11-08 13:04:49 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
nonce := suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address)
|
2021-11-08 13:04:49 +00:00
|
|
|
|
|
|
|
var erc20DeployTx *types.MsgEthereumTx
|
2021-12-28 07:59:28 +00:00
|
|
|
if suite.enableFeemarket {
|
2021-11-08 13:04:49 +00:00
|
|
|
erc20DeployTx = types.NewTxContract(
|
|
|
|
chainID,
|
|
|
|
nonce,
|
|
|
|
nil, // amount
|
|
|
|
res.Gas, // gasLimit
|
|
|
|
nil, // gasPrice
|
|
|
|
suite.app.FeeMarketKeeper.GetBaseFee(suite.ctx),
|
|
|
|
big.NewInt(1),
|
|
|
|
data, // input
|
|
|
|
ðtypes.AccessList{}, // accesses
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
erc20DeployTx = types.NewTxContract(
|
|
|
|
chainID,
|
|
|
|
nonce,
|
|
|
|
nil, // amount
|
|
|
|
res.Gas, // gasLimit
|
|
|
|
nil, // gasPrice
|
|
|
|
nil, nil,
|
|
|
|
data, // input
|
|
|
|
nil, // accesses
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
erc20DeployTx.From = suite.address.Hex()
|
|
|
|
err = erc20DeployTx.Sign(ethtypes.LatestSignerForChainID(chainID), suite.signer)
|
|
|
|
require.NoError(t, err)
|
|
|
|
rsp, err := suite.app.EvmKeeper.EthereumTx(ctx, erc20DeployTx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, rsp.VmError)
|
|
|
|
return crypto.CreateAddress(suite.address, nonce)
|
|
|
|
}
|
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
func (suite *KeeperTestSuite) TestBaseFee() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
enableLondonHF bool
|
|
|
|
enableFeemarket bool
|
|
|
|
expectBaseFee *big.Int
|
|
|
|
}{
|
|
|
|
{"not enable london HF, not enable feemarket", false, false, nil},
|
|
|
|
{"enable london HF, not enable feemarket", true, false, big.NewInt(0)},
|
|
|
|
{"enable london HF, enable feemarket", true, true, big.NewInt(1000000000)},
|
|
|
|
{"not enable london HF, enable feemarket", false, true, nil},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
suite.enableFeemarket = tc.enableFeemarket
|
|
|
|
suite.enableLondonHF = tc.enableLondonHF
|
|
|
|
suite.SetupTest()
|
|
|
|
suite.app.EvmKeeper.BeginBlock(suite.ctx, abci.RequestBeginBlock{})
|
|
|
|
params := suite.app.EvmKeeper.GetParams(suite.ctx)
|
|
|
|
ethCfg := params.ChainConfig.EthereumConfig(suite.app.EvmKeeper.ChainID())
|
2022-04-30 16:11:28 +00:00
|
|
|
baseFee := suite.app.EvmKeeper.GetBaseFee(suite.ctx, ethCfg)
|
2021-12-28 07:59:28 +00:00
|
|
|
suite.Require().Equal(tc.expectBaseFee, baseFee)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
suite.enableFeemarket = false
|
|
|
|
suite.enableLondonHF = true
|
|
|
|
}
|
2022-10-21 16:59:03 +00:00
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestGetAccountStorage() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
malleate func()
|
|
|
|
expRes []int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"Only one account that's not a contract (no storage)",
|
|
|
|
func() {},
|
|
|
|
[]int{0},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Two accounts - one contract (with storage), one wallet",
|
|
|
|
func() {
|
|
|
|
supply := big.NewInt(100)
|
|
|
|
suite.DeployTestContract(suite.T(), suite.address, supply)
|
|
|
|
},
|
|
|
|
[]int{2, 0},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
suite.SetupTest()
|
|
|
|
tc.malleate()
|
|
|
|
i := 0
|
|
|
|
suite.app.AccountKeeper.IterateAccounts(suite.ctx, func(account authtypes.AccountI) bool {
|
|
|
|
ethAccount, ok := account.(ethermint.EthAccountI)
|
|
|
|
if !ok {
|
|
|
|
// ignore non EthAccounts
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := ethAccount.EthAddress()
|
|
|
|
storage := suite.app.EvmKeeper.GetAccountStorage(suite.ctx, addr)
|
|
|
|
|
|
|
|
suite.Require().Equal(tc.expRes[i], len(storage))
|
|
|
|
i++
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestGetAccountOrEmpty() {
|
|
|
|
empty := statedb.Account{
|
|
|
|
Balance: new(big.Int),
|
|
|
|
CodeHash: types.EmptyCodeHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
supply := big.NewInt(100)
|
|
|
|
contractAddr := suite.DeployTestContract(suite.T(), suite.address, supply)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
addr common.Address
|
|
|
|
expEmpty bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"unexisting account - get empty",
|
|
|
|
common.Address{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"existing contract account",
|
|
|
|
contractAddr,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
res := suite.app.EvmKeeper.GetAccountOrEmpty(suite.ctx, tc.addr)
|
|
|
|
if tc.expEmpty {
|
|
|
|
suite.Require().Equal(empty, res)
|
|
|
|
} else {
|
|
|
|
suite.Require().NotEqual(empty, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|