package keeper_test
import (
"errors"
"math/big"
"testing"
evmtypes "github.com/cerc-io/laconicd/x/evm/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"
)
var templateAccessListTx = ðtypes.AccessListTx{
GasPrice: big.NewInt(1),
Gas: 21000,
To: &common.Address{},
Value: big.NewInt(0),
Data: []byte{},
}
var templateLegacyTx = ðtypes.LegacyTx{
var templateDynamicFeeTx = ðtypes.DynamicFeeTx{
GasFeeCap: big.NewInt(10),
GasTipCap: big.NewInt(2),
func newSignedEthTx(
txData ethtypes.TxData,
nonce uint64,
addr sdk.Address,
krSigner keyring.Signer,
ethSigner ethtypes.Signer,
) (*ethtypes.Transaction, error) {
var ethTx *ethtypes.Transaction
switch txData := txData.(type) {
case *ethtypes.AccessListTx:
txData.Nonce = nonce
ethTx = ethtypes.NewTx(txData)
case *ethtypes.LegacyTx:
case *ethtypes.DynamicFeeTx:
default:
return nil, errors.New("unknown transaction type!")
sig, _, err := krSigner.SignByAddress(addr, ethTx.Hash().Bytes())
if err != nil {
return nil, err
ethTx, err = ethTx.WithSignature(ethSigner, sig)
return ethTx, nil
func newEthMsgTx(
blockHeight int64,
address common.Address,
cfg *params.ChainConfig,
txType byte,
data []byte,
accessList ethtypes.AccessList,
) (*evmtypes.MsgEthereumTx, *big.Int, error) {
var (
ethTx *ethtypes.Transaction
baseFee *big.Int
switch txType {
case ethtypes.LegacyTxType:
templateLegacyTx.Nonce = nonce
if data != nil {
templateLegacyTx.Data = data
ethTx = ethtypes.NewTx(templateLegacyTx)
case ethtypes.AccessListTxType:
templateAccessListTx.Nonce = nonce
templateAccessListTx.Data = data
} else {
templateAccessListTx.Data = []byte{}
templateAccessListTx.AccessList = accessList
ethTx = ethtypes.NewTx(templateAccessListTx)
case ethtypes.DynamicFeeTxType:
templateDynamicFeeTx.Nonce = nonce
ethTx = ethtypes.NewTx(templateDynamicFeeTx)
baseFee = big.NewInt(3)
return nil, baseFee, errors.New("unsupport tx type")
msg := &evmtypes.MsgEthereumTx{}
msg.FromEthereumTx(ethTx)
msg.From = address.Hex()
return msg, baseFee, msg.Sign(ethSigner, krSigner)
func newNativeMessage(
) (core.Message, error) {
msgSigner := ethtypes.MakeSigner(cfg, big.NewInt(blockHeight))
msg, baseFee, err := newEthMsgTx(nonce, blockHeight, address, cfg, krSigner, ethSigner, txType, data, accessList)
m, err := msg.AsMessage(msgSigner, baseFee)
return m, nil
func BenchmarkApplyTransaction(b *testing.B) {
suite := KeeperTestSuite{enableLondonHF: true}
suite.SetupTest()
ethSigner := ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
b.StopTimer()
tx, err := newSignedEthTx(templateAccessListTx,
suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address),
sdk.AccAddress(suite.address.Bytes()),
suite.signer,
ethSigner,
require.NoError(b, err)
b.StartTimer()
resp, err := suite.app.EvmKeeper.ApplyTransaction(suite.ctx, tx)
require.False(b, resp.Failed())
func BenchmarkApplyTransactionWithLegacyTx(b *testing.B) {
tx, err := newSignedEthTx(templateLegacyTx,
func BenchmarkApplyTransactionWithDynamicFeeTx(b *testing.B) {
suite := KeeperTestSuite{enableFeemarket: true, enableLondonHF: true}
tx, err := newSignedEthTx(templateDynamicFeeTx,
func BenchmarkApplyMessage(b *testing.B) {
params := suite.app.EvmKeeper.GetParams(suite.ctx)
ethCfg := params.ChainConfig.EthereumConfig(suite.app.EvmKeeper.ChainID())
signer := ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
m, err := newNativeMessage(
suite.ctx.BlockHeight(),
suite.address,
ethCfg,
signer,
ethtypes.AccessListTxType,
nil,
resp, err := suite.app.EvmKeeper.ApplyMessage(suite.ctx, m, nil, true)
func BenchmarkApplyMessageWithLegacyTx(b *testing.B) {
ethtypes.LegacyTxType,
func BenchmarkApplyMessageWithDynamicFeeTx(b *testing.B) {
ethtypes.DynamicFeeTxType,