diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 8f5f291c07..87a18f131b 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1165,6 +1165,20 @@ func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.S return result, nil } +func updateHeaderForPendingBlocks(blockNrOrHash rpc.BlockNumberOrHash, header *types.Header) *types.Header { + if blockNrOrHash.BlockNumber != nil && + *blockNrOrHash.BlockNumber == rpc.PendingBlockNumber { + headerCopy := *header + now := uint64(time.Now().Unix()) + if now > headerCopy.Time { + headerCopy.Time = now + } + headerCopy.Number = new(big.Int).Add(headerCopy.Number, common.Big1) + return &headerCopy + } + return header +} + func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *StateOverride, blockOverrides *BlockOverrides, timeout time.Duration, globalGasCap uint64, runMode core.MessageRunMode) (*core.ExecutionResult, error) { defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now()) @@ -1172,6 +1186,7 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash if state == nil || err != nil { return nil, err } + header = updateHeaderForPendingBlocks(blockNrOrHash, header) return doCall(ctx, b, args, state, header, overrides, blockOverrides, timeout, globalGasCap, runMode) } @@ -1303,6 +1318,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr if err := overrides.Apply(state); err != nil { return 0, err } + header = updateHeaderForPendingBlocks(blockNrOrHash, header) // Recap the highest gas limit with account's available balance. if feeCap.BitLen() != 0 { @@ -1399,7 +1415,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr // value is capped by both `args.Gas` (if non-nil & non-zero) and the backend's RPCGasCap // configuration (if non-zero). func (s *BlockChainAPI) EstimateGas(ctx context.Context, args TransactionArgs, blockNrOrHash *rpc.BlockNumberOrHash, overrides *StateOverride) (hexutil.Uint64, error) { - bNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber) + bNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) if blockNrOrHash != nil { bNrOrHash = *blockNrOrHash }