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

feat: implement eth_feeHistory endpoint #502

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ptrus
Copy link
Member

@ptrus ptrus commented Jan 5, 2024

Fixes #501

Implements eth_feeHistory endpoint. The query supports returning the history for the maximum last 20 blocks (this arbitrarily set limit is allowed as per spec: Requested range of blocks. Clients will return less than the requested range if not all blocks are available.)

To avoid fetching multiple blocks, transactions, and receipts from DB on every fee_history query (which can be expensive) the fee_history for last 20 blocks is continuously pre-computed in the gas oracle (only the percentiles are computed on the fly).

TODO:

  • test

@ptrus ptrus force-pushed the ptrus/feature/fee-history branch 4 times, most recently from 09f6075 to bbe0ba6 Compare January 5, 2024 16:16
Copy link

codecov bot commented Jan 5, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 44 lines in your changes are missing coverage. Please review.

Project coverage is 62.45%. Comparing base (1c3f6a2) to head (bbe0ba6).
Report is 70 commits behind head on main.

❗ Current head bbe0ba6 differs from pull request most recent head 7c10b2e. Consider uploading reports for the commit 7c10b2e to get more accurate results

Files Patch % Lines
gas/backend.go 68.80% 28 Missing and 6 partials ⚠️
rpc/eth/api.go 41.17% 7 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #502      +/-   ##
==========================================
+ Coverage   62.31%   62.45%   +0.13%     
==========================================
  Files          38       37       -1     
  Lines        3962     4051      +89     
==========================================
+ Hits         2469     2530      +61     
- Misses       1285     1307      +22     
- Partials      208      214       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@CedarMist
Copy link
Member

CedarMist commented Feb 4, 2024

Metamask uses eth_feeHistory to calculate the different transaction priorities for gas price.

See metamask source:

This doesn't include the current txpool as BTC fee estimation does.

Recently we had a situation where many transactions were queued up, and we were not sure if the fee estimation was returning accurate data, or if the 'Speed Up' button in MetaMask would work correctly.

If eth_feeHistory isn't supported it will fallbcak to eth_gasPrice, in an ideal situation would be to return the average of the minimum and median for having the transaction included within the next block.

@ptrus ptrus force-pushed the ptrus/feature/fee-history branch 3 times, most recently from 0533998 to 7c10b2e Compare May 6, 2024 10:40
@ptrus ptrus force-pushed the ptrus/feature/fee-history branch 11 times, most recently from 606a775 to c3fd2da Compare October 29, 2024 09:28
@ptrus ptrus marked this pull request as ready for review October 29, 2024 09:34
@ptrus ptrus requested a review from kostko as a code owner October 29, 2024 09:34
@ptrus ptrus requested review from matevz and CedarMist and removed request for kostko October 29, 2024 09:35
@matevz matevz requested a review from ZigaMr November 5, 2024 16:10
@@ -146,6 +146,26 @@ func TestEth_GasPrice(t *testing.T) {
t.Logf("gas price: %v", price)
}

func TestEth_FeeHistory(t *testing.T) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion, maybe we can test with additional arguments here.
For example
{ name: "Basic query with percentiles", blockCount: 10, lastBlock: nil, percentiles: []float64{0.25, 0.5, 0.75, 1}, expectErr: false, },
{ name: "Query with no reward percentiles", blockCount: 5, lastBlock: nil, percentiles: nil, expectErr: false, },
{ name: "Query specific block range", blockCount: 3, lastBlock: big.NewInt(5), percentiles: []float64{0.5}, expectErr: false, },
{ name: "Invalid block count", blockCount: 0, lastBlock: nil, percentiles: []float64{0.5}, expectErr: true, },
{ name: "Invalid percentile", blockCount: 5, lastBlock: nil, percentiles: []float64{1.5}, // Invalid percentile > 1 expectErr: true, },

Copy link

@ZigaMr ZigaMr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@@ -185,4 +190,6 @@ func TestGasPriceOracle(t *testing.T) {

require.EqualValues(coreClient.minGasPrice.ToBigInt(), gasPriceOracle.GasPrice(), "oracle should return gas reported by the node query")
gasPriceOracle.Stop()

// TODO: emit blocks with transactions, to test fee history.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a difference testing it here compared to rpc_test?

)

// feeHistoryWindowSize is the number of recent blocks to store for the fee history query.
const feeHistoryWindowSize = 20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this configurable? How did you come up with 20?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arbitrarily chosen, mostly because computing the response is not the most effective, as you must go through all requested blocks. I can make it configurable, but I would keep the default low, unless there is a use case for a larger default.

Most API's I have looked at have a limit of 1024, but they always mention that the node can return less than the requested range if not all blocks are available. The reasonable usages of eth_feeHistory for gas estimation usually use small values:

// No transactions in the block.
if len(d.sortedTxs) == 0 {
for i := range rewards {
rewards[i] = (*hexutil.Big)(common.Big0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be the minimum required gas price for Sapphire (0.0000001 per gas unit) instead of 0? I'm not sure if there's another EVM call to fetch that info and dApps will fail otherwise on fresh chains.

// Query fee history.
feeHistory, err := ec.FeeHistory(context.Background(), 10, nil, []float64{0.25, 0.5, 0.75, 1})
require.NoError(t, err, "get fee history")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check that the fees were not zero. (the minimum gas price is set to 0.0000001 in the test docker image, so the fees should also be non-zero)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

eth_feeHistory not supported
4 participants