Skip to content

Commit

Permalink
fix: reintroduce missing queries
Browse files Browse the repository at this point in the history
Signed-off-by: Sam H. Smith <[email protected]>
  • Loading branch information
SamHSmith committed Oct 28, 2024
1 parent 74c6d86 commit b090d49
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 94 deletions.
150 changes: 90 additions & 60 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use paste::paste;

use pyo3::{
exceptions::{PyRuntimeError, PyValueError},
prelude::*,
Expand All @@ -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::*;
Expand Down Expand Up @@ -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)
}
Expand All @@ -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<Vec<String>> {
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)
}
Expand All @@ -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<Vec<String>> {
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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -240,26 +264,24 @@ impl Client {

let mut items = Vec::new();
for item in val {
items.push(
item.to_string()
);
items.push(item.to_string());
}
Ok(items)
}

fn query_all_roles_of_account(&self, account_id: &str) -> PyResult<Vec<String>> {
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)
}
Expand All @@ -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<Vec<PyTransactionQueryOutput>> {
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<PyTransactionQueryOutput> {
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<()> {
Expand Down
16 changes: 4 additions & 12 deletions src/data_model/account.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -64,7 +56,7 @@ impl PyAccount {
}

#[getter]
fn get_metadata(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
fn get_metadata(&self, _py: Python<'_>) -> PyResult<Py<PyDict>> {
//MetadataWrapper(self.0.metadata.clone()).into_py(py)
unimplemented!();
}
Expand Down
2 changes: 1 addition & 1 deletion src/data_model/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand Down
6 changes: 1 addition & 5 deletions src/data_model/domain.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -61,7 +57,7 @@ impl PyDomain {
}

#[getter]
fn get_metadata(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
fn get_metadata(&self, _py: Python<'_>) -> PyResult<Py<PyDict>> {
//MetadataWrapper(self.0.metadata.clone()).into_py(py)
unimplemented!();
}
Expand Down
12 changes: 1 addition & 11 deletions src/data_model/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/isi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -148,8 +146,11 @@ impl PyInstruction {
grant_to_account_id: &str,
permission_tokens: Vec<(&str, &str)>,
) -> PyResult<PyInstruction> {
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)
Expand Down

0 comments on commit b090d49

Please sign in to comment.