Skip to main content

reth_node_core/args/
rpc_server.rs

1//! clap [Args](clap::Args) for RPC related arguments.
2
3use crate::args::{
4    types::{MaxU32, ZeroAsNoneU64},
5    GasPriceOracleArgs, RpcStateCacheArgs,
6};
7use alloy_primitives::map::AddressSet;
8use alloy_rpc_types_engine::JwtSecret;
9use clap::{
10    builder::{PossibleValue, RangedU64ValueParser, Resettable, TypedValueParser},
11    Arg, Args, Command,
12};
13use rand::Rng;
14use reth_cli_util::{parse_duration_from_secs_or_ms, parse_ether_value};
15use reth_rpc_eth_types::builder::config::PendingBlockKind;
16use reth_rpc_server_types::{constants, RethRpcModule, RpcModuleSelection};
17use std::{
18    ffi::OsStr,
19    net::{IpAddr, Ipv4Addr},
20    path::PathBuf,
21    sync::OnceLock,
22    time::Duration,
23};
24use url::Url;
25
26use super::types::MaxOr;
27
28/// Global static RPC server defaults
29static RPC_SERVER_DEFAULTS: OnceLock<DefaultRpcServerArgs> = OnceLock::new();
30
31/// Default max number of subscriptions per connection.
32pub(crate) const RPC_DEFAULT_MAX_SUBS_PER_CONN: u32 = 1024;
33
34/// Default max request size in MB.
35pub(crate) const RPC_DEFAULT_MAX_REQUEST_SIZE_MB: u32 = 15;
36
37/// Default max response size in MB.
38///
39/// This is only relevant for very large trace responses.
40pub(crate) const RPC_DEFAULT_MAX_RESPONSE_SIZE_MB: u32 = 160;
41
42/// Default number of incoming connections.
43///
44/// This restricts how many active connections (http, ws) the server accepts.
45/// Once exceeded, the server can reject new connections.
46pub(crate) const RPC_DEFAULT_MAX_CONNECTIONS: u32 = 500;
47
48/// Default values for RPC server that can be customized
49///
50/// Global defaults can be set via [`DefaultRpcServerArgs::try_init`].
51#[derive(Debug, Clone)]
52pub struct DefaultRpcServerArgs {
53    http: bool,
54    http_addr: IpAddr,
55    http_port: u16,
56    http_disable_compression: bool,
57    http_api: Option<RpcModuleSelection>,
58    http_corsdomain: Option<String>,
59    ws: bool,
60    ws_addr: IpAddr,
61    ws_port: u16,
62    ws_allowed_origins: Option<String>,
63    ws_api: Option<RpcModuleSelection>,
64    ipcdisable: bool,
65    ipcpath: String,
66    ipc_socket_permissions: Option<String>,
67    auth_addr: IpAddr,
68    auth_port: u16,
69    auth_jwtsecret: Option<PathBuf>,
70    auth_ipc: bool,
71    auth_ipc_path: String,
72    disable_auth_server: bool,
73    rpc_jwtsecret: Option<JwtSecret>,
74    rpc_max_request_size: MaxU32,
75    rpc_max_response_size: MaxU32,
76    rpc_max_subscriptions_per_connection: MaxU32,
77    rpc_max_connections: MaxU32,
78    rpc_max_tracing_requests: usize,
79    rpc_max_blocking_io_requests: usize,
80    rpc_max_trace_filter_blocks: u64,
81    rpc_max_blocks_per_filter: ZeroAsNoneU64,
82    rpc_max_logs_per_response: ZeroAsNoneU64,
83    rpc_gas_cap: u64,
84    rpc_evm_memory_limit: u64,
85    rpc_tx_fee_cap: u128,
86    rpc_max_simulate_blocks: u64,
87    rpc_eth_proof_window: u64,
88    rpc_proof_permits: usize,
89    rpc_pending_block: PendingBlockKind,
90    rpc_forwarder: Option<Url>,
91    builder_disallow: Option<AddressSet>,
92    rpc_state_cache: RpcStateCacheArgs,
93    gas_price_oracle: GasPriceOracleArgs,
94    rpc_send_raw_transaction_sync_timeout: Duration,
95}
96
97impl DefaultRpcServerArgs {
98    /// Initialize the global RPC server defaults with this configuration
99    pub fn try_init(self) -> Result<(), Self> {
100        RPC_SERVER_DEFAULTS.set(self)
101    }
102
103    /// Get a reference to the global RPC server defaults
104    pub fn get_global() -> &'static Self {
105        RPC_SERVER_DEFAULTS.get_or_init(Self::default)
106    }
107
108    /// Set the default HTTP enabled state
109    pub const fn with_http(mut self, v: bool) -> Self {
110        self.http = v;
111        self
112    }
113
114    /// Set the default HTTP address
115    pub const fn with_http_addr(mut self, v: IpAddr) -> Self {
116        self.http_addr = v;
117        self
118    }
119
120    /// Set the default HTTP port
121    pub const fn with_http_port(mut self, v: u16) -> Self {
122        self.http_port = v;
123        self
124    }
125
126    /// Set whether to disable HTTP compression by default
127    pub const fn with_http_disable_compression(mut self, v: bool) -> Self {
128        self.http_disable_compression = v;
129        self
130    }
131
132    /// Set the default HTTP API modules
133    pub fn with_http_api(mut self, v: Option<RpcModuleSelection>) -> Self {
134        self.http_api = v;
135        self
136    }
137
138    /// Set the default HTTP CORS domain
139    pub fn with_http_corsdomain(mut self, v: Option<String>) -> Self {
140        self.http_corsdomain = v;
141        self
142    }
143
144    /// Set the default WS enabled state
145    pub const fn with_ws(mut self, v: bool) -> Self {
146        self.ws = v;
147        self
148    }
149
150    /// Set the default WS address
151    pub const fn with_ws_addr(mut self, v: IpAddr) -> Self {
152        self.ws_addr = v;
153        self
154    }
155
156    /// Set the default WS port
157    pub const fn with_ws_port(mut self, v: u16) -> Self {
158        self.ws_port = v;
159        self
160    }
161
162    /// Set the default WS allowed origins
163    pub fn with_ws_allowed_origins(mut self, v: Option<String>) -> Self {
164        self.ws_allowed_origins = v;
165        self
166    }
167
168    /// Set the default WS API modules
169    pub fn with_ws_api(mut self, v: Option<RpcModuleSelection>) -> Self {
170        self.ws_api = v;
171        self
172    }
173
174    /// Set whether to disable IPC by default
175    pub const fn with_ipcdisable(mut self, v: bool) -> Self {
176        self.ipcdisable = v;
177        self
178    }
179
180    /// Set the default IPC path
181    pub fn with_ipcpath(mut self, v: String) -> Self {
182        self.ipcpath = v;
183        self
184    }
185
186    /// Set the default IPC socket permissions
187    pub fn with_ipc_socket_permissions(mut self, v: Option<String>) -> Self {
188        self.ipc_socket_permissions = v;
189        self
190    }
191
192    /// Set the default auth server address
193    pub const fn with_auth_addr(mut self, v: IpAddr) -> Self {
194        self.auth_addr = v;
195        self
196    }
197
198    /// Set the default auth server port
199    pub const fn with_auth_port(mut self, v: u16) -> Self {
200        self.auth_port = v;
201        self
202    }
203
204    /// Set the default auth JWT secret path
205    pub fn with_auth_jwtsecret(mut self, v: Option<PathBuf>) -> Self {
206        self.auth_jwtsecret = v;
207        self
208    }
209
210    /// Set the default auth IPC enabled state
211    pub const fn with_auth_ipc(mut self, v: bool) -> Self {
212        self.auth_ipc = v;
213        self
214    }
215
216    /// Set the default auth IPC path
217    pub fn with_auth_ipc_path(mut self, v: String) -> Self {
218        self.auth_ipc_path = v;
219        self
220    }
221
222    /// Set whether to disable the auth server by default
223    pub const fn with_disable_auth_server(mut self, v: bool) -> Self {
224        self.disable_auth_server = v;
225        self
226    }
227
228    /// Set the default RPC JWT secret
229    pub const fn with_rpc_jwtsecret(mut self, v: Option<JwtSecret>) -> Self {
230        self.rpc_jwtsecret = v;
231        self
232    }
233
234    /// Set the default max request size
235    pub const fn with_rpc_max_request_size(mut self, v: MaxU32) -> Self {
236        self.rpc_max_request_size = v;
237        self
238    }
239
240    /// Set the default max response size
241    pub const fn with_rpc_max_response_size(mut self, v: MaxU32) -> Self {
242        self.rpc_max_response_size = v;
243        self
244    }
245
246    /// Set the default max subscriptions per connection
247    pub const fn with_rpc_max_subscriptions_per_connection(mut self, v: MaxU32) -> Self {
248        self.rpc_max_subscriptions_per_connection = v;
249        self
250    }
251
252    /// Set the default max connections
253    pub const fn with_rpc_max_connections(mut self, v: MaxU32) -> Self {
254        self.rpc_max_connections = v;
255        self
256    }
257
258    /// Set the default max tracing requests
259    pub const fn with_rpc_max_tracing_requests(mut self, v: usize) -> Self {
260        self.rpc_max_tracing_requests = v;
261        self
262    }
263
264    /// Set the default max blocking IO requests
265    pub const fn with_rpc_max_blocking_io_requests(mut self, v: usize) -> Self {
266        self.rpc_max_blocking_io_requests = v;
267        self
268    }
269
270    /// Set the default max trace filter blocks
271    pub const fn with_rpc_max_trace_filter_blocks(mut self, v: u64) -> Self {
272        self.rpc_max_trace_filter_blocks = v;
273        self
274    }
275
276    /// Set the default max blocks per filter
277    pub const fn with_rpc_max_blocks_per_filter(mut self, v: ZeroAsNoneU64) -> Self {
278        self.rpc_max_blocks_per_filter = v;
279        self
280    }
281
282    /// Set the default max logs per response
283    pub const fn with_rpc_max_logs_per_response(mut self, v: ZeroAsNoneU64) -> Self {
284        self.rpc_max_logs_per_response = v;
285        self
286    }
287
288    /// Set the default gas cap
289    pub const fn with_rpc_gas_cap(mut self, v: u64) -> Self {
290        self.rpc_gas_cap = v;
291        self
292    }
293
294    /// Set the default EVM memory limit
295    pub const fn with_rpc_evm_memory_limit(mut self, v: u64) -> Self {
296        self.rpc_evm_memory_limit = v;
297        self
298    }
299
300    /// Set the default tx fee cap
301    pub const fn with_rpc_tx_fee_cap(mut self, v: u128) -> Self {
302        self.rpc_tx_fee_cap = v;
303        self
304    }
305
306    /// Set the default max simulate blocks
307    pub const fn with_rpc_max_simulate_blocks(mut self, v: u64) -> Self {
308        self.rpc_max_simulate_blocks = v;
309        self
310    }
311
312    /// Set the default eth proof window
313    pub const fn with_rpc_eth_proof_window(mut self, v: u64) -> Self {
314        self.rpc_eth_proof_window = v;
315        self
316    }
317
318    /// Set the default proof permits
319    pub const fn with_rpc_proof_permits(mut self, v: usize) -> Self {
320        self.rpc_proof_permits = v;
321        self
322    }
323
324    /// Set the default pending block kind
325    pub const fn with_rpc_pending_block(mut self, v: PendingBlockKind) -> Self {
326        self.rpc_pending_block = v;
327        self
328    }
329
330    /// Set the default RPC forwarder
331    pub fn with_rpc_forwarder(mut self, v: Option<Url>) -> Self {
332        self.rpc_forwarder = v;
333        self
334    }
335
336    /// Set the default builder disallow addresses
337    pub fn with_builder_disallow(mut self, v: Option<AddressSet>) -> Self {
338        self.builder_disallow = v;
339        self
340    }
341
342    /// Set the default RPC state cache args
343    pub const fn with_rpc_state_cache(mut self, v: RpcStateCacheArgs) -> Self {
344        self.rpc_state_cache = v;
345        self
346    }
347
348    /// Set the default gas price oracle args
349    pub const fn with_gas_price_oracle(mut self, v: GasPriceOracleArgs) -> Self {
350        self.gas_price_oracle = v;
351        self
352    }
353
354    /// Set the default send raw transaction sync timeout
355    pub const fn with_rpc_send_raw_transaction_sync_timeout(mut self, v: Duration) -> Self {
356        self.rpc_send_raw_transaction_sync_timeout = v;
357        self
358    }
359}
360
361impl Default for DefaultRpcServerArgs {
362    fn default() -> Self {
363        Self {
364            http: false,
365            http_addr: Ipv4Addr::LOCALHOST.into(),
366            http_port: constants::DEFAULT_HTTP_RPC_PORT,
367            http_disable_compression: false,
368            http_api: None,
369            http_corsdomain: None,
370            ws: false,
371            ws_addr: Ipv4Addr::LOCALHOST.into(),
372            ws_port: constants::DEFAULT_WS_RPC_PORT,
373            ws_allowed_origins: None,
374            ws_api: None,
375            ipcdisable: false,
376            ipcpath: constants::DEFAULT_IPC_ENDPOINT.to_string(),
377            ipc_socket_permissions: None,
378            auth_addr: Ipv4Addr::LOCALHOST.into(),
379            auth_port: constants::DEFAULT_AUTH_PORT,
380            auth_jwtsecret: None,
381            auth_ipc: false,
382            auth_ipc_path: constants::DEFAULT_ENGINE_API_IPC_ENDPOINT.to_string(),
383            disable_auth_server: false,
384            rpc_jwtsecret: None,
385            rpc_max_request_size: RPC_DEFAULT_MAX_REQUEST_SIZE_MB.into(),
386            rpc_max_response_size: RPC_DEFAULT_MAX_RESPONSE_SIZE_MB.into(),
387            rpc_max_subscriptions_per_connection: RPC_DEFAULT_MAX_SUBS_PER_CONN.into(),
388            rpc_max_connections: RPC_DEFAULT_MAX_CONNECTIONS.into(),
389            rpc_max_tracing_requests: constants::default_max_tracing_requests(),
390            rpc_max_blocking_io_requests: constants::DEFAULT_MAX_BLOCKING_IO_REQUEST,
391            rpc_max_trace_filter_blocks: constants::DEFAULT_MAX_TRACE_FILTER_BLOCKS,
392            rpc_max_blocks_per_filter: constants::DEFAULT_MAX_BLOCKS_PER_FILTER.into(),
393            rpc_max_logs_per_response: (constants::DEFAULT_MAX_LOGS_PER_RESPONSE as u64).into(),
394            rpc_gas_cap: constants::gas_oracle::RPC_DEFAULT_GAS_CAP,
395            rpc_evm_memory_limit: (1 << 32) - 1,
396            rpc_tx_fee_cap: constants::DEFAULT_TX_FEE_CAP_WEI,
397            rpc_max_simulate_blocks: constants::DEFAULT_MAX_SIMULATE_BLOCKS,
398            rpc_eth_proof_window: constants::DEFAULT_ETH_PROOF_WINDOW,
399            rpc_proof_permits: constants::DEFAULT_PROOF_PERMITS,
400            rpc_pending_block: PendingBlockKind::Full,
401            rpc_forwarder: None,
402            builder_disallow: None,
403            rpc_state_cache: RpcStateCacheArgs::default(),
404            gas_price_oracle: GasPriceOracleArgs::default(),
405            rpc_send_raw_transaction_sync_timeout:
406                constants::RPC_DEFAULT_SEND_RAW_TX_SYNC_TIMEOUT_SECS,
407        }
408    }
409}
410
411/// Parameters for configuring the rpc more granularity via CLI
412#[derive(Debug, Clone, Args, PartialEq, Eq)]
413#[command(next_help_heading = "RPC")]
414pub struct RpcServerArgs {
415    /// Enable the HTTP-RPC server
416    #[arg(long, default_value_if("dev", "true", "true"), default_value_t = DefaultRpcServerArgs::get_global().http)]
417    pub http: bool,
418
419    /// Http server address to listen on
420    #[arg(long = "http.addr", default_value_t = DefaultRpcServerArgs::get_global().http_addr)]
421    pub http_addr: IpAddr,
422
423    /// Http server port to listen on
424    #[arg(long = "http.port", default_value_t = DefaultRpcServerArgs::get_global().http_port)]
425    pub http_port: u16,
426
427    /// Disable compression for HTTP responses
428    #[arg(long = "http.disable-compression", default_value_t = DefaultRpcServerArgs::get_global().http_disable_compression)]
429    pub http_disable_compression: bool,
430
431    /// Rpc Modules to be configured for the HTTP server
432    #[arg(long = "http.api", value_parser = RpcModuleSelectionValueParser::default(), default_value = Resettable::from(DefaultRpcServerArgs::get_global().http_api.as_ref().map(|v| v.to_string().into())))]
433    pub http_api: Option<RpcModuleSelection>,
434
435    /// Http Corsdomain to allow request from
436    #[arg(long = "http.corsdomain", default_value = Resettable::from(DefaultRpcServerArgs::get_global().http_corsdomain.as_ref().map(|v| v.to_string().into())))]
437    pub http_corsdomain: Option<String>,
438
439    /// Enable the WS-RPC server
440    #[arg(long, default_value_t = DefaultRpcServerArgs::get_global().ws)]
441    pub ws: bool,
442
443    /// Ws server address to listen on
444    #[arg(long = "ws.addr", default_value_t = DefaultRpcServerArgs::get_global().ws_addr)]
445    pub ws_addr: IpAddr,
446
447    /// Ws server port to listen on
448    #[arg(long = "ws.port", default_value_t = DefaultRpcServerArgs::get_global().ws_port)]
449    pub ws_port: u16,
450
451    /// Origins from which to accept `WebSocket` requests
452    #[arg(id = "ws.origins", long = "ws.origins", alias = "ws.corsdomain", default_value = Resettable::from(DefaultRpcServerArgs::get_global().ws_allowed_origins.as_ref().map(|v| v.to_string().into())))]
453    pub ws_allowed_origins: Option<String>,
454
455    /// Rpc Modules to be configured for the WS server
456    #[arg(long = "ws.api", value_parser = RpcModuleSelectionValueParser::default(), default_value = Resettable::from(DefaultRpcServerArgs::get_global().ws_api.as_ref().map(|v| v.to_string().into())))]
457    pub ws_api: Option<RpcModuleSelection>,
458
459    /// Disable the IPC-RPC server
460    #[arg(long, default_value_t = DefaultRpcServerArgs::get_global().ipcdisable)]
461    pub ipcdisable: bool,
462
463    /// Filename for IPC socket/pipe within the datadir
464    #[arg(long, default_value_t = DefaultRpcServerArgs::get_global().ipcpath.clone())]
465    pub ipcpath: String,
466
467    /// Set the permissions for the IPC socket file, in octal format.
468    ///
469    /// If not specified, the permissions will be set by the system's umask.
470    #[arg(long = "ipc.permissions", default_value = Resettable::from(DefaultRpcServerArgs::get_global().ipc_socket_permissions.as_ref().map(|v| v.to_string().into())))]
471    pub ipc_socket_permissions: Option<String>,
472
473    /// Auth server address to listen on
474    #[arg(long = "authrpc.addr", default_value_t = DefaultRpcServerArgs::get_global().auth_addr)]
475    pub auth_addr: IpAddr,
476
477    /// Auth server port to listen on
478    #[arg(long = "authrpc.port", default_value_t = DefaultRpcServerArgs::get_global().auth_port)]
479    pub auth_port: u16,
480
481    /// Path to a JWT secret to use for the authenticated engine-API RPC server.
482    ///
483    /// This will enforce JWT authentication for all requests coming from the consensus layer.
484    ///
485    /// If no path is provided, a secret will be generated and stored in the datadir under
486    /// `<DIR>/<CHAIN_ID>/jwt.hex`. For mainnet this would be `~/.reth/mainnet/jwt.hex` by default.
487    #[arg(long = "authrpc.jwtsecret", value_name = "PATH", global = true, required = false, default_value = Resettable::from(DefaultRpcServerArgs::get_global().auth_jwtsecret.as_ref().map(|v| v.to_string_lossy().into())))]
488    pub auth_jwtsecret: Option<PathBuf>,
489
490    /// Enable auth engine API over IPC
491    #[arg(long, default_value_t = DefaultRpcServerArgs::get_global().auth_ipc)]
492    pub auth_ipc: bool,
493
494    /// Filename for auth IPC socket/pipe within the datadir
495    #[arg(long = "auth-ipc.path", default_value_t = DefaultRpcServerArgs::get_global().auth_ipc_path.clone())]
496    pub auth_ipc_path: String,
497
498    /// Disable the auth/engine API server.
499    ///
500    /// This will prevent the authenticated engine-API server from starting. Use this if you're
501    /// running a node that doesn't need to serve engine API requests.
502    #[arg(long = "disable-auth-server", alias = "disable-engine-api", default_value_t = DefaultRpcServerArgs::get_global().disable_auth_server)]
503    pub disable_auth_server: bool,
504
505    /// Hex encoded JWT secret to authenticate the regular RPC server(s), see `--http.api` and
506    /// `--ws.api`.
507    ///
508    /// This is __not__ used for the authenticated engine-API RPC server, see
509    /// `--authrpc.jwtsecret`.
510    #[arg(long = "rpc.jwtsecret", value_name = "HEX", global = true, required = false, default_value = Resettable::from(DefaultRpcServerArgs::get_global().rpc_jwtsecret.as_ref().map(|v| format!("{:?}", v).into())))]
511    pub rpc_jwtsecret: Option<JwtSecret>,
512
513    /// Set the maximum RPC request payload size for both HTTP and WS in megabytes.
514    #[arg(long = "rpc.max-request-size", alias = "rpc-max-request-size", default_value_t = DefaultRpcServerArgs::get_global().rpc_max_request_size)]
515    pub rpc_max_request_size: MaxU32,
516
517    /// Set the maximum RPC response payload size for both HTTP and WS in megabytes.
518    #[arg(long = "rpc.max-response-size", alias = "rpc-max-response-size", visible_alias = "rpc.returndata.limit", default_value_t = DefaultRpcServerArgs::get_global().rpc_max_response_size)]
519    pub rpc_max_response_size: MaxU32,
520
521    /// Set the maximum concurrent subscriptions per connection.
522    #[arg(long = "rpc.max-subscriptions-per-connection", alias = "rpc-max-subscriptions-per-connection", default_value_t = DefaultRpcServerArgs::get_global().rpc_max_subscriptions_per_connection)]
523    pub rpc_max_subscriptions_per_connection: MaxU32,
524
525    /// Maximum number of RPC server connections.
526    #[arg(long = "rpc.max-connections", alias = "rpc-max-connections", value_name = "COUNT", default_value_t = DefaultRpcServerArgs::get_global().rpc_max_connections)]
527    pub rpc_max_connections: MaxU32,
528
529    /// Maximum number of concurrent tracing requests.
530    ///
531    /// By default this chooses a sensible value based on the number of available cores.
532    /// Tracing requests are generally CPU bound.
533    /// Choosing a value that is higher than the available CPU cores can have a negative impact on
534    /// the performance of the node and affect the node's ability to maintain sync.
535    #[arg(long = "rpc.max-tracing-requests", alias = "rpc-max-tracing-requests", value_name = "COUNT", default_value_t = DefaultRpcServerArgs::get_global().rpc_max_tracing_requests)]
536    pub rpc_max_tracing_requests: usize,
537
538    /// Maximum number of concurrent blocking IO requests.
539    ///
540    /// Blocking IO requests include `eth_call`, `eth_estimateGas`, and similar methods that
541    /// require EVM execution. These are spawned as blocking tasks to avoid blocking the async
542    /// runtime.
543    #[arg(long = "rpc.max-blocking-io-requests", alias = "rpc-max-blocking-io-requests", value_name = "COUNT", default_value_t = DefaultRpcServerArgs::get_global().rpc_max_blocking_io_requests)]
544    pub rpc_max_blocking_io_requests: usize,
545
546    /// Maximum number of blocks for `trace_filter` requests.
547    #[arg(long = "rpc.max-trace-filter-blocks", alias = "rpc-max-trace-filter-blocks", value_name = "COUNT", default_value_t = DefaultRpcServerArgs::get_global().rpc_max_trace_filter_blocks)]
548    pub rpc_max_trace_filter_blocks: u64,
549
550    /// Maximum number of blocks that could be scanned per filter request. (0 = entire chain)
551    #[arg(long = "rpc.max-blocks-per-filter", alias = "rpc-max-blocks-per-filter", value_name = "COUNT", default_value_t = DefaultRpcServerArgs::get_global().rpc_max_blocks_per_filter)]
552    pub rpc_max_blocks_per_filter: ZeroAsNoneU64,
553
554    /// Maximum number of logs that can be returned in a single response. (0 = no limit)
555    #[arg(long = "rpc.max-logs-per-response", alias = "rpc-max-logs-per-response", value_name = "COUNT", default_value_t = DefaultRpcServerArgs::get_global().rpc_max_logs_per_response)]
556    pub rpc_max_logs_per_response: ZeroAsNoneU64,
557
558    /// Maximum gas limit for `eth_call` and call tracing RPC methods.
559    #[arg(
560        long = "rpc.gascap",
561        alias = "rpc-gascap",
562        value_name = "GAS_CAP",
563        value_parser = MaxOr::new(RangedU64ValueParser::<u64>::new().range(1..)),
564        default_value_t = DefaultRpcServerArgs::get_global().rpc_gas_cap
565    )]
566    pub rpc_gas_cap: u64,
567
568    /// Maximum memory the EVM can allocate per RPC request.
569    #[arg(
570        long = "rpc.evm-memory-limit",
571        alias = "rpc-evm-memory-limit",
572        value_name = "MEMORY_LIMIT",
573        value_parser = MaxOr::new(RangedU64ValueParser::<u64>::new().range(1..)),
574        default_value_t = DefaultRpcServerArgs::get_global().rpc_evm_memory_limit
575    )]
576    pub rpc_evm_memory_limit: u64,
577
578    /// Maximum eth transaction fee (in ether) that can be sent via the RPC APIs (0 = no cap)
579    #[arg(
580        long = "rpc.txfeecap",
581        alias = "rpc-txfeecap",
582        value_name = "TX_FEE_CAP",
583        value_parser = parse_ether_value,
584        default_value = "1.0"
585    )]
586    pub rpc_tx_fee_cap: u128,
587
588    /// Maximum number of blocks for `eth_simulateV1` call.
589    #[arg(
590        long = "rpc.max-simulate-blocks",
591        value_name = "BLOCKS_COUNT",
592        default_value_t = DefaultRpcServerArgs::get_global().rpc_max_simulate_blocks
593    )]
594    pub rpc_max_simulate_blocks: u64,
595
596    /// The maximum proof window for historical proof generation.
597    /// This value allows for generating historical proofs up to
598    /// configured number of blocks from current tip (up to `tip - window`).
599    #[arg(
600        long = "rpc.eth-proof-window",
601        default_value_t = DefaultRpcServerArgs::get_global().rpc_eth_proof_window,
602        value_parser = RangedU64ValueParser::<u64>::new().range(..=constants::MAX_ETH_PROOF_WINDOW)
603    )]
604    pub rpc_eth_proof_window: u64,
605
606    /// Maximum number of concurrent getproof requests.
607    #[arg(long = "rpc.proof-permits", alias = "rpc-proof-permits", value_name = "COUNT", default_value_t = DefaultRpcServerArgs::get_global().rpc_proof_permits)]
608    pub rpc_proof_permits: usize,
609
610    /// Configures the pending block behavior for RPC responses.
611    ///
612    /// Options: full (include all transactions), empty (header only), none (disable pending
613    /// blocks).
614    #[arg(long = "rpc.pending-block", default_value = "full", value_name = "KIND")]
615    pub rpc_pending_block: PendingBlockKind,
616
617    /// Endpoint to forward transactions to.
618    #[arg(long = "rpc.forwarder", alias = "rpc-forwarder", value_name = "FORWARDER")]
619    pub rpc_forwarder: Option<Url>,
620
621    /// Path to file containing disallowed addresses, json-encoded list of strings. Block
622    /// validation API will reject blocks containing transactions from these addresses.
623    #[arg(long = "builder.disallow", value_name = "PATH", value_parser = reth_cli_util::parsers::read_json_from_file::<AddressSet>, default_value = Resettable::from(DefaultRpcServerArgs::get_global().builder_disallow.as_ref().map(|v| format!("{:?}", v).into())))]
624    pub builder_disallow: Option<AddressSet>,
625
626    /// State cache configuration.
627    #[command(flatten)]
628    pub rpc_state_cache: RpcStateCacheArgs,
629
630    /// Gas price oracle configuration.
631    #[command(flatten)]
632    pub gas_price_oracle: GasPriceOracleArgs,
633
634    /// Timeout for `send_raw_transaction_sync` RPC method.
635    #[arg(
636        long = "rpc.send-raw-transaction-sync-timeout",
637        value_name = "SECONDS",
638        default_value = "30s",
639        value_parser = parse_duration_from_secs_or_ms,
640    )]
641    pub rpc_send_raw_transaction_sync_timeout: Duration,
642
643    /// Skip invalid transactions in `testing_buildBlockV1` instead of failing.
644    ///
645    /// When enabled, transactions that fail execution will be skipped, and all subsequent
646    /// transactions from the same sender will also be skipped.
647    #[arg(long = "testing.skip-invalid-transactions", default_value_t = true)]
648    pub testing_skip_invalid_transactions: bool,
649
650    /// Force upcasting EIP-4844 blob sidecars to EIP-7594 format when Osaka is active.
651    ///
652    /// When enabled, blob transactions submitted via `eth_sendRawTransaction` with EIP-4844
653    /// sidecars will be automatically converted to EIP-7594 format if the next block is Osaka.
654    /// By default this is disabled, meaning transactions are submitted as-is.
655    #[arg(long = "rpc.force-blob-sidecar-upcasting", default_value_t = false)]
656    pub rpc_force_blob_sidecar_upcasting: bool,
657}
658
659impl RpcServerArgs {
660    /// Enables the HTTP-RPC server.
661    pub const fn with_http(mut self) -> Self {
662        self.http = true;
663        self
664    }
665
666    /// Configures modules for the HTTP-RPC server.
667    pub fn with_http_api(mut self, http_api: RpcModuleSelection) -> Self {
668        self.http_api = Some(http_api);
669        self
670    }
671
672    /// Enables the WS-RPC server.
673    pub const fn with_ws(mut self) -> Self {
674        self.ws = true;
675        self
676    }
677
678    /// Configures modules for WS-RPC server.
679    pub fn with_ws_api(mut self, ws_api: RpcModuleSelection) -> Self {
680        self.ws_api = Some(ws_api);
681        self
682    }
683
684    /// Enables the Auth IPC
685    pub const fn with_auth_ipc(mut self) -> Self {
686        self.auth_ipc = true;
687        self
688    }
689
690    /// Configures modules for both the HTTP-RPC server and WS-RPC server.
691    ///
692    /// This is the same as calling both [`Self::with_http_api`] and [`Self::with_ws_api`].
693    pub fn with_api(self, api: RpcModuleSelection) -> Self {
694        self.with_http_api(api.clone()).with_ws_api(api)
695    }
696
697    /// Change rpc port numbers based on the instance number, if provided.
698    /// * The `auth_port` is scaled by a factor of `instance * 100`
699    /// * The `http_port` is scaled by a factor of `-instance`
700    /// * The `ws_port` is scaled by a factor of `instance * 2`
701    /// * The `ipcpath` is appended with the instance number: `/tmp/reth.ipc-<instance>`
702    ///
703    /// # Panics
704    /// Warning: if `instance` is zero in debug mode, this will panic.
705    ///
706    /// This will also panic in debug mode if either:
707    /// * `instance` is greater than `655` (scaling would overflow `u16`)
708    /// * `self.auth_port / 100 + (instance - 1)` would overflow `u16`
709    ///
710    /// In release mode, this will silently wrap around.
711    pub fn adjust_instance_ports(&mut self, instance: Option<u16>) {
712        if let Some(instance) = instance {
713            debug_assert_ne!(instance, 0, "instance must be non-zero");
714            // auth port is scaled by a factor of instance * 100
715            self.auth_port += instance * 100 - 100;
716            // http port is scaled by a factor of -instance
717            self.http_port -= instance - 1;
718            // ws port is scaled by a factor of instance * 2
719            self.ws_port += instance * 2 - 2;
720            // append instance file to ipc path
721            self.ipcpath = format!("{}-{}", self.ipcpath, instance);
722        }
723    }
724
725    /// Set the http port to zero, to allow the OS to assign a random unused port when the rpc
726    /// server binds to a socket.
727    pub const fn with_http_unused_port(mut self) -> Self {
728        self.http_port = 0;
729        self
730    }
731
732    /// Set the ws port to zero, to allow the OS to assign a random unused port when the rpc
733    /// server binds to a socket.
734    pub const fn with_ws_unused_port(mut self) -> Self {
735        self.ws_port = 0;
736        self
737    }
738
739    /// Set the auth port to zero, to allow the OS to assign a random unused port when the rpc
740    /// server binds to a socket.
741    pub const fn with_auth_unused_port(mut self) -> Self {
742        self.auth_port = 0;
743        self
744    }
745
746    /// Append a random string to the ipc path, to prevent possible collisions when multiple nodes
747    /// are being run on the same machine.
748    pub fn with_ipc_random_path(mut self) -> Self {
749        let random_string: String =
750            rand::rng().sample_iter(rand::distr::Alphanumeric).take(8).map(char::from).collect();
751        self.ipcpath = format!("{}-{}", self.ipcpath, random_string);
752        self
753    }
754
755    /// Configure all ports to be set to a random unused port when bound, and set the IPC path to a
756    /// random path.
757    pub fn with_unused_ports(mut self) -> Self {
758        self = self.with_http_unused_port();
759        self = self.with_ws_unused_port();
760        self = self.with_auth_unused_port();
761        self = self.with_ipc_random_path();
762        self
763    }
764
765    /// Apply a function to the args.
766    pub fn apply<F>(self, f: F) -> Self
767    where
768        F: FnOnce(Self) -> Self,
769    {
770        f(self)
771    }
772
773    /// Configures the timeout for send raw transaction sync.
774    pub const fn with_send_raw_transaction_sync_timeout(mut self, timeout: Duration) -> Self {
775        self.rpc_send_raw_transaction_sync_timeout = timeout;
776        self
777    }
778
779    /// Enables forced blob sidecar upcasting from EIP-4844 to EIP-7594 format.
780    pub const fn with_force_blob_sidecar_upcasting(mut self) -> Self {
781        self.rpc_force_blob_sidecar_upcasting = true;
782        self
783    }
784}
785
786impl Default for RpcServerArgs {
787    fn default() -> Self {
788        let DefaultRpcServerArgs {
789            http,
790            http_addr,
791            http_port,
792            http_disable_compression,
793            http_api,
794            http_corsdomain,
795            ws,
796            ws_addr,
797            ws_port,
798            ws_allowed_origins,
799            ws_api,
800            ipcdisable,
801            ipcpath,
802            ipc_socket_permissions,
803            auth_addr,
804            auth_port,
805            auth_jwtsecret,
806            auth_ipc,
807            auth_ipc_path,
808            disable_auth_server,
809            rpc_jwtsecret,
810            rpc_max_request_size,
811            rpc_max_response_size,
812            rpc_max_subscriptions_per_connection,
813            rpc_max_connections,
814            rpc_max_tracing_requests,
815            rpc_max_blocking_io_requests,
816            rpc_max_trace_filter_blocks,
817            rpc_max_blocks_per_filter,
818            rpc_max_logs_per_response,
819            rpc_gas_cap,
820            rpc_evm_memory_limit,
821            rpc_tx_fee_cap,
822            rpc_max_simulate_blocks,
823            rpc_eth_proof_window,
824            rpc_proof_permits,
825            rpc_pending_block,
826            rpc_forwarder,
827            builder_disallow,
828            rpc_state_cache,
829            gas_price_oracle,
830            rpc_send_raw_transaction_sync_timeout,
831        } = DefaultRpcServerArgs::get_global().clone();
832        Self {
833            http,
834            http_addr,
835            http_port,
836            http_disable_compression,
837            http_api,
838            http_corsdomain,
839            ws,
840            ws_addr,
841            ws_port,
842            ws_allowed_origins,
843            ws_api,
844            ipcdisable,
845            ipcpath,
846            ipc_socket_permissions,
847            auth_addr,
848            auth_port,
849            auth_jwtsecret,
850            auth_ipc,
851            auth_ipc_path,
852            disable_auth_server,
853            rpc_jwtsecret,
854            rpc_max_request_size,
855            rpc_max_response_size,
856            rpc_max_subscriptions_per_connection,
857            rpc_max_connections,
858            rpc_max_tracing_requests,
859            rpc_max_blocking_io_requests,
860            rpc_max_trace_filter_blocks,
861            rpc_max_blocks_per_filter,
862            rpc_max_logs_per_response,
863            rpc_gas_cap,
864            rpc_evm_memory_limit,
865            rpc_tx_fee_cap,
866            rpc_max_simulate_blocks,
867            rpc_eth_proof_window,
868            rpc_proof_permits,
869            rpc_pending_block,
870            rpc_forwarder,
871            builder_disallow,
872            rpc_state_cache,
873            gas_price_oracle,
874            rpc_send_raw_transaction_sync_timeout,
875            testing_skip_invalid_transactions: true,
876            rpc_force_blob_sidecar_upcasting: false,
877        }
878    }
879}
880
881/// clap value parser for [`RpcModuleSelection`] with configurable validation.
882#[derive(Clone, Debug, Default)]
883#[non_exhaustive]
884struct RpcModuleSelectionValueParser;
885
886impl TypedValueParser for RpcModuleSelectionValueParser {
887    type Value = RpcModuleSelection;
888
889    fn parse_ref(
890        &self,
891        _cmd: &Command,
892        _arg: Option<&Arg>,
893        value: &OsStr,
894    ) -> Result<Self::Value, clap::Error> {
895        let val =
896            value.to_str().ok_or_else(|| clap::Error::new(clap::error::ErrorKind::InvalidUtf8))?;
897        // This will now accept any module name, creating Other(name) for unknowns
898        Ok(val
899            .parse::<RpcModuleSelection>()
900            .expect("RpcModuleSelection parsing cannot fail with Other variant"))
901    }
902
903    fn possible_values(&self) -> Option<Box<dyn Iterator<Item = PossibleValue> + '_>> {
904        // Only show standard modules in help text (excludes "other")
905        let values = RethRpcModule::standard_variant_names().map(PossibleValue::new);
906        Some(Box::new(values))
907    }
908}
909
910#[cfg(test)]
911mod tests {
912    use super::*;
913    use clap::{Args, Parser};
914
915    /// A helper type to parse Args more easily
916    #[derive(Parser)]
917    struct CommandParser<T: Args> {
918        #[command(flatten)]
919        args: T,
920    }
921
922    #[test]
923    fn test_rpc_server_args_parser() {
924        let args =
925            CommandParser::<RpcServerArgs>::parse_from(["reth", "--http.api", "eth,admin,debug"])
926                .args;
927
928        let apis = args.http_api.unwrap();
929        let expected = RpcModuleSelection::try_from_selection(["eth", "admin", "debug"]).unwrap();
930
931        assert_eq!(apis, expected);
932    }
933
934    #[test]
935    fn test_rpc_server_eth_call_bundle_args() {
936        let args =
937            CommandParser::<RpcServerArgs>::parse_from(["reth", "--http.api", "eth,admin,debug"])
938                .args;
939
940        let apis = args.http_api.unwrap();
941        let expected = RpcModuleSelection::try_from_selection(["eth", "admin", "debug"]).unwrap();
942
943        assert_eq!(apis, expected);
944    }
945
946    #[test]
947    fn test_rpc_server_args_parser_none() {
948        let args = CommandParser::<RpcServerArgs>::parse_from(["reth", "--http.api", "none"]).args;
949        let apis = args.http_api.unwrap();
950        let expected = RpcModuleSelection::Selection(Default::default());
951        assert_eq!(apis, expected);
952    }
953
954    #[test]
955    fn rpc_server_args_default_sanity_test() {
956        let default_args = RpcServerArgs::default();
957        let args = CommandParser::<RpcServerArgs>::parse_from(["reth"]).args;
958
959        assert_eq!(args, default_args);
960    }
961
962    #[test]
963    fn test_rpc_tx_fee_cap_parse_integer() {
964        let args = CommandParser::<RpcServerArgs>::parse_from(["reth", "--rpc.txfeecap", "2"]).args;
965        let expected = 2_000_000_000_000_000_000u128; // 2 ETH in wei
966        assert_eq!(args.rpc_tx_fee_cap, expected);
967    }
968
969    #[test]
970    fn test_rpc_tx_fee_cap_parse_decimal() {
971        let args =
972            CommandParser::<RpcServerArgs>::parse_from(["reth", "--rpc.txfeecap", "1.5"]).args;
973        let expected = 1_500_000_000_000_000_000u128; // 1.5 ETH in wei
974        assert_eq!(args.rpc_tx_fee_cap, expected);
975    }
976
977    #[test]
978    fn test_rpc_tx_fee_cap_parse_zero() {
979        let args = CommandParser::<RpcServerArgs>::parse_from(["reth", "--rpc.txfeecap", "0"]).args;
980        assert_eq!(args.rpc_tx_fee_cap, 0); // 0 = no cap
981    }
982
983    #[test]
984    fn test_rpc_tx_fee_cap_parse_none() {
985        let args = CommandParser::<RpcServerArgs>::parse_from(["reth"]).args;
986        let expected = 1_000_000_000_000_000_000u128;
987        assert_eq!(args.rpc_tx_fee_cap, expected); // 1 ETH default cap
988    }
989
990    #[test]
991    fn test_rpc_server_args() {
992        let args = RpcServerArgs {
993            http: true,
994            http_addr: "127.0.0.1".parse().unwrap(),
995            http_port: 8545,
996            http_disable_compression: false,
997            http_api: Some(RpcModuleSelection::try_from_selection(["eth", "admin"]).unwrap()),
998            http_corsdomain: Some("*".to_string()),
999            ws: true,
1000            ws_addr: "127.0.0.1".parse().unwrap(),
1001            ws_port: 8546,
1002            ws_allowed_origins: Some("*".to_string()),
1003            ws_api: Some(RpcModuleSelection::try_from_selection(["eth", "admin"]).unwrap()),
1004            ipcdisable: false,
1005            ipcpath: "reth.ipc".to_string(),
1006            ipc_socket_permissions: Some("0o666".to_string()),
1007            auth_addr: "127.0.0.1".parse().unwrap(),
1008            auth_port: 8551,
1009            auth_jwtsecret: Some(std::path::PathBuf::from("/tmp/jwt.hex")),
1010            auth_ipc: false,
1011            auth_ipc_path: "engine.ipc".to_string(),
1012            disable_auth_server: false,
1013            rpc_jwtsecret: Some(
1014                JwtSecret::from_hex(
1015                    "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
1016                )
1017                .unwrap(),
1018            ),
1019            rpc_max_request_size: 15u32.into(),
1020            rpc_max_response_size: 160u32.into(),
1021            rpc_max_subscriptions_per_connection: 1024u32.into(),
1022            rpc_max_connections: 500u32.into(),
1023            rpc_max_tracing_requests: 16,
1024            rpc_max_blocking_io_requests: 256,
1025            rpc_max_trace_filter_blocks: 4000,
1026            rpc_max_blocks_per_filter: 1000u64.into(),
1027            rpc_max_logs_per_response: 10000u64.into(),
1028            rpc_gas_cap: 50_000_000,
1029            rpc_evm_memory_limit: 256,
1030            rpc_tx_fee_cap: 2_000_000_000_000_000_000u128,
1031            rpc_max_simulate_blocks: 256,
1032            rpc_eth_proof_window: 100_000,
1033            rpc_proof_permits: 16,
1034            rpc_pending_block: PendingBlockKind::Full,
1035            rpc_forwarder: Some("http://localhost:8545".parse().unwrap()),
1036            builder_disallow: None,
1037            rpc_state_cache: RpcStateCacheArgs {
1038                max_blocks: 5000,
1039                max_receipts: 2000,
1040                max_headers: 1000,
1041                max_concurrent_db_requests: 512,
1042                max_cached_tx_hashes: 30_000,
1043            },
1044            gas_price_oracle: GasPriceOracleArgs {
1045                blocks: 20,
1046                ignore_price: 2,
1047                max_price: 500_000_000_000,
1048                percentile: 60,
1049                default_suggested_fee: None,
1050            },
1051            rpc_send_raw_transaction_sync_timeout: std::time::Duration::from_secs(30),
1052            testing_skip_invalid_transactions: true,
1053            rpc_force_blob_sidecar_upcasting: false,
1054        };
1055
1056        let parsed_args = CommandParser::<RpcServerArgs>::parse_from([
1057            "reth",
1058            "--http",
1059            "--http.addr",
1060            "127.0.0.1",
1061            "--http.port",
1062            "8545",
1063            "--http.api",
1064            "eth,admin",
1065            "--http.corsdomain",
1066            "*",
1067            "--ws",
1068            "--ws.addr",
1069            "127.0.0.1",
1070            "--ws.port",
1071            "8546",
1072            "--ws.origins",
1073            "*",
1074            "--ws.api",
1075            "eth,admin",
1076            "--ipcpath",
1077            "reth.ipc",
1078            "--ipc.permissions",
1079            "0o666",
1080            "--authrpc.addr",
1081            "127.0.0.1",
1082            "--authrpc.port",
1083            "8551",
1084            "--authrpc.jwtsecret",
1085            "/tmp/jwt.hex",
1086            "--auth-ipc.path",
1087            "engine.ipc",
1088            "--rpc.jwtsecret",
1089            "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
1090            "--rpc.max-request-size",
1091            "15",
1092            "--rpc.max-response-size",
1093            "160",
1094            "--rpc.max-subscriptions-per-connection",
1095            "1024",
1096            "--rpc.max-connections",
1097            "500",
1098            "--rpc.max-tracing-requests",
1099            "16",
1100            "--rpc.max-blocking-io-requests",
1101            "256",
1102            "--rpc.max-trace-filter-blocks",
1103            "4000",
1104            "--rpc.max-blocks-per-filter",
1105            "1000",
1106            "--rpc.max-logs-per-response",
1107            "10000",
1108            "--rpc.gascap",
1109            "50000000",
1110            "--rpc.evm-memory-limit",
1111            "256",
1112            "--rpc.txfeecap",
1113            "2.0",
1114            "--rpc.max-simulate-blocks",
1115            "256",
1116            "--rpc.eth-proof-window",
1117            "100000",
1118            "--rpc.proof-permits",
1119            "16",
1120            "--rpc.pending-block",
1121            "full",
1122            "--rpc.forwarder",
1123            "http://localhost:8545",
1124            "--rpc-cache.max-blocks",
1125            "5000",
1126            "--rpc-cache.max-receipts",
1127            "2000",
1128            "--rpc-cache.max-headers",
1129            "1000",
1130            "--rpc-cache.max-concurrent-db-requests",
1131            "512",
1132            "--gpo.blocks",
1133            "20",
1134            "--gpo.ignoreprice",
1135            "2",
1136            "--gpo.maxprice",
1137            "500000000000",
1138            "--gpo.percentile",
1139            "60",
1140            "--rpc.send-raw-transaction-sync-timeout",
1141            "30s",
1142            "--testing.skip-invalid-transactions",
1143        ])
1144        .args;
1145
1146        assert_eq!(parsed_args, args);
1147    }
1148}