From 83a9b993a15f5eb040db20559ee026847d6b9ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Thu, 10 Mar 2022 16:25:23 +0100 Subject: [PATCH] wallet: add a 3 sec timeout for detecting validator --- nym-wallet/src-tauri/Cargo.toml | 2 +- .../src/operations/mixnet/account.rs | 44 ++++++++++++++----- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/nym-wallet/src-tauri/Cargo.toml b/nym-wallet/src-tauri/Cargo.toml index 255d35c9d8..34f1b6f598 100644 --- a/nym-wallet/src-tauri/Cargo.toml +++ b/nym-wallet/src-tauri/Cargo.toml @@ -30,7 +30,7 @@ strum = { version = "0.23", features = ["derive"] } tauri = { version = "=1.0.0-rc.2", features = ["clipboard-all", "shell-open", "window-maximize"] } tendermint-rpc = "0.23.0" thiserror = "1.0" -tokio = { version = "1.10", features = ["sync"] } +tokio = { version = "1.10", features = ["sync", "time"] } toml = "0.5.8" url = "2.2" diff --git a/nym-wallet/src-tauri/src/operations/mixnet/account.rs b/nym-wallet/src-tauri/src/operations/mixnet/account.rs index a90f486486..f5d9824fe1 100644 --- a/nym-wallet/src-tauri/src/operations/mixnet/account.rs +++ b/nym-wallet/src-tauri/src/operations/mixnet/account.rs @@ -10,10 +10,13 @@ use config::defaults::ValidatorDetails; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::convert::TryInto; +use std::hash::Hash; use std::str::FromStr; use std::sync::Arc; +use std::time::Duration; use strum::IntoEnumIterator; use tokio::sync::RwLock; +use tokio::time::error::Elapsed; use validator_client::nymd::error::NymdError; use validator_client::nymd::SigningNymdClient; use validator_client::Client; @@ -150,7 +153,17 @@ async fn _connect_with_mnemonic( validators .entry(network) // We always have at least one hardcoded defalt validator - .or_insert_with(|| config.get_validators(network).next().unwrap().clone()); + .or_insert_with(|| { + let default_validator = config.get_validators(network).next().unwrap().clone(); + println!( + "Using default for {network}: {}, {}", + default_validator.nymd_url(), + default_validator + .api_url() + .map_or_else(|| "empty".to_string(), |url| url.to_string()), + ); + default_validator + }); } // Now we are ready to create the clients that we will use @@ -203,21 +216,32 @@ async fn select_validators( config: &Config, mnemonic: &Mnemonic, ) -> Result, BackendError> { + use tokio::time::timeout; let validators = futures::future::join_all(Network::iter().map(|network| { - try_connect_to_validators( - config.get_validators(network), - config, - network, - mnemonic.clone(), + timeout( + Duration::from_millis(3000), + try_connect_to_validators( + config.get_validators(network), + config, + network, + mnemonic.clone(), + ), ) })) .await; - // Collect for the purpose of returning any errors encountered - let validators = validators.into_iter().collect::, _>>()?; + let validators = validators + .into_iter() + // Filter out networks that timed out. + .filter_map(Result::ok) + // Rewrap so that the Result is outermost, so we can return any errors encountered + .collect::, _>>()? + .into_iter() + // Filter out networks where we exhausted all listed validators. + .flatten() + .collect::>(); - // Filter out networks we were not able to connect to - Ok(validators.into_iter().flatten().collect::>()) + Ok(validators) } async fn try_connect_to_validators(