From b090d49acadd8306cb94fb5c296c5c43814ae237 Mon Sep 17 00:00:00 2001 From: "Sam H. Smith" Date: Mon, 28 Oct 2024 22:55:33 +0100 Subject: [PATCH] fix: reintroduce missing queries Signed-off-by: Sam H. Smith --- src/client.rs | 150 +++++++++++++++++++++++--------------- src/data_model/account.rs | 16 +--- src/data_model/asset.rs | 2 +- src/data_model/domain.rs | 6 +- src/data_model/mod.rs | 12 +-- src/isi.rs | 11 +-- 6 files changed, 103 insertions(+), 94 deletions(-) diff --git a/src/client.rs b/src/client.rs index 8d224586..486d503f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,5 +1,3 @@ -use paste::paste; - use pyo3::{ exceptions::{PyRuntimeError, PyValueError}, prelude::*, @@ -12,13 +10,11 @@ use iroha::config::{BasicAuth, WebLogin}; use std::num::NonZeroU64; use std::str::FromStr; -use crate::data_model::asset::{PyAsset, PyAssetDefinition, PyAssetDefinitionId, PyAssetId}; use crate::data_model::block::*; use crate::data_model::crypto::*; use crate::data_model::role::*; use crate::data_model::tx::*; -use crate::data_model::PyMirror; -use crate::{data_model::account::PyAccountId, isi::PyInstruction}; +use crate::isi::PyInstruction; use iroha_crypto::{Hash, HashOf}; use iroha_data_model::account::AccountId; use iroha_data_model::prelude::*; @@ -144,9 +140,7 @@ impl Client { let mut items = Vec::new(); for item in val { - items.push( - item.id.to_string() - ); + items.push(item.id.to_string()); } Ok(items) } @@ -160,9 +154,27 @@ impl Client { let mut items = Vec::new(); for item in val { - items.push( - item.id.to_string() - ); + items.push(item.id.to_string()); + } + Ok(items) + } + + fn query_all_accounts_in_domain(&self, domain_id: &str) -> PyResult> { + let val = self + .client + .query(query::account::FindAccounts) + .execute_all() + .map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?; + + let mut items = Vec::new(); + for item in val { + let string = item.id.to_string(); + if string.len() > domain_id.len() + 1 { + let string = &string[string.len() - domain_id.len() - 1..]; + if &string[0..1] == "@" && &string[1..] == domain_id { + items.push(item.id.to_string()); + } + } } Ok(items) } @@ -176,9 +188,27 @@ impl Client { let mut items = Vec::new(); for item in val { - items.push( - item.id.to_string() - ); + items.push(item.id.to_string()); + } + Ok(items) + } + + fn query_all_assets_owned_by_account(&self, account_id: &str) -> PyResult> { + let val = self + .client + .query(query::asset::FindAssets) + .execute_all() + .map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?; + + let mut items = Vec::new(); + for item in val { + let string = item.id.to_string(); + if string.len() > account_id.len() + 1 { + let string = &string[string.len() - account_id.len() - 1..]; + if &string[0..1] == "#" && &string[1..] == account_id { + items.push(item.id.to_string()); + } + } } Ok(items) } @@ -192,9 +222,7 @@ impl Client { let mut items = Vec::new(); for item in val { - items.push( - item.id.to_string() - ); + items.push(item.id.to_string()); } Ok(items) } @@ -208,9 +236,7 @@ impl Client { let mut items = Vec::new(); for item in val { - items.push( - item.into() - ); + items.push(item.into()); } Ok(items) } @@ -224,9 +250,7 @@ impl Client { let mut items = Vec::new(); for item in val { - items.push( - item.into() - ); + items.push(item.into()); } Ok(items) } @@ -240,9 +264,7 @@ impl Client { let mut items = Vec::new(); for item in val { - items.push( - item.to_string() - ); + items.push(item.to_string()); } Ok(items) } @@ -250,16 +272,16 @@ impl Client { fn query_all_roles_of_account(&self, account_id: &str) -> PyResult> { let val = self .client - .query(query::role::FindRolesByAccountId { id: AccountId::from_str(account_id) - .map_err(|e| PyValueError::new_err(e.to_string()))?}) + .query(query::role::FindRolesByAccountId { + id: AccountId::from_str(account_id) + .map_err(|e| PyValueError::new_err(e.to_string()))?, + }) .execute_all() .map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?; let mut items = Vec::new(); for item in val { - items.push( - item.to_string() - ); + items.push(item.to_string()); } Ok(items) } @@ -273,41 +295,49 @@ impl Client { let mut items = Vec::new(); for item in val { - items.push( - item.into() - ); + items.push(item.into()); } Ok(items) } -} -macro_rules! register_query { - ($query_name:ty; $ret:ty) => { - register_query!($query_name; $ret;); - }; - ($query_name:ty; $ret:ty; $($param_name:ident: $param_typ:ty),*) => { - paste! { - #[pymethods] - impl Client { - fn [<$query_name:snake>]( - &self, - $($param_name: $param_typ),* - ) -> PyResult<$ret> { - #[allow(unused_imports)] - use std::ops::Deref as _; - - let query = iroha_data_model::query::prelude::$query_name { - $( - $param_name: $param_name.deref().clone().into() - ),* - }; - let val = self.client.request(query) - .map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?; - val.mirror() - } + fn query_all_transactions_by_account( + &self, + account_id: &str, + ) -> PyResult> { + let val = self + .client + .query(query::transaction::FindTransactions) + .execute_all() + .map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?; + + let mut items = Vec::new(); + for item in val { + if item.transaction.value.payload().authority.to_string() == account_id { + items.push(item.into()); } } - }; + Ok(items) + } + + fn query_transaction_by_hash( + &self, + tx_hash: [u8; Hash::LENGTH], + ) -> PyResult { + let val = self + .client + .query(query::transaction::FindTransactions) + .execute_all() + .map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?; + + for item in val { + if item.transaction.value.hash() + == HashOf::from_untyped_unchecked(Hash::prehashed(tx_hash)) + { + return Ok(item.into()); + } + } + Err(PyRuntimeError::new_err("Transaction not found.")) + } } pub fn register_items(_py: Python<'_>, module: &PyModule) -> PyResult<()> { diff --git a/src/data_model/account.rs b/src/data_model/account.rs index cc760693..3108e7b0 100644 --- a/src/data_model/account.rs +++ b/src/data_model/account.rs @@ -1,18 +1,10 @@ -use iroha_data_model::account::{prelude::*, Account, NewAccount}; +use iroha_data_model::account::{prelude::*, Account}; -use pyo3::{ - exceptions::PyValueError, - prelude::*, - types::{PyDict, PyList}, -}; -use std::collections::BTreeMap; +use pyo3::{exceptions::PyValueError, prelude::*, types::PyDict}; use crate::mirror_struct; -use super::{ - asset::{PyAsset, PyAssetId}, - crypto::PyPublicKey, -}; +use super::crypto::PyPublicKey; mirror_struct! { #[derive(PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -64,7 +56,7 @@ impl PyAccount { } #[getter] - fn get_metadata(&self, py: Python<'_>) -> PyResult> { + fn get_metadata(&self, _py: Python<'_>) -> PyResult> { //MetadataWrapper(self.0.metadata.clone()).into_py(py) unimplemented!(); } diff --git a/src/data_model/asset.rs b/src/data_model/asset.rs index 485f3a81..ff5cafa4 100644 --- a/src/data_model/asset.rs +++ b/src/data_model/asset.rs @@ -280,7 +280,7 @@ impl PyAsset { Decimal::from_i128_with_scale(n.mantissa() as i128, n.scale()).into_py(py); Ok(quantity.into()) } - AssetValue::Store(v) => { + AssetValue::Store(_v) => { //let dict = MetadataWrapper(v.clone()).into_py(py)?; //Ok(dict.into()) unimplemented!(); diff --git a/src/data_model/domain.rs b/src/data_model/domain.rs index aec83cf5..e596e657 100644 --- a/src/data_model/domain.rs +++ b/src/data_model/domain.rs @@ -1,14 +1,10 @@ use iroha_data_model::domain::prelude::*; -use iroha_data_model::domain::NewDomain; use pyo3::types::PyDict; use pyo3::{exceptions::PyValueError, prelude::*}; use crate::data_model::account::*; -use crate::data_model::asset::*; use crate::mirror_struct; -use std::collections::BTreeMap; - mirror_struct! { /// Domain id DomainId @@ -61,7 +57,7 @@ impl PyDomain { } #[getter] - fn get_metadata(&self, py: Python<'_>) -> PyResult> { + fn get_metadata(&self, _py: Python<'_>) -> PyResult> { //MetadataWrapper(self.0.metadata.clone()).into_py(py) unimplemented!(); } diff --git a/src/data_model/mod.rs b/src/data_model/mod.rs index 7a4c547f..a62e5f27 100644 --- a/src/data_model/mod.rs +++ b/src/data_model/mod.rs @@ -1,16 +1,6 @@ -use derive_more::{From, Into}; - -use iroha_data_model::{metadata::Metadata}; -use iroha_data_model::query::SingularQueryOutputBox; -use pyo3::{ - exceptions::PyRuntimeError, - prelude::*, - types::{PyDict, PyList, PyString}, -}; +use pyo3::prelude::*; use self::account::*; -use self::asset::*; -use self::domain::*; pub mod account; pub mod asset; diff --git a/src/isi.rs b/src/isi.rs index bc251895..88bc40f9 100644 --- a/src/isi.rs +++ b/src/isi.rs @@ -6,9 +6,7 @@ use pyo3::{exceptions::PyValueError, prelude::*}; use std::str::FromStr; -use crate::data_model::account::PyAccountId; -use crate::data_model::asset::{PyAssetDefinitionId, PyAssetType, PyNewAssetDefinition}; -use crate::data_model::crypto::*; +use crate::data_model::asset::PyAssetType; use rust_decimal::{prelude::FromPrimitive, Decimal}; #[derive(Debug, Clone)] @@ -148,8 +146,11 @@ impl PyInstruction { grant_to_account_id: &str, permission_tokens: Vec<(&str, &str)>, ) -> PyResult { - let mut role = - Role::new(RoleId::from_str(role_id).map_err(|e| PyValueError::new_err(e.to_string()))?, AccountId::from_str(grant_to_account_id).map_err(|e| PyValueError::new_err(e.to_string()))?); + let mut role = Role::new( + RoleId::from_str(role_id).map_err(|e| PyValueError::new_err(e.to_string()))?, + AccountId::from_str(grant_to_account_id) + .map_err(|e| PyValueError::new_err(e.to_string()))?, + ); for (definition_id, json_string) in permission_tokens { role = role.add_permission(Permission::new( iroha_schema::Ident::from_str(definition_id)