evm: benchmark contract execution (#436)

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
yihuang 2021-08-30 11:14:58 +08:00 committed by GitHub
parent 0cdedfc1fd
commit a5ab608336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 184 additions and 84 deletions

View File

@ -212,4 +212,12 @@ contract StandardTokenMock is ERC20 {
allowed[_account][msg.sender] = allowed[_account][msg.sender].sub(_amount); allowed[_account][msg.sender] = allowed[_account][msg.sender].sub(_amount);
_burn(_account, _amount); _burn(_account, _amount);
} }
// For benchmarks
event TestLog(address sender, uint i);
function benchmarkLogs(uint n) public {
for (uint i=0; i<n; i++) {
emit TestLog(msg.sender, i);
}
}
} }

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,79 @@
package keeper_test
import (
"math/big"
"testing"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
ethermint "github.com/tharsis/ethermint/types"
"github.com/tharsis/ethermint/x/evm/types"
)
func SetupContract(b *testing.B) (*KeeperTestSuite, common.Address) {
suite := KeeperTestSuite{}
suite.DoSetupTest(b)
amt := sdk.Coins{ethermint.NewPhotonCoinInt64(1000000000000000000)}
err := suite.app.BankKeeper.MintCoins(suite.ctx, types.ModuleName, amt)
require.NoError(b, err)
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, suite.address.Bytes(), amt)
require.NoError(b, err)
contractAddr := suite.DeployTestContract(b, suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
suite.Commit()
return &suite, contractAddr
}
type TxBuilder func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx
func DoBenchmark(b *testing.B, txBuilder TxBuilder) {
suite, contractAddr := SetupContract(b)
msg := txBuilder(suite, contractAddr)
msg.From = suite.address.Hex()
err := msg.Sign(ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID()), suite.signer)
require.NoError(b, err)
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
ctx, _ := suite.ctx.CacheContext()
// deduct fee first
txData, err := types.UnpackTxData(msg.Data)
require.NoError(b, err)
fees := sdk.Coins{sdk.NewCoin(suite.EvmDenom(), sdk.NewIntFromBigInt(txData.Fee()))}
err = authante.DeductFees(suite.app.BankKeeper, suite.ctx, suite.app.AccountKeeper.GetAccount(ctx, msg.GetFrom()), fees)
require.NoError(b, err)
rsp, err := suite.app.EvmKeeper.EthereumTx(sdk.WrapSDKContext(ctx), msg)
require.NoError(b, err)
require.False(b, rsp.Failed())
}
}
func BenchmarkTokenTransfer(b *testing.B) {
DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx {
input, err := ContractABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
require.NoError(b, err)
nonce := suite.app.EvmKeeper.GetNonce(suite.address)
return types.NewTx(suite.app.EvmKeeper.ChainID(), nonce, &contract, big.NewInt(0), 410000, big.NewInt(1), input, nil)
})
}
func BenchmarkEmitLogs(b *testing.B) {
DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx {
input, err := ContractABI.Pack("benchmarkLogs", big.NewInt(1000))
require.NoError(b, err)
nonce := suite.app.EvmKeeper.GetNonce(suite.address)
return types.NewTx(suite.app.EvmKeeper.ChainID(), nonce, &contract, big.NewInt(0), 4100000, big.NewInt(1), input, nil)
})
}

View File

@ -1,19 +1,16 @@
package keeper_test package keeper_test
import ( import (
_ "embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"math/big" "math/big"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
ethcmn "github.com/ethereum/go-ethereum/common" ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types" ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -27,29 +24,6 @@ import (
//Not valid Ethereum address //Not valid Ethereum address
const invalidAddress = "0x0000" const invalidAddress = "0x0000"
var (
//go:embed ERC20Contract.json
compiledContractJSON []byte
contractBin []byte
contractABI abi.ABI
)
func init() {
var tmp struct {
Abi string
Bin string
}
err := json.Unmarshal(compiledContractJSON, &tmp)
if err != nil {
panic(err)
}
contractBin = common.FromHex(tmp.Bin)
err = json.Unmarshal([]byte(tmp.Abi), &contractABI)
if err != nil {
panic(err)
}
}
func (suite *KeeperTestSuite) TestQueryAccount() { func (suite *KeeperTestSuite) TestQueryAccount() {
var ( var (
req *types.QueryAccountRequest req *types.QueryAccountRequest
@ -707,46 +681,6 @@ func (suite *KeeperTestSuite) TestQueryValidatorAccount() {
} }
} }
// DeployTestContract deploy a test erc20 contract and returns the contract address
func (suite *KeeperTestSuite) deployTestContract(owner common.Address, supply *big.Int) common.Address {
ctx := sdk.WrapSDKContext(suite.ctx)
chainID := suite.app.EvmKeeper.ChainID()
ctorArgs, err := contractABI.Pack("", owner, supply)
suite.Require().NoError(err)
data := append(contractBin, ctorArgs...)
args, err := json.Marshal(&types.CallArgs{
From: &suite.address,
Data: (*hexutil.Bytes)(&data),
})
suite.Require().NoError(err)
res, err := suite.queryClient.EstimateGas(ctx, &types.EthCallRequest{
Args: args,
GasCap: 25_000_000,
})
suite.Require().NoError(err)
nonce := suite.app.EvmKeeper.GetNonce(suite.address)
erc20DeployTx := types.NewTxContract(
chainID,
nonce,
nil, // amount
res.Gas, // gasLimit
nil, // gasPrice
data, // input
nil, // accesses
)
erc20DeployTx.From = suite.address.Hex()
err = erc20DeployTx.Sign(ethtypes.LatestSignerForChainID(chainID), suite.signer)
suite.Require().NoError(err)
rsp, err := suite.app.EvmKeeper.EthereumTx(ctx, erc20DeployTx)
suite.Require().NoError(err)
suite.Require().Empty(rsp.VmError)
return crypto.CreateAddress(suite.address, nonce)
}
func (suite *KeeperTestSuite) TestEstimateGas() { func (suite *KeeperTestSuite) TestEstimateGas() {
ctx := sdk.WrapSDKContext(suite.ctx) ctx := sdk.WrapSDKContext(suite.ctx)
gasHelper := hexutil.Uint64(20000) gasHelper := hexutil.Uint64(20000)
@ -784,19 +718,19 @@ func (suite *KeeperTestSuite) TestEstimateGas() {
}, false, 0}, }, false, 0},
// estimate gas of an erc20 contract deployment, the exact gas number is checked with geth // estimate gas of an erc20 contract deployment, the exact gas number is checked with geth
{"contract deployment", func() { {"contract deployment", func() {
ctorArgs, err := contractABI.Pack("", &suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt()) ctorArgs, err := ContractABI.Pack("", &suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
suite.Require().NoError(err) suite.Require().NoError(err)
data := append(contractBin, ctorArgs...) data := append(ContractBin, ctorArgs...)
args = types.CallArgs{ args = types.CallArgs{
From: &suite.address, From: &suite.address,
Data: (*hexutil.Bytes)(&data), Data: (*hexutil.Bytes)(&data),
} }
}, true, 1144643}, }, true, 1186778},
// estimate gas of an erc20 transfer, the exact gas number is checked with geth // estimate gas of an erc20 transfer, the exact gas number is checked with geth
{"erc20 transfer", func() { {"erc20 transfer", func() {
contractAddr := suite.deployTestContract(suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt()) contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
suite.Commit() suite.Commit()
transferData, err := contractABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000)) transferData, err := ContractABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
suite.Require().NoError(err) suite.Require().NoError(err)
args = types.CallArgs{To: &contractAddr, From: &suite.address, Data: (*hexutil.Bytes)(&transferData)} args = types.CallArgs{To: &contractAddr, From: &suite.address, Data: (*hexutil.Bytes)(&transferData)}
}, true, 51880}, }, true, 51880},

View File

@ -1,9 +1,13 @@
package keeper_test package keeper_test
import ( import (
_ "embed"
"encoding/json"
"math/big"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/baseapp"
@ -20,19 +24,46 @@ import (
"github.com/tharsis/ethermint/app" "github.com/tharsis/ethermint/app"
"github.com/tharsis/ethermint/crypto/ethsecp256k1" "github.com/tharsis/ethermint/crypto/ethsecp256k1"
"github.com/tharsis/ethermint/encoding" "github.com/tharsis/ethermint/encoding"
"github.com/tharsis/ethermint/server/config"
"github.com/tharsis/ethermint/tests" "github.com/tharsis/ethermint/tests"
ethermint "github.com/tharsis/ethermint/types" ethermint "github.com/tharsis/ethermint/types"
"github.com/tharsis/ethermint/x/evm/types" "github.com/tharsis/ethermint/x/evm/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
ethcmn "github.com/ethereum/go-ethereum/common" ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types" ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
) )
var (
//go:embed ERC20Contract.json
compiledContractJSON []byte
ContractBin []byte
ContractABI abi.ABI
)
func init() {
var tmp struct {
Abi string
Bin string
}
err := json.Unmarshal(compiledContractJSON, &tmp)
if err != nil {
panic(err)
}
ContractBin = common.FromHex(tmp.Bin)
err = json.Unmarshal([]byte(tmp.Abi), &ContractABI)
if err != nil {
panic(err)
}
}
var testTokens = sdk.NewIntWithDecimal(1000, 18) var testTokens = sdk.NewIntWithDecimal(1000, 18)
type KeeperTestSuite struct { type KeeperTestSuite struct {
@ -52,18 +83,19 @@ type KeeperTestSuite struct {
signer keyring.Signer signer keyring.Signer
} }
func (suite *KeeperTestSuite) SetupTest() { /// DoSetupTest setup test environment, it uses`require.TestingT` to support both `testing.T` and `testing.B`.
func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {
checkTx := false checkTx := false
// account key // account key
priv, err := ethsecp256k1.GenerateKey() priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err) require.NoError(t, err)
suite.address = ethcmn.BytesToAddress(priv.PubKey().Address().Bytes()) suite.address = ethcmn.BytesToAddress(priv.PubKey().Address().Bytes())
suite.signer = tests.NewSigner(priv) suite.signer = tests.NewSigner(priv)
// consensus key // consensus key
priv, err = ethsecp256k1.GenerateKey() priv, err = ethsecp256k1.GenerateKey()
suite.Require().NoError(err) require.NoError(t, err)
suite.consAddress = sdk.ConsAddress(priv.PubKey().Address()) suite.consAddress = sdk.ConsAddress(priv.PubKey().Address())
suite.app = app.Setup(checkTx) suite.app = app.Setup(checkTx)
@ -89,9 +121,9 @@ func (suite *KeeperTestSuite) SetupTest() {
valAddr := sdk.ValAddress(suite.address.Bytes()) valAddr := sdk.ValAddress(suite.address.Bytes())
validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{}) validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{})
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator) err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
suite.Require().NoError(err) require.NoError(t, err)
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator) err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
suite.Require().NoError(err) require.NoError(t, err)
suite.app.StakingKeeper.SetValidator(suite.ctx, validator) suite.app.StakingKeeper.SetValidator(suite.ctx, validator)
encodingConfig := encoding.MakeConfig(app.ModuleBasics) encodingConfig := encoding.MakeConfig(app.ModuleBasics)
@ -101,13 +133,20 @@ func (suite *KeeperTestSuite) SetupTest() {
// mint some tokens to coinbase address // mint some tokens to coinbase address
_, bankKeeper := suite.initKeepersWithmAccPerms() _, bankKeeper := suite.initKeepersWithmAccPerms()
ctx := sdk.WrapSDKContext(suite.ctx) require.NoError(t, err)
rsp, err := suite.queryClient.Params(ctx, &types.QueryParamsRequest{}) initCoin := sdk.NewCoins(sdk.NewCoin(suite.EvmDenom(), testTokens))
suite.Require().NoError(err)
initCoin := sdk.NewCoins(sdk.NewCoin(rsp.Params.EvmDenom, testTokens))
err = simapp.FundAccount(bankKeeper, suite.ctx, acc.GetAddress(), initCoin) err = simapp.FundAccount(bankKeeper, suite.ctx, acc.GetAddress(), initCoin)
suite.Require().NoError(err) require.NoError(t, err)
}
func (suite *KeeperTestSuite) SetupTest() {
suite.DoSetupTest(suite.T())
}
func (suite *KeeperTestSuite) EvmDenom() string {
ctx := sdk.WrapSDKContext(suite.ctx)
rsp, _ := suite.queryClient.Params(ctx, &types.QueryParamsRequest{})
return rsp.Params.EvmDenom
} }
// Commit and begin new block // Commit and begin new block
@ -146,6 +185,46 @@ func (suite *KeeperTestSuite) initKeepersWithmAccPerms() (authkeeper.AccountKeep
return authKeeper, keeper return authKeeper, keeper
} }
// 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()
ctorArgs, err := ContractABI.Pack("", owner, supply)
require.NoError(t, err)
data := append(ContractBin, ctorArgs...)
args, err := json.Marshal(&types.CallArgs{
From: &suite.address,
Data: (*hexutil.Bytes)(&data),
})
require.NoError(t, err)
res, err := suite.queryClient.EstimateGas(ctx, &types.EthCallRequest{
Args: args,
GasCap: uint64(config.DefaultGasCap),
})
require.NoError(t, err)
nonce := suite.app.EvmKeeper.GetNonce(suite.address)
erc20DeployTx := types.NewTxContract(
chainID,
nonce,
nil, // amount
res.Gas, // gasLimit
nil, // gasPrice
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)
}
func TestKeeperTestSuite(t *testing.T) { func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite)) suite.Run(t, new(KeeperTestSuite))
} }