From 701d9585a776513e8efab6054176894918520cf2 Mon Sep 17 00:00:00 2001 From: s0c5 Date: Tue, 30 Jul 2024 07:29:13 +0200 Subject: [PATCH] feat: fix max buffer to sender channel --- sube/src/lib.rs | 5 ++++- sube/src/meta_ext.rs | 2 +- sube/src/rpc.rs | 10 +++++----- sube/src/ws.rs | 16 ++++++++++++---- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/sube/src/lib.rs b/sube/src/lib.rs index c096c52..e36dbb3 100644 --- a/sube/src/lib.rs +++ b/sube/src/lib.rs @@ -488,7 +488,10 @@ pub enum Error { AccountNotFound, ConstantNotFound(String), Platform(String), - CantInitBackend + CantInitBackend, + CantDecodeReponseForMeta, + CantDecodeRawQueryResponse, + CantFindMethodInPallet, } impl fmt::Display for Error { diff --git a/sube/src/meta_ext.rs b/sube/src/meta_ext.rs index 3841625..d7b0178 100644 --- a/sube/src/meta_ext.rs +++ b/sube/src/meta_ext.rs @@ -127,7 +127,7 @@ impl StorageKey { .storage .as_ref() .and_then(|s| s.entries.iter().find(|e| e.name == item)) - .ok_or(crate::Error::StorageKeyNotFound)?; + .ok_or(crate::Error::CantFindMethodInPallet)?; log::trace!("map_keys={}", map_keys.iter().map(|x| x.as_ref()).collect::>().join(", ")); entry.ty.key(registry, &meta.name, &entry.name, map_keys) } diff --git a/sube/src/rpc.rs b/sube/src/rpc.rs index 62f520e..bdda6ad 100644 --- a/sube/src/rpc.rs +++ b/sube/src/rpc.rs @@ -52,7 +52,7 @@ impl Backend for RpcClient { ) .await .map_err(|err| { - log::info!("error {:?}", err); + log::error!("error state_queryStorageAt {:?}", err); crate::Error::StorageKeyNotFound }) } @@ -75,7 +75,7 @@ impl Backend for RpcClient { ) .await .map_err(|err| { - log::info!("error {:?}", err); + log::error!("error paged {:?}", err); crate::Error::StorageKeyNotFound })?; log::info!("rpc call {:?}", r); @@ -91,12 +91,12 @@ impl Backend for RpcClient { .rpc("state_getStorage", &[&format!("\"{}\"", &key)]) .await .map_err(|e| { - log::debug!("RPC failure: {}", e); + log::error!("RPC failure: {}", e); // NOTE it could fail for more reasons crate::Error::StorageKeyNotFound })?; - let response = hex::decode(&res[2..]).map_err(|_err| crate::Error::StorageKeyNotFound)?; + let response = hex::decode(&res[2..]).map_err(|_err| crate::Error::CantDecodeRawQueryResponse)?; Ok(response) } @@ -119,7 +119,7 @@ impl Backend for RpcClient { .rpc("state_getMetadata", &[]) .await .map_err(|e| crate::Error::Node(e.to_string()))?; - let response = hex::decode(&res[2..]).map_err(|_err| crate::Error::StorageKeyNotFound)?; + let response = hex::decode(&res[2..]).map_err(|_err| crate::Error::CantDecodeReponseForMeta)?; let meta = from_bytes(&mut response.as_slice()).map_err(|_| crate::Error::BadMetadata)?; log::trace!("Metadata {:#?}", meta); Ok(meta) diff --git a/sube/src/ws.rs b/sube/src/ws.rs index e7ff0ac..2a0e807 100644 --- a/sube/src/ws.rs +++ b/sube/src/ws.rs @@ -22,6 +22,8 @@ use crate::{ Error, }; +const MAX_BUFFER: usize = usize::MAX >> 3; + type Id = u32; pub struct Backend { @@ -60,13 +62,19 @@ impl Rpc for Backend { .lock() .await .try_send(Message::Text(msg)) - .map_err(|_| standard_error(StandardError::InternalError, None))?; + .map_err(|err| { + log::error!("Error tx lock message: {:?}", err); + standard_error(StandardError::InternalError, None) + })?; log::info!("sent CMD"); // wait for the matching response to arrive let res = recv .await - .map_err(|_| standard_error(StandardError::InternalError, None))? + .map_err(|err| { + log::error!("Error receiving message: {:?}", err); + standard_error(StandardError::InternalError, None) + })? .result()?; Ok(res) @@ -84,8 +92,8 @@ impl Backend { let (tx, rx) = ewebsock::connect(url, ewebsock::Options::default()).map_err(Error::Platform)?; - - let (sender, recv) = mpsc::channel::(0); + + let (sender, recv) = mpsc::channel::(MAX_BUFFER); let backend = Backend { tx: Mutex::new(sender),