Repository navigation
Expand file tree
/
Copy pathconfig.rs
More file actions
205 lines (160 loc) · 5.48 KB
/
Copy pathconfig.rs
File metadata and controls
205 lines (160 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use std::{process, sync::LazyLock};
use clap::Parser;
use thiserror::Error;
use crate::server::{
config::{ConfigLogging, ConfigLoggingError, ConfigMetrics, ConfigMetricsError},
proxy::config::{
ConfigAuthrpc,
ConfigAuthrpcError,
ConfigCircuitBreaker,
ConfigCircuitBreakerError,
ConfigFlashblocks,
ConfigFlashblocksError,
ConfigRpc,
ConfigRpcError,
ConfigTls,
ConfigTlsError,
},
};
pub(crate) const ALREADY_VALIDATED: &str = "parameter must have been validated already";
pub(crate) static PARALLELISM: LazyLock<usize> =
LazyLock::new(|| std::thread::available_parallelism().map_or(2, std::num::NonZero::get));
pub(crate) static PARALLELISM_STRING: LazyLock<String> = LazyLock::new(|| PARALLELISM.to_string());
// Config --------------------------------------------------------------
#[derive(Clone, Parser)]
#[command(about, author, long_about = None, term_width = 90, version)]
pub struct Config {
#[command(flatten)]
pub(crate) authrpc: ConfigAuthrpc,
#[command(flatten)]
pub(crate) circuit_breaker: ConfigCircuitBreaker,
#[command(flatten)]
pub(crate) flashblocks: ConfigFlashblocks,
#[command(flatten)]
pub(crate) logging: ConfigLogging,
#[command(flatten)]
pub(crate) metrics: ConfigMetrics,
#[command(flatten)]
pub(crate) rpc: ConfigRpc,
#[command(flatten)]
pub(crate) tls: ConfigTls,
}
impl Config {
pub fn setup() -> Self {
let mut res = Config::parse();
if let Some(errs) = res.clone().validate() {
for err in errs.iter() {
eprintln!("fatal: {}", err);
}
process::exit(1);
};
res.logging.setup_logging();
res.preprocess();
res
}
pub(crate) fn validate(self) -> Option<Vec<ConfigError>> {
let mut errs: Vec<ConfigError> = vec![];
// authrpc proxy
if self.rpc.enabled &&
let Some(_errs) = self.authrpc.validate()
{
errs.append(&mut _errs.into_iter().map(|err| err.into()).collect());
}
// circuit-breaker
if let Some(_errs) = self.circuit_breaker.validate() {
errs.append(&mut _errs.into_iter().map(|err| err.into()).collect());
}
// flashblocks proxy
if self.flashblocks.enabled &&
let Some(_errs) = self.flashblocks.validate()
{
errs.append(&mut _errs.into_iter().map(|err| err.into()).collect());
}
// logging
if let Some(_errs) = self.logging.validate() {
errs.append(&mut _errs.into_iter().map(|err| err.into()).collect());
}
// metrics
if let Some(_errs) = self.metrics.validate() {
errs.append(&mut _errs.into_iter().map(|err| err.into()).collect());
}
// rpc proxy
if self.rpc.enabled &&
let Some(_errs) = self.rpc.validate()
{
errs.append(&mut _errs.into_iter().map(|err| err.into()).collect());
}
// tls
if (!self.tls.certificate.is_empty() || !self.tls.key.is_empty()) &&
let Some(_errs) = self.tls.validate()
{
errs.append(&mut _errs.into_iter().map(|err| err.into()).collect());
}
if !self.authrpc.enabled && !self.flashblocks.enabled && !self.rpc.enabled {
errs.push(ConfigError::NoEnabledProxies);
}
match errs.len() {
0 => None,
_ => Some(errs),
}
}
pub(crate) fn preprocess(&mut self) {
self.authrpc.preprocess();
self.rpc.preprocess();
}
}
// ConfigError ---------------------------------------------------------
#[derive(Debug, Error)]
pub(crate) enum ConfigError {
#[error("invalid authrpc proxy configuration: {0}")]
ConfigAuthrpcInvalid(ConfigAuthrpcError),
#[error("invalid circuit-breaker configuration: {0}")]
ConfigCircuitBreakerInvalid(ConfigCircuitBreakerError),
#[error("invalid flashblocks proxy configuration: {0}")]
ConfigFlashblocksInvalid(ConfigFlashblocksError),
#[error("invalid logging configuration: {0}")]
ConfigLoggingInvalid(ConfigLoggingError),
#[error("invalid metrics configuration: {0}")]
ConfigMetricsInvalid(ConfigMetricsError),
#[error("invalid rpc proxy configuration: {0}")]
ConfigRpcInvalid(ConfigRpcError),
#[error("invalid tls configuration: {0}")]
ConfigTlsInvalid(ConfigTlsError),
#[error("no enabled proxies")]
NoEnabledProxies,
}
impl From<ConfigAuthrpcError> for ConfigError {
fn from(err: ConfigAuthrpcError) -> Self {
Self::ConfigAuthrpcInvalid(err)
}
}
impl From<ConfigCircuitBreakerError> for ConfigError {
fn from(err: ConfigCircuitBreakerError) -> Self {
Self::ConfigCircuitBreakerInvalid(err)
}
}
impl From<ConfigFlashblocksError> for ConfigError {
fn from(err: ConfigFlashblocksError) -> Self {
Self::ConfigFlashblocksInvalid(err)
}
}
impl From<ConfigLoggingError> for ConfigError {
fn from(err: ConfigLoggingError) -> Self {
Self::ConfigLoggingInvalid(err)
}
}
impl From<ConfigMetricsError> for ConfigError {
fn from(err: ConfigMetricsError) -> Self {
Self::ConfigMetricsInvalid(err)
}
}
impl From<ConfigRpcError> for ConfigError {
fn from(err: ConfigRpcError) -> Self {
Self::ConfigRpcInvalid(err)
}
}
impl From<ConfigTlsError> for ConfigError {
fn from(err: ConfigTlsError) -> Self {
Self::ConfigTlsInvalid(err)
}
}