Skip to main content

reth_rpc_eth_types/
gas_oracle.rs

1//! An implementation of the eth gas price oracle, used for providing gas price estimates based on
2//! previous blocks.
3
4use super::{EthApiError, EthResult, EthStateCache, RpcInvalidTransactionError};
5use alloy_consensus::{constants::GWEI_TO_WEI, BlockHeader, Transaction, TxReceipt};
6use alloy_eips::BlockNumberOrTag;
7use alloy_primitives::{B256, U256};
8use alloy_rpc_types_eth::BlockId;
9use derive_more::{Deref, DerefMut, From, Into};
10use itertools::Itertools;
11use reth_rpc_server_types::{
12    constants,
13    constants::gas_oracle::{
14        DEFAULT_GAS_PRICE_BLOCKS, DEFAULT_GAS_PRICE_PERCENTILE, DEFAULT_IGNORE_GAS_PRICE,
15        DEFAULT_MAX_GAS_PRICE, MAX_HEADER_HISTORY, MAX_REWARD_PERCENTILE_COUNT, SAMPLE_NUMBER,
16    },
17};
18use reth_storage_api::{BlockReaderIdExt, NodePrimitivesProvider};
19use schnellru::{ByLength, LruMap};
20use serde::{Deserialize, Serialize};
21use std::fmt::{self, Debug, Formatter};
22use tokio::sync::Mutex;
23use tracing::warn;
24
25/// The default gas limit for `eth_call` and adjacent calls. See
26/// [`RPC_DEFAULT_GAS_CAP`](constants::gas_oracle::RPC_DEFAULT_GAS_CAP).
27pub const RPC_DEFAULT_GAS_CAP: GasCap = GasCap(constants::gas_oracle::RPC_DEFAULT_GAS_CAP);
28
29/// Settings for the [`GasPriceOracle`]
30#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
31#[serde(rename_all = "camelCase")]
32pub struct GasPriceOracleConfig {
33    /// The number of populated blocks to produce the gas price estimate
34    pub blocks: u32,
35
36    /// The percentile of gas prices to use for the estimate
37    pub percentile: u32,
38
39    /// The maximum number of headers to keep in the cache
40    pub max_header_history: u64,
41
42    /// The maximum number of blocks for estimating gas price
43    pub max_block_history: u64,
44
45    /// The maximum number for reward percentiles.
46    ///
47    /// This effectively limits how many transactions and receipts are fetched to compute the
48    /// reward percentile.
49    pub max_reward_percentile_count: u64,
50
51    /// The default gas price to use if there are no blocks to use
52    pub default_suggested_fee: Option<U256>,
53
54    /// The maximum gas price to use for the estimate
55    pub max_price: Option<U256>,
56
57    /// The minimum gas price, under which the sample will be ignored
58    pub ignore_price: Option<U256>,
59}
60
61impl Default for GasPriceOracleConfig {
62    fn default() -> Self {
63        Self {
64            blocks: DEFAULT_GAS_PRICE_BLOCKS,
65            percentile: DEFAULT_GAS_PRICE_PERCENTILE,
66            max_header_history: MAX_HEADER_HISTORY,
67            max_block_history: MAX_HEADER_HISTORY,
68            max_reward_percentile_count: MAX_REWARD_PERCENTILE_COUNT,
69            default_suggested_fee: None,
70            max_price: Some(DEFAULT_MAX_GAS_PRICE),
71            ignore_price: Some(DEFAULT_IGNORE_GAS_PRICE),
72        }
73    }
74}
75
76/// Calculates a gas price depending on recent blocks.
77#[derive(Debug)]
78pub struct GasPriceOracle<Provider>
79where
80    Provider: NodePrimitivesProvider,
81{
82    /// The type used to subscribe to block events and get block info
83    provider: Provider,
84    /// The cache for blocks
85    cache: EthStateCache<Provider::Primitives>,
86    /// The config for the oracle
87    oracle_config: GasPriceOracleConfig,
88    /// The price under which the sample will be ignored.
89    ignore_price: Option<u128>,
90    /// Stores the latest calculated price and its block hash and Cache stores the lowest effective
91    /// tip values of recent blocks
92    inner: Mutex<GasPriceOracleInner>,
93}
94
95impl<Provider> GasPriceOracle<Provider>
96where
97    Provider: BlockReaderIdExt + NodePrimitivesProvider,
98{
99    /// Creates and returns the [`GasPriceOracle`].
100    pub fn new(
101        provider: Provider,
102        mut oracle_config: GasPriceOracleConfig,
103        cache: EthStateCache<Provider::Primitives>,
104    ) -> Self {
105        // sanitize the percentile to be less than 100
106        if oracle_config.percentile > 100 {
107            warn!(prev_percentile = ?oracle_config.percentile, "Invalid configured gas price percentile, assuming 100.");
108            oracle_config.percentile = 100;
109        }
110        let ignore_price = oracle_config.ignore_price.map(|price| price.saturating_to());
111
112        // this is the number of blocks that we will cache the values for
113        let cached_values = (oracle_config.blocks * 5).max(oracle_config.max_block_history as u32);
114        let inner = Mutex::new(GasPriceOracleInner {
115            last_price: GasPriceOracleResult {
116                block_hash: B256::ZERO,
117                price: oracle_config
118                    .default_suggested_fee
119                    .unwrap_or_else(|| GasPriceOracleResult::default().price),
120            },
121            lowest_effective_tip_cache: EffectiveTipLruCache(LruMap::new(ByLength::new(
122                cached_values,
123            ))),
124        });
125
126        Self { provider, oracle_config, cache, ignore_price, inner }
127    }
128
129    /// Returns the configuration of the gas price oracle.
130    pub const fn config(&self) -> &GasPriceOracleConfig {
131        &self.oracle_config
132    }
133
134    /// Suggests a gas price estimate based on recent blocks, using the configured percentile.
135    pub async fn suggest_tip_cap(&self) -> EthResult<U256> {
136        let header = self
137            .provider
138            .sealed_header_by_number_or_tag(BlockNumberOrTag::Latest)?
139            .ok_or(EthApiError::HeaderNotFound(BlockId::latest()))?;
140
141        let mut inner = self.inner.lock().await;
142
143        // if we have stored a last price, then we check whether or not it was for the same head
144        if inner.last_price.block_hash == header.hash() {
145            return Ok(inner.last_price.price)
146        }
147
148        // if all responses are empty, then we can return a maximum of 2*check_block blocks' worth
149        // of prices
150        //
151        // we only return more than check_block blocks' worth of prices if one or more return empty
152        // transactions
153        let mut current_hash = header.hash();
154        let mut results = Vec::new();
155        let mut populated_blocks = 0;
156
157        // we only check a maximum of 2 * max_block_history, or the number of blocks in the chain
158        let max_blocks = header.number().min(self.oracle_config.max_block_history * 2);
159
160        for _ in 0..max_blocks {
161            // Check if current hash is in cache
162            let (parent_hash, block_values) =
163                if let Some(vals) = inner.lowest_effective_tip_cache.get(&current_hash) {
164                    vals.to_owned()
165                } else {
166                    // Otherwise we fetch it using get_block_values
167                    let (parent_hash, block_values) = self
168                        .get_block_values(current_hash, SAMPLE_NUMBER)
169                        .await?
170                        .ok_or(EthApiError::HeaderNotFound(current_hash.into()))?;
171                    inner
172                        .lowest_effective_tip_cache
173                        .insert(current_hash, (parent_hash, block_values.clone()));
174                    (parent_hash, block_values)
175                };
176
177            if block_values.is_empty() {
178                // For empty blocks, use zero gas price to signal no demand
179                results.push(U256::ZERO);
180            } else {
181                results.extend(block_values);
182                populated_blocks += 1;
183            }
184
185            // break when we have enough populated blocks
186            if populated_blocks >= self.oracle_config.blocks {
187                break
188            }
189
190            current_hash = parent_hash;
191        }
192
193        // sort results then take the configured percentile result
194        let mut price = if results.is_empty() {
195            inner.last_price.price
196        } else {
197            results.sort_unstable();
198            *results.get((results.len() - 1) * self.oracle_config.percentile as usize / 100).expect(
199                "gas price index is a percent of nonzero array length, so a value always exists",
200            )
201        };
202
203        // constrain to the max price
204        if let Some(max_price) = self.oracle_config.max_price &&
205            price > max_price
206        {
207            price = max_price;
208        }
209
210        inner.last_price = GasPriceOracleResult { block_hash: header.hash(), price };
211
212        Ok(price)
213    }
214
215    /// Get the `limit` lowest effective tip values for the given block. If the oracle has a
216    /// configured `ignore_price` threshold, then tip values under that threshold will be ignored
217    /// before returning a result.
218    ///
219    /// If the block cannot be found, then this will return `None`.
220    ///
221    /// This method also returns the parent hash for the given block.
222    async fn get_block_values(
223        &self,
224        block_hash: B256,
225        limit: usize,
226    ) -> EthResult<Option<(B256, Vec<U256>)>> {
227        // check the cache (this will hit the disk if the block is not cached)
228        let Some(block) = self.cache.get_recovered_block(block_hash).await? else {
229            return Ok(None)
230        };
231
232        let base_fee_per_gas = block.base_fee_per_gas();
233        let parent_hash = block.parent_hash();
234
235        // sort the functions by ascending effective tip first
236        let sorted_transactions = block.transactions_recovered().sorted_by_cached_key(|tx| {
237            if let Some(base_fee) = base_fee_per_gas {
238                (*tx).effective_tip_per_gas(base_fee)
239            } else {
240                Some((*tx).priority_fee_or_price())
241            }
242        });
243
244        let mut prices = Vec::with_capacity(limit);
245
246        for tx in sorted_transactions {
247            let effective_tip = if let Some(base_fee) = base_fee_per_gas {
248                tx.effective_tip_per_gas(base_fee)
249            } else {
250                Some(tx.priority_fee_or_price())
251            };
252
253            // ignore transactions with a tip under the configured threshold
254            if let Some(ignore_under) = self.ignore_price &&
255                effective_tip < Some(ignore_under)
256            {
257                continue
258            }
259
260            // check if the sender was the coinbase, if so, ignore
261            if tx.signer() == block.beneficiary() {
262                continue
263            }
264
265            // a `None` effective_gas_tip represents a transaction where the max_fee_per_gas is
266            // less than the base fee which would be invalid
267            prices.push(U256::from(effective_tip.ok_or(RpcInvalidTransactionError::FeeCapTooLow)?));
268
269            // we have enough entries
270            if prices.len() >= limit {
271                break
272            }
273        }
274
275        Ok(Some((parent_hash, prices)))
276    }
277
278    /// Suggests a max priority fee value using a simplified and more predictable algorithm
279    /// appropriate for chains like Optimism with a single known block builder.
280    ///
281    /// It returns either:
282    /// - The minimum suggested priority fee when blocks have capacity
283    /// - 10% above the median effective priority fee from the last block when at capacity
284    ///
285    /// A block is considered at capacity if its total gas used plus the maximum single transaction
286    /// gas would exceed the block's gas limit.
287    pub async fn op_suggest_tip_cap(&self, min_suggested_priority_fee: U256) -> EthResult<U256> {
288        let header = self
289            .provider
290            .sealed_header_by_number_or_tag(BlockNumberOrTag::Latest)?
291            .ok_or(EthApiError::HeaderNotFound(BlockId::latest()))?;
292
293        let mut inner = self.inner.lock().await;
294
295        // if we have stored a last price, then we check whether or not it was for the same head
296        if inner.last_price.block_hash == header.hash() {
297            return Ok(inner.last_price.price);
298        }
299
300        let mut suggestion = min_suggested_priority_fee;
301
302        // find the maximum gas used by any of the transactions in the block to use as the
303        // capacity margin for the block, if no receipts are found return the
304        // suggested_min_priority_fee
305        let receipts = self
306            .cache
307            .get_receipts(header.hash())
308            .await?
309            .ok_or(EthApiError::ReceiptsNotFound(BlockId::latest()))?;
310
311        let mut max_tx_gas_used = 0u64;
312        let mut last_cumulative_gas = 0;
313        for receipt in receipts.as_ref() {
314            let cumulative_gas = receipt.cumulative_gas_used();
315            // get the gas used by each transaction in the block, by subtracting the
316            // cumulative gas used of the previous transaction from the cumulative gas used of
317            // the current transaction. This is because there is no gas_used()
318            // method on the Receipt trait.
319            let gas_used = cumulative_gas - last_cumulative_gas;
320            max_tx_gas_used = max_tx_gas_used.max(gas_used);
321            last_cumulative_gas = cumulative_gas;
322        }
323
324        // if the block is at capacity, the suggestion must be increased
325        if header.gas_used() + max_tx_gas_used > header.gas_limit() {
326            let Some(median_tip) = self.get_block_median_tip(header.hash()).await? else {
327                return Ok(suggestion);
328            };
329
330            let new_suggestion = median_tip + median_tip / U256::from(10);
331
332            if new_suggestion > suggestion {
333                suggestion = new_suggestion;
334            }
335        }
336
337        // constrain to the max price
338        if let Some(max_price) = self.oracle_config.max_price &&
339            suggestion > max_price
340        {
341            suggestion = max_price;
342        }
343
344        inner.last_price = GasPriceOracleResult { block_hash: header.hash(), price: suggestion };
345
346        Ok(suggestion)
347    }
348
349    /// Get the median tip value for the given block. This is useful for determining
350    /// tips when a block is at capacity.
351    ///
352    /// If the block cannot be found or has no transactions, this will return `None`.
353    pub async fn get_block_median_tip(&self, block_hash: B256) -> EthResult<Option<U256>> {
354        // check the cache (this will hit the disk if the block is not cached)
355        let Some(block) = self.cache.get_recovered_block(block_hash).await? else {
356            return Ok(None)
357        };
358
359        let base_fee_per_gas = block.base_fee_per_gas();
360
361        // Filter, sort and collect the prices
362        let prices = block
363            .transactions_recovered()
364            .filter_map(|tx| {
365                if let Some(base_fee) = base_fee_per_gas {
366                    (*tx).effective_tip_per_gas(base_fee)
367                } else {
368                    Some((*tx).priority_fee_or_price())
369                }
370            })
371            .sorted()
372            .collect::<Vec<_>>();
373
374        let median = if prices.is_empty() {
375            // if there are no prices, return `None`
376            None
377        } else if prices.len() % 2 == 1 {
378            Some(U256::from(prices[prices.len() / 2]))
379        } else {
380            Some(U256::from((prices[prices.len() / 2 - 1] + prices[prices.len() / 2]) / 2))
381        };
382
383        Ok(median)
384    }
385}
386/// Container type for mutable inner state of the [`GasPriceOracle`]
387#[derive(Debug)]
388struct GasPriceOracleInner {
389    last_price: GasPriceOracleResult,
390    lowest_effective_tip_cache: EffectiveTipLruCache,
391}
392
393/// Wrapper struct for `LruMap`
394#[derive(Deref, DerefMut)]
395pub struct EffectiveTipLruCache(LruMap<B256, (B256, Vec<U256>), ByLength>);
396
397impl Debug for EffectiveTipLruCache {
398    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
399        f.debug_struct("EffectiveTipLruCache")
400            .field("cache_length", &self.len())
401            .field("cache_memory_usage", &self.memory_usage())
402            .finish()
403    }
404}
405
406/// Stores the last result that the oracle returned
407#[derive(Debug, Clone)]
408pub struct GasPriceOracleResult {
409    /// The block hash that the oracle used to calculate the price
410    pub block_hash: B256,
411    /// The price that the oracle calculated
412    pub price: U256,
413}
414
415impl Default for GasPriceOracleResult {
416    fn default() -> Self {
417        Self { block_hash: B256::ZERO, price: U256::from(GWEI_TO_WEI) }
418    }
419}
420
421/// The wrapper type for gas limit
422#[derive(Debug, Clone, Copy, From, Into)]
423pub struct GasCap(pub u64);
424
425impl Default for GasCap {
426    fn default() -> Self {
427        RPC_DEFAULT_GAS_CAP
428    }
429}