1
1
use std:: { collections:: HashMap , sync:: Arc } ;
2
2
3
- use eyre:: { eyre, ContextCompat } ;
3
+ use eyre:: { eyre, ContextCompat , Result , WrapErr } ;
4
4
use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
5
5
6
6
use crate :: {
@@ -43,29 +43,29 @@ pub struct CommitBoostConfig {
43
43
pub metrics : MetricsConfig ,
44
44
}
45
45
46
- fn load_from_file < T : DeserializeOwned > ( path : & str ) -> T {
47
- let config_file = std :: fs :: read_to_string ( path )
48
- . unwrap_or_else ( |_| panic ! ( "Unable to find config file: '{}'" , path ) ) ;
49
- toml:: from_str ( & config_file) . unwrap ( )
46
+ fn load_from_file < T : DeserializeOwned > ( path : & str ) -> Result < T > {
47
+ let config_file =
48
+ std :: fs :: read_to_string ( path ) . wrap_err ( format ! ( "Unable to find config file: {path}" ) ) ? ;
49
+ toml:: from_str ( & config_file) . wrap_err ( "could not deserialize toml from string" )
50
50
}
51
51
52
- fn load_file_from_env < T : DeserializeOwned > ( env : & str ) -> T {
53
- let path = std:: env:: var ( env) . unwrap_or_else ( |_| panic ! ( "{env} is not set" ) ) ;
52
+ fn load_file_from_env < T : DeserializeOwned > ( env : & str ) -> Result < T > {
53
+ let path = std:: env:: var ( env) . wrap_err ( format ! ( "{env} is not set" ) ) ? ;
54
54
load_from_file ( & path)
55
55
}
56
56
57
57
/// Loads a map of module id -> jwt token from a json env
58
- fn load_jwts ( ) -> HashMap < String , String > {
59
- let jwts = std:: env:: var ( JWTS_ENV ) . unwrap_or_else ( |_| panic ! ( "{JWTS_ENV} is not set" ) ) ;
60
- serde_json:: from_str ( & jwts) . unwrap_or_else ( |_| panic ! ( "Failed to parse jwts: {jwts}" ) )
58
+ fn load_jwts ( ) -> Result < HashMap < String , String > > {
59
+ let jwts = std:: env:: var ( JWTS_ENV ) . wrap_err ( format ! ( "{JWTS_ENV} is not set" ) ) ? ;
60
+ serde_json:: from_str ( & jwts) . wrap_err ( "could not deserialize json from string" )
61
61
}
62
62
63
63
impl CommitBoostConfig {
64
- pub fn from_file ( path : & str ) -> Self {
64
+ pub fn from_file ( path : & str ) -> Result < Self > {
65
65
load_from_file ( path)
66
66
}
67
67
68
- pub fn from_env_path ( ) -> Self {
68
+ pub fn from_env_path ( ) -> Result < Self > {
69
69
load_file_from_env ( CB_CONFIG_ENV )
70
70
}
71
71
}
@@ -92,18 +92,18 @@ pub struct StartSignerConfig {
92
92
}
93
93
94
94
impl StartSignerConfig {
95
- pub fn load_from_env ( ) -> Self {
96
- let config = CommitBoostConfig :: from_env_path ( ) ;
95
+ pub fn load_from_env ( ) -> Result < Self > {
96
+ let config = CommitBoostConfig :: from_env_path ( ) ? ;
97
97
98
- let jwts = load_jwts ( ) ;
99
- let server_port = load_env_var_infallible ( SIGNER_SERVER_ENV ) . parse ( ) . unwrap ( ) ;
98
+ let jwts = load_jwts ( ) ? ;
99
+ let server_port = load_env_var ( SIGNER_SERVER_ENV ) ? . parse ( ) ? ;
100
100
101
- StartSignerConfig {
101
+ Ok ( StartSignerConfig {
102
102
chain : config. chain ,
103
103
loader : config. signer . expect ( "Signer config is missing" ) . loader ,
104
104
server_port,
105
105
jwts,
106
- }
106
+ } )
107
107
}
108
108
}
109
109
@@ -121,9 +121,9 @@ pub struct ModuleMetricsConfig {
121
121
}
122
122
123
123
impl ModuleMetricsConfig {
124
- pub fn load_from_env ( ) -> Self {
125
- let server_port = load_env_var_infallible ( METRICS_SERVER_ENV ) . parse ( ) . unwrap ( ) ;
126
- ModuleMetricsConfig { server_port }
124
+ pub fn load_from_env ( ) -> Result < Self > {
125
+ let server_port = load_env_var ( METRICS_SERVER_ENV ) ? . parse ( ) ? ;
126
+ Ok ( ModuleMetricsConfig { server_port } )
127
127
}
128
128
}
129
129
@@ -163,9 +163,10 @@ fn default_pbs() -> String {
163
163
}
164
164
165
165
/// Loads the default pbs config, i.e. with no signer client or custom data
166
- pub fn load_pbs_config ( ) -> eyre:: Result < PbsModuleConfig < ( ) > > {
167
- let config = CommitBoostConfig :: from_env_path ( ) ;
168
- let relay_clients = config. relays . into_iter ( ) . map ( RelayClient :: new) . collect ( ) ;
166
+ pub fn load_pbs_config ( ) -> Result < PbsModuleConfig < ( ) > > {
167
+ let config = CommitBoostConfig :: from_env_path ( ) ?;
168
+ let relay_clients =
169
+ config. relays . into_iter ( ) . map ( RelayClient :: new) . collect :: < Result < Vec < RelayClient > > > ( ) ?;
169
170
170
171
Ok ( PbsModuleConfig {
171
172
chain : config. chain ,
@@ -177,7 +178,7 @@ pub fn load_pbs_config() -> eyre::Result<PbsModuleConfig<()>> {
177
178
}
178
179
179
180
/// Loads a custom pbs config, i.e. with signer client and/or custom data
180
- pub fn load_pbs_custom_config < T : DeserializeOwned > ( ) -> eyre :: Result < PbsModuleConfig < T > > {
181
+ pub fn load_pbs_custom_config < T : DeserializeOwned > ( ) -> Result < PbsModuleConfig < T > > {
181
182
#[ derive( Debug , Deserialize ) ]
182
183
struct CustomPbsConfig < U > {
183
184
#[ serde( flatten) ]
@@ -194,17 +195,19 @@ pub fn load_pbs_custom_config<T: DeserializeOwned>() -> eyre::Result<PbsModuleCo
194
195
}
195
196
196
197
// load module config including the extra data (if any)
197
- let cb_config: StubConfig < T > = load_file_from_env ( CB_CONFIG_ENV ) ;
198
- let relay_clients = cb_config. relays . into_iter ( ) . map ( RelayClient :: new) . collect ( ) ;
198
+ let cb_config: StubConfig < T > = load_file_from_env ( CB_CONFIG_ENV ) ?;
199
+ let relay_clients =
200
+ cb_config. relays . into_iter ( ) . map ( RelayClient :: new) . collect :: < Result < Vec < RelayClient > > > ( ) ?;
199
201
200
202
let signer_client = if cb_config. pbs . static_config . with_signer {
201
203
// if custom pbs requires a signer client, load jwt
202
- let module_jwt = load_env_var_infallible ( MODULE_JWT_ENV ) ;
203
- let signer_server_address = load_env_var_infallible ( SIGNER_SERVER_ENV ) ;
204
+ let module_jwt = load_env_var ( MODULE_JWT_ENV ) ? ;
205
+ let signer_server_address = load_env_var ( SIGNER_SERVER_ENV ) ? ;
204
206
Some ( SignerClient :: new ( signer_server_address, & module_jwt) )
205
207
} else {
206
208
None
207
- } ;
209
+ }
210
+ . transpose ( ) ?;
208
211
209
212
Ok ( PbsModuleConfig {
210
213
chain : cb_config. chain ,
@@ -242,10 +245,10 @@ pub struct StartModuleConfig<T = ()> {
242
245
/// - [CB_CONFIG_ENV] - the path to the config file
243
246
/// - [MODULE_JWT_ENV] - the jwt token for the module
244
247
// TODO: add metrics url here
245
- pub fn load_module_config < T : DeserializeOwned > ( ) -> eyre :: Result < StartModuleConfig < T > > {
246
- let module_id = load_env_var_infallible ( MODULE_ID_ENV ) ;
247
- let module_jwt = load_env_var_infallible ( MODULE_JWT_ENV ) ;
248
- let signer_server_address = load_env_var_infallible ( SIGNER_SERVER_ENV ) ;
248
+ pub fn load_module_config < T : DeserializeOwned > ( ) -> Result < StartModuleConfig < T > > {
249
+ let module_id = load_env_var ( MODULE_ID_ENV ) ? ;
250
+ let module_jwt = load_env_var ( MODULE_JWT_ENV ) ? ;
251
+ let signer_server_address = load_env_var ( SIGNER_SERVER_ENV ) ? ;
249
252
250
253
#[ derive( Debug , Deserialize ) ]
251
254
struct ThisModuleConfig < U > {
@@ -269,7 +272,7 @@ pub fn load_module_config<T: DeserializeOwned>() -> eyre::Result<StartModuleConf
269
272
}
270
273
271
274
// load module config including the extra data (if any)
272
- let cb_config: StubConfig < T > = load_file_from_env ( CB_CONFIG_ENV ) ;
275
+ let cb_config: StubConfig < T > = load_file_from_env ( CB_CONFIG_ENV ) ? ;
273
276
274
277
// find all matching modules config
275
278
let matches: Vec < ThisModuleConfig < T > > = cb_config
@@ -286,7 +289,7 @@ pub fn load_module_config<T: DeserializeOwned>() -> eyre::Result<StartModuleConf
286
289
. find ( |m| m. static_config . id == module_id)
287
290
. wrap_err ( format ! ( "failed to find module for {module_id}" ) ) ?;
288
291
289
- let signer_client = SignerClient :: new ( signer_server_address, & module_jwt) ;
292
+ let signer_client = SignerClient :: new ( signer_server_address, & module_jwt) ? ;
290
293
291
294
Ok ( StartModuleConfig {
292
295
id : module_config. static_config . id ,
@@ -297,7 +300,6 @@ pub fn load_module_config<T: DeserializeOwned>() -> eyre::Result<StartModuleConf
297
300
}
298
301
}
299
302
300
- // TODO: propagate errors
301
- pub fn load_env_var_infallible ( env : & str ) -> String {
302
- std:: env:: var ( env) . unwrap_or_else ( |_| panic ! ( "{env} is not set" ) )
303
+ pub fn load_env_var ( env : & str ) -> Result < String > {
304
+ std:: env:: var ( env) . wrap_err ( "{env} is not set" )
303
305
}
0 commit comments