Skip to main content

reth_rpc_eth_api/helpers/
fee.rs

1//! Loads fee history from database. Helper trait for `eth_` fee and transaction RPC methods.
2
3use super::LoadBlock;
4use crate::FromEthApiError;
5use alloy_consensus::BlockHeader;
6use alloy_eips::eip7840::BlobParams;
7use alloy_primitives::U256;
8use alloy_rpc_types_eth::{BlockNumberOrTag, FeeHistory};
9use futures::{Future, StreamExt};
10use reth_chainspec::{ChainSpecProvider, EthChainSpec};
11use reth_primitives_traits::BlockBody;
12use reth_rpc_eth_types::{
13    fee_history::calculate_reward_percentiles_for_block, utils::checked_blob_gas_used_ratio,
14    EthApiError, FeeHistoryCache, FeeHistoryEntry, GasPriceOracle, RpcInvalidTransactionError,
15};
16use reth_storage_api::{
17    BlockIdReader, BlockNumReader, BlockReaderIdExt, HeaderProvider, ProviderHeader,
18};
19use tracing::debug;
20
21/// Fee related functions for the [`EthApiServer`](crate::EthApiServer) trait in the
22/// `eth_` namespace.
23pub trait EthFees:
24    LoadFee<
25    Provider: ChainSpecProvider<ChainSpec: EthChainSpec<Header = ProviderHeader<Self::Provider>>>,
26>
27{
28    /// Returns a suggestion for a gas price for legacy transactions.
29    ///
30    /// See also: <https://github.com/ethereum/pm/issues/328#issuecomment-853234014>
31    fn gas_price(&self) -> impl Future<Output = Result<U256, Self::Error>> + Send
32    where
33        Self: LoadBlock,
34    {
35        LoadFee::gas_price(self)
36    }
37
38    /// Returns a suggestion for a base fee for blob transactions.
39    fn blob_base_fee(&self) -> impl Future<Output = Result<U256, Self::Error>> + Send
40    where
41        Self: LoadBlock,
42    {
43        LoadFee::blob_base_fee(self)
44    }
45
46    /// Returns a suggestion for the priority fee (the tip)
47    fn suggested_priority_fee(&self) -> impl Future<Output = Result<U256, Self::Error>> + Send
48    where
49        Self: 'static,
50    {
51        LoadFee::suggested_priority_fee(self)
52    }
53
54    /// Reports the fee history, for the given amount of blocks, up until the given newest block.
55    ///
56    /// If `reward_percentiles` are provided the [`FeeHistory`] will include the _approximated_
57    /// rewards for the requested range.
58    fn fee_history(
59        &self,
60        mut block_count: u64,
61        mut newest_block: BlockNumberOrTag,
62        reward_percentiles: Option<Vec<f64>>,
63    ) -> impl Future<Output = Result<FeeHistory, Self::Error>> + Send {
64        async move {
65            if block_count == 0 {
66                return Ok(FeeHistory::default())
67            }
68
69            // ensure the given reward percentiles aren't excessive
70            if reward_percentiles.as_ref().map(|perc| perc.len() as u64) >
71                Some(self.gas_oracle().config().max_reward_percentile_count)
72            {
73                return Err(EthApiError::InvalidRewardPercentiles.into())
74            }
75
76            // If reward percentiles were specified, we
77            // need to validate that they are monotonically
78            // increasing and 0 <= p <= 100
79            // Note: The types used ensure that the percentiles are never < 0
80            if let Some(percentiles) = &reward_percentiles &&
81                (percentiles.iter().any(|p| *p < 0.0 || *p > 100.0) ||
82                    percentiles.windows(2).any(|w| w[0] > w[1]))
83            {
84                return Err(EthApiError::InvalidRewardPercentiles.into())
85            }
86
87            // See https://github.com/ethereum/go-ethereum/blob/2754b197c935ee63101cbbca2752338246384fec/eth/gasprice/feehistory.go#L218C8-L225
88            let max_fee_history = if reward_percentiles.is_none() {
89                self.gas_oracle().config().max_header_history
90            } else {
91                self.gas_oracle().config().max_block_history
92            };
93
94            if block_count > max_fee_history {
95                debug!(
96                    requested = block_count,
97                    truncated = max_fee_history,
98                    "Sanitizing fee history block count"
99                );
100                block_count = max_fee_history
101            }
102
103            if newest_block.is_pending() {
104                // cap the target block since we don't have fee history for the pending block
105                newest_block = BlockNumberOrTag::Latest;
106            }
107
108            // For explicit block numbers, validate against chain head before resolution
109            if let BlockNumberOrTag::Number(requested) = newest_block {
110                let latest_block =
111                    self.provider().best_block_number().map_err(Self::Error::from_eth_err)?;
112                if requested > latest_block {
113                    return Err(
114                        EthApiError::RequestBeyondHead { requested, head: latest_block }.into()
115                    )
116                }
117            }
118
119            let end_block = self
120                .provider()
121                .block_number_for_id(newest_block.into())
122                .map_err(Self::Error::from_eth_err)?
123                .ok_or(EthApiError::HeaderNotFound(newest_block.into()))?;
124
125            // need to add 1 to the end block to get the correct (inclusive) range
126            let end_block_plus = end_block + 1;
127            // Ensure that we would not be querying outside of genesis
128            if end_block_plus < block_count {
129                block_count = end_block_plus;
130            }
131
132            // Fetch the headers and ensure we got all of them
133            //
134            // Treat a request for 1 block as a request for `newest_block..=newest_block`,
135            // otherwise `newest_block - 2`
136            // NOTE: We ensured that block count is capped
137            let start_block = end_block_plus - block_count;
138
139            // Collect base fees, gas usage ratios and (optionally) reward percentile data
140            // Pre-allocate capacity: base_fee and blob_fee need +1 for the next block's fee
141            let mut base_fee_per_gas: Vec<u128> = Vec::with_capacity(block_count as usize + 1);
142            let mut gas_used_ratio: Vec<f64> = Vec::with_capacity(block_count as usize);
143
144            let mut base_fee_per_blob_gas: Vec<u128> = Vec::with_capacity(block_count as usize + 1);
145            let mut blob_gas_used_ratio: Vec<f64> = Vec::with_capacity(block_count as usize);
146
147            let mut rewards: Vec<Vec<u128>> = if reward_percentiles.is_some() {
148                Vec::with_capacity(block_count as usize)
149            } else {
150                Vec::new()
151            };
152
153            // Check if the requested range is within the cache bounds
154            let fee_entries = self.fee_history_cache().get_history(start_block, end_block).await;
155
156            if let Some(fee_entries) = fee_entries {
157                if fee_entries.len() != block_count as usize {
158                    return Err(EthApiError::InvalidBlockRange.into())
159                }
160
161                for entry in &fee_entries {
162                    base_fee_per_gas
163                        .push(entry.header.base_fee_per_gas().unwrap_or_default() as u128);
164                    gas_used_ratio.push(entry.gas_used_ratio);
165                    base_fee_per_blob_gas.push(entry.base_fee_per_blob_gas.unwrap_or_default());
166                    blob_gas_used_ratio.push(entry.blob_gas_used_ratio);
167
168                    if let Some(percentiles) = &reward_percentiles {
169                        let mut block_rewards = Vec::with_capacity(percentiles.len());
170                        for &percentile in percentiles {
171                            block_rewards.push(self.approximate_percentile(entry, percentile));
172                        }
173                        rewards.push(block_rewards);
174                    }
175                }
176                let last_entry = fee_entries.last().expect("is not empty");
177
178                // Also need to include the `base_fee_per_gas` and `base_fee_per_blob_gas` for the
179                // next block
180                base_fee_per_gas.push(
181                    self.provider()
182                        .chain_spec()
183                        .next_block_base_fee(&last_entry.header, last_entry.header.timestamp())
184                        .unwrap_or_default() as u128,
185                );
186
187                base_fee_per_blob_gas.push(last_entry.next_block_blob_fee().unwrap_or_default());
188            } else {
189                // read the requested header range
190                let headers = self.provider()
191                    .sealed_headers_range(start_block..=end_block)
192                    .map_err(Self::Error::from_eth_err)?;
193                if headers.len() != block_count as usize {
194                    return Err(EthApiError::InvalidBlockRange.into())
195                }
196
197                let chain_spec = self.provider().chain_spec();
198                for header in &headers {
199                    base_fee_per_gas.push(header.base_fee_per_gas().unwrap_or_default() as u128);
200                    gas_used_ratio.push(header.gas_used() as f64 / header.gas_limit() as f64);
201
202                    let blob_params = chain_spec
203                        .blob_params_at_timestamp(header.timestamp())
204                        .unwrap_or_else(BlobParams::cancun);
205
206                    base_fee_per_blob_gas.push(header.blob_fee(blob_params).unwrap_or_default());
207                    blob_gas_used_ratio.push(checked_blob_gas_used_ratio(
208                        header.blob_gas_used().unwrap_or_default(),
209                        blob_params.max_blob_gas_per_block(),
210                    ));
211                }
212
213                if let Some(percentiles) = reward_percentiles.as_ref().filter(|p| !p.is_empty()) {
214                    let hashes: Vec<_> = headers.iter().map(|h| h.hash()).collect();
215                    let mut stream =
216                        futures::stream::iter(hashes)
217                            .map(|hash| self.cache().get_block_and_receipts(hash))
218                            .buffered(4);
219                    let mut header_idx = 0;
220                    while let Some(result) = stream.next().await {
221                        let header = &headers[header_idx];
222                        header_idx += 1;
223                        let (block, receipts) = result
224                            .map_err(Self::Error::from_eth_err)?
225                            .ok_or(EthApiError::InvalidBlockRange)?;
226                        rewards.push(
227                            calculate_reward_percentiles_for_block(
228                                percentiles,
229                                header.gas_used(),
230                                header.base_fee_per_gas().unwrap_or_default(),
231                                block.body().transactions(),
232                                &receipts,
233                            )
234                            .unwrap_or_default(),
235                        );
236                    }
237                }
238
239                // The spec states that `base_fee_per_gas` "[..] includes the next block after the
240                // newest of the returned range, because this value can be derived from the
241                // newest block"
242                //
243                // The unwrap is safe since we checked earlier that we got at least 1 header.
244                let last_header = headers.last().expect("is present");
245                base_fee_per_gas.push(
246                    chain_spec
247                        .next_block_base_fee(last_header.header(), last_header.timestamp())
248                        .unwrap_or_default() as u128,
249                );
250                // Same goes for the `base_fee_per_blob_gas`:
251                // > "[..] includes the next block after the newest of the returned range, because this value can be derived from the newest block.
252                base_fee_per_blob_gas.push(
253                    last_header
254                    .maybe_next_block_blob_fee(
255                        chain_spec.blob_params_at_timestamp(last_header.timestamp())
256                    ).unwrap_or_default()
257                );
258            };
259
260            Ok(FeeHistory {
261                base_fee_per_gas,
262                gas_used_ratio,
263                base_fee_per_blob_gas,
264                blob_gas_used_ratio,
265                oldest_block: start_block,
266                reward: reward_percentiles.map(|_| rewards),
267            })
268        }
269    }
270
271    /// Approximates reward at a given percentile for a specific block
272    /// Based on the configured resolution
273    fn approximate_percentile(
274        &self,
275        entry: &FeeHistoryEntry<ProviderHeader<Self::Provider>>,
276        requested_percentile: f64,
277    ) -> u128 {
278        let resolution = self.fee_history_cache().resolution();
279        let rounded_percentile =
280            (requested_percentile * resolution as f64).round() / resolution as f64;
281        let clamped_percentile = rounded_percentile.clamp(0.0, 100.0);
282
283        // Calculate the index in the precomputed rewards array
284        let index = (clamped_percentile / (1.0 / resolution as f64)).round() as usize;
285        // Fetch the reward from the FeeHistoryEntry
286        entry.rewards.get(index).copied().unwrap_or_default()
287    }
288}
289
290/// Loads fee from database.
291///
292/// Behaviour shared by several `eth_` RPC methods, not exclusive to `eth_` fees RPC methods.
293pub trait LoadFee: LoadBlock
294where
295    Self::Provider: BlockReaderIdExt,
296{
297    /// Returns a handle for reading gas price.
298    ///
299    /// Data access in default (L1) trait method implementations.
300    fn gas_oracle(&self) -> &GasPriceOracle<Self::Provider>;
301
302    /// Returns a handle for reading fee history data from memory.
303    ///
304    /// Data access in default (L1) trait method implementations.
305    fn fee_history_cache(&self) -> &FeeHistoryCache<ProviderHeader<Self::Provider>>;
306
307    /// Returns the gas price if it is set, otherwise fetches a suggested gas price for legacy
308    /// transactions.
309    fn legacy_gas_price(
310        &self,
311        gas_price: Option<U256>,
312    ) -> impl Future<Output = Result<U256, Self::Error>> + Send {
313        async move {
314            match gas_price {
315                Some(gas_price) => Ok(gas_price),
316                None => {
317                    // fetch a suggested gas price
318                    self.gas_price().await
319                }
320            }
321        }
322    }
323
324    /// Returns the EIP-1559 fees if they are set, otherwise fetches a suggested gas price for
325    /// EIP-1559 transactions.
326    ///
327    /// Returns (`base_fee`, `priority_fee`)
328    fn eip1559_fees(
329        &self,
330        base_fee: Option<U256>,
331        max_priority_fee_per_gas: Option<U256>,
332    ) -> impl Future<Output = Result<(U256, U256), Self::Error>> + Send {
333        async move {
334            let base_fee = match base_fee {
335                Some(base_fee) => base_fee,
336                None => {
337                    // Derive the pending base fee from the latest header
338                    let latest = self
339                        .provider()
340                        .latest_header()
341                        .map_err(Self::Error::from_eth_err)?
342                        .ok_or(EthApiError::HeaderNotFound(BlockNumberOrTag::Latest.into()))?;
343                    let pending_base_fee = self
344                        .provider()
345                        .chain_spec()
346                        .next_block_base_fee(&latest, latest.timestamp())
347                        .ok_or(EthApiError::InvalidTransaction(
348                            RpcInvalidTransactionError::TxTypeNotSupported,
349                        ))?;
350                    U256::from(pending_base_fee)
351                }
352            };
353
354            let max_priority_fee_per_gas = match max_priority_fee_per_gas {
355                Some(max_priority_fee_per_gas) => max_priority_fee_per_gas,
356                None => self.suggested_priority_fee().await?,
357            };
358            Ok((base_fee, max_priority_fee_per_gas))
359        }
360    }
361
362    /// Returns the EIP-4844 blob fee if it is set, otherwise fetches a blob fee.
363    fn eip4844_blob_fee(
364        &self,
365        blob_fee: Option<U256>,
366    ) -> impl Future<Output = Result<U256, Self::Error>> + Send {
367        async move {
368            match blob_fee {
369                Some(blob_fee) => Ok(blob_fee),
370                None => self.blob_base_fee().await,
371            }
372        }
373    }
374
375    /// Returns a suggestion for a gas price for legacy transactions.
376    ///
377    /// See also: <https://github.com/ethereum/pm/issues/328#issuecomment-853234014>
378    fn gas_price(&self) -> impl Future<Output = Result<U256, Self::Error>> + Send {
379        async move {
380            let header = self.provider().latest_header().map_err(Self::Error::from_eth_err)?;
381            let suggested_tip = self.suggested_priority_fee().await?;
382            let base_fee = header.and_then(|h| h.base_fee_per_gas()).unwrap_or_default();
383            Ok(suggested_tip + U256::from(base_fee))
384        }
385    }
386
387    /// Returns a suggestion for a base fee for blob transactions.
388    fn blob_base_fee(&self) -> impl Future<Output = Result<U256, Self::Error>> + Send {
389        async move {
390            self.provider()
391                .latest_header()
392                .map_err(Self::Error::from_eth_err)?
393                .and_then(|h| {
394                    h.maybe_next_block_blob_fee(
395                        self.provider().chain_spec().blob_params_at_timestamp(h.timestamp()),
396                    )
397                })
398                .ok_or(EthApiError::ExcessBlobGasNotSet.into())
399                .map(U256::from)
400        }
401    }
402
403    /// Returns a suggestion for the priority fee (the tip)
404    fn suggested_priority_fee(&self) -> impl Future<Output = Result<U256, Self::Error>> + Send
405    where
406        Self: 'static,
407    {
408        async move { self.gas_oracle().suggest_tip_cap().await.map_err(Self::Error::from_eth_err) }
409    }
410}