From cb6718700522795d7301a85cb2a8ccdf8d728800 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Tue, 4 Jan 2022 16:25:58 +0800 Subject: [PATCH 1/3] fix: set correct nonce in EthCall/EstimateGas Closes: #870 --- CHANGELOG.md | 1 + x/evm/keeper/grpc_query.go | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39124bc531..eb7e1d3e78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (evm) [tharsis#838](https://github.com/tharsis/ethermint/pull/838) Fix splitting of trace.Memory into 32 chunks. * (rpc) [tharsis#860](https://github.com/tharsis/ethermint/pull/860) Fix `eth_getLogs` when specify blockHash without address/topics, and limit the response size. * (rpc) [tharsis#865](https://github.com/tharsis/ethermint/pull/865) Fix RPC Filter parameters being ignored +* (evm) [tharsis#]() Set correct nonce in `EthCall` and `EstimateGas` grpc query. ## [v0.9.0] - 2021-12-01 diff --git a/x/evm/keeper/grpc_query.go b/x/evm/keeper/grpc_query.go index c9158dd0de..0eaebbc156 100644 --- a/x/evm/keeper/grpc_query.go +++ b/x/evm/keeper/grpc_query.go @@ -225,6 +225,10 @@ func (k Keeper) EthCall(c context.Context, req *types.EthCallRequest) (*types.Ms return nil, status.Error(codes.Internal, err.Error()) } + // ApplyMessageWithConfig expect correct nonce set in msg + nonce := k.GetNonce(args.GetFrom()) + args.Nonce = (*hexutil.Uint64)(&nonce) + msg, err := args.ToMessage(req.GasCap, cfg.BaseFee) if err != nil { return nil, status.Error(codes.InvalidArgument, err.Error()) @@ -290,6 +294,10 @@ func (k Keeper) EstimateGas(c context.Context, req *types.EthCallRequest) (*type return nil, status.Error(codes.Internal, "failed to load evm config") } + // ApplyMessageWithConfig expect correct nonce set in msg + nonce := k.GetNonce(args.GetFrom()) + args.Nonce = (*hexutil.Uint64)(&nonce) + // Create a helper to check if a gas allowance results in an executable transaction executable := func(gas uint64) (vmerror bool, rsp *types.MsgEthereumTxResponse, err error) { args.Gas = (*hexutil.Uint64)(&gas) From de9475fbe343d6b0950c227cc3ef883f2a580694 Mon Sep 17 00:00:00 2001 From: yihuang Date: Tue, 4 Jan 2022 16:35:47 +0800 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb7e1d3e78..80b6adc4d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,7 +66,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (evm) [tharsis#838](https://github.com/tharsis/ethermint/pull/838) Fix splitting of trace.Memory into 32 chunks. * (rpc) [tharsis#860](https://github.com/tharsis/ethermint/pull/860) Fix `eth_getLogs` when specify blockHash without address/topics, and limit the response size. * (rpc) [tharsis#865](https://github.com/tharsis/ethermint/pull/865) Fix RPC Filter parameters being ignored -* (evm) [tharsis#]() Set correct nonce in `EthCall` and `EstimateGas` grpc query. +* (evm) [tharsis#871](https://github.com/tharsis/ethermint/pull/871) Set correct nonce in `EthCall` and `EstimateGas` grpc query. ## [v0.9.0] - 2021-12-01 From abcfbffa8f98634a5b50ba1813eb63ed6048e1da Mon Sep 17 00:00:00 2001 From: HuangYi Date: Tue, 4 Jan 2022 17:52:33 +0800 Subject: [PATCH 3/3] unit test --- x/evm/keeper/grpc_query_test.go | 35 +++++++++++++++++++++++++++++++++ x/evm/keeper/keeper_test.go | 5 ++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index e770115949..582c8f74bd 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -11,6 +11,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/tharsis/ethermint/crypto/ethsecp256k1" + "github.com/tharsis/ethermint/server/config" ethermint "github.com/tharsis/ethermint/types" "github.com/tharsis/ethermint/x/evm/types" ) @@ -859,3 +861,36 @@ func (suite *KeeperTestSuite) TestTraceBlock() { suite.enableFeemarket = false // reset flag } + +func (suite *KeeperTestSuite) TestNonceInQuery() { + suite.SetupTest() + priv, err := ethsecp256k1.GenerateKey() + suite.Require().NoError(err) + address := common.BytesToAddress(priv.PubKey().Address().Bytes()) + suite.Require().Equal(uint64(0), suite.app.EvmKeeper.GetNonce(address)) + supply := sdk.NewIntWithDecimal(1000, 18).BigInt() + + // accupy nonce 0 + _ = suite.DeployTestContract(suite.T(), address, supply) + + // do an EthCall/EstimateGas with nonce 0 + ctorArgs, err := types.ERC20Contract.ABI.Pack("", address, supply) + data := append(types.ERC20Contract.Bin, ctorArgs...) + args, err := json.Marshal(&types.TransactionArgs{ + From: &address, + Data: (*hexutil.Bytes)(&data), + }) + suite.Require().NoError(err) + + _, err = suite.queryClient.EstimateGas(sdk.WrapSDKContext(suite.ctx), &types.EthCallRequest{ + Args: args, + GasCap: uint64(config.DefaultGasCap), + }) + suite.Require().NoError(err) + + _, err = suite.queryClient.EthCall(sdk.WrapSDKContext(suite.ctx), &types.EthCallRequest{ + Args: args, + GasCap: uint64(config.DefaultGasCap), + }) + suite.Require().NoError(err) +} diff --git a/x/evm/keeper/keeper_test.go b/x/evm/keeper/keeper_test.go index d29e73d18d..105016326b 100644 --- a/x/evm/keeper/keeper_test.go +++ b/x/evm/keeper/keeper_test.go @@ -220,9 +220,8 @@ func (suite *KeeperTestSuite) DeployTestContract(t require.TestingT, owner commo data := append(types.ERC20Contract.Bin, ctorArgs...) args, err := json.Marshal(&types.TransactionArgs{ - From: &suite.address, - Data: (*hexutil.Bytes)(&data), - Nonce: (*hexutil.Uint64)(&nonce), + From: &suite.address, + Data: (*hexutil.Bytes)(&data), }) require.NoError(t, err)