@@ -19,7 +19,11 @@ use clap::ArgMatches;
1919use itc_rest_client:: rest_client:: Url ;
2020use parse_duration:: parse;
2121use serde:: { Deserialize , Serialize } ;
22- use std:: time:: Duration ;
22+ use std:: {
23+ fs,
24+ path:: { Path , PathBuf } ,
25+ time:: Duration ,
26+ } ;
2327
2428static DEFAULT_NODE_SERVER : & str = "ws://127.0.0.1" ;
2529static DEFAULT_NODE_PORT : & str = "9944" ;
@@ -31,29 +35,31 @@ static DEFAULT_UNTRUSTED_HTTP_PORT: &str = "4545";
3135
3236#[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
3337pub struct Config {
34- pub node_ip : String ,
35- pub node_port : String ,
36- pub worker_ip : String ,
38+ node_ip : String ,
39+ node_port : String ,
40+ worker_ip : String ,
3741 /// Trusted worker address that will be advertised on the parentchain.
38- pub trusted_external_worker_address : Option < String > ,
42+ trusted_external_worker_address : Option < String > ,
3943 /// Port to directly communicate with the trusted tls server inside the enclave.
40- pub trusted_worker_port : String ,
44+ trusted_worker_port : String ,
4145 /// Untrusted worker address that will be returned by the dedicated trusted ws rpc call.
42- pub untrusted_external_worker_address : Option < String > ,
46+ untrusted_external_worker_address : Option < String > ,
4347 /// Port to the untrusted ws of the validateer.
44- pub untrusted_worker_port : String ,
48+ untrusted_worker_port : String ,
4549 /// Mutual remote attestation address that will be returned by the dedicated trusted ws rpc call.
46- pub mu_ra_external_address : Option < String > ,
50+ mu_ra_external_address : Option < String > ,
4751 /// Port for mutual-remote attestation requests.
48- pub mu_ra_port : String ,
52+ mu_ra_port : String ,
4953 /// Enable the metrics server
50- pub enable_metrics_server : bool ,
54+ enable_metrics_server : bool ,
5155 /// Port for the metrics server
52- pub metrics_server_port : String ,
56+ metrics_server_port : String ,
5357 /// Port for the untrusted HTTP server (e.g. for `is_initialized`)
54- pub untrusted_http_port : String ,
58+ untrusted_http_port : String ,
59+ /// Data directory used by all the services.
60+ data_dir : PathBuf ,
5561 /// Config of the 'run' subcommand
56- pub run_config : Option < RunConfig > ,
62+ run_config : Option < RunConfig > ,
5763}
5864
5965#[ allow( clippy:: too_many_arguments) ]
@@ -71,6 +77,7 @@ impl Config {
7177 enable_metrics_server : bool ,
7278 metrics_server_port : String ,
7379 untrusted_http_port : String ,
80+ data_dir : PathBuf ,
7481 run_config : Option < RunConfig > ,
7582 ) -> Self {
7683 Self {
@@ -86,6 +93,7 @@ impl Config {
8693 enable_metrics_server,
8794 metrics_server_port,
8895 untrusted_http_port,
96+ data_dir,
8997 run_config,
9098 }
9199 }
@@ -131,6 +139,18 @@ impl Config {
131139 }
132140 }
133141
142+ pub fn data_dir ( & self ) -> & Path {
143+ self . data_dir . as_path ( )
144+ }
145+
146+ pub fn run_config ( & self ) -> & Option < RunConfig > {
147+ & self . run_config
148+ }
149+
150+ pub fn enable_metrics_server ( & self ) -> bool {
151+ self . enable_metrics_server
152+ }
153+
134154 pub fn try_parse_metrics_server_port ( & self ) -> Option < u16 > {
135155 self . metrics_server_port . parse :: < u16 > ( ) . ok ( )
136156 }
@@ -149,6 +169,25 @@ impl From<&ArgMatches<'_>> for Config {
149169 let metrics_server_port = m. value_of ( "metrics-port" ) . unwrap_or ( DEFAULT_METRICS_PORT ) ;
150170 let untrusted_http_port =
151171 m. value_of ( "untrusted-http-port" ) . unwrap_or ( DEFAULT_UNTRUSTED_HTTP_PORT ) ;
172+
173+ let data_dir = match m. value_of ( "data-dir" ) {
174+ Some ( d) => {
175+ let p = PathBuf :: from ( d) ;
176+ if !p. exists ( ) {
177+ log:: info!( "Creating new data-directory for the service {}." , p. display( ) ) ;
178+ fs:: create_dir_all ( p. as_path ( ) ) . unwrap ( ) ;
179+ } else {
180+ log:: info!( "Starting service in existing directory {}." , p. display( ) ) ;
181+ }
182+ p
183+ } ,
184+ None => {
185+ log:: warn!( "[Config] defaulting to data-dir = PWD because it was previous behaviour. This might change soon.\
186+ Please pass the data-dir explicitly to ensure nothing breaks in your setup.") ;
187+ pwd ( )
188+ } ,
189+ } ;
190+
152191 let run_config = m. subcommand_matches ( "run" ) . map ( RunConfig :: from) ;
153192
154193 Self :: new (
@@ -167,6 +206,7 @@ impl From<&ArgMatches<'_>> for Config {
167206 is_metrics_server_enabled,
168207 metrics_server_port. to_string ( ) ,
169208 untrusted_http_port. to_string ( ) ,
209+ data_dir,
170210 run_config,
171211 )
172212 }
@@ -225,6 +265,10 @@ fn add_port_if_necessary(url: &str, port: &str) -> String {
225265 }
226266}
227267
268+ pub fn pwd ( ) -> PathBuf {
269+ std:: env:: current_dir ( ) . expect ( "works on all supported platforms; qed." )
270+ }
271+
228272#[ cfg( test) ]
229273mod test {
230274 use super :: * ;
@@ -247,6 +291,7 @@ mod test {
247291 assert ! ( config. mu_ra_external_address. is_none( ) ) ;
248292 assert ! ( !config. enable_metrics_server) ;
249293 assert_eq ! ( config. untrusted_http_port, DEFAULT_UNTRUSTED_HTTP_PORT ) ;
294+ assert_eq ! ( config. data_dir, pwd( ) ) ;
250295 assert ! ( config. run_config. is_none( ) ) ;
251296 }
252297
0 commit comments