Skip to content

Commit

Permalink
[feature] #2050: Add role-related queries.
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Petrosyan <[email protected]>
  • Loading branch information
appetrosyan committed Apr 26, 2022
1 parent f79a8a8 commit 1cd3e70
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 10 deletions.
2 changes: 1 addition & 1 deletion client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use eyre::{eyre, Result, WrapErr};
use http_client::WebSocketStream;
use iroha_config::{GetConfiguration, PostConfiguration};
use iroha_crypto::{HashOf, KeyPair};
use iroha_data_model::prelude::*;
use iroha_data_model::{prelude::*, query::SignedQueryRequest};
use iroha_logger::prelude::*;
use iroha_telemetry::metrics::Status;
use iroha_version::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion core/src/smartcontracts/isi/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ pub mod isi {

/// Asset-related query implementations.
pub mod query {
use eyre::{Result, WrapErr};
use eyre::{Result, WrapErr as _};
use iroha_logger::prelude::*;

use super::*;
Expand Down
5 changes: 5 additions & 0 deletions core/src/smartcontracts/isi/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,13 @@ impl<W: WorldTrait> ValidQuery<W> for QueryBox {
FindTransactionByHash(query) => query.execute_into_value(wsv),
FindPermissionTokensByAccountId(query) => query.execute_into_value(wsv),
FindAssetDefinitionKeyValueByIdAndKey(query) => query.execute_into_value(wsv),
FindAllTriggers(query) => query.execute_into_value(wsv),
FindTriggerById(query) => query.execute_into_value(wsv),
FindTriggerKeyValueByIdAndKey(query) => query.execute_into_value(wsv),
FindAllRoles(query) => query.execute_into_value(wsv),
FindAllRoleIds(query) => query.execute_into_value(wsv),
FindRolesByAccountId(query) => query.execute_into_value(wsv),
FindRoleByRoleId(query) => query.execute_into_value(wsv),
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions core/src/smartcontracts/isi/triggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,37 @@ pub mod isi {
}
}
}

pub mod query {
use iroha_logger::prelude::*;

use super::*;
use crate::{
prelude::*,
smartcontracts::{isi::prelude::WorldTrait, query::Error},
};

impl<W: WorldTrait> ValidQuery<W> for FindAllActiveTriggers {
#[log]
#[metrics(+"find_all_active_triggers")]
fn execute(&self, wsv: &WorldStateView<W>) -> Result<Self::Output, Error> {
Ok(wsv.world.triggers.iter().cloned().collect())
}
}

impl<W: WorldTrait> ValidQuery<W> for FindTriggerById {
#[log]
#[metrics(+"find_trigger_by_id")]
fn execute(&self, wsv: &WorldStateView<W>) -> Result<Self::Output, Error> {
todo!()
}
}

impl<W: WorldTrait> ValidQuery<W> for FindTriggerKeyValueByIdAndKey {
#[log]
#[metrics(+"find_trigger_key_value_by_id_and_key")]
fn execute(&self, wsv: &WorldStateView<W>) -> Result<Self::Output, Error> {
todo!()
}
}
}
37 changes: 36 additions & 1 deletion core/src/smartcontracts/isi/world.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! `World`-related ISI implementations.

use iroha_telemetry::metrics;

use super::prelude::*;
use crate::prelude::*;

/// Iroha Special Instructions that have `World` as their target.
pub mod isi {
use eyre::Result;
use iroha_data_model::prelude::*;
use iroha_telemetry::metrics;

use super::*;

Expand Down Expand Up @@ -202,6 +203,7 @@ pub mod query {

impl<W: WorldTrait> ValidQuery<W> for FindAllRoles {
#[log]
#[metrics(+"find_all_roles")]
fn execute(&self, wsv: &WorldStateView<W>) -> Result<Self::Output, Error> {
Ok(wsv
.world
Expand All @@ -212,8 +214,41 @@ pub mod query {
}
}

#[cfg(feature = "roles")]
impl<W: WorldTrait> ValidQuery<W> for FindAllRoleIds {
#[log]
#[metrics(+"find_all_role_ids")]
fn execute(&self, wsv: &WorldStateView<W>) -> Result<Self::Output, Error> {
Ok(wsv
.world
.roles
.iter()
// To me, this should probably be a method, not a field.
.map(|role| role.id().clone())
.collect())
}
}

#[cfg(feature = "roles")]
impl<W: WorldTrait> ValidQuery<W> for FindRoleByRoleId {
#[log]
#[metrics(+"find_role_by_role_id")]
fn execute(&self, wsv: &WorldStateView<W>) -> Result<Self::Output, Error> {
let role_id = self
.id
.evaluate(wsv, &Context::new())
.map_err(|e| Error::Evaluate(e.to_string()))?;

wsv.world.roles.get(&role_id).map_or_else(
|| Err(Error::Find(Box::new(FindError::Role(role_id)))),
|role_ref| Ok(role_ref.clone()),
)
}
}

impl<W: WorldTrait> ValidQuery<W> for FindAllPeers {
#[log]
#[metrics("find_all_peers")]
fn execute(&self, wsv: &WorldStateView<W>) -> Result<Self::Output, Error> {
Ok(wsv.peers())
}
Expand Down
151 changes: 144 additions & 7 deletions data_model/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use iroha_version::prelude::*;
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};

use self::{account::*, asset::*, domain::*, peer::*, permissions::*, role::*, transaction::*};
use self::{
account::*, asset::*, domain::*, peer::*, permissions::*, role::*, transaction::*, trigger::*,
};
use crate::{account::Account, pagination::Pagination, Identifiable, Value};

/// Sized container for all possible Queries.
Expand Down Expand Up @@ -80,8 +82,18 @@ pub enum QueryBox {
FindTransactionByHash(FindTransactionByHash),
/// [`FindPermissionTokensByAccountId`] variant.
FindPermissionTokensByAccountId(FindPermissionTokensByAccountId),
/// [`FindAllTriggers`] variant.
FindAllTriggers(FindAllActiveTriggers),
/// [`FindTriggerById`] variant.
FindTriggerById(FindTriggerById),
/// [`FindTriggerKeyValueByIdAndKey`] variant.
FindTriggerKeyValueByIdAndKey(FindTriggerKeyValueByIdAndKey),
/// [`FindAllRoles`] variant.
FindAllRoles(FindAllRoles),
/// [`FindAllRoleIds`] variant.
FindAllRoleIds(FindAllRoleIds),
/// [`FindRoleByRoleId`] variant.
FindRoleByRoleId(FindRoleByRoleId),
/// [`FindRolesByAccountId`] variant.
FindRolesByAccountId(FindRolesByAccountId),
}
Expand Down Expand Up @@ -219,7 +231,52 @@ pub mod role {
type Output = Vec<Role>;
}

/// `FindRolesByAccountId` Iroha Query will find an `Role`s for a specified account.
/// `FindAllRoles` Iroha Query will find all `Roles`s presented.
#[derive(
Debug,
Clone,
Copy,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
)]
pub struct FindAllRoleIds {}

impl Query for FindAllRoleIds {
type Output = Vec<<Role as Identifiable>::Id>;
}

/// `FindRoleByRoleId` Iroha Query to find the [`Role`] which has the given [`Id`]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
)]
pub struct FindRoleByRoleId {
/// `Id` of the `Role` to find
pub id: EvaluatesTo<<Role as Identifiable>::Id>,
}

impl Query for FindRoleByRoleId {
type Output = Role;
}

/// `FindRolesByAccountId` Iroha Query will find an [`Role`]s for a specified account.
#[derive(
Debug,
Clone,
Expand All @@ -235,16 +292,16 @@ pub mod role {
)]
pub struct FindRolesByAccountId {
/// `Id` of an account to find.
pub id: EvaluatesTo<AccountId>,
pub id: EvaluatesTo<<Account as Identifiable>::Id>,
}

impl Query for FindRolesByAccountId {
type Output = Vec<RoleId>;
type Output = Vec<<Role as Identifiable>::Id>;
}

/// The prelude re-exports most commonly used traits, structs and macros from this module.
pub mod prelude {
pub use super::{FindAllRoles, FindRolesByAccountId};
pub use super::{FindAllRoleIds, FindAllRoles, FindRoleByRoleId, FindRolesByAccountId};
}
}

Expand Down Expand Up @@ -1047,6 +1104,86 @@ pub mod peer {
}
}

pub mod trigger {
#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec::Vec};

use iroha_schema::prelude::*;
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};

use super::Query;
use crate::{expression::EvaluatesTo, trigger::Trigger, Identifiable, Name, Value};

#[derive(
Debug,
Clone,
Copy,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
)]
pub struct FindAllActiveTriggers {}

impl Query for FindAllActiveTriggers {
type Output = Vec<Trigger>;
}

#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
)]
pub struct FindTriggerById {
id: <Trigger as Identifiable>::Id,
}

impl Query for FindTriggerById {
type Output = Trigger;
}

#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
)]
pub struct FindTriggerKeyValueByIdAndKey {
id: <Trigger as Identifiable>::Id,
key: EvaluatesTo<Name>,
}

impl Query for FindTriggerKeyValueByIdAndKey {
type Output = Value;
}

pub mod prelude {
pub use super::{FindAllActiveTriggers, FindTriggerById, FindTriggerKeyValueByIdAndKey};
}
}

pub mod transaction {
//! Queries related to `Transaction`.

Expand Down Expand Up @@ -1139,8 +1276,8 @@ pub mod transaction {
pub mod prelude {
pub use super::{
account::prelude::*, asset::prelude::*, domain::prelude::*, peer::prelude::*,
permissions::prelude::*, role::prelude::*, transaction::*, PaginatedQueryResult, Query,
QueryBox, QueryResult, SignedQueryRequest, VersionedPaginatedQueryResult,
permissions::prelude::*, role::prelude::*, transaction::*, trigger::prelude::*,
PaginatedQueryResult, Query, QueryBox, QueryResult, VersionedPaginatedQueryResult,
VersionedQueryResult,
};
#[cfg(feature = "warp")]
Expand Down
8 changes: 8 additions & 0 deletions permissions_validators/src/private_blockchain/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ impl<W: WorldTrait> IsAllowed<W, QueryBox> for OnlyAccountsDomain {
Err("Only access to the domain of the account is permitted.".to_owned())
}
FindAllRoles(_) => Ok(()),
#[cfg(feature = "roles")]
FindAllRoleIds(_) => Ok(()),
#[cfg(feature = "roles")]
FindRoleByRoleId(_) => Ok(()),
FindAllPeers(_) => Ok(()),
FindAccountById(query) => {
let account_id = query
Expand Down Expand Up @@ -262,6 +266,10 @@ impl<W: WorldTrait> IsAllowed<W, QueryBox> for OnlyAccountsData {
Err("Only access to the assets of the same domain is permitted.".to_owned())
}
FindAllRoles(_) => Ok(()),
#[cfg(feature = "roles")]
FindAllRoleIds(_) => Ok(()),
#[cfg(feature = "roles")]
FindRoleByRoleId(_) => Ok(()),
FindAllPeers(_) => Ok(()),
FindAccountById(query) => {
let account_id = query
Expand Down

0 comments on commit 1cd3e70

Please sign in to comment.