Skip to content

Commit

Permalink
[api] fix gas estimation calc bug (#2786)
Browse files Browse the repository at this point in the history
* fixed gas estimation bug in api.go
  • Loading branch information
Liuhaai authored Sep 17, 2021
1 parent feb462e commit d10dabe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ func (api *Server) estimateActionGasConsumptionForExecution(exec *iotextypes.Exe
}
estimatedGas := receipt.GasConsumed
enough, _, err = api.isGasLimitEnough(callerAddr, sc, nonce, estimatedGas)
if err != nil {
if err != nil && err != action.ErrOutOfGas {
return nil, status.Error(codes.Internal, err.Error())
}
if !enough {
Expand All @@ -1573,7 +1573,7 @@ func (api *Server) estimateActionGasConsumptionForExecution(exec *iotextypes.Exe
for low <= high {
mid := (low + high) / 2
enough, _, err = api.isGasLimitEnough(callerAddr, sc, nonce, mid)
if err != nil {
if err != nil && err != action.ErrOutOfGas {
return nil, status.Error(codes.Internal, err.Error())
}
if enough {
Expand Down
34 changes: 34 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2765,3 +2765,37 @@ func TestServer_GetActPoolActions(t *testing.T) {
_, err = svr.GetActPoolActions(context.Background(), &iotexapi.GetActPoolActionsRequest{ActionHashes: []string{hex.EncodeToString(h3[:])}})
require.Error(err)
}

func TestServer_GetEstimateGasSpecial(t *testing.T) {
require := require.New(t)
cfg := newConfig(t)

svr, bfIndexFile, err := createServer(cfg, true)
require.NoError(err)
defer func() {
testutil.CleanupPath(t, bfIndexFile)
}()

// deploy self-desturct contract
contractCode := "608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550610196806100606000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632e64cec11461004657806343d726d6146100645780636057361d1461006e575b600080fd5b61004e61008a565b60405161005b9190610124565b60405180910390f35b61006c610094565b005b610088600480360381019061008391906100ec565b6100cd565b005b6000600154905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b8060018190555050565b6000813590506100e681610149565b92915050565b6000602082840312156100fe57600080fd5b600061010c848285016100d7565b91505092915050565b61011e8161013f565b82525050565b60006020820190506101396000830184610115565b92915050565b6000819050919050565b6101528161013f565b811461015d57600080fd5b5056fea264697066735822122060e7a28baea4232a95074b94b50009d1d7b99302ef6556a1f3ce7f46a49f8cc064736f6c63430008000033"
contract, err := deployContract(svr, identityset.PrivateKey(13), 1, svr.bc.TipHeight(), contractCode)

require.NoError(err)
require.True(len(contract) > 0)

// call self-destuct func, which will invoke gas refund policy
data := "43d726d6"
byteCodes, err := hex.DecodeString(data)
require.NoError(err)
execution, err := action.NewExecution(contract, 2, big.NewInt(0), 0, big.NewInt(0), byteCodes)
require.NoError(err)
request := &iotexapi.EstimateActionGasConsumptionRequest{
Action: &iotexapi.EstimateActionGasConsumptionRequest_Execution{
Execution: execution.Proto(),
},
CallerAddress: identityset.Address(13).String(),
}
res, err := svr.EstimateActionGasConsumption(context.Background(), request)
require.NoError(err)
require.Equal(uint64(10777), res.Gas)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ require (

replace github.com/ethereum/go-ethereum => github.com/iotexproject/go-ethereum v0.4.0

replace golang.org/x/xerrors => golang.org/x/xerrors v0.0.0-20190212162355-a5947ffaace3
replace golang.org/x/xerrors => golang.org/x/xerrors v0.0.0-20190212162355-a5947ffaace3
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1588,4 +1588,4 @@ rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
sourcegraph.com/sourcegraph/go-diff v0.5.0/go.mod h1:kuch7UrkMzY0X+p9CRK03kfuPQ2zzQcaEFbx8wA8rck=
sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0=
sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0=

0 comments on commit d10dabe

Please sign in to comment.