From e49a3e131361da0d3c769c781eab29ec07f76765 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 16 Sep 2020 17:19:45 +1000 Subject: [PATCH] Return result in client builder --- beacon_node/client/src/builder.rs | 50 +++++++++++++++---------------- beacon_node/src/lib.rs | 19 ++++++------ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/beacon_node/client/src/builder.rs b/beacon_node/client/src/builder.rs index 1fccad6129a..d530605adb2 100644 --- a/beacon_node/client/src/builder.rs +++ b/beacon_node/client/src/builder.rs @@ -335,19 +335,25 @@ where /// If type inference errors are being raised, see the comment on the definition of `Self`. pub fn build( self, - ) -> Client< - Witness< - TStoreMigrator, - TSlotClock, - TEth1Backend, - TEthSpec, - TEventHandler, - THotStore, - TColdStore, + ) -> Result< + Client< + Witness< + TStoreMigrator, + TSlotClock, + TEth1Backend, + TEthSpec, + TEventHandler, + THotStore, + TColdStore, + >, >, + String, > { - // TODO: fix unwrap. - let log = self.runtime_context.as_ref().unwrap().log().clone(); + let runtime_context = self + .runtime_context + .as_ref() + .ok_or_else(|| "build requires a runtime context".to_string())?; + let log = runtime_context.log().clone(); let http_api_listen_addr = if self.http_api_config.enabled { let ctx = Arc::new(http_api::Context { @@ -358,16 +364,13 @@ where log: log.clone(), }); - // TODO - let exit = self.runtime_context.as_ref().unwrap().executor.exit(); + let exit = runtime_context.executor.exit(); let (listen_addr, server) = http_api::serve(ctx, exit) - .map_err(|e| format!("Unable to start HTTP API server: {:?}", e)) - .unwrap(); // TODO + .map_err(|e| format!("Unable to start HTTP API server: {:?}", e))?; - self.runtime_context + runtime_context .clone() - .unwrap() .executor .spawn_without_exit(async move { server.await }, "http-api"); @@ -386,15 +389,12 @@ where log: log.clone(), }); - // TODO - let exit = self.runtime_context.as_ref().unwrap().executor.exit(); + let exit = runtime_context.executor.exit(); let (listen_addr, server) = http_metrics::serve(ctx, exit) - .map_err(|e| format!("Unable to start HTTP API server: {:?}", e)) - .unwrap(); // TODO + .map_err(|e| format!("Unable to start HTTP API server: {:?}", e))?; - self.runtime_context - .unwrap() + runtime_context .executor .spawn_without_exit(async move { server.await }, "http-api"); @@ -404,13 +404,13 @@ where None }; - Client { + Ok(Client { beacon_chain: self.beacon_chain, network_globals: self.network_globals, http_api_listen_addr, http_metrics_listen_addr, websocket_listen_addr: self.websocket_listen_addr, - } + }) } } diff --git a/beacon_node/src/lib.rs b/beacon_node/src/lib.rs index b00ffd8de79..feff1e3206f 100644 --- a/beacon_node/src/lib.rs +++ b/beacon_node/src/lib.rs @@ -124,16 +124,15 @@ impl ProductionBeaconNode { // Inject the executor into the discv5 network config. client_config.network.discv5_config.executor = Some(Box::new(executor)); - Ok(Self( - builder - .build_beacon_chain()? - .network(&client_config.network) - .await? - .notifier()? - .http_api_config(client_config.http_api.clone()) - .http_metrics_config(client_config.http_metrics.clone()) - .build(), - )) + builder + .build_beacon_chain()? + .network(&client_config.network) + .await? + .notifier()? + .http_api_config(client_config.http_api.clone()) + .http_metrics_config(client_config.http_metrics.clone()) + .build() + .map(Self) } pub fn into_inner(self) -> ProductionClient {