Skip to content

Commit

Permalink
refactor(queries): remove now unused singular queries
Browse files Browse the repository at this point in the history
Signed-off-by: ⭐️NINIKA⭐️ <[email protected]>
  • Loading branch information
DCNick3 committed Nov 29, 2024
1 parent 077ea6b commit f704b6e
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 513 deletions.
16 changes: 0 additions & 16 deletions crates/iroha_core/src/smartcontracts/isi/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use iroha_data_model::{prelude::*, query::error::FindError};
use iroha_telemetry::metrics;

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

impl Registrable for iroha_data_model::account::NewAccount {
type Target = Account;
Expand Down Expand Up @@ -444,7 +443,6 @@ pub mod query {
permission::Permission,
query::{dsl::CompoundPredicate, error::QueryExecutionFail as Error},
};
use iroha_primitives::json::Json;

use super::*;
use crate::{smartcontracts::ValidQuery, state::StateReadOnly};
Expand Down Expand Up @@ -497,20 +495,6 @@ pub mod query {
}
}

impl ValidSingularQuery for FindAccountMetadata {
#[metrics(+"find_account_key_value_by_id_and_key")]
fn execute(&self, state_ro: &impl StateReadOnly) -> Result<Json, Error> {
let id = &self.id;
let key = &self.key;
iroha_logger::trace!(%id, %key);
state_ro
.world()
.map_account(id, |account| account.metadata.get(key).cloned())?
.ok_or_else(|| FindError::MetadataKey(key.clone()).into())
.map(Into::into)
}
}

impl ValidQuery for FindAccountsWithAsset {
#[metrics(+"find_accounts_with_asset")]
fn execute(
Expand Down
54 changes: 1 addition & 53 deletions crates/iroha_core/src/smartcontracts/isi/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,9 @@ pub mod isi {
pub mod query {
use eyre::Result;
use iroha_data_model::{
asset::{Asset, AssetDefinition, AssetValue},
asset::{Asset, AssetDefinition},
query::{dsl::CompoundPredicate, error::QueryExecutionFail as Error},
};
use iroha_primitives::json::Json;

use super::*;
use crate::{smartcontracts::ValidQuery, state::StateReadOnly};
Expand Down Expand Up @@ -460,55 +459,4 @@ pub mod query {
.cloned())
}
}

impl ValidSingularQuery for FindAssetQuantityById {
#[metrics(+"find_asset_quantity_by_id")]
fn execute(&self, state_ro: &impl StateReadOnly) -> Result<Numeric, Error> {
let id = &self.id;
iroha_logger::trace!(%id);
let value = state_ro
.world()
.asset(id)
.map_err(|asset_err| {
if let Err(definition_err) = state_ro.world().asset_definition(&id.definition) {
Error::Find(definition_err)
} else {
asset_err
}
})?
.value;

match value {
AssetValue::Store(_) => Err(Error::Conversion(
"Can't get quantity for strore asset".to_string(),
)),
AssetValue::Numeric(numeric) => Ok(numeric),
}
}
}

impl ValidSingularQuery for FindAssetMetadata {
#[metrics(+"find_asset_key_value_by_id_and_key")]
fn execute(&self, state_ro: &impl StateReadOnly) -> Result<Json, Error> {
let id = &self.id;
let key = &self.key;
let asset = state_ro.world().asset(id).map_err(|asset_err| {
if let Err(definition_err) = state_ro.world().asset_definition(&id.definition) {
Error::Find(definition_err)
} else {
asset_err
}
})?;
iroha_logger::trace!(%id, %key);
let AssetValue::Store(store) = &asset.value else {
return Err(Error::Conversion("expected store, found other".to_owned()));
};

store
.get(key)
.ok_or_else(|| Error::Find(FindError::MetadataKey(key.clone())))
.cloned()
.map(Into::into)
}
}
}
33 changes: 0 additions & 33 deletions crates/iroha_core/src/smartcontracts/isi/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,10 @@ pub mod isi {

/// Query module provides [`Query`] Domain related implementations.
pub mod query {
use eyre::Result;
use iroha_data_model::{
domain::Domain,
query::{dsl::CompoundPredicate, error::QueryExecutionFail},
};
use iroha_primitives::json::Json;

use super::*;
use crate::{smartcontracts::ValidQuery, state::StateReadOnly};
Expand All @@ -404,35 +402,4 @@ pub mod query {
.cloned())
}
}

impl ValidSingularQuery for FindDomainMetadata {
#[metrics(+"find_domain_key_value_by_id_and_key")]
fn execute(&self, state_ro: &impl StateReadOnly) -> Result<Json, QueryExecutionFail> {
let id = &self.id;
let key = &self.key;
iroha_logger::trace!(%id, %key);
state_ro
.world()
.map_domain(id, |domain| domain.metadata.get(key).cloned())?
.ok_or_else(|| FindError::MetadataKey(key.clone()).into())
.map(Into::into)
}
}

impl ValidSingularQuery for FindAssetDefinitionMetadata {
#[metrics(+"find_asset_definition_key_value_by_id_and_key")]
fn execute(&self, state_ro: &impl StateReadOnly) -> Result<Json, QueryExecutionFail> {
let id = &self.id;
let key = &self.key;
iroha_logger::trace!(%id, %key);
Ok(state_ro
.world()
.asset_definition(id)?
.metadata
.get(key)
.ok_or(FindError::MetadataKey(key.clone()))
.cloned()
.map(Into::into)?)
}
}
}
4 changes: 2 additions & 2 deletions crates/iroha_core/src/smartcontracts/isi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use mv::storage::StorageReadOnly;

use super::Execute;
use crate::{
prelude::*,
smartcontracts::triggers::set::SetReadOnly,
state::{StateReadOnly, StateTransaction, WorldReadOnly},
};
Expand Down Expand Up @@ -228,6 +227,7 @@ pub mod prelude {
mod tests {
use std::sync::Arc;

use iroha_crypto::KeyPair;
use iroha_test_samples::{
gen_account_in, ALICE_ID, SAMPLE_GENESIS_ACCOUNT_ID, SAMPLE_GENESIS_ACCOUNT_KEYPAIR,
};
Expand All @@ -239,7 +239,7 @@ mod tests {
kura::Kura,
query::store::LiveQueryStore,
state::{State, World},
tx::AcceptTransactionFail,
tx::{AcceptTransactionFail, AcceptedTransaction},
};

fn state_with_test_domains(kura: &Arc<Kura>) -> Result<State> {
Expand Down
101 changes: 0 additions & 101 deletions crates/iroha_core/src/smartcontracts/isi/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,30 +236,12 @@ impl ValidQueryRequest {
match self.0 {
QueryRequest::Singular(singular_query) => {
let output = match singular_query {
SingularQueryBox::FindAssetQuantityById(q) => {
SingularQueryOutputBox::from(q.execute(state)?)
}
SingularQueryBox::FindExecutorDataModel(q) => {
SingularQueryOutputBox::from(q.execute(state)?)
}
SingularQueryBox::FindParameters(q) => {
SingularQueryOutputBox::from(q.execute(state)?)
}
SingularQueryBox::FindDomainMetadata(q) => {
SingularQueryOutputBox::from(q.execute(state)?)
}
SingularQueryBox::FindAccountMetadata(q) => {
SingularQueryOutputBox::from(q.execute(state)?)
}
SingularQueryBox::FindAssetMetadata(q) => {
SingularQueryOutputBox::from(q.execute(state)?)
}
SingularQueryBox::FindAssetDefinitionMetadata(q) => {
SingularQueryOutputBox::from(q.execute(state)?)
}
SingularQueryBox::FindTriggerMetadata(q) => {
SingularQueryOutputBox::from(q.execute(state)?)
}
};

Ok(QueryResponse::Singular(output))
Expand Down Expand Up @@ -359,7 +341,6 @@ impl ValidQueryRequest {
mod tests {
use iroha_crypto::{Hash, KeyPair};
use iroha_data_model::{block::BlockHeader, query::dsl::CompoundPredicate};
use iroha_primitives::json::Json;
use iroha_test_samples::{gen_account_in, ALICE_ID, ALICE_KEYPAIR};
use nonzero_ext::nonzero;
use tokio::test;
Expand All @@ -384,36 +365,6 @@ mod tests {
World::with([domain], [account], [asset_definition])
}

fn world_with_test_asset_with_metadata() -> World {
let asset_definition_id = "rose#wonderland"
.parse::<AssetDefinitionId>()
.expect("Valid");
let domain = Domain::new("wonderland".parse().expect("Valid")).build(&ALICE_ID);
let account = Account::new(ALICE_ID.clone()).build(&ALICE_ID);
let asset_definition =
AssetDefinition::numeric(asset_definition_id.clone()).build(&ALICE_ID);

let mut store = Metadata::default();
store.insert("Bytes".parse().expect("Valid"), vec![1_u32, 2_u32, 3_u32]);
let asset_id = AssetId::new(asset_definition_id, account.id().clone());
let asset = Asset::new(asset_id, AssetValue::Store(store));

World::with_assets([domain], [account], [asset_definition], [asset])
}

fn world_with_test_account_with_metadata() -> Result<World> {
let mut metadata = Metadata::default();
metadata.insert("Bytes".parse()?, vec![1_u32, 2_u32, 3_u32]);

let domain = Domain::new("wonderland".parse()?).build(&ALICE_ID);
let account = Account::new(ALICE_ID.clone())
.with_metadata(metadata)
.build(&ALICE_ID);
let asset_definition_id = "rose#wonderland".parse().expect("Valid");
let asset_definition = AssetDefinition::numeric(asset_definition_id).build(&ALICE_ID);
Ok(World::with([domain], [account], [asset_definition]))
}

fn state_with_test_blocks_and_transactions(
blocks: u64,
valid_tx_per_block: usize,
Expand Down Expand Up @@ -490,31 +441,6 @@ mod tests {
Ok(state)
}

#[test]
async fn asset_store() -> Result<()> {
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::start_test();
let state = State::new(world_with_test_asset_with_metadata(), kura, query_handle);

let asset_definition_id = "rose#wonderland".parse()?;
let asset_id = AssetId::new(asset_definition_id, ALICE_ID.clone());
let bytes = FindAssetMetadata::new(asset_id, "Bytes".parse()?).execute(&state.view())?;
assert_eq!(Json::from(vec![1_u32, 2_u32, 3_u32,]), bytes,);
Ok(())
}

#[test]
async fn account_metadata() -> Result<()> {
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::start_test();
let state = State::new(world_with_test_account_with_metadata()?, kura, query_handle);

let bytes =
FindAccountMetadata::new(ALICE_ID.clone(), "Bytes".parse()?).execute(&state.view())?;
assert_eq!(Json::from(vec![1_u32, 2_u32, 3_u32,]), bytes,);
Ok(())
}

#[test]
async fn find_all_blocks() -> Result<()> {
let num_blocks = 100;
Expand Down Expand Up @@ -676,31 +602,4 @@ mod tests {
}
Ok(())
}

#[test]
async fn domain_metadata() -> Result<()> {
let kura = Kura::blank_kura_for_testing();
let state = {
let mut metadata = Metadata::default();
metadata.insert("Bytes".parse()?, vec![1_u32, 2_u32, 3_u32]);
let domain = Domain::new("wonderland".parse()?)
.with_metadata(metadata)
.build(&ALICE_ID);
let account = Account::new(ALICE_ID.clone()).build(&ALICE_ID);
let asset_definition_id = "rose#wonderland".parse()?;
let asset_definition = AssetDefinition::numeric(asset_definition_id).build(&ALICE_ID);
let query_handle = LiveQueryStore::start_test();
State::new(
World::with([domain], [account], [asset_definition]),
kura,
query_handle,
)
};

let domain_id = "wonderland".parse()?;
let key = "Bytes".parse()?;
let bytes = FindDomainMetadata::new(domain_id, key).execute(&state.view())?;
assert_eq!(Json::from(vec![1_u32, 2_u32, 3_u32,]), bytes,);
Ok(())
}
}
22 changes: 0 additions & 22 deletions crates/iroha_core/src/smartcontracts/isi/triggers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ pub mod query {
},
trigger::{Trigger, TriggerId},
};
use iroha_primitives::json::Json;

use super::*;
use crate::{
Expand Down Expand Up @@ -363,25 +362,4 @@ pub mod query {
.filter(move |trigger| filter.applies(trigger)))
}
}

impl ValidSingularQuery for FindTriggerMetadata {
#[metrics(+"find_trigger_key_value_by_id_and_key")]
fn execute(&self, state_ro: &impl StateReadOnly) -> Result<Json, Error> {
let id = &self.id;
let key = &self.key;
iroha_logger::trace!(%id, %key);
state_ro
.world()
.triggers()
.inspect_by_id(id, |action| {
action
.metadata()
.get(key)
.cloned()
.ok_or_else(|| FindError::MetadataKey(key.clone()).into())
})
.ok_or_else(|| Error::Find(FindError::Trigger(id.clone())))?
.map(Into::into)
}
}
}
6 changes: 0 additions & 6 deletions crates/iroha_data_model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,10 @@ mod seal {
// Boxed queries
SingularQueryBox,
FindAccounts,
FindAccountMetadata,
FindAccountsWithAsset,
FindAssets,
FindAssetsDefinitions,
FindAssetQuantityById,
FindAssetMetadata,
FindAssetDefinitionMetadata,
FindDomains,
FindDomainMetadata,
FindPeers,
FindBlocks,
FindBlockHeaders,
Expand All @@ -140,7 +135,6 @@ mod seal {
FindExecutorDataModel,
FindActiveTriggerIds,
FindTriggers,
FindTriggerMetadata,
FindRoles,
FindRoleIds,
FindRolesByAccountId,
Expand Down
Loading

0 comments on commit f704b6e

Please sign in to comment.