From 2d34a5ec3de5605b7dbdb20bd2b496d737c9ad82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Mon, 23 Sep 2024 17:40:44 +0200 Subject: [PATCH] Create trait for status events --- Cargo.lock | 1 + common/bandwidth-controller/Cargo.toml | 7 ++++--- common/bandwidth-controller/src/event.rs | 6 ++++++ common/client-core/src/error.rs | 6 ++++++ common/socks5-client-core/src/error.rs | 6 ++++++ common/task/src/manager.rs | 14 +++++++++++++- sdk/rust/nym-sdk/src/mixnet/client.rs | 1 + 7 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 475d2e657c..475b6e3308 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4646,6 +4646,7 @@ dependencies = [ "nym-ecash-contract-common", "nym-ecash-time", "nym-network-defaults", + "nym-task", "nym-validator-client", "rand", "thiserror", diff --git a/common/bandwidth-controller/Cargo.toml b/common/bandwidth-controller/Cargo.toml index 386489ea7a..1f5a4e9586 100644 --- a/common/bandwidth-controller/Cargo.toml +++ b/common/bandwidth-controller/Cargo.toml @@ -14,14 +14,15 @@ thiserror = { workspace = true } url = { workspace = true } zeroize = { workspace = true } -nym-ecash-time = { path = "../ecash-time" } nym-credential-storage = { path = "../credential-storage" } nym-credentials = { path = "../credentials" } nym-credentials-interface = { path = "../credentials-interface" } nym-crypto = { path = "../crypto", features = ["rand", "asymmetric", "stream_cipher", "aes", "hashing"] } -nym-network-defaults = { path = "../network-defaults" } -nym-validator-client = { path = "../client-libs/validator-client", default-features = false } nym-ecash-contract-common = { path = "../cosmwasm-smart-contracts/ecash-contract" } +nym-ecash-time = { path = "../ecash-time" } +nym-network-defaults = { path = "../network-defaults" } +nym-task = { path = "../task" } +nym-validator-client = { path = "../client-libs/validator-client", default-features = false } [target."cfg(not(target_arch = \"wasm32\"))".dependencies.nym-validator-client] path = "../client-libs/validator-client" diff --git a/common/bandwidth-controller/src/event.rs b/common/bandwidth-controller/src/event.rs index 287598f959..bfd5372520 100644 --- a/common/bandwidth-controller/src/event.rs +++ b/common/bandwidth-controller/src/event.rs @@ -11,3 +11,9 @@ pub enum BandwidthStatusMessage { #[error("no bandwidth left")] NoBandwidth, } + +impl nym_task::manager::TaskEvent for BandwidthStatusMessage { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} diff --git a/common/client-core/src/error.rs b/common/client-core/src/error.rs index 4d412902e0..1f6fd21a25 100644 --- a/common/client-core/src/error.rs +++ b/common/client-core/src/error.rs @@ -221,3 +221,9 @@ pub enum ClientCoreStatusMessage { #[error("The connected gateway is very slow, or the connection to it is very slow")] GatewayIsVerySlow, } + +impl nym_task::manager::TaskEvent for ClientCoreStatusMessage { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} diff --git a/common/socks5-client-core/src/error.rs b/common/socks5-client-core/src/error.rs index a88f707897..65dd23bd53 100644 --- a/common/socks5-client-core/src/error.rs +++ b/common/socks5-client-core/src/error.rs @@ -28,3 +28,9 @@ impl From for Socks5ClientCoreError { } } } + +impl nym_task::manager::TaskEvent for Socks5ClientCoreError { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} diff --git a/common/task/src/manager.rs b/common/task/src/manager.rs index d354a06db8..3f9d6499ce 100644 --- a/common/task/src/manager.rs +++ b/common/task/src/manager.rs @@ -3,6 +3,7 @@ use futures::{future::pending, FutureExt, SinkExt, StreamExt}; use log::{log, Level}; +use std::any::Any; use std::sync::atomic::{AtomicBool, Ordering}; use std::{error::Error, time::Duration}; use tokio::sync::{ @@ -22,7 +23,8 @@ pub(crate) type SentError = Box; type ErrorSender = mpsc::UnboundedSender; type ErrorReceiver = mpsc::UnboundedReceiver; -pub type SentStatus = Box; +// pub type SentStatus = Box; +pub type SentStatus = Box; pub type StatusSender = futures::channel::mpsc::Sender; pub type StatusReceiver = futures::channel::mpsc::Receiver; @@ -40,6 +42,10 @@ enum TaskError { UnexpectedHalt { shutdown_name: Option }, } +pub trait TaskEvent: std::error::Error + Send + Sync + Any { + fn as_any(&self) -> &dyn Any; +} + // TODO: possibly we should create a `Status` trait instead of reusing `Error` #[derive(thiserror::Error, Debug, PartialEq, Eq)] pub enum TaskStatus { @@ -49,6 +55,12 @@ pub enum TaskStatus { ReadyWithGateway(String), } +impl TaskEvent for TaskStatus { + fn as_any(&self) -> &dyn Any { + self + } +} + /// Listens to status and error messages from tasks, as well as notifying them to gracefully /// shutdown. Keeps track of if task stop unexpectedly, such as in a panic. #[derive(Debug)] diff --git a/sdk/rust/nym-sdk/src/mixnet/client.rs b/sdk/rust/nym-sdk/src/mixnet/client.rs index 14138b0baf..8f4b6caa28 100644 --- a/sdk/rust/nym-sdk/src/mixnet/client.rs +++ b/sdk/rust/nym-sdk/src/mixnet/client.rs @@ -655,6 +655,7 @@ where .next() .await .ok_or(Error::Socks5NotStarted)? + .as_any() .downcast_ref::() .ok_or(Error::Socks5NotStarted)? {