Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update blockContext's time field for pending blocks to match current time. Enable eth_estimateGas and eth_call rpcs to utilize this #286

Merged
merged 5 commits into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,13 +1165,28 @@ 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())

state, header, err := b.StateAndHeaderByNumberOrHash(ctx, 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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
Loading