Skip to content

feat: baseFeePerGas is not a constant#5178

Draft
bootcodes wants to merge 6 commits intohiero-ledger:mainfrom
bootcodes:5130-baseFeePerGas-should-be-computed-value
Draft

feat: baseFeePerGas is not a constant#5178
bootcodes wants to merge 6 commits intohiero-ledger:mainfrom
bootcodes:5130-baseFeePerGas-should-be-computed-value

Conversation

@bootcodes
Copy link
Copy Markdown
Contributor

@bootcodes bootcodes commented Mar 26, 2026

Description

This PR fixes an issue where baseFeePerGas in eth_getBlockByNumber, eth_getBlockByHash, and eth_feeHistory was always set to the live network gas price, regardless of which block was being queried. This caused incorrect values for historical blocks and made the field meaningless as a signal for actual fees paid in a block.

The fix computes baseFeePerGas accurately:

  • Non-empty blocks: weighted average of gas_used × gas_price across all transactions in the block, converted from tinybars to weibars.
  • Empty blocks (no transactions or gas_used = 0): falls back to GET /api/v1/network/fees?timestamp=lte:{block.timestamp.to}
  • eth_feeHistory (non-fixed path): the latest block uses the live gas price from gasPrice() to avoid mirror node propagation delays; historical blocks use the most recent transaction's gas_price in that block, with the same empty-block fallback.
  • gasUsedRatio: now computed as block.gas_used / obtainBlockGasLimit(block.hapi_version) instead of the hardcoded constant 0.5.

Need #5140 and #5134 to be merged first and this PR needs to be refactored to utilize the latest changes.

Related issue(s)

Fixes #5130

Testing Guide

  1. Run unit tests for the relay package:
  • npx lerna exec --scope @hashgraph/json-rpc-relay -- npx ts-mocha tests/lib/eth/eth_getBlockByNumber.spec.ts --exit

  • npx lerna exec --scope @hashgraph/json-rpc-relay -- npx ts-mocha tests/lib/eth/eth_feeHistory.spec.ts --exit

  1. Verify eth_getBlockByNumber returns a baseFeePerGas that reflects actual transaction gas prices for non-empty blocks, and a timestamped fee schedule value for empty blocks.

  2. Verify eth_feeHistory with ETH_FEE_HISTORY_FIXED=false returns baseFeePerGas values that differ across historical blocks when different fees were in effect, and that gasUsedRatio values reflect real block utilization rather than the constant 0.5.

Changes from original design (optional)

The original description specified GET /api/v1/network/exchangerate as the data source for empty-block fallback. That endpoint returns a HBAR/USD exchange rate (cents per HBAR), not a gas price. The implementation uses GET /api/v1/network/fees?timestamp=lte:{block.timestamp.to} instead, which is the direct source of the gas price in tinybars at a given timestamp.

Additional work needed (optional)

Acceptance test coverage for the ETH_FEE_HISTORY_FIXED=false code path requires starting the relay server with that flag set, which is a CI configuration change outside the scope of this PR.

Checklist

  • I've assigned an assignee to this PR and related issue(s) (if applicable)
  • I've assigned a label to this PR and related issue(s) (if applicable)
  • I've assigned a milestone to this PR and related issue(s) (if applicable)
  • I've updated documentation (code comments, README, etc. if applicable)
  • I've done sufficient testing (unit, integration, etc.)

…to CommonService.ts

Signed-off-by: Thomas Boot <thomas.boot@swirldslabs.com>
…kWorker.ts

Signed-off-by: Thomas Boot <thomas.boot@swirldslabs.com>
Signed-off-by: Thomas Boot <thomas.boot@swirldslabs.com>
Signed-off-by: Thomas Boot <thomas.boot@swirldslabs.com>
@bootcodes bootcodes added the enhancement New feature or request label Mar 27, 2026
@bootcodes bootcodes added this to the 0.77.0 milestone Mar 27, 2026
Signed-off-by: Thomas Boot <thomas.boot@swirldslabs.com>
Signed-off-by: Thomas Boot <thomas.boot@swirldslabs.com>
@bootcodes bootcodes self-assigned this Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

baseFeeperGas need to be a calculated value

1 participant