evm: estimate gas unit tests (#324)

* add some unit tests for estimateGas

Also add some test environment setup, Closes #323

test estimateGas of erc20 token transfer

fix failed test case

the trick is to keep a clean transient store, by doing a commit

put artifacts to external file

* fix test failure

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
yihuang 2021-07-23 22:24:36 +08:00 committed by GitHub
parent e61594e10a
commit f6dc80d949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 255 additions and 22 deletions

View File

@ -15,7 +15,7 @@ jobs:
# - uses: actions/checkout@v2.3.4
# - uses: actions/setup-go@v2.1.3
# with:
# go-version: 1.15
# go-version: 1.16
# - uses: technote-space/get-diff-action@v4.2
# id: git_diff
# with:
@ -62,7 +62,7 @@ jobs:
- uses: actions/checkout@v2.3.4
- uses: actions/setup-go@v2.1.3
with:
go-version: 1.15
go-version: 1.16
- uses: technote-space/get-diff-action@v4.2
id: git_diff
with:
@ -101,7 +101,7 @@ jobs:
- uses: actions/checkout@v2.3.4
- uses: actions/setup-go@v2.1.3
with:
go-version: 1.15
go-version: 1.16
- uses: technote-space/get-diff-action@v4.2
id: git_diff
with:
@ -140,7 +140,7 @@ jobs:
- uses: actions/checkout@v2.3.4
- uses: actions/setup-go@v2.1.3
with:
go-version: 1.15
go-version: 1.16
- uses: technote-space/get-diff-action@v4.2
id: git_diff
with:
@ -179,7 +179,7 @@ jobs:
- uses: actions/checkout@v2.3.4
- uses: actions/setup-go@v2.1.3
with:
go-version: 1.15
go-version: 1.16
- uses: technote-space/get-diff-action@v4.2
id: git_diff
with:
@ -218,7 +218,7 @@ jobs:
# - uses: actions/checkout@v2.3.4
# - uses: actions/setup-go@v2.1.3
# with:
# go-version: 1.15
# go-version: 1.16
# - uses: technote-space/get-diff-action@v4.2
# id: git_diff
# with:

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/tharsis/ethermint
go 1.15
go 1.16
require (
github.com/armon/go-metrics v0.3.9

6
scripts/gen-tests-artifacts.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
# prepare sloc v0.5.17 in PATH
solc --combined-json bin,abi --allow-paths . ./tests/solidity/suites/staking/contracts/test/mocks/StandardTokenMock.sol \
| jq ".contracts.\"./tests/solidity/suites/staking/contracts/test/mocks/StandardTokenMock.sol:StandardTokenMock\"" \
> x/evm/keeper/ERC20Contract.json

File diff suppressed because one or more lines are too long

View File

@ -406,6 +406,10 @@ func (k Keeper) EstimateGas(c context.Context, req *types.EthCallRequest) (*type
ctx := sdk.UnwrapSDKContext(c)
k.WithContext(ctx)
if req.GasCap < ethparams.TxGas {
return nil, status.Error(codes.InvalidArgument, "gas cap cannot be lower than 21,000")
}
var args types.CallArgs
err := json.Unmarshal(req.Args, &args)
if err != nil {

View File

@ -1,13 +1,19 @@
package keeper_test
import (
_ "embed"
"encoding/json"
"fmt"
"math/big"
"google.golang.org/grpc/metadata"
"github.com/ethereum/go-ethereum/accounts/abi"
"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"
"github.com/ethereum/go-ethereum/crypto"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -21,6 +27,29 @@ import (
//Not valid Ethereum address
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() {
var (
req *types.QueryAccountRequest
@ -686,3 +715,122 @@ 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: uint64(ethermint.DefaultRPCGasLimit),
})
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() {
ctx := sdk.WrapSDKContext(suite.ctx)
gasHelper := hexutil.Uint64(20000)
var (
args types.CallArgs
gasCap uint64
)
testCases := []struct {
msg string
malleate func()
expPass bool
expGas uint64
}{
// should success, because transfer value is zero
{"default args", func() {
args = types.CallArgs{To: &common.Address{}}
}, true, 21000},
// should fail, because the default From address(zero address) don't have fund
{"not enough balance", func() {
args = types.CallArgs{To: &common.Address{}, Value: (*hexutil.Big)(big.NewInt(100))}
}, false, 0},
// should success, enough balance now
{"enough balance", func() {
args = types.CallArgs{To: &common.Address{}, From: &suite.address, Value: (*hexutil.Big)(big.NewInt(100))}
}, false, 0},
// should success, because gas limit lower than 21000 is ignored
{"gas exceed allowance", func() {
args = types.CallArgs{To: &common.Address{}, Gas: &gasHelper}
}, true, 21000},
// should fail, invalid gas cap
{"gas exceed global allowance", func() {
args = types.CallArgs{To: &common.Address{}}
gasCap = 20000
}, false, 0},
// estimate gas of an erc20 contract deployment, the exact gas number is checked with geth
{"contract deployment", func() {
ctorArgs, err := contractABI.Pack("", &suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
suite.Require().NoError(err)
data := append(contractBin, ctorArgs...)
args = types.CallArgs{
From: &suite.address,
Data: (*hexutil.Bytes)(&data),
}
}, true, 1144643},
// estimate gas of an erc20 transfer, the exact gas number is checked with geth
{"erc20 transfer", func() {
contractAddr := suite.deployTestContract(suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
suite.Commit()
transferData, err := contractABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
suite.Require().NoError(err)
args = types.CallArgs{To: &contractAddr, From: &suite.address, Data: (*hexutil.Bytes)(&transferData)}
}, true, 51880},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest()
gasCap = ethermint.DefaultRPCGasLimit
tc.malleate()
args, err := json.Marshal(&args)
suite.Require().NoError(err)
req := types.EthCallRequest{
Args: args,
GasCap: gasCap,
}
rsp, err := suite.queryClient.EstimateGas(ctx, &req)
if tc.expPass {
suite.Require().NoError(err)
suite.Require().Equal(tc.expGas, rsp.Gas)
} else {
suite.Require().Error(err)
}
})
}
}

View File

@ -8,13 +8,19 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/tharsis/ethermint/app"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
"github.com/tharsis/ethermint/encoding"
"github.com/tharsis/ethermint/tests"
ethermint "github.com/tharsis/ethermint/types"
"github.com/tharsis/ethermint/x/evm/types"
@ -23,15 +29,11 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
const addrHex = "0x756F45E3FA69347A9A973A725E3C98bC4db0b4c1"
const hex = "0x0d87a3a5f73140f46aac1bf419263e4e94e87c292f25007700ab7f2060e2af68"
var (
hash = ethcmn.FromHex(hex)
)
var testTokens = sdk.NewIntWithDecimal(1000, 18)
type KeeperTestSuite struct {
suite.Suite
@ -45,16 +47,33 @@ type KeeperTestSuite struct {
// for generate test tx
clientCtx client.Context
ethSigner ethtypes.Signer
appCodec codec.Codec
signer keyring.Signer
}
func (suite *KeeperTestSuite) SetupTest() {
checkTx := false
suite.app = app.Setup(checkTx)
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 1, ChainID: "ethermint-1", Time: time.Now().UTC()})
suite.app.EvmKeeper.WithContext(suite.ctx)
// account key
priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err)
suite.address = ethcmn.BytesToAddress(priv.PubKey().Address().Bytes())
suite.signer = tests.NewSigner(priv)
suite.address = ethcmn.HexToAddress(addrHex)
// consensus key
priv, err = ethsecp256k1.GenerateKey()
suite.Require().NoError(err)
suite.consAddress = sdk.ConsAddress(priv.PubKey().Address())
suite.app = app.Setup(checkTx)
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{
Height: 1,
ChainID: "ethermint-1",
Time: time.Now().UTC(),
ProposerAddress: suite.consAddress.Bytes(),
})
suite.app.EvmKeeper.WithContext(suite.ctx)
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, suite.app.EvmKeeper)
@ -67,17 +86,64 @@ func (suite *KeeperTestSuite) SetupTest() {
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)
priv, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err)
valAddr := sdk.ValAddress(suite.address.Bytes())
validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{})
suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
suite.Require().NoError(err)
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
suite.Require().NoError(err)
suite.app.StakingKeeper.SetValidator(suite.ctx, validator)
suite.consAddress = sdk.ConsAddress(priv.PubKey().Address())
encodingConfig := encoding.MakeConfig(app.ModuleBasics)
suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig)
suite.ethSigner = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
suite.appCodec = encodingConfig.Marshaler
// mint some tokens to coinbase address
_, bankKeeper := suite.initKeepersWithmAccPerms()
ctx := sdk.WrapSDKContext(suite.ctx)
rsp, err := suite.queryClient.Params(ctx, &types.QueryParamsRequest{})
suite.Require().NoError(err)
initCoin := sdk.NewCoins(sdk.NewCoin(rsp.Params.EvmDenom, testTokens))
err = simapp.FundAccount(bankKeeper, suite.ctx, acc.GetAddress(), initCoin)
suite.Require().NoError(err)
}
// Commit and begin new block
func (suite *KeeperTestSuite) Commit() {
suite.app.Commit()
header := suite.ctx.BlockHeader()
header.Height += 1
suite.app.BeginBlock(abci.RequestBeginBlock{
Header: header,
})
// update ctx
suite.ctx = suite.app.BaseApp.NewContext(false, header)
suite.app.EvmKeeper.WithContext(suite.ctx)
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, suite.app.EvmKeeper)
suite.queryClient = types.NewQueryClient(queryHelper)
}
// initKeepersWithmAccPerms construct a bank keeper that can mint tokens out of thin air
func (suite *KeeperTestSuite) initKeepersWithmAccPerms() (authkeeper.AccountKeeper, bankkeeper.BaseKeeper) {
maccPerms := app.GetMaccPerms()
maccPerms[authtypes.Burner] = []string{authtypes.Burner}
maccPerms[authtypes.Minter] = []string{authtypes.Minter}
authKeeper := authkeeper.NewAccountKeeper(
suite.appCodec, suite.app.GetKey(types.StoreKey), suite.app.GetSubspace(types.ModuleName),
authtypes.ProtoBaseAccount, maccPerms,
)
keeper := bankkeeper.NewBaseKeeper(
suite.appCodec, suite.app.GetKey(types.StoreKey), authKeeper,
suite.app.GetSubspace(types.ModuleName), map[string]bool{},
)
return authKeeper, keeper
}
func TestKeeperTestSuite(t *testing.T) {

View File

@ -20,7 +20,12 @@ func (suite *KeeperTestSuite) TestGetCoinbaseAddress() {
}{
{
"validator not found",
func() {},
func() {
header := suite.ctx.BlockHeader()
header.ProposerAddress = []byte{}
suite.ctx = suite.ctx.WithBlockHeader(header)
suite.app.EvmKeeper.WithContext(suite.ctx)
},
false,
},
{