Skip to content

Commit

Permalink
support gasPrice param in estimatedGas API (#3985)
Browse files Browse the repository at this point in the history
  • Loading branch information
millken authored Nov 23, 2023
1 parent 5df2c68 commit b31df43
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 15 deletions.
1 change: 0 additions & 1 deletion api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions api/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions api/web3server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 15 additions & 6 deletions api/web3server_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -305,30 +306,38 @@ 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()
}

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)
}
}

valStr := in.Get("params.0.value").String()
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)
}
}

Expand All @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion api/web3server_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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},
},
Expand All @@ -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},
},
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit b31df43

Please sign in to comment.