Skip to content

Commit

Permalink
Merge pull request #286 from OffchainLabs/update-pendingblk-blkctxtime
Browse files Browse the repository at this point in the history
Update blockContext's time field for pending blocks to match current time. Enable eth_estimateGas and eth_call rpcs to utilize this
  • Loading branch information
PlasmaPower authored Feb 5, 2024
2 parents db9ad5a + 517ea7a commit d933404
Showing 1 changed file with 17 additions and 1 deletion.
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

0 comments on commit d933404

Please sign in to comment.