Skip to content

Commit

Permalink
move estimateGasErrorRatio to gasestimator and export it
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshvanahalli committed Apr 17, 2024
1 parent 976a8e9 commit 077f2db
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
4 changes: 4 additions & 0 deletions eth/gasestimator/gasestimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import (
"github.com/ethereum/go-ethereum/params"
)

// EstimateGasErrorRatio is the amount of overestimation eth_estimateGas is
// allowed to produce in order to speed up calculations.
const EstimateGasErrorRatio = 0.015

// Options are the contextual parameters to execute the requested call.
//
// Whilst it would be possible to pass a blockchain object that aggregates all
Expand Down
3 changes: 1 addition & 2 deletions graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ func TestGraphQLBlockSerialization(t *testing.T) {
// should return `estimateGas` as decimal
{
body: `{"query": "{block{ estimateGas(data:{}) }}"}`,
// want: `{"data":{"block":{"estimateGas":"0xd221"}}}`, Nitro needs estimateGas to provide exact estimation i.e gasestimatior's ErrorRatio option is zero
want: `{"data":{"block":{"estimateGas":"0xcf08"}}}`,
want: `{"data":{"block":{"estimateGas":"0xd221"}}}`,
code: 200,
},
// should return `status` as decimal
Expand Down
15 changes: 6 additions & 9 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ import (
"github.com/tyler-smith/go-bip39"
)

// estimateGasErrorRatio is the amount of overestimation eth_estimateGas is
// allowed to produce in order to speed up calculations.
const estimateGasErrorRatio = 0.015

func fallbackClientFor(b Backend, err error) types.FallbackClient {
if !errors.Is(err, types.ErrUseFallback) {
return nil
Expand Down Expand Up @@ -1284,11 +1280,12 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr

// Construct the gas estimator option from the user input
opts := &gasestimator.Options{
Config: b.ChainConfig(),
Chain: NewChainContext(ctx, b),
Header: header,
State: state,
Backend: b,
Config: b.ChainConfig(),
Chain: NewChainContext(ctx, b),
Header: header,
State: state,
Backend: b,
ErrorRatio: gasestimator.EstimateGasErrorRatio,
}
// Run the gas estimation andwrap any revertals into a custom return
call, err := args.ToMessage(gasCap, header, state, core.MessageGasEstimationMode)
Expand Down
3 changes: 2 additions & 1 deletion internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/gasestimator"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/internal/blocktest"
Expand Down Expand Up @@ -743,7 +744,7 @@ func TestEstimateGas(t *testing.T) {
t.Errorf("test %d: want no error, have %v", i, err)
continue
}
if float64(result) > float64(tc.want)*(1+estimateGasErrorRatio) {
if float64(result) > float64(tc.want)*(1+gasestimator.EstimateGasErrorRatio) {
t.Errorf("test %d, result mismatch, have\n%v\n, want\n%v\n", i, uint64(result), tc.want)
}
}
Expand Down

0 comments on commit 077f2db

Please sign in to comment.