From 3aafe54887be796b4cee1cd75d839635c78b5fe7 Mon Sep 17 00:00:00 2001 From: Drazen Urch Date: Fri, 2 Jul 2021 10:50:49 +0200 Subject: [PATCH] Remove mut where possible, parallel cache requests (#668) --- clients/native/src/commands/init.rs | 2 +- clients/socks5/src/commands/init.rs | 2 +- clients/webassembly/src/client/mod.rs | 2 +- .../client-libs/validator-client/src/lib.rs | 12 +++++----- gateway/src/node/mod.rs | 2 +- mixnode/src/node/mod.rs | 2 +- validator-api/src/cache.rs | 22 +++++-------------- 7 files changed, 16 insertions(+), 28 deletions(-) diff --git a/clients/native/src/commands/init.rs b/clients/native/src/commands/init.rs index 8558648244..2f05089ae1 100644 --- a/clients/native/src/commands/init.rs +++ b/clients/native/src/commands/init.rs @@ -109,7 +109,7 @@ async fn gateway_details( chosen_gateway_id: Option<&str>, ) -> gateway::Node { let validator_client_config = validator_client::Config::new(validator_servers, mixnet_contract); - let mut validator_client = validator_client::Client::new(validator_client_config); + let validator_client = validator_client::Client::new(validator_client_config); let gateways = validator_client.get_gateways().await.unwrap(); let valid_gateways = gateways diff --git a/clients/socks5/src/commands/init.rs b/clients/socks5/src/commands/init.rs index 0a73d524bf..fcbcb07c79 100644 --- a/clients/socks5/src/commands/init.rs +++ b/clients/socks5/src/commands/init.rs @@ -110,7 +110,7 @@ async fn gateway_details( chosen_gateway_id: Option<&str>, ) -> gateway::Node { let validator_client_config = validator_client::Config::new(validator_servers, mixnet_contract); - let mut validator_client = validator_client::Client::new(validator_client_config); + let validator_client = validator_client::Client::new(validator_client_config); let gateways = validator_client.get_gateways().await.unwrap(); let valid_gateways = gateways diff --git a/clients/webassembly/src/client/mod.rs b/clients/webassembly/src/client/mod.rs index 4f9fc1e404..eda8316a8b 100644 --- a/clients/webassembly/src/client/mod.rs +++ b/clients/webassembly/src/client/mod.rs @@ -255,7 +255,7 @@ impl NymClient { vec![self.validator_server.clone()], &self.mixnet_contract_address, ); - let mut validator_client = validator_client::Client::new(validator_client_config); + let validator_client = validator_client::Client::new(validator_client_config); let mixnodes = match validator_client.get_mix_nodes().await { Err(err) => panic!("{}", err), diff --git a/common/client-libs/validator-client/src/lib.rs b/common/client-libs/validator-client/src/lib.rs index 3e9099e13f..a59e515388 100644 --- a/common/client-libs/validator-client/src/lib.rs +++ b/common/client-libs/validator-client/src/lib.rs @@ -153,7 +153,7 @@ impl Client { } async fn get_mix_nodes_paged( - &mut self, + &self, start_after: Option, ) -> Result { let query_content_json = serde_json::to_string(&QueryRequest::GetMixNodes { @@ -168,7 +168,7 @@ impl Client { self.query_validators(query_content).await } - pub async fn get_mix_nodes(&mut self) -> Result, ValidatorClientError> { + pub async fn get_mix_nodes(&self) -> Result, ValidatorClientError> { let mut mixnodes = Vec::new(); let mut start_after = None; loop { @@ -186,7 +186,7 @@ impl Client { } async fn get_gateways_paged( - &mut self, + &self, start_after: Option, ) -> Result { let query_content_json = serde_json::to_string(&QueryRequest::GetGateways { @@ -201,7 +201,7 @@ impl Client { self.query_validators(query_content).await } - pub async fn get_gateways(&mut self) -> Result, ValidatorClientError> { + pub async fn get_gateways(&self) -> Result, ValidatorClientError> { let mut gateways = Vec::new(); let mut start_after = None; loop { @@ -218,9 +218,7 @@ impl Client { Ok(gateways) } - pub async fn get_layer_distribution( - &mut self, - ) -> Result { + pub async fn get_layer_distribution(&self) -> Result { // serialization of an empty enum can't fail... let query_content_json = serde_json::to_string(&QueryRequest::LayerDistribution {}).unwrap(); diff --git a/gateway/src/node/mod.rs b/gateway/src/node/mod.rs index 8552c37a85..a9b7b473a5 100644 --- a/gateway/src/node/mod.rs +++ b/gateway/src/node/mod.rs @@ -137,7 +137,7 @@ impl Gateway { self.config.get_validator_rest_endpoints(), self.config.get_validator_mixnet_contract_address(), ); - let mut validator_client = validator_client::Client::new(validator_client_config); + let validator_client = validator_client::Client::new(validator_client_config); let existing_gateways = match validator_client.get_gateways().await { Ok(gateways) => gateways, diff --git a/mixnode/src/node/mod.rs b/mixnode/src/node/mod.rs index 94babb9232..34a2ac6743 100644 --- a/mixnode/src/node/mod.rs +++ b/mixnode/src/node/mod.rs @@ -183,7 +183,7 @@ impl MixNode { self.config.get_validator_rest_endpoints(), self.config.get_validator_mixnet_contract_address(), ); - let mut validator_client = validator_client::Client::new(validator_client_config); + let validator_client = validator_client::Client::new(validator_client_config); let existing_nodes = match validator_client.get_mix_nodes().await { Ok(nodes) => nodes, diff --git a/validator-api/src/cache.rs b/validator-api/src/cache.rs index cc67848da7..15cf38b943 100644 --- a/validator-api/src/cache.rs +++ b/validator-api/src/cache.rs @@ -39,22 +39,12 @@ impl ValidatorCache { } pub async fn cache(&mut self) -> Result<()> { - // We need to make validator_api non mut first - // tokio::join!(self.cache_mixnodes(), self.cache_gateways()); - self.cache_mixnodes().await?; - self.cache_gateways().await?; - Ok(()) - } - - async fn cache_mixnodes(&mut self) -> Result<()> { - let mixnodes = self.validator_client.get_mix_nodes().await?; - self.mixnodes.set(mixnodes); - Ok(()) - } - - async fn cache_gateways(&mut self) -> Result<()> { - let gateways = self.validator_client.get_gateways().await?; - self.gateways.set(gateways); + let (mixnodes, gateways) = tokio::join!( + self.validator_client.get_mix_nodes(), + self.validator_client.get_gateways() + ); + self.mixnodes.set(mixnodes?); + self.gateways.set(gateways?); Ok(()) }