From 408396c90032ffe8ea54ec650ad09a5848b374d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Wed, 4 Jan 2023 15:41:11 +0100 Subject: [PATCH] client-core: remove generic parameter for ClientCoreError (#2765) * client-core: remove generic parameter for ClientCoreError * fix compilation --- .../client-core/src/client/base_client/mod.rs | 18 +++++--- .../client/base_client/non_wasm_helpers.rs | 12 ++++-- clients/client-core/src/error.rs | 7 ++-- clients/client-core/src/init/helpers.rs | 20 +++------ clients/client-core/src/init/mod.rs | 42 +++++++------------ clients/native/src/commands/init.rs | 6 +-- clients/native/src/error.rs | 3 +- clients/socks5/src/commands/init.rs | 2 +- clients/socks5/src/error.rs | 3 +- nym-connect/src-tauri/src/config/mod.rs | 2 +- nym-connect/src-tauri/src/error.rs | 3 +- 11 files changed, 53 insertions(+), 65 deletions(-) diff --git a/clients/client-core/src/client/base_client/mod.rs b/clients/client-core/src/client/base_client/mod.rs index 2a00a780cc..4757c3bf2c 100644 --- a/clients/client-core/src/client/base_client/mod.rs +++ b/clients/client-core/src/client/base_client/mod.rs @@ -230,7 +230,7 @@ where mixnet_message_sender: MixnetMessageSender, ack_sender: AcknowledgementSender, shutdown: TaskClient, - ) -> Result> { + ) -> Result { let gateway_id = self.gateway_config.gateway_id.clone(); if gateway_id.is_empty() { return Err(ClientCoreError::GatewayIdUnknown); @@ -285,7 +285,7 @@ where refresh_rate: Duration, topology_accessor: TopologyAccessor, shutdown: TaskClient, - ) -> Result<(), ClientCoreError> { + ) -> Result<(), ClientCoreError> { let topology_refresher_config = TopologyRefresherConfig::new( nym_api_urls, refresh_rate, @@ -328,12 +328,17 @@ where async fn setup_persistent_reply_storage( backend: B, shutdown: TaskClient, - ) -> Result> { + ) -> Result + where + ::StorageError: Sync + Send, + { let persistent_storage = PersistentReplyStorage::new(backend); let mem_store = persistent_storage .load_state_from_backend() .await - .map_err(|err| ClientCoreError::SurbStorageError { source: err })?; + .map_err(|err| ClientCoreError::SurbStorageError { + source: Box::new(err), + })?; let store_clone = mem_store.clone(); spawn_future(async move { @@ -345,7 +350,10 @@ where Ok(mem_store) } - pub async fn start_base(mut self) -> Result> { + pub async fn start_base(mut self) -> Result + where + ::StorageError: Sync + Send, + { info!("Starting nym client"); // channels for inter-component communication // TODO: make the channels be internally created by the relevant components diff --git a/clients/client-core/src/client/base_client/non_wasm_helpers.rs b/clients/client-core/src/client/base_client/non_wasm_helpers.rs index 2d710052b1..996dcc8fc3 100644 --- a/clients/client-core/src/client/base_client/non_wasm_helpers.rs +++ b/clients/client-core/src/client/base_client/non_wasm_helpers.rs @@ -14,13 +14,15 @@ use time::OffsetDateTime; async fn setup_fresh_backend>( db_path: P, debug_config: &DebugConfig, -) -> Result> { +) -> Result { info!("creating fresh surb database"); let mut storage_backend = match fs_backend::Backend::init(db_path).await { Ok(backend) => backend, Err(err) => { error!("failed to setup persistent storage backend for our reply needs: {err}"); - return Err(ClientCoreError::SurbStorageError { source: err }); + return Err(ClientCoreError::SurbStorageError { + source: Box::new(err), + }); } }; @@ -34,7 +36,9 @@ async fn setup_fresh_backend>( storage_backend .init_fresh(&mem_store) .await - .map_err(|err| ClientCoreError::SurbStorageError { source: err })?; + .map_err(|err| ClientCoreError::SurbStorageError { + source: Box::new(err), + })?; Ok(storage_backend) } @@ -63,7 +67,7 @@ fn archive_corrupted_database>(db_path: P) -> io::Result<()> { pub async fn setup_fs_reply_surb_backend>( db_path: P, debug_config: &DebugConfig, -) -> Result> { +) -> Result { // if the database file doesnt exist, initialise fresh storage, otherwise attempt to load the existing one let db_path = db_path.as_ref(); if db_path.exists() { diff --git a/clients/client-core/src/error.rs b/clients/client-core/src/error.rs index 44973c5f30..07bd9442ec 100644 --- a/clients/client-core/src/error.rs +++ b/clients/client-core/src/error.rs @@ -1,14 +1,13 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -use crate::client::replies::reply_storage::ReplyStorageBackend; use crypto::asymmetric::identity::Ed25519RecoveryError; use gateway_client::error::GatewayClientError; use topology::NymTopologyError; use validator_client::ValidatorClientError; #[derive(thiserror::Error, Debug)] -pub enum ClientCoreError { +pub enum ClientCoreError { #[error("I/O error: {0}")] IoError(#[from] std::io::Error), @@ -40,7 +39,9 @@ pub enum ClientCoreError { InsufficientNetworkTopology(#[from] NymTopologyError), #[error("experienced a failure with our reply surb persistent storage: {source}")] - SurbStorageError { source: B::StorageError }, + SurbStorageError { + source: Box, + }, #[error("The gateway id is invalid - {0}")] UnableToCreatePublicKeyFromGatewayId(Ed25519RecoveryError), diff --git a/clients/client-core/src/init/helpers.rs b/clients/client-core/src/init/helpers.rs index 31d76e86da..b3662149b7 100644 --- a/clients/client-core/src/init/helpers.rs +++ b/clients/client-core/src/init/helpers.rs @@ -1,7 +1,6 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -use crate::client::replies::reply_storage::ReplyStorageBackend; use crate::{ client::key_manager::KeyManager, config::{persistence::key_pathfinder::ClientKeyPathfinder, Config}, @@ -17,13 +16,10 @@ use tap::TapFallible; use topology::{filter::VersionFilterable, gateway}; use url::Url; -pub(super) async fn query_gateway_details( +pub(super) async fn query_gateway_details( validator_servers: Vec, chosen_gateway_id: Option, -) -> Result> -where - B: ReplyStorageBackend, -{ +) -> Result { let nym_api = validator_servers .choose(&mut thread_rng()) .ok_or(ClientCoreError::ListOfNymApisIsEmpty)?; @@ -55,13 +51,10 @@ where } } -async fn register_with_gateway( +async fn register_with_gateway( gateway: &gateway::Node, our_identity: Arc, -) -> Result, ClientCoreError> -where - B: ReplyStorageBackend, -{ +) -> Result, ClientCoreError> { let timeout = Duration::from_millis(1500); let mut gateway_client = GatewayClient::new_init( gateway.clients_address(), @@ -81,13 +74,12 @@ where Ok(shared_keys) } -pub(super) async fn register_with_gateway_and_store_keys( +pub(super) async fn register_with_gateway_and_store_keys( gateway_details: gateway::Node, config: &Config, -) -> Result<(), ClientCoreError> +) -> Result<(), ClientCoreError> where T: NymConfig, - B: ReplyStorageBackend, { let mut rng = OsRng; let mut key_manager = KeyManager::new(&mut rng); diff --git a/clients/client-core/src/init/mod.rs b/clients/client-core/src/init/mod.rs index fa6c62c097..820e229a35 100644 --- a/clients/client-core/src/init/mod.rs +++ b/clients/client-core/src/init/mod.rs @@ -12,7 +12,6 @@ use tap::TapFallible; use config::NymConfig; use crypto::asymmetric::{encryption, identity}; -use crate::client::replies::reply_storage::ReplyStorageBackend; use crate::{ config::{ persistence::key_pathfinder::ClientKeyPathfinder, ClientCoreConfigTrait, Config, @@ -63,13 +62,12 @@ impl Display for InitResults { /// Convenience function for setting up the gateway for a client. Depending on the arguments given /// it will do the sensible thing. -pub async fn setup_gateway( +pub async fn setup_gateway( register_gateway: bool, user_chosen_gateway_id: Option, config: &Config, -) -> Result> +) -> Result where - B: ReplyStorageBackend, C: NymConfig + ClientCoreConfigTrait, T: NymConfig, { @@ -79,19 +77,18 @@ where } else if let Some(user_chosen_gateway_id) = user_chosen_gateway_id { config_gateway_with_existing_keys(user_chosen_gateway_id, config).await } else { - reuse_existing_gateway_config::(&id) + reuse_existing_gateway_config::(&id) } } /// Get the gateway details by querying the validator-api. Either pick one at random or use /// the chosen one if it's among the available ones. /// Saves keys to disk, specified by the paths in `config`. -pub async fn register_with_gateway( +pub async fn register_with_gateway( user_chosen_gateway_id: Option, config: &Config, -) -> Result> +) -> Result where - B: ReplyStorageBackend, T: NymConfig, { println!("Configuring gateway"); @@ -111,12 +108,11 @@ where /// create any keys. /// This assumes that the user knows what they are doing, and that the existing keys are valid for /// the gateway being used -pub async fn config_gateway_with_existing_keys( +pub async fn config_gateway_with_existing_keys( user_chosen_gateway_id: String, config: &Config, -) -> Result> +) -> Result where - B: ReplyStorageBackend, T: NymConfig, { println!("Using gateway provided by user, keeping existing keys"); @@ -127,11 +123,8 @@ where } /// Read and reuse the existing gateway configuration from a file that was generate earlier. -pub fn reuse_existing_gateway_config( - id: &str, -) -> Result> +pub fn reuse_existing_gateway_config(id: &str) -> Result where - B: ReplyStorageBackend, T: NymConfig + ClientCoreConfigTrait, { println!("Not registering gateway, will reuse existing config and keys"); @@ -150,19 +143,15 @@ where } /// Get the client address by loading the keys from stored files. -pub fn get_client_address_from_stored_keys( +pub fn get_client_address_from_stored_keys( config: &Config, -) -> Result> +) -> Result where T: config::NymConfig, - B: ReplyStorageBackend, { - fn load_identity_keys( + fn load_identity_keys( pathfinder: &ClientKeyPathfinder, - ) -> Result> - where - B: ReplyStorageBackend, - { + ) -> Result { let identity_keypair: identity::KeyPair = pemstore::load_keypair(&pemstore::KeyPairPath::new( pathfinder.private_identity_key().to_owned(), @@ -172,12 +161,9 @@ where Ok(identity_keypair) } - fn load_sphinx_keys( + fn load_sphinx_keys( pathfinder: &ClientKeyPathfinder, - ) -> Result> - where - B: ReplyStorageBackend, - { + ) -> Result { let sphinx_keypair: encryption::KeyPair = pemstore::load_keypair(&pemstore::KeyPairPath::new( pathfinder.private_encryption_key().to_owned(), diff --git a/clients/native/src/commands/init.rs b/clients/native/src/commands/init.rs index 098fab5754..8c04ceb126 100644 --- a/clients/native/src/commands/init.rs +++ b/clients/native/src/commands/init.rs @@ -137,7 +137,7 @@ pub(crate) async fn execute(args: &Init) -> Result<(), ClientError> { // Setup gateway by either registering a new one, or creating a new config from the selected // one but with keys kept, or reusing the gateway configuration. - let gateway = client_core::init::setup_gateway::<_, Config, _>( + let gateway = client_core::init::setup_gateway::( register_gateway, user_chosen_gateway_id, config.get_base(), @@ -155,14 +155,14 @@ pub(crate) async fn execute(args: &Init) -> Result<(), ClientError> { let address = client_core::init::get_client_address_from_stored_keys(config.get_base())?; let init_results = InitResults::new(&config, &address); - println!("{}", init_results); + println!("{init_results}"); // Output summary to a json file, if specified if args.output_json { client_core::init::output_to_json(&init_results, "client_init_results.json"); } - println!("\nThe address of this client is: {}\n", address); + println!("\nThe address of this client is: {address}\n"); Ok(()) } diff --git a/clients/native/src/error.rs b/clients/native/src/error.rs index 050e79ebc5..700e6e9336 100644 --- a/clients/native/src/error.rs +++ b/clients/native/src/error.rs @@ -1,4 +1,3 @@ -use client_core::client::replies::reply_storage::fs_backend; use client_core::error::ClientCoreError; #[derive(thiserror::Error, Debug)] @@ -7,7 +6,7 @@ pub enum ClientError { IoError(#[from] std::io::Error), #[error("client-core error: {0}")] - ClientCoreError(#[from] ClientCoreError), + ClientCoreError(#[from] ClientCoreError), #[error("Failed to load config for: {0}")] FailedToLoadConfig(String), diff --git a/clients/socks5/src/commands/init.rs b/clients/socks5/src/commands/init.rs index 2f53e61d08..48f2ae0ec9 100644 --- a/clients/socks5/src/commands/init.rs +++ b/clients/socks5/src/commands/init.rs @@ -150,7 +150,7 @@ pub(crate) async fn execute(args: &Init) -> Result<(), Socks5ClientError> { // Setup gateway by either registering a new one, or creating a new config from the selected // one but with keys kept, or reusing the gateway configuration. - let gateway = client_core::init::setup_gateway::<_, Config, _>( + let gateway = client_core::init::setup_gateway::( register_gateway, user_chosen_gateway_id, config.get_base(), diff --git a/clients/socks5/src/error.rs b/clients/socks5/src/error.rs index 6318d0ec0a..96e98f91cc 100644 --- a/clients/socks5/src/error.rs +++ b/clients/socks5/src/error.rs @@ -1,5 +1,4 @@ use crate::socks::types::SocksProxyError; -use client_core::client::replies::reply_storage::fs_backend; use client_core::error::ClientCoreError; use socks5_requests::ConnectionId; @@ -9,7 +8,7 @@ pub enum Socks5ClientError { IoError(#[from] std::io::Error), #[error("client-core error: {0}")] - ClientCoreError(#[from] ClientCoreError), + ClientCoreError(#[from] ClientCoreError), #[error("SOCKS proxy error")] SocksProxyError(SocksProxyError), diff --git a/nym-connect/src-tauri/src/config/mod.rs b/nym-connect/src-tauri/src/config/mod.rs index 34bf7efcfb..200bc46862 100644 --- a/nym-connect/src-tauri/src/config/mod.rs +++ b/nym-connect/src-tauri/src/config/mod.rs @@ -134,7 +134,7 @@ pub async fn init_socks5_config(provider_address: String, chosen_gateway_id: Str } // Setup gateway by either registering a new one, or reusing exiting keys - let gateway = client_core::init::setup_gateway::<_, Socks5Config, _>( + let gateway = client_core::init::setup_gateway::( register_gateway, Some(chosen_gateway_id), config.get_base(), diff --git a/nym-connect/src-tauri/src/error.rs b/nym-connect/src-tauri/src/error.rs index 57fc38e1c8..124eeeddc4 100644 --- a/nym-connect/src-tauri/src/error.rs +++ b/nym-connect/src-tauri/src/error.rs @@ -1,4 +1,3 @@ -use client_core::client::replies::reply_storage::fs_backend; use client_core::error::ClientCoreError; use serde::{Serialize, Serializer}; use thiserror::Error; @@ -39,7 +38,7 @@ pub enum BackendError { #[error("{source}")] ClientCoreError { #[from] - source: ClientCoreError, + source: ClientCoreError, }, #[error("{source}")] ApiClientError {