From 2cf585183b6bc365dd6008596ea05e0a22602a45 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Thu, 14 Mar 2024 16:03:34 +0800 Subject: [PATCH 1/3] Problem: Int64 is not checked in gas related api --- CHANGELOG.md | 1 + rpc/backend/chain_info.go | 2 +- rpc/backend/node_info.go | 6 +++--- rpc/backend/node_info_test.go | 12 ++++++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index feee883435..7ed320ae2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ - (evm) [#405](https://github.com/crypto-org-chain/ethermint/pull/405) Avoid duplicate cache events emitted from evm hooks. - (rpc) [#406](https://github.com/crypto-org-chain/ethermint/pull/406) Align filter rule for eth_getLogs when toBlock is newer than latest or extract error occurs. - (rpc) [#409](https://github.com/crypto-org-chain/ethermint/pull/409) Fix nextBaseFee in eth_feeHistory before fee market param change. +- (rpc) [#425](https://github.com/crypto-org-chain/ethermint/pull/425) Avoid Int64() out of bound error in gas related api. ### Improvements diff --git a/rpc/backend/chain_info.go b/rpc/backend/chain_info.go index 5aed9c5444..7ba147722f 100644 --- a/rpc/backend/chain_info.go +++ b/rpc/backend/chain_info.go @@ -303,7 +303,7 @@ func (b *Backend) FeeHistory( // Although we don't support tx prioritization yet, but we return a positive value to help client to // mitigate the base fee changes. func (b *Backend) SuggestGasTipCap(baseFee *big.Int) (*big.Int, error) { - if baseFee == nil { + if baseFee == nil || !baseFee.IsInt64() { // london hardfork not enabled or feemarket not enabled return big.NewInt(0), nil } diff --git a/rpc/backend/node_info.go b/rpc/backend/node_info.go index 2737c84a2c..695d1377ec 100644 --- a/rpc/backend/node_info.go +++ b/rpc/backend/node_info.go @@ -345,10 +345,10 @@ func (b *Backend) RPCMinGasPrice() int64 { } minGasPrice := b.cfg.GetMinGasPrices() - amt := minGasPrice.AmountOf(evmParams.Params.EvmDenom).TruncateInt64() - if amt == 0 { + amt := minGasPrice.AmountOf(evmParams.Params.EvmDenom).TruncateInt() + if !amt.IsInt64() || amt.IsZero() { return ethermint.DefaultGasPrice } - return amt + return amt.Int64() } diff --git a/rpc/backend/node_info_test.go b/rpc/backend/node_info_test.go index 56f1551c3e..6a9c27291e 100644 --- a/rpc/backend/node_info_test.go +++ b/rpc/backend/node_info_test.go @@ -4,6 +4,7 @@ import ( "fmt" "math/big" + "cosmossdk.io/math" tmrpcclient "github.com/cometbft/cometbft/rpc/client" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -41,6 +42,17 @@ func (suite *BackendTestSuite) TestRPCMinGasPrice() { ethermint.DefaultGasPrice, true, }, + { + "pass - min gas price exceeds math.MaxUint64", + func() { + queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient) + RegisterParamsWithoutHeader(queryClient, 1) + amt, _ := math.NewIntFromString("18446744073709551616") + suite.backend.cfg.SetMinGasPrices([]sdk.DecCoin{sdk.NewDecCoin(ethermint.AttoPhoton, amt)}) + }, + ethermint.DefaultGasPrice, + true, + }, } for _, tc := range testCases { From 970d5736687f7e271f4e16f2a29b5c862924abce Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 15 Mar 2024 09:40:19 +0800 Subject: [PATCH 2/3] Apply suggestions from code review --- rpc/backend/backend.go | 2 +- rpc/backend/call_tx.go | 2 +- rpc/backend/node_info.go | 10 +++++----- rpc/backend/node_info_test.go | 10 ++++++---- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/rpc/backend/backend.go b/rpc/backend/backend.go index a9d39d9256..9f7e65ccc6 100644 --- a/rpc/backend/backend.go +++ b/rpc/backend/backend.go @@ -71,7 +71,7 @@ type EVMBackend interface { RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection RPCEVMTimeout() time.Duration // global timeout for eth_call over rpc: DoS protection RPCTxFeeCap() float64 // RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for send-transaction variants. The unit is ether. - RPCMinGasPrice() int64 + RPCMinGasPrice() *big.Int // Sign Tx Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error) diff --git a/rpc/backend/call_tx.go b/rpc/backend/call_tx.go index 01c3e71f14..c97e1ce5d1 100644 --- a/rpc/backend/call_tx.go +++ b/rpc/backend/call_tx.go @@ -432,7 +432,7 @@ func (b *Backend) GasPrice() (*hexutil.Big, error) { } result = result.Add(result, head.BaseFee) } else { - result = big.NewInt(b.RPCMinGasPrice()) + result = b.RPCMinGasPrice() } // return at least GlobalMinGasPrice from FeeMarket module diff --git a/rpc/backend/node_info.go b/rpc/backend/node_info.go index 695d1377ec..14972ab3e0 100644 --- a/rpc/backend/node_info.go +++ b/rpc/backend/node_info.go @@ -338,17 +338,17 @@ func (b *Backend) RPCBlockRangeCap() int32 { // RPCMinGasPrice returns the minimum gas price for a transaction obtained from // the node config. If set value is 0, it will default to 20. -func (b *Backend) RPCMinGasPrice() int64 { +func (b *Backend) RPCMinGasPrice() *big.Int { evmParams, err := b.queryClient.Params(b.ctx, &evmtypes.QueryParamsRequest{}) if err != nil { - return ethermint.DefaultGasPrice + return new(big.Int).SetInt64(ethermint.DefaultGasPrice) } minGasPrice := b.cfg.GetMinGasPrices() amt := minGasPrice.AmountOf(evmParams.Params.EvmDenom).TruncateInt() - if !amt.IsInt64() || amt.IsZero() { - return ethermint.DefaultGasPrice + if amt.IsZero() { + return new(big.Int).SetInt64(ethermint.DefaultGasPrice) } - return amt.Int64() + return amt.BigInt() } diff --git a/rpc/backend/node_info_test.go b/rpc/backend/node_info_test.go index 6a9c27291e..1210670f08 100644 --- a/rpc/backend/node_info_test.go +++ b/rpc/backend/node_info_test.go @@ -18,10 +18,12 @@ import ( ) func (suite *BackendTestSuite) TestRPCMinGasPrice() { + defaultPrice := new(big.Int).SetInt64(ethermint.DefaultGasPrice) + bigPrice, _ := new(big.Int).SetString("18446744073709551616", 10) testCases := []struct { name string registerMock func() - expMinGasPrice int64 + expMinGasPrice *big.Int expPass bool }{ { @@ -30,7 +32,7 @@ func (suite *BackendTestSuite) TestRPCMinGasPrice() { queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient) RegisterParamsWithoutHeaderError(queryClient, 1) }, - ethermint.DefaultGasPrice, + defaultPrice, true, }, { @@ -39,7 +41,7 @@ func (suite *BackendTestSuite) TestRPCMinGasPrice() { queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient) RegisterParamsWithoutHeader(queryClient, 1) }, - ethermint.DefaultGasPrice, + defaultPrice, true, }, { @@ -50,7 +52,7 @@ func (suite *BackendTestSuite) TestRPCMinGasPrice() { amt, _ := math.NewIntFromString("18446744073709551616") suite.backend.cfg.SetMinGasPrices([]sdk.DecCoin{sdk.NewDecCoin(ethermint.AttoPhoton, amt)}) }, - ethermint.DefaultGasPrice, + bigPrice, true, }, } From f437aea65752ee7191355de23c74c9ec01914011 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 15 Mar 2024 09:50:35 +0800 Subject: [PATCH 3/3] revert --- rpc/backend/chain_info.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpc/backend/chain_info.go b/rpc/backend/chain_info.go index 7ba147722f..5aed9c5444 100644 --- a/rpc/backend/chain_info.go +++ b/rpc/backend/chain_info.go @@ -303,7 +303,7 @@ func (b *Backend) FeeHistory( // Although we don't support tx prioritization yet, but we return a positive value to help client to // mitigate the base fee changes. func (b *Backend) SuggestGasTipCap(baseFee *big.Int) (*big.Int, error) { - if baseFee == nil || !baseFee.IsInt64() { + if baseFee == nil { // london hardfork not enabled or feemarket not enabled return big.NewInt(0), nil }