From f35f2649ef6611519f22d05dd89fd5cebbc39835 Mon Sep 17 00:00:00 2001 From: Simon Wicky Date: Fri, 10 Nov 2023 10:54:15 +0100 Subject: [PATCH] tweak to not try_send every time --- .../client/base_client/non_wasm_helpers.rs | 2 +- .../websocket/connection_handler/coconut.rs | 85 ++++++++----------- 2 files changed, 38 insertions(+), 49 deletions(-) diff --git a/common/client-core/src/client/base_client/non_wasm_helpers.rs b/common/client-core/src/client/base_client/non_wasm_helpers.rs index 393404437b..20c7b79b59 100644 --- a/common/client-core/src/client/base_client/non_wasm_helpers.rs +++ b/common/client-core/src/client/base_client/non_wasm_helpers.rs @@ -126,7 +126,7 @@ pub fn create_bandwidth_controller_with_urls( ) -> BandwidthController { let client = default_query_dkg_client(nyxd_url); - BandwidthController::new(storage, client, ecash_keypair, ecash_params) //SW fill in those todo + BandwidthController::new(storage, client, ecash_keypair, ecash_params) } pub fn default_query_dkg_client_from_config(config: &Config) -> QueryHttpRpcNyxdClient { diff --git a/gateway/src/node/client_handling/websocket/connection_handler/coconut.rs b/gateway/src/node/client_handling/websocket/connection_handler/coconut.rs index 05961b9625..2fb49f1424 100644 --- a/gateway/src/node/client_handling/websocket/connection_handler/coconut.rs +++ b/gateway/src/node/client_handling/websocket/connection_handler/coconut.rs @@ -19,6 +19,7 @@ use std::sync::Mutex; use tokio::time::{interval, Duration}; const TIME_RANGE_SEC: i64 = 30; +const CRED_SENDING_INTERVAL: u64 = 300; pub(crate) struct EcashVerifier { nyxd_client: DirectSigningHttpRpcNyxdClient, @@ -207,48 +208,48 @@ impl CredentialSender { } } - async fn handle_credential(&mut self, credential: CredentialToBeSent) { - self.pending.push_back(credential); - self.try_empty_pending().await; + async fn send_credential(request: &VerifyCredentialBody, endpoint: &CoconutApiClient) -> bool { + match endpoint + .api_client + .verify_bandwidth_credential(&request) + .await + { + Ok(res) => { + if !res.verification_result { + log::debug!( + "Validator {} didn't accept the credential.", + endpoint.api_client.nym_api.current_url() + ); + } + //Credential was sent + true + } + Err(e) => { + log::warn!("Validator {} could not be reached. There might be a problem with the coconut endpoint - {:?}", endpoint.api_client.nym_api.current_url(), e); + false + } + } } + async fn handle_credential(&mut self, credential: CredentialToBeSent) { + if !Self::send_credential(&credential.0, &credential.1).await { + self.pending.push_back(credential); + } + } + async fn try_empty_pending(&mut self) { + log::debug!("Trying to send unsent payments"); let mut new_pending = VecDeque::new(); for credential in &self.pending { - match credential - .1 - .api_client - .verify_bandwidth_credential(&credential.0) - .await - { - Ok(res) => { - if !res.verification_result { - log::debug!( - "Validator {} didn't accept the credential.", - credential.1.api_client.nym_api.current_url() - ); - } - } - Err(e) => { - log::warn!("Validator {} could not be reached. There might be a problem with the coconut endpoint - {:?}", credential - .1.api_client.nym_api.current_url(), e); - new_pending.push_back(credential.clone()); - } + if !Self::send_credential(&credential.0, &credential.1).await { + new_pending.push_back(credential.clone()); } } self.pending = new_pending; } - fn start(self, shutdown: nym_task::TaskClient) { - tokio::spawn(async move { self.run(shutdown).await }); - - //spawn a new thread that tries to send all pending Credentials. - //if it cannot send something, back of the queue and retry in 5min - //if everything has been sent, stop running - } - async fn run(mut self, mut shutdown: nym_task::TaskClient) { - log::debug!("Starting Ecash CredentialSender"); - let mut interval = interval(Duration::from_secs(300)); + log::info!("Starting Ecash CredentialSender"); + let mut interval = interval(Duration::from_secs(CRED_SENDING_INTERVAL)); while !shutdown.is_shutdown() { tokio::select! { @@ -262,20 +263,8 @@ impl CredentialSender { } } } + + fn start(self, shutdown: nym_task::TaskClient) { + tokio::spawn(async move { self.run(shutdown).await }); + } } - -//let ret = client.api_client.verify_bandwidth_credential(&req).await; - -// match ret { -// Ok(res) => { -// if !res.verification_result { -// debug!( -// "Validator {} didn't accept the credential.", -// client.api_client.nym_api.current_url() -// ); -// } -// } -// Err(e) => { -// warn!("Validator {} could not be reached. There might be a problem with the coconut endpoint - {:?}", client.api_client.nym_api.current_url(), e); -// } -// }