diff --git a/api/coreservice.go b/api/coreservice.go index 3ec74fad9e..89cf665353 100644 --- a/api/coreservice.go +++ b/api/coreservice.go @@ -1438,7 +1438,6 @@ func (core *coreService) EstimateExecutionGasConsumption(ctx context.Context, sc return 0, status.Error(codes.InvalidArgument, err.Error()) } sc.SetNonce(state.PendingNonce()) - sc.SetGasPrice(big.NewInt(0)) blockGasLimit := core.bc.Genesis().BlockGasLimit sc.SetGasLimit(blockGasLimit) enough, receipt, err := core.isGasLimitEnough(ctx, callerAddr, sc) diff --git a/api/grpcserver.go b/api/grpcserver.go index 51df6128d3..494d18d2d4 100644 --- a/api/grpcserver.go +++ b/api/grpcserver.go @@ -401,6 +401,17 @@ func (svr *gRPCHandler) EstimateActionGasConsumption(ctx context.Context, in *io if err := sc.LoadProto(in.GetExecution()); err != nil { return nil, status.Error(codes.InvalidArgument, err.Error()) } + var ( + gasPrice *big.Int = big.NewInt(0) + ok bool + ) + if in.GetGasPrice() != "" { + gasPrice, ok = big.NewInt(0).SetString(in.GetGasPrice(), 10) + if !ok { + return nil, status.Error(codes.InvalidArgument, "invalid gas price") + } + } + sc.SetGasPrice(gasPrice) ret, err := svr.coreService.EstimateExecutionGasConsumption(ctx, sc, callerAddr) if err != nil { return nil, err diff --git a/api/web3server.go b/api/web3server.go index ee25da35f7..cd73740844 100644 --- a/api/web3server.go +++ b/api/web3server.go @@ -345,7 +345,7 @@ func (svr *web3Handler) getTransactionCount(in *gjson.Result) (interface{}, erro } func (svr *web3Handler) call(in *gjson.Result) (interface{}, error) { - callerAddr, to, gasLimit, value, data, err := parseCallObject(in) + callerAddr, to, gasLimit, gasPrice, value, data, err := parseCallObject(in) if err != nil { return nil, err } @@ -382,7 +382,7 @@ func (svr *web3Handler) call(in *gjson.Result) (interface{}, error) { } return "0x" + ret, nil } - exec, _ := action.NewExecution(to, 0, value, gasLimit, big.NewInt(0), data) + exec, _ := action.NewExecution(to, 0, value, gasLimit, gasPrice, data) ret, receipt, err := svr.coreService.ReadContract(context.Background(), callerAddr, exec) if err != nil { return nil, err @@ -394,20 +394,20 @@ func (svr *web3Handler) call(in *gjson.Result) (interface{}, error) { } func (svr *web3Handler) estimateGas(in *gjson.Result) (interface{}, error) { - from, to, gasLimit, value, data, err := parseCallObject(in) + from, to, gasLimit, gasPrice, value, data, err := parseCallObject(in) if err != nil { return nil, err } var tx *types.Transaction if len(to) == 0 { - tx = types.NewContractCreation(0, value, gasLimit, big.NewInt(0), data) + tx = types.NewContractCreation(0, value, gasLimit, gasPrice, data) } else { toAddr, err := addrutil.IoAddrToEvmAddr(to) if err != nil { return "", err } - tx = types.NewTransaction(0, toAddr, value, gasLimit, big.NewInt(0), data) + tx = types.NewTransaction(0, toAddr, value, gasLimit, gasPrice, data) } elp, err := svr.ethTxToEnvelope(tx) if err != nil { @@ -972,7 +972,7 @@ func (svr *web3Handler) traceCall(ctx context.Context, in *gjson.Result) (interf callerAddr address.Address ) blkNumOrHashObj, options := in.Get("params.1"), in.Get("params.2") - callerAddr, contractAddr, gasLimit, value, callData, err = parseCallObject(in) + callerAddr, contractAddr, gasLimit, _, value, callData, err = parseCallObject(in) if err != nil { return nil, err } diff --git a/api/web3server_utils.go b/api/web3server_utils.go index bfa9676d37..86797c0280 100644 --- a/api/web3server_utils.go +++ b/api/web3server_utils.go @@ -291,11 +291,12 @@ func parseLogRequest(in gjson.Result) (*filterObject, error) { return &logReq, nil } -func parseCallObject(in *gjson.Result) (address.Address, string, uint64, *big.Int, []byte, error) { +func parseCallObject(in *gjson.Result) (address.Address, string, uint64, *big.Int, *big.Int, []byte, error) { var ( from address.Address to string gasLimit uint64 + gasPrice *big.Int = big.NewInt(0) value *big.Int = big.NewInt(0) data []byte err error @@ -305,14 +306,14 @@ func parseCallObject(in *gjson.Result) (address.Address, string, uint64, *big.In fromStr = "0x0000000000000000000000000000000000000000" } if from, err = ethAddrToIoAddr(fromStr); err != nil { - return nil, "", 0, nil, nil, err + return nil, "", 0, nil, nil, nil, err } toStr := in.Get("params.0.to").String() if toStr != "" { ioAddr, err := ethAddrToIoAddr(toStr) if err != nil { - return nil, "", 0, nil, nil, err + return nil, "", 0, nil, nil, nil, err } to = ioAddr.String() } @@ -320,7 +321,15 @@ func parseCallObject(in *gjson.Result) (address.Address, string, uint64, *big.In gasStr := in.Get("params.0.gas").String() if gasStr != "" { if gasLimit, err = hexStringToNumber(gasStr); err != nil { - return nil, "", 0, nil, nil, err + return nil, "", 0, nil, nil, nil, err + } + } + + gasPriceStr := in.Get("params.0.gasPrice").String() + if gasPriceStr != "" { + var ok bool + if gasPrice, ok = new(big.Int).SetString(util.Remove0xPrefix(gasPriceStr), 16); !ok { + return nil, "", 0, nil, nil, nil, errors.Wrapf(errUnkownType, "gasPrice: %s", gasPriceStr) } } @@ -328,7 +337,7 @@ func parseCallObject(in *gjson.Result) (address.Address, string, uint64, *big.In if valStr != "" { var ok bool if value, ok = new(big.Int).SetString(util.Remove0xPrefix(valStr), 16); !ok { - return nil, "", 0, nil, nil, errors.Wrapf(errUnkownType, "value: %s", valStr) + return nil, "", 0, nil, nil, nil, errors.Wrapf(errUnkownType, "value: %s", valStr) } } @@ -338,7 +347,7 @@ func parseCallObject(in *gjson.Result) (address.Address, string, uint64, *big.In } else { data = common.FromHex(in.Get("params.0.data").String()) } - return from, to, gasLimit, value, data, nil + return from, to, gasLimit, gasPrice, value, data, nil } func (svr *web3Handler) getLogQueryRange(fromStr, toStr string, logHeight uint64) (from uint64, to uint64, hasNewLogs bool, err error) { diff --git a/api/web3server_utils_test.go b/api/web3server_utils_test.go index c346edf3fc..ee65750523 100644 --- a/api/web3server_utils_test.go +++ b/api/web3server_utils_test.go @@ -19,6 +19,7 @@ func TestParseCallObject(t *testing.T) { from string to string gasLimit uint64 + gasPrice *big.Int value *big.Int data []byte err error @@ -37,6 +38,7 @@ func TestParseCallObject(t *testing.T) { from: address.ZeroAddress, to: "io10sfcvmuj2000083qqd8d6qg7r457vll9gly090", gasLimit: 20000, + gasPrice: new(big.Int).SetInt64(1000000000000), value: new(big.Int).SetInt64(1), data: []byte{0x6d, 0x4c, 0xe6, 0x3c}, }, @@ -54,6 +56,7 @@ func TestParseCallObject(t *testing.T) { from: address.ZeroAddress, to: "io10sfcvmuj2000083qqd8d6qg7r457vll9gly090", gasLimit: 20000, + gasPrice: new(big.Int).SetInt64(1000000000000), value: new(big.Int).SetInt64(1), data: []byte{0x6d, 0x4c, 0xe6, 0x3c}, }, @@ -62,10 +65,11 @@ func TestParseCallObject(t *testing.T) { for _, test := range testData { t.Run(test.name, func(t *testing.T) { in := gjson.Parse(test.input) - from, to, gasLimit, value, data, err := parseCallObject(&in) + from, to, gasLimit, gasPrice, value, data, err := parseCallObject(&in) require.Equal(test.from, from.String()) require.Equal(test.to, to) require.Equal(test.gasLimit, gasLimit) + require.Equal(test.gasPrice, gasPrice) require.Equal(test.value, value) require.Equal(test.data, data) require.Equal(test.err, err) diff --git a/go.mod b/go.mod index 811b96551a..63cdc3b262 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/iotexproject/iotex-address v0.2.8 github.com/iotexproject/iotex-antenna-go/v2 v2.5.1 github.com/iotexproject/iotex-election v0.3.5-0.20210611041425-20ddf674363d - github.com/iotexproject/iotex-proto v0.5.15-0.20231108184801-bc89358f2344 + github.com/iotexproject/iotex-proto v0.5.15-0.20231122045957-12615a5b58b2 github.com/libp2p/go-libp2p-core v0.8.5 github.com/mackerelio/go-osstat v0.2.4 github.com/miguelmota/go-ethereum-hdwallet v0.1.1 diff --git a/go.sum b/go.sum index 46a9a1bc01..3cf3db3f68 100644 --- a/go.sum +++ b/go.sum @@ -513,6 +513,10 @@ github.com/iotexproject/iotex-election v0.3.5-0.20210611041425-20ddf674363d/go.m github.com/iotexproject/iotex-proto v0.5.0/go.mod h1:Xg6REkv+nTZN+OC22xXIQuqKdTWWHwOAJEXCoMpDwtI= github.com/iotexproject/iotex-proto v0.5.15-0.20231108184801-bc89358f2344 h1:Io64R5K/xpDIvvRXVzRq8Z1QQr/C+MN9780Jj2aj6u8= github.com/iotexproject/iotex-proto v0.5.15-0.20231108184801-bc89358f2344/go.mod h1:wQpCk3Df0fPID+K8ohiICGj+cWRmcQ3wanT+aSrnIPo= +github.com/iotexproject/iotex-proto v0.5.15-0.20231122045957-12615a5b58b2 h1:1XnGeZyXtaFXPravh8vvdWcWdUH7WhzFYPoongTfSdU= +github.com/iotexproject/iotex-proto v0.5.15-0.20231122045957-12615a5b58b2/go.mod h1:wQpCk3Df0fPID+K8ohiICGj+cWRmcQ3wanT+aSrnIPo= +github.com/iotexproject/iotex-proto v0.5.15 h1:9+6szZDQ1HhSFKyB2kVlVPXdCFAHHw72VVGcYXQ7P/w= +github.com/iotexproject/iotex-proto v0.5.15/go.mod h1:wQpCk3Df0fPID+K8ohiICGj+cWRmcQ3wanT+aSrnIPo= github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= github.com/ipfs/go-cid v0.0.2/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=