2021-09-24 15:58:22 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
evmtypes "github.com/cerc-io/laconicd/x/evm/types"
|
2021-09-24 15:58:22 +00:00
|
|
|
"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{
|
|
|
|
GasPrice: big.NewInt(1),
|
|
|
|
Gas: 21000,
|
|
|
|
To: &common.Address{},
|
|
|
|
Value: big.NewInt(0),
|
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2021-10-19 08:49:29 +00:00
|
|
|
var templateDynamicFeeTx = ðtypes.DynamicFeeTx{
|
|
|
|
GasFeeCap: big.NewInt(10),
|
|
|
|
GasTipCap: big.NewInt(2),
|
|
|
|
Gas: 21000,
|
|
|
|
To: &common.Address{},
|
|
|
|
Value: big.NewInt(0),
|
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:58:22 +00:00
|
|
|
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:
|
|
|
|
txData.Nonce = nonce
|
|
|
|
ethTx = ethtypes.NewTx(txData)
|
2021-10-19 08:49:29 +00:00
|
|
|
case *ethtypes.DynamicFeeTx:
|
|
|
|
txData.Nonce = nonce
|
|
|
|
ethTx = ethtypes.NewTx(txData)
|
2021-09-24 15:58:22 +00:00
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ethTx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNativeMessage(
|
|
|
|
nonce uint64,
|
|
|
|
blockHeight int64,
|
|
|
|
address common.Address,
|
|
|
|
cfg *params.ChainConfig,
|
|
|
|
krSigner keyring.Signer,
|
|
|
|
ethSigner ethtypes.Signer,
|
2021-10-19 08:49:29 +00:00
|
|
|
txType byte,
|
2021-10-20 19:09:06 +00:00
|
|
|
data []byte,
|
|
|
|
accessList ethtypes.AccessList,
|
2021-09-24 15:58:22 +00:00
|
|
|
) (core.Message, error) {
|
|
|
|
msgSigner := ethtypes.MakeSigner(cfg, big.NewInt(blockHeight))
|
|
|
|
|
2021-10-05 15:38:20 +00:00
|
|
|
var (
|
|
|
|
ethTx *ethtypes.Transaction
|
|
|
|
baseFee *big.Int
|
|
|
|
)
|
2021-10-19 08:49:29 +00:00
|
|
|
|
|
|
|
switch txType {
|
|
|
|
case ethtypes.LegacyTxType:
|
2021-09-24 15:58:22 +00:00
|
|
|
templateLegacyTx.Nonce = nonce
|
2021-10-20 19:09:06 +00:00
|
|
|
if data != nil {
|
|
|
|
templateLegacyTx.Data = data
|
|
|
|
}
|
2021-09-24 15:58:22 +00:00
|
|
|
ethTx = ethtypes.NewTx(templateLegacyTx)
|
2021-10-19 08:49:29 +00:00
|
|
|
case ethtypes.AccessListTxType:
|
2021-09-24 15:58:22 +00:00
|
|
|
templateAccessListTx.Nonce = nonce
|
2021-10-20 19:09:06 +00:00
|
|
|
if data != nil {
|
|
|
|
templateAccessListTx.Data = data
|
|
|
|
} else {
|
|
|
|
templateAccessListTx.Data = []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
templateAccessListTx.AccessList = accessList
|
2021-09-24 15:58:22 +00:00
|
|
|
ethTx = ethtypes.NewTx(templateAccessListTx)
|
2021-10-19 08:49:29 +00:00
|
|
|
case ethtypes.DynamicFeeTxType:
|
|
|
|
templateDynamicFeeTx.Nonce = nonce
|
2021-10-20 19:09:06 +00:00
|
|
|
|
|
|
|
if data != nil {
|
|
|
|
templateAccessListTx.Data = data
|
|
|
|
} else {
|
|
|
|
templateAccessListTx.Data = []byte{}
|
|
|
|
}
|
|
|
|
templateAccessListTx.AccessList = accessList
|
2021-10-19 08:49:29 +00:00
|
|
|
ethTx = ethtypes.NewTx(templateDynamicFeeTx)
|
|
|
|
baseFee = big.NewInt(3)
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unsupport tx type")
|
2021-09-24 15:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := &evmtypes.MsgEthereumTx{}
|
|
|
|
msg.FromEthereumTx(ethTx)
|
|
|
|
msg.From = address.Hex()
|
|
|
|
|
|
|
|
if err := msg.Sign(ethSigner, krSigner); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-19 08:49:29 +00:00
|
|
|
m, err := msg.AsMessage(msgSigner, baseFee)
|
2021-09-24 15:58:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkApplyTransaction(b *testing.B) {
|
2021-12-28 07:59:28 +00:00
|
|
|
suite := KeeperTestSuite{enableLondonHF: true}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2021-09-24 15:58:22 +00:00
|
|
|
|
|
|
|
ethSigner := ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
tx, err := newSignedEthTx(templateAccessListTx,
|
2022-01-05 07:28:27 +00:00
|
|
|
suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address),
|
2021-09-24 15:58:22 +00:00
|
|
|
sdk.AccAddress(suite.address.Bytes()),
|
|
|
|
suite.signer,
|
|
|
|
ethSigner,
|
|
|
|
)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.StartTimer()
|
2022-01-05 07:28:27 +00:00
|
|
|
resp, err := suite.app.EvmKeeper.ApplyTransaction(suite.ctx, tx)
|
2021-09-24 15:58:22 +00:00
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.False(b, resp.Failed())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkApplyTransactionWithLegacyTx(b *testing.B) {
|
2021-12-28 07:59:28 +00:00
|
|
|
suite := KeeperTestSuite{enableLondonHF: true}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2021-09-24 15:58:22 +00:00
|
|
|
|
|
|
|
ethSigner := ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
tx, err := newSignedEthTx(templateLegacyTx,
|
2022-01-05 07:28:27 +00:00
|
|
|
suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address),
|
2021-09-24 15:58:22 +00:00
|
|
|
sdk.AccAddress(suite.address.Bytes()),
|
|
|
|
suite.signer,
|
|
|
|
ethSigner,
|
|
|
|
)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.StartTimer()
|
2022-01-05 07:28:27 +00:00
|
|
|
resp, err := suite.app.EvmKeeper.ApplyTransaction(suite.ctx, tx)
|
2021-09-24 15:58:22 +00:00
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.False(b, resp.Failed())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 08:49:29 +00:00
|
|
|
func BenchmarkApplyTransactionWithDynamicFeeTx(b *testing.B) {
|
2021-12-28 07:59:28 +00:00
|
|
|
suite := KeeperTestSuite{enableFeemarket: true, enableLondonHF: true}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2021-10-19 08:49:29 +00:00
|
|
|
|
|
|
|
ethSigner := ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
tx, err := newSignedEthTx(templateDynamicFeeTx,
|
2022-01-05 07:28:27 +00:00
|
|
|
suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address),
|
2021-10-19 08:49:29 +00:00
|
|
|
sdk.AccAddress(suite.address.Bytes()),
|
|
|
|
suite.signer,
|
|
|
|
ethSigner,
|
|
|
|
)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.StartTimer()
|
2022-01-05 07:28:27 +00:00
|
|
|
resp, err := suite.app.EvmKeeper.ApplyTransaction(suite.ctx, tx)
|
2021-10-19 08:49:29 +00:00
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.False(b, resp.Failed())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 17:21:03 +00:00
|
|
|
func BenchmarkApplyMessage(b *testing.B) {
|
2021-12-28 07:59:28 +00:00
|
|
|
suite := KeeperTestSuite{enableLondonHF: true}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2021-09-24 15:58:22 +00:00
|
|
|
|
|
|
|
params := suite.app.EvmKeeper.GetParams(suite.ctx)
|
|
|
|
ethCfg := params.ChainConfig.EthereumConfig(suite.app.EvmKeeper.ChainID())
|
|
|
|
signer := ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
m, err := newNativeMessage(
|
2022-01-05 07:28:27 +00:00
|
|
|
suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address),
|
2021-09-24 15:58:22 +00:00
|
|
|
suite.ctx.BlockHeight(),
|
|
|
|
suite.address,
|
|
|
|
ethCfg,
|
|
|
|
suite.signer,
|
|
|
|
signer,
|
2021-10-19 08:49:29 +00:00
|
|
|
ethtypes.AccessListTxType,
|
2021-10-20 19:09:06 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
2021-09-24 15:58:22 +00:00
|
|
|
)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.StartTimer()
|
2022-01-05 07:28:27 +00:00
|
|
|
resp, err := suite.app.EvmKeeper.ApplyMessage(suite.ctx, m, nil, true)
|
2021-09-24 15:58:22 +00:00
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.False(b, resp.Failed())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 17:21:03 +00:00
|
|
|
func BenchmarkApplyMessageWithLegacyTx(b *testing.B) {
|
2021-12-28 07:59:28 +00:00
|
|
|
suite := KeeperTestSuite{enableLondonHF: true}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2021-09-24 15:58:22 +00:00
|
|
|
|
|
|
|
params := suite.app.EvmKeeper.GetParams(suite.ctx)
|
|
|
|
ethCfg := params.ChainConfig.EthereumConfig(suite.app.EvmKeeper.ChainID())
|
|
|
|
signer := ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
m, err := newNativeMessage(
|
2022-01-05 07:28:27 +00:00
|
|
|
suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address),
|
2021-09-24 15:58:22 +00:00
|
|
|
suite.ctx.BlockHeight(),
|
|
|
|
suite.address,
|
|
|
|
ethCfg,
|
|
|
|
suite.signer,
|
|
|
|
signer,
|
2021-10-19 08:49:29 +00:00
|
|
|
ethtypes.LegacyTxType,
|
2021-10-20 19:09:06 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
2021-10-19 08:49:29 +00:00
|
|
|
)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.StartTimer()
|
2022-01-05 07:28:27 +00:00
|
|
|
resp, err := suite.app.EvmKeeper.ApplyMessage(suite.ctx, m, nil, true)
|
2021-10-19 08:49:29 +00:00
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.False(b, resp.Failed())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 17:21:03 +00:00
|
|
|
func BenchmarkApplyMessageWithDynamicFeeTx(b *testing.B) {
|
2021-12-28 07:59:28 +00:00
|
|
|
suite := KeeperTestSuite{enableFeemarket: true, enableLondonHF: true}
|
2022-10-10 10:38:33 +00:00
|
|
|
suite.SetupTest()
|
2021-10-19 08:49:29 +00:00
|
|
|
|
|
|
|
params := suite.app.EvmKeeper.GetParams(suite.ctx)
|
|
|
|
ethCfg := params.ChainConfig.EthereumConfig(suite.app.EvmKeeper.ChainID())
|
|
|
|
signer := ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
m, err := newNativeMessage(
|
2022-01-05 07:28:27 +00:00
|
|
|
suite.app.EvmKeeper.GetNonce(suite.ctx, suite.address),
|
2021-10-19 08:49:29 +00:00
|
|
|
suite.ctx.BlockHeight(),
|
|
|
|
suite.address,
|
|
|
|
ethCfg,
|
|
|
|
suite.signer,
|
|
|
|
signer,
|
|
|
|
ethtypes.DynamicFeeTxType,
|
2021-10-20 19:09:06 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
2021-09-24 15:58:22 +00:00
|
|
|
)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.StartTimer()
|
2022-01-05 07:28:27 +00:00
|
|
|
resp, err := suite.app.EvmKeeper.ApplyMessage(suite.ctx, m, nil, true)
|
2021-09-24 15:58:22 +00:00
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.False(b, resp.Failed())
|
|
|
|
}
|
|
|
|
}
|