1use 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
25pub const RPC_DEFAULT_GAS_CAP: GasCap = GasCap(constants::gas_oracle::RPC_DEFAULT_GAS_CAP);
28
29#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
31#[serde(rename_all = "camelCase")]
32pub struct GasPriceOracleConfig {
33 pub blocks: u32,
35
36 pub percentile: u32,
38
39 pub max_header_history: u64,
41
42 pub max_block_history: u64,
44
45 pub max_reward_percentile_count: u64,
50
51 pub default_suggested_fee: Option<U256>,
53
54 pub max_price: Option<U256>,
56
57 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#[derive(Debug)]
78pub struct GasPriceOracle<Provider>
79where
80 Provider: NodePrimitivesProvider,
81{
82 provider: Provider,
84 cache: EthStateCache<Provider::Primitives>,
86 oracle_config: GasPriceOracleConfig,
88 ignore_price: Option<u128>,
90 inner: Mutex<GasPriceOracleInner>,
93}
94
95impl<Provider> GasPriceOracle<Provider>
96where
97 Provider: BlockReaderIdExt + NodePrimitivesProvider,
98{
99 pub fn new(
101 provider: Provider,
102 mut oracle_config: GasPriceOracleConfig,
103 cache: EthStateCache<Provider::Primitives>,
104 ) -> Self {
105 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 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 pub const fn config(&self) -> &GasPriceOracleConfig {
131 &self.oracle_config
132 }
133
134 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 inner.last_price.block_hash == header.hash() {
145 return Ok(inner.last_price.price)
146 }
147
148 let mut current_hash = header.hash();
154 let mut results = Vec::new();
155 let mut populated_blocks = 0;
156
157 let max_blocks = header.number().min(self.oracle_config.max_block_history * 2);
159
160 for _ in 0..max_blocks {
161 let (parent_hash, block_values) =
163 if let Some(vals) = inner.lowest_effective_tip_cache.get(¤t_hash) {
164 vals.to_owned()
165 } else {
166 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 results.push(U256::ZERO);
180 } else {
181 results.extend(block_values);
182 populated_blocks += 1;
183 }
184
185 if populated_blocks >= self.oracle_config.blocks {
187 break
188 }
189
190 current_hash = parent_hash;
191 }
192
193 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 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 async fn get_block_values(
223 &self,
224 block_hash: B256,
225 limit: usize,
226 ) -> EthResult<Option<(B256, Vec<U256>)>> {
227 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 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 if let Some(ignore_under) = self.ignore_price &&
255 effective_tip < Some(ignore_under)
256 {
257 continue
258 }
259
260 if tx.signer() == block.beneficiary() {
262 continue
263 }
264
265 prices.push(U256::from(effective_tip.ok_or(RpcInvalidTransactionError::FeeCapTooLow)?));
268
269 if prices.len() >= limit {
271 break
272 }
273 }
274
275 Ok(Some((parent_hash, prices)))
276 }
277
278 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 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 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 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 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 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 pub async fn get_block_median_tip(&self, block_hash: B256) -> EthResult<Option<U256>> {
354 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 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 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#[derive(Debug)]
388struct GasPriceOracleInner {
389 last_price: GasPriceOracleResult,
390 lowest_effective_tip_cache: EffectiveTipLruCache,
391}
392
393#[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#[derive(Debug, Clone)]
408pub struct GasPriceOracleResult {
409 pub block_hash: B256,
411 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#[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}