reth_rpc_eth_types/builder/
config.rs1use std::time::Duration;
4
5use crate::{
6 EthStateCacheConfig, FeeHistoryCacheConfig, ForwardConfig, GasPriceOracleConfig,
7 RPC_DEFAULT_GAS_CAP,
8};
9use reqwest::Url;
10use reth_rpc_server_types::constants::{
11 default_max_tracing_requests, DEFAULT_ETH_PROOF_WINDOW, DEFAULT_MAX_ACTIVE_FILTERS,
12 DEFAULT_MAX_BLOCKING_IO_REQUEST, DEFAULT_MAX_BLOCKS_PER_FILTER, DEFAULT_MAX_LOGS_PER_RESPONSE,
13 DEFAULT_MAX_SIMULATE_BLOCKS, DEFAULT_MAX_TRACE_FILTER_BLOCKS, DEFAULT_PROOF_PERMITS,
14 RPC_DEFAULT_SEND_RAW_TX_SYNC_TIMEOUT_SECS,
15};
16use serde::{Deserialize, Serialize};
17
18pub const DEFAULT_STALE_FILTER_TTL: Duration = Duration::from_secs(5 * 60);
20
21#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Default)]
23#[serde(rename_all = "lowercase")]
24pub enum PendingBlockKind {
25 Empty,
27 None,
29 #[default]
31 Full,
32}
33
34impl std::str::FromStr for PendingBlockKind {
35 type Err = String;
36
37 fn from_str(s: &str) -> Result<Self, Self::Err> {
38 match s.to_lowercase().as_str() {
39 "empty" => Ok(Self::Empty),
40 "none" => Ok(Self::None),
41 "full" => Ok(Self::Full),
42 _ => Err(format!(
43 "Invalid pending block kind: {s}. Valid options are: empty, none, full"
44 )),
45 }
46 }
47}
48
49impl PendingBlockKind {
50 pub const fn is_none(&self) -> bool {
52 matches!(self, Self::None)
53 }
54
55 pub const fn is_empty(&self) -> bool {
57 matches!(self, Self::Empty)
58 }
59}
60
61#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
63pub struct EthConfig {
64 pub cache: EthStateCacheConfig,
66 pub gas_oracle: GasPriceOracleConfig,
68 pub eth_proof_window: u64,
70 pub max_tracing_requests: usize,
72 pub max_blocking_io_requests: usize,
81 pub max_trace_filter_blocks: u64,
83 pub max_blocks_per_filter: u64,
85 pub max_logs_per_response: usize,
87 pub rpc_gas_cap: u64,
91 pub rpc_max_simulate_blocks: u64,
93 pub rpc_max_trace_bytes: Option<usize>,
95 pub compute_state_root_for_eth_simulate: bool,
97 pub stale_filter_ttl: Duration,
100 pub fee_history_cache: FeeHistoryCacheConfig,
102 pub proof_permits: usize,
104 pub max_batch_size: usize,
106 pub pending_block_kind: PendingBlockKind,
108 pub raw_tx_forwarder: ForwardConfig,
110 pub send_raw_transaction_sync_timeout: Duration,
112 pub rpc_evm_memory_limit: u64,
114 pub force_blob_sidecar_upcasting: bool,
119 #[serde(default)]
124 pub max_response_size: Option<usize>,
125 #[serde(default = "default_max_active_filters")]
129 pub max_active_filters: Option<usize>,
130}
131
132const fn default_max_active_filters() -> Option<usize> {
135 Some(DEFAULT_MAX_ACTIVE_FILTERS)
136}
137
138impl EthConfig {
139 pub fn filter_config(&self) -> EthFilterConfig {
141 EthFilterConfig::default()
142 .max_blocks_per_filter(self.max_blocks_per_filter)
143 .max_logs_per_response(self.max_logs_per_response)
144 .stale_filter_ttl(self.stale_filter_ttl)
145 .max_response_size(self.max_response_size)
146 .max_active_filters(self.max_active_filters)
147 }
148}
149
150impl Default for EthConfig {
151 fn default() -> Self {
152 Self {
153 cache: EthStateCacheConfig::default(),
154 gas_oracle: GasPriceOracleConfig::default(),
155 eth_proof_window: DEFAULT_ETH_PROOF_WINDOW,
156 max_tracing_requests: default_max_tracing_requests(),
157 max_blocking_io_requests: DEFAULT_MAX_BLOCKING_IO_REQUEST,
158 max_trace_filter_blocks: DEFAULT_MAX_TRACE_FILTER_BLOCKS,
159 max_blocks_per_filter: DEFAULT_MAX_BLOCKS_PER_FILTER,
160 max_logs_per_response: DEFAULT_MAX_LOGS_PER_RESPONSE,
161 rpc_gas_cap: RPC_DEFAULT_GAS_CAP.into(),
162 rpc_max_simulate_blocks: DEFAULT_MAX_SIMULATE_BLOCKS,
163 rpc_max_trace_bytes: None,
164 compute_state_root_for_eth_simulate: false,
165 stale_filter_ttl: DEFAULT_STALE_FILTER_TTL,
166 fee_history_cache: FeeHistoryCacheConfig::default(),
167 proof_permits: DEFAULT_PROOF_PERMITS,
168 max_batch_size: 1,
169 pending_block_kind: PendingBlockKind::Full,
170 raw_tx_forwarder: ForwardConfig::default(),
171 send_raw_transaction_sync_timeout: RPC_DEFAULT_SEND_RAW_TX_SYNC_TIMEOUT_SECS,
172 rpc_evm_memory_limit: 100 * 1024 * 1024,
173 force_blob_sidecar_upcasting: false,
174 max_response_size: None,
175 max_active_filters: Some(DEFAULT_MAX_ACTIVE_FILTERS),
176 }
177 }
178}
179
180impl EthConfig {
181 pub const fn state_cache(mut self, cache: EthStateCacheConfig) -> Self {
183 self.cache = cache;
184 self
185 }
186
187 pub const fn gpo_config(mut self, gas_oracle_config: GasPriceOracleConfig) -> Self {
189 self.gas_oracle = gas_oracle_config;
190 self
191 }
192
193 pub const fn max_tracing_requests(mut self, max_requests: usize) -> Self {
195 self.max_tracing_requests = max_requests;
196 self
197 }
198
199 pub const fn max_blocking_io_requests(mut self, max_requests: usize) -> Self {
201 self.max_blocking_io_requests = max_requests;
202 self
203 }
204
205 pub const fn max_blocks_per_filter(mut self, max_blocks: u64) -> Self {
207 self.max_blocks_per_filter = max_blocks;
208 self
209 }
210
211 pub const fn max_trace_filter_blocks(mut self, max_blocks: u64) -> Self {
213 self.max_trace_filter_blocks = max_blocks;
214 self
215 }
216
217 pub const fn max_logs_per_response(mut self, max_logs: usize) -> Self {
219 self.max_logs_per_response = max_logs;
220 self
221 }
222
223 pub const fn rpc_gas_cap(mut self, rpc_gas_cap: u64) -> Self {
226 self.rpc_gas_cap = rpc_gas_cap;
227 self
228 }
229
230 pub const fn rpc_max_simulate_blocks(mut self, max_blocks: u64) -> Self {
232 self.rpc_max_simulate_blocks = max_blocks;
233 self
234 }
235
236 pub const fn rpc_max_trace_bytes(mut self, max_trace_bytes: Option<usize>) -> Self {
238 self.rpc_max_trace_bytes = max_trace_bytes;
239 self
240 }
241
242 pub const fn compute_state_root_for_eth_simulate(mut self, enabled: bool) -> Self {
244 self.compute_state_root_for_eth_simulate = enabled;
245 self
246 }
247
248 pub const fn eth_proof_window(mut self, window: u64) -> Self {
250 self.eth_proof_window = window;
251 self
252 }
253
254 pub const fn proof_permits(mut self, permits: usize) -> Self {
256 self.proof_permits = permits;
257 self
258 }
259
260 pub const fn max_batch_size(mut self, max_batch_size: usize) -> Self {
262 self.max_batch_size = max_batch_size;
263 self
264 }
265
266 pub const fn pending_block_kind(mut self, pending_block_kind: PendingBlockKind) -> Self {
268 self.pending_block_kind = pending_block_kind;
269 self
270 }
271
272 pub fn raw_tx_forwarder(mut self, tx_forwarder: Option<Url>) -> Self {
274 if let Some(tx_forwarder) = tx_forwarder {
275 self.raw_tx_forwarder.tx_forwarder = Some(tx_forwarder);
276 }
277 self
278 }
279
280 pub const fn send_raw_transaction_sync_timeout(mut self, timeout: Duration) -> Self {
282 self.send_raw_transaction_sync_timeout = timeout;
283 self
284 }
285
286 pub const fn rpc_evm_memory_limit(mut self, memory_limit: u64) -> Self {
288 self.rpc_evm_memory_limit = memory_limit;
289 self
290 }
291
292 pub const fn force_blob_sidecar_upcasting(mut self, force: bool) -> Self {
294 self.force_blob_sidecar_upcasting = force;
295 self
296 }
297
298 pub const fn max_response_size(mut self, size: usize) -> Self {
300 self.max_response_size = Some(size);
301 self
302 }
303
304 pub const fn max_active_filters(mut self, max: Option<usize>) -> Self {
307 self.max_active_filters = max;
308 self
309 }
310}
311
312#[derive(Debug, Clone, PartialEq, Eq)]
314pub struct EthFilterConfig {
315 pub max_blocks_per_filter: Option<u64>,
319 pub max_logs_per_response: Option<usize>,
323 pub stale_filter_ttl: Duration,
328 pub max_response_size: Option<usize>,
332 pub max_active_filters: Option<usize>,
336}
337
338impl EthFilterConfig {
339 pub const fn max_blocks_per_filter(mut self, num: u64) -> Self {
341 self.max_blocks_per_filter = Some(num);
342 self
343 }
344
345 pub const fn max_logs_per_response(mut self, num: usize) -> Self {
348 self.max_logs_per_response = Some(num);
349 self
350 }
351
352 pub const fn stale_filter_ttl(mut self, duration: Duration) -> Self {
354 self.stale_filter_ttl = duration;
355 self
356 }
357
358 pub const fn max_response_size(mut self, size: Option<usize>) -> Self {
361 self.max_response_size = size;
362 self
363 }
364
365 pub const fn max_active_filters(mut self, max: Option<usize>) -> Self {
368 self.max_active_filters = max;
369 self
370 }
371}
372
373impl Default for EthFilterConfig {
374 fn default() -> Self {
375 Self {
376 max_blocks_per_filter: None,
377 max_logs_per_response: None,
378 stale_filter_ttl: DEFAULT_STALE_FILTER_TTL,
380 max_response_size: None,
381 max_active_filters: Some(DEFAULT_MAX_ACTIVE_FILTERS),
382 }
383 }
384}
385
386#[cfg(test)]
387mod tests {
388 use super::*;
389
390 #[test]
391 fn missing_max_active_filters_deserializes_to_the_default_cap() {
392 let mut config = serde_json::to_value(EthConfig::default()).unwrap();
393 config.as_object_mut().unwrap().remove("max_active_filters").unwrap();
394
395 let config = serde_json::from_value::<EthConfig>(config).unwrap();
396 assert_eq!(config.max_active_filters, Some(DEFAULT_MAX_ACTIVE_FILTERS));
397 assert_eq!(config.filter_config().max_active_filters, Some(DEFAULT_MAX_ACTIVE_FILTERS));
398 assert_eq!(EthFilterConfig::default().max_active_filters, Some(DEFAULT_MAX_ACTIVE_FILTERS));
399 }
400}