agglayer_config/
rpc.rs

1use std::{net::Ipv4Addr, time::Duration};
2
3use jsonrpsee::core::TEN_MB_SIZE_BYTES;
4use serde::{Deserialize, Serialize};
5use serde_with::serde_as;
6
7use crate::{Port, PortDefaults};
8
9pub enum GrpcService {}
10impl PortDefaults for GrpcService {
11    const DEFAULT: u16 = 9089;
12    const ENV_VAR: Option<&str> = Some("AGGLAYER_GRPC_PORT");
13}
14
15pub enum ReadRpcService {}
16impl PortDefaults for ReadRpcService {
17    const DEFAULT: u16 = 9090;
18    const ENV_VAR: Option<&str> = Some("AGGLAYER_READRPC_PORT");
19}
20
21pub enum AdminService {}
22impl PortDefaults for AdminService {
23    const DEFAULT: u16 = 9091;
24    const ENV_VAR: Option<&str> = Some("AGGLAYER_ADMIN_PORT");
25}
26
27/// The local RPC server configuration.
28#[serde_as]
29#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
30#[serde(rename_all = "kebab-case")]
31pub struct RpcConfig {
32    /// The default port for the local gRPC server.
33    /// Overridden by `AGGLAYER_GRPC_PORT` environment variable, defaults to
34    /// 9089.
35    #[serde(default)]
36    pub grpc_port: Port<GrpcService>,
37
38    /// The default port for the local ReadRPC server.
39    /// Overridden by `AGGLAYER_READRPC_PORT` environment variable, defaults to
40    /// 9090.
41    #[serde(default)]
42    pub readrpc_port: Port<ReadRpcService>,
43
44    /// The default port for the local AdminRPC server.
45    /// Overridden by `AGGLAYER_ADMIN_PORT` environment variable, defaults to
46    /// 9091.
47    #[serde(default)]
48    pub admin_port: Port<AdminService>,
49
50    #[serde(default = "default_host")]
51    pub host: Ipv4Addr,
52
53    /// The maximum size of the request body in bytes.
54    #[serde(
55        skip_serializing_if = "same_as_default_body_size",
56        default = "default_body_size"
57    )]
58    pub max_request_body_size: u32,
59
60    /// The maximum size of the response body in bytes.
61    #[serde(
62        skip_serializing_if = "same_as_default_body_size",
63        default = "default_body_size"
64    )]
65    pub max_response_body_size: u32,
66
67    /// The maximum number of connections.
68    #[serde(
69        skip_serializing_if = "same_as_default_max_connections",
70        default = "default_max_connections"
71    )]
72    pub max_connections: u32,
73
74    /// The maximum number of requests in a batch request. If `None`, the
75    /// batch request limit is unlimited.
76    #[serde(skip_serializing_if = "crate::is_default")]
77    pub batch_request_limit: Option<u32>,
78
79    /// The interval at which to send ping messages
80    #[serde(skip)]
81    #[serde_as(as = "Option<crate::with::HumanDuration>")]
82    pub ping_interval: Option<Duration>,
83
84    /// Timeout for completion of an RPC request to the AggLayer node.
85    #[serde_as(as = "crate::with::HumanDuration")]
86    #[serde(default = "default_request_timeout")]
87    pub request_timeout: Duration,
88}
89
90impl Default for RpcConfig {
91    fn default() -> Self {
92        Self {
93            grpc_port: Default::default(),
94            readrpc_port: Default::default(),
95            admin_port: Default::default(),
96            host: default_host(),
97            max_request_body_size: default_body_size(),
98            max_response_body_size: default_body_size(),
99            max_connections: default_max_connections(),
100            batch_request_limit: None,
101            ping_interval: None,
102            request_timeout: default_request_timeout(),
103        }
104    }
105}
106
107/// The default maximum number of connections.
108fn default_max_connections() -> u32 {
109    100
110}
111
112/// The default size of the request and response bodies in bytes.
113fn default_body_size() -> u32 {
114    TEN_MB_SIZE_BYTES
115}
116
117/// The default host for the local RPC server.
118const fn default_host() -> Ipv4Addr {
119    Ipv4Addr::new(0, 0, 0, 0)
120}
121
122/// Default timeout for completion of an RPC request to the AggLayer node.
123const fn default_request_timeout() -> Duration {
124    Duration::from_secs(180)
125}
126
127fn same_as_default_body_size(size: &u32) -> bool {
128    *size == default_body_size()
129}
130
131fn same_as_default_max_connections(max_connections: &u32) -> bool {
132    *max_connections == default_max_connections()
133}