reth_rpc_eth_types/builder/
config.rs

1//! Configuration for `eth` namespace APIs.
2
3use std::time::Duration;
4
5use crate::{
6    EthStateCacheConfig, FeeHistoryCacheConfig, GasPriceOracleConfig, RPC_DEFAULT_GAS_CAP,
7};
8use reth_rpc_server_types::constants::{
9    default_max_tracing_requests, DEFAULT_ETH_PROOF_WINDOW, DEFAULT_MAX_BLOCKS_PER_FILTER,
10    DEFAULT_MAX_LOGS_PER_RESPONSE, DEFAULT_MAX_SIMULATE_BLOCKS, DEFAULT_MAX_TRACE_FILTER_BLOCKS,
11    DEFAULT_PROOF_PERMITS,
12};
13use serde::{Deserialize, Serialize};
14
15/// Default value for stale filter ttl
16pub const DEFAULT_STALE_FILTER_TTL: Duration = Duration::from_secs(5 * 60);
17
18/// Additional config values for the eth namespace.
19#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
20pub struct EthConfig {
21    /// Settings for the caching layer
22    pub cache: EthStateCacheConfig,
23    /// Settings for the gas price oracle
24    pub gas_oracle: GasPriceOracleConfig,
25    /// The maximum number of blocks into the past for generating state proofs.
26    pub eth_proof_window: u64,
27    /// The maximum number of tracing calls that can be executed in concurrently.
28    pub max_tracing_requests: usize,
29    /// Maximum number of blocks for `trace_filter` requests.
30    pub max_trace_filter_blocks: u64,
31    /// Maximum number of blocks that could be scanned per filter request in `eth_getLogs` calls.
32    pub max_blocks_per_filter: u64,
33    /// Maximum number of logs that can be returned in a single response in `eth_getLogs` calls.
34    pub max_logs_per_response: usize,
35    /// Gas limit for `eth_call` and call tracing RPC methods.
36    ///
37    /// Defaults to [`RPC_DEFAULT_GAS_CAP`]
38    pub rpc_gas_cap: u64,
39    /// Max number of blocks for `eth_simulateV1`.
40    pub rpc_max_simulate_blocks: u64,
41    ///
42    /// Sets TTL for stale filters
43    pub stale_filter_ttl: Duration,
44    /// Settings for the fee history cache
45    pub fee_history_cache: FeeHistoryCacheConfig,
46    /// The maximum number of getproof calls that can be executed concurrently.
47    pub proof_permits: usize,
48}
49
50impl EthConfig {
51    /// Returns the filter config for the `eth_filter` handler.
52    pub fn filter_config(&self) -> EthFilterConfig {
53        EthFilterConfig::default()
54            .max_blocks_per_filter(self.max_blocks_per_filter)
55            .max_logs_per_response(self.max_logs_per_response)
56            .stale_filter_ttl(self.stale_filter_ttl)
57    }
58}
59
60impl Default for EthConfig {
61    fn default() -> Self {
62        Self {
63            cache: EthStateCacheConfig::default(),
64            gas_oracle: GasPriceOracleConfig::default(),
65            eth_proof_window: DEFAULT_ETH_PROOF_WINDOW,
66            max_tracing_requests: default_max_tracing_requests(),
67            max_trace_filter_blocks: DEFAULT_MAX_TRACE_FILTER_BLOCKS,
68            max_blocks_per_filter: DEFAULT_MAX_BLOCKS_PER_FILTER,
69            max_logs_per_response: DEFAULT_MAX_LOGS_PER_RESPONSE,
70            rpc_gas_cap: RPC_DEFAULT_GAS_CAP.into(),
71            rpc_max_simulate_blocks: DEFAULT_MAX_SIMULATE_BLOCKS,
72            stale_filter_ttl: DEFAULT_STALE_FILTER_TTL,
73            fee_history_cache: FeeHistoryCacheConfig::default(),
74            proof_permits: DEFAULT_PROOF_PERMITS,
75        }
76    }
77}
78
79impl EthConfig {
80    /// Configures the caching layer settings
81    pub const fn state_cache(mut self, cache: EthStateCacheConfig) -> Self {
82        self.cache = cache;
83        self
84    }
85
86    /// Configures the gas price oracle settings
87    pub const fn gpo_config(mut self, gas_oracle_config: GasPriceOracleConfig) -> Self {
88        self.gas_oracle = gas_oracle_config;
89        self
90    }
91
92    /// Configures the maximum number of tracing requests
93    pub const fn max_tracing_requests(mut self, max_requests: usize) -> Self {
94        self.max_tracing_requests = max_requests;
95        self
96    }
97
98    /// Configures the maximum block length to scan per `eth_getLogs` request
99    pub const fn max_blocks_per_filter(mut self, max_blocks: u64) -> Self {
100        self.max_blocks_per_filter = max_blocks;
101        self
102    }
103
104    /// Configures the maximum number of blocks for `trace_filter` requests
105    pub const fn max_trace_filter_blocks(mut self, max_blocks: u64) -> Self {
106        self.max_trace_filter_blocks = max_blocks;
107        self
108    }
109
110    /// Configures the maximum number of logs per response
111    pub const fn max_logs_per_response(mut self, max_logs: usize) -> Self {
112        self.max_logs_per_response = max_logs;
113        self
114    }
115
116    /// Configures the maximum gas limit for `eth_call` and call tracing RPC methods
117    pub const fn rpc_gas_cap(mut self, rpc_gas_cap: u64) -> Self {
118        self.rpc_gas_cap = rpc_gas_cap;
119        self
120    }
121
122    /// Configures the maximum gas limit for `eth_call` and call tracing RPC methods
123    pub const fn rpc_max_simulate_blocks(mut self, max_blocks: u64) -> Self {
124        self.rpc_max_simulate_blocks = max_blocks;
125        self
126    }
127
128    /// Configures the maximum proof window for historical proof generation.
129    pub const fn eth_proof_window(mut self, window: u64) -> Self {
130        self.eth_proof_window = window;
131        self
132    }
133
134    /// Configures the number of getproof requests
135    pub const fn proof_permits(mut self, permits: usize) -> Self {
136        self.proof_permits = permits;
137        self
138    }
139}
140
141/// Config for the filter
142#[derive(Debug, Clone, PartialEq, Eq)]
143pub struct EthFilterConfig {
144    /// Maximum number of blocks that a filter can scan for logs.
145    ///
146    /// If `None` then no limit is enforced.
147    pub max_blocks_per_filter: Option<u64>,
148    /// Maximum number of logs that can be returned in a single response in `eth_getLogs` calls.
149    ///
150    /// If `None` then no limit is enforced.
151    pub max_logs_per_response: Option<usize>,
152    /// How long a filter remains valid after the last poll.
153    ///
154    /// A filter is considered stale if it has not been polled for longer than this duration and
155    /// will be removed.
156    pub stale_filter_ttl: Duration,
157}
158
159impl EthFilterConfig {
160    /// Sets the maximum number of blocks that a filter can scan for logs.
161    pub const fn max_blocks_per_filter(mut self, num: u64) -> Self {
162        self.max_blocks_per_filter = Some(num);
163        self
164    }
165
166    /// Sets the maximum number of logs that can be returned in a single response in `eth_getLogs`
167    /// calls.
168    pub const fn max_logs_per_response(mut self, num: usize) -> Self {
169        self.max_logs_per_response = Some(num);
170        self
171    }
172
173    /// Sets how long a filter remains valid after the last poll before it will be removed.
174    pub const fn stale_filter_ttl(mut self, duration: Duration) -> Self {
175        self.stale_filter_ttl = duration;
176        self
177    }
178}
179
180impl Default for EthFilterConfig {
181    fn default() -> Self {
182        Self {
183            max_blocks_per_filter: None,
184            max_logs_per_response: None,
185            // 5min
186            stale_filter_ttl: Duration::from_secs(5 * 60),
187        }
188    }
189}