1use 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
28static RPC_SERVER_DEFAULTS: OnceLock<DefaultRpcServerArgs> = OnceLock::new();
30
31pub(crate) const RPC_DEFAULT_MAX_SUBS_PER_CONN: u32 = 1024;
33
34pub(crate) const RPC_DEFAULT_MAX_REQUEST_SIZE_MB: u32 = 15;
36
37pub(crate) const RPC_DEFAULT_MAX_RESPONSE_SIZE_MB: u32 = 160;
41
42pub(crate) const RPC_DEFAULT_MAX_CONNECTIONS: u32 = 500;
47
48#[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 pub fn try_init(self) -> Result<(), Self> {
100 RPC_SERVER_DEFAULTS.set(self)
101 }
102
103 pub fn get_global() -> &'static Self {
105 RPC_SERVER_DEFAULTS.get_or_init(Self::default)
106 }
107
108 pub const fn with_http(mut self, v: bool) -> Self {
110 self.http = v;
111 self
112 }
113
114 pub const fn with_http_addr(mut self, v: IpAddr) -> Self {
116 self.http_addr = v;
117 self
118 }
119
120 pub const fn with_http_port(mut self, v: u16) -> Self {
122 self.http_port = v;
123 self
124 }
125
126 pub const fn with_http_disable_compression(mut self, v: bool) -> Self {
128 self.http_disable_compression = v;
129 self
130 }
131
132 pub fn with_http_api(mut self, v: Option<RpcModuleSelection>) -> Self {
134 self.http_api = v;
135 self
136 }
137
138 pub fn with_http_corsdomain(mut self, v: Option<String>) -> Self {
140 self.http_corsdomain = v;
141 self
142 }
143
144 pub const fn with_ws(mut self, v: bool) -> Self {
146 self.ws = v;
147 self
148 }
149
150 pub const fn with_ws_addr(mut self, v: IpAddr) -> Self {
152 self.ws_addr = v;
153 self
154 }
155
156 pub const fn with_ws_port(mut self, v: u16) -> Self {
158 self.ws_port = v;
159 self
160 }
161
162 pub fn with_ws_allowed_origins(mut self, v: Option<String>) -> Self {
164 self.ws_allowed_origins = v;
165 self
166 }
167
168 pub fn with_ws_api(mut self, v: Option<RpcModuleSelection>) -> Self {
170 self.ws_api = v;
171 self
172 }
173
174 pub const fn with_ipcdisable(mut self, v: bool) -> Self {
176 self.ipcdisable = v;
177 self
178 }
179
180 pub fn with_ipcpath(mut self, v: String) -> Self {
182 self.ipcpath = v;
183 self
184 }
185
186 pub fn with_ipc_socket_permissions(mut self, v: Option<String>) -> Self {
188 self.ipc_socket_permissions = v;
189 self
190 }
191
192 pub const fn with_auth_addr(mut self, v: IpAddr) -> Self {
194 self.auth_addr = v;
195 self
196 }
197
198 pub const fn with_auth_port(mut self, v: u16) -> Self {
200 self.auth_port = v;
201 self
202 }
203
204 pub fn with_auth_jwtsecret(mut self, v: Option<PathBuf>) -> Self {
206 self.auth_jwtsecret = v;
207 self
208 }
209
210 pub const fn with_auth_ipc(mut self, v: bool) -> Self {
212 self.auth_ipc = v;
213 self
214 }
215
216 pub fn with_auth_ipc_path(mut self, v: String) -> Self {
218 self.auth_ipc_path = v;
219 self
220 }
221
222 pub const fn with_disable_auth_server(mut self, v: bool) -> Self {
224 self.disable_auth_server = v;
225 self
226 }
227
228 pub const fn with_rpc_jwtsecret(mut self, v: Option<JwtSecret>) -> Self {
230 self.rpc_jwtsecret = v;
231 self
232 }
233
234 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 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 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 pub const fn with_rpc_max_connections(mut self, v: MaxU32) -> Self {
254 self.rpc_max_connections = v;
255 self
256 }
257
258 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 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 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 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 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 pub const fn with_rpc_gas_cap(mut self, v: u64) -> Self {
290 self.rpc_gas_cap = v;
291 self
292 }
293
294 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 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 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 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 pub const fn with_rpc_proof_permits(mut self, v: usize) -> Self {
320 self.rpc_proof_permits = v;
321 self
322 }
323
324 pub const fn with_rpc_pending_block(mut self, v: PendingBlockKind) -> Self {
326 self.rpc_pending_block = v;
327 self
328 }
329
330 pub fn with_rpc_forwarder(mut self, v: Option<Url>) -> Self {
332 self.rpc_forwarder = v;
333 self
334 }
335
336 pub fn with_builder_disallow(mut self, v: Option<AddressSet>) -> Self {
338 self.builder_disallow = v;
339 self
340 }
341
342 pub const fn with_rpc_state_cache(mut self, v: RpcStateCacheArgs) -> Self {
344 self.rpc_state_cache = v;
345 self
346 }
347
348 pub const fn with_gas_price_oracle(mut self, v: GasPriceOracleArgs) -> Self {
350 self.gas_price_oracle = v;
351 self
352 }
353
354 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#[derive(Debug, Clone, Args, PartialEq, Eq)]
413#[command(next_help_heading = "RPC")]
414pub struct RpcServerArgs {
415 #[arg(long, default_value_if("dev", "true", "true"), default_value_t = DefaultRpcServerArgs::get_global().http)]
417 pub http: bool,
418
419 #[arg(long = "http.addr", default_value_t = DefaultRpcServerArgs::get_global().http_addr)]
421 pub http_addr: IpAddr,
422
423 #[arg(long = "http.port", default_value_t = DefaultRpcServerArgs::get_global().http_port)]
425 pub http_port: u16,
426
427 #[arg(long = "http.disable-compression", default_value_t = DefaultRpcServerArgs::get_global().http_disable_compression)]
429 pub http_disable_compression: bool,
430
431 #[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 #[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 #[arg(long, default_value_t = DefaultRpcServerArgs::get_global().ws)]
441 pub ws: bool,
442
443 #[arg(long = "ws.addr", default_value_t = DefaultRpcServerArgs::get_global().ws_addr)]
445 pub ws_addr: IpAddr,
446
447 #[arg(long = "ws.port", default_value_t = DefaultRpcServerArgs::get_global().ws_port)]
449 pub ws_port: u16,
450
451 #[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 #[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 #[arg(long, default_value_t = DefaultRpcServerArgs::get_global().ipcdisable)]
461 pub ipcdisable: bool,
462
463 #[arg(long, default_value_t = DefaultRpcServerArgs::get_global().ipcpath.clone())]
465 pub ipcpath: String,
466
467 #[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 #[arg(long = "authrpc.addr", default_value_t = DefaultRpcServerArgs::get_global().auth_addr)]
475 pub auth_addr: IpAddr,
476
477 #[arg(long = "authrpc.port", default_value_t = DefaultRpcServerArgs::get_global().auth_port)]
479 pub auth_port: u16,
480
481 #[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 #[arg(long, default_value_t = DefaultRpcServerArgs::get_global().auth_ipc)]
492 pub auth_ipc: bool,
493
494 #[arg(long = "auth-ipc.path", default_value_t = DefaultRpcServerArgs::get_global().auth_ipc_path.clone())]
496 pub auth_ipc_path: String,
497
498 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[arg(long = "rpc.pending-block", default_value = "full", value_name = "KIND")]
615 pub rpc_pending_block: PendingBlockKind,
616
617 #[arg(long = "rpc.forwarder", alias = "rpc-forwarder", value_name = "FORWARDER")]
619 pub rpc_forwarder: Option<Url>,
620
621 #[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 #[command(flatten)]
628 pub rpc_state_cache: RpcStateCacheArgs,
629
630 #[command(flatten)]
632 pub gas_price_oracle: GasPriceOracleArgs,
633
634 #[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 #[arg(long = "testing.skip-invalid-transactions", default_value_t = true)]
648 pub testing_skip_invalid_transactions: bool,
649
650 #[arg(long = "rpc.force-blob-sidecar-upcasting", default_value_t = false)]
656 pub rpc_force_blob_sidecar_upcasting: bool,
657}
658
659impl RpcServerArgs {
660 pub const fn with_http(mut self) -> Self {
662 self.http = true;
663 self
664 }
665
666 pub fn with_http_api(mut self, http_api: RpcModuleSelection) -> Self {
668 self.http_api = Some(http_api);
669 self
670 }
671
672 pub const fn with_ws(mut self) -> Self {
674 self.ws = true;
675 self
676 }
677
678 pub fn with_ws_api(mut self, ws_api: RpcModuleSelection) -> Self {
680 self.ws_api = Some(ws_api);
681 self
682 }
683
684 pub const fn with_auth_ipc(mut self) -> Self {
686 self.auth_ipc = true;
687 self
688 }
689
690 pub fn with_api(self, api: RpcModuleSelection) -> Self {
694 self.with_http_api(api.clone()).with_ws_api(api)
695 }
696
697 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 self.auth_port += instance * 100 - 100;
716 self.http_port -= instance - 1;
718 self.ws_port += instance * 2 - 2;
720 self.ipcpath = format!("{}-{}", self.ipcpath, instance);
722 }
723 }
724
725 pub const fn with_http_unused_port(mut self) -> Self {
728 self.http_port = 0;
729 self
730 }
731
732 pub const fn with_ws_unused_port(mut self) -> Self {
735 self.ws_port = 0;
736 self
737 }
738
739 pub const fn with_auth_unused_port(mut self) -> Self {
742 self.auth_port = 0;
743 self
744 }
745
746 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 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 pub fn apply<F>(self, f: F) -> Self
767 where
768 F: FnOnce(Self) -> Self,
769 {
770 f(self)
771 }
772
773 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 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#[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 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 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 #[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; 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; 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); }
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); }
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}