Skip to content

Commit

Permalink
internal/ethapi: EstimateGas should use LatestBlockNumber by default (#…
Browse files Browse the repository at this point in the history
…20)

### Description

upstream PR:
[go-ethereum#24363](ethereum/go-ethereum#24363)

We met a problem that if the `args TransactionArgs` depends on the
latest committed block N, `PendingBlockNumber` is still N-1, which
causes the estimation to fail.

`LatestBlockNumber` is a better default value IMO.


ethereum/go-ethereum#24363 (comment)
:

```
if use pendingblocknumber,then in code:
block, err := b.BlockByNumberOrHash(ctx, blockNrOrHash)
you may get error or "block not found".
```
  • Loading branch information
0xcb9ff9 committed Sep 3, 2023
1 parent 2e2db2f commit 38a0fee
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,8 +1052,8 @@ func (p *Pending) Call(ctx context.Context, args struct {
func (p *Pending) EstimateGas(ctx context.Context, args struct {
Data ethapi.TransactionArgs
}) (Long, error) {
pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
gas, err := ethapi.DoEstimateGas(ctx, p.backend, args.Data, pendingBlockNr, p.backend.RPCGasCap())
latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
gas, err := ethapi.DoEstimateGas(ctx, p.backend, args.Data, latestBlockNr, p.backend.RPCGasCap())
return Long(gas), err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
// EstimateGas returns an estimate of the amount of gas needed to execute the
// given transaction against the current pending block.
func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args TransactionArgs, blockNrOrHash *rpc.BlockNumberOrHash) (hexutil.Uint64, error) {
bNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber)
bNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
if blockNrOrHash != nil {
bNrOrHash = *blockNrOrHash
}
Expand Down

0 comments on commit 38a0fee

Please sign in to comment.