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

Merged
merged 2 commits into from
Nov 20, 2024
Merged

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 (configurable) 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 (configurable) 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 84.97409% with 29 lines in your changes missing coverage. Please review.

Project coverage is 59.04%. Comparing base (8395380) to head (1ca1883).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
gas/fee_history.go 79.27% 17 Missing and 6 partials ⚠️
gas/backend.go 90.00% 2 Missing and 1 partial ⚠️
rpc/eth/api.go 86.36% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #502      +/-   ##
==========================================
+ Coverage   58.59%   59.04%   +0.44%     
==========================================
  Files          39       40       +1     
  Lines        4229     4380     +151     
==========================================
+ Hits         2478     2586     +108     
- Misses       1545     1572      +27     
- Partials      206      222      +16     

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


🚨 Try these New Features:

@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
@ptrus ptrus force-pushed the ptrus/feature/fee-history branch from c3fd2da to f35d9e9 Compare October 30, 2024 09:05
@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
Member Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, added!

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

This was a lettover comment, removed

)

// 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:

Copy link
Member Author

@ptrus ptrus Nov 20, 2024

Choose a reason for hiding this comment

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

This is now also configurable via config.

// 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.

Copy link
Member Author

@ptrus ptrus Nov 20, 2024

Choose a reason for hiding this comment

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

This is covered by feehistory.BaseFee (not via the rewards field), and is handled correctly even if there's no transactions in recent blocks.

// 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)

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a check to ensure the returnedfeeHistory.BaseFee's are > 0.

@ptrus ptrus force-pushed the ptrus/feature/fee-history branch 8 times, most recently from 6e9d31b to eca135b Compare November 20, 2024 09:18
@ptrus ptrus force-pushed the ptrus/feature/fee-history branch from eca135b to 1ca1883 Compare November 20, 2024 09:29
@ptrus ptrus enabled auto-merge November 20, 2024 09:34
@ptrus ptrus merged commit ae0573a into main Nov 20, 2024
8 checks passed
@ptrus ptrus deleted the ptrus/feature/fee-history branch November 20, 2024 09:35
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