Skip to content

Commit

Permalink
feat!: validate the new queries in the executor
Browse files Browse the repository at this point in the history
Signed-off-by: ⭐️NINIKA⭐️ <[email protected]>
  • Loading branch information
DCNick3 committed Jul 10, 2024
1 parent 157d6cd commit ee16955
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 153 deletions.
Binary file modified configs/swarm/executor.wasm
Binary file not shown.
53 changes: 31 additions & 22 deletions core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use iroha_data_model::{
account::AccountId,
executor as data_model_executor,
isi::InstructionBox,
query::QueryRequest2,
query::{QueryBox2, QueryRequest2},
transaction::{Executable, SignedTransaction},
ValidationFail,
};
Expand Down Expand Up @@ -199,33 +199,42 @@ impl Executor {
/// - Executor denied the operation.
pub fn validate_query<S: StateReadOnly>(
&self,
_state_ro: &S,
_authority: &AccountId,
_query: &QueryRequest2,
state_ro: &S,
authority: &AccountId,
query: &QueryRequest2,
) -> Result<(), ValidationFail> {
trace!("Running query validation");

// TODO: actually validate the query request

Ok(())
// Ok(())

let query = match query {
QueryRequest2::Singular(singular) => QueryBox2::Singular(singular.clone()),
QueryRequest2::StartIterable(iterable) => QueryBox2::Iterable(iterable.clone()),
QueryRequest2::ContinueIterable(_) => {
// The iterable query was already validated when it started
return Ok(());
}
};

match self {
Self::Initial => Ok(()),
Self::UserProvided(loaded_executor) => {
let runtime =
wasm::RuntimeBuilder::<wasm::state::executor::ValidateQuery<S>>::new()
.with_engine(state_ro.engine().clone()) // Cloning engine is cheap, see [`wasmtime::Engine`] docs
.with_config(state_ro.world().parameters().executor)
.build()?;

// match self {
// Self::Initial => Ok(()),
// Self::UserProvided(loaded_executor) => {
// let runtime =
// wasm::RuntimeBuilder::<wasm::state::executor::ValidateQuery<S>>::new()
// .with_engine(state_ro.engine().clone()) // Cloning engine is cheap, see [`wasmtime::Engine`] docs
// .with_config(state_ro.world().parameters().executor)
// .build()?;
//
// runtime.execute_executor_validate_query(
// state_ro,
// authority,
// &loaded_executor.module,
// query,
// )?
// }
// }
runtime.execute_executor_validate_query(
state_ro,
authority,
&loaded_executor.module,
query,
)?
}
}
}

/// Migrate executor to a new user-provided one.
Expand Down
28 changes: 28 additions & 0 deletions core/src/smartcontracts/isi/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,32 @@ pub mod query {
))
}
}

impl ValidIterableQuery for FindAccountsWithAsset {
#[metrics(+"find_accounts_with_asset")]
fn execute<'state>(
self,
filter: CompoundPredicate<AccountPredicateBox>,
state_ro: &'state impl StateReadOnly,
) -> std::result::Result<impl Iterator<Item = Account> + 'state, Error> {
let asset_definition_id = self.asset_definition.clone();
iroha_logger::trace!(%asset_definition_id);

Ok(state_ro
.world()
.accounts_iter()
.filter(move |account| {
state_ro
.world()
.assets()
.get(&AssetId::new(
asset_definition_id.clone(),
account.id().clone(),
))
.is_some()
})
.filter(move |&account| filter.applies(account))
.cloned())
}
}
}
6 changes: 2 additions & 4 deletions core/src/smartcontracts/isi/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ pub mod query {
) -> Result<impl Iterator<Item = Asset> + 'state, Error> {
Ok(state_ro
.world()
.accounts_iter()
.flat_map(|account| account.assets.values())
.assets_iter()
.filter(move |&asset| filter.applies(asset))
.cloned())
}
Expand All @@ -483,8 +482,7 @@ pub mod query {
) -> Result<impl Iterator<Item = AssetDefinition> + 'state, Error> {
Ok(state_ro
.world()
.domains_iter()
.flat_map(|domain| domain.asset_definitions.values())
.asset_definitions_iter()
.filter(move |&asset_definition| filter.applies(asset_definition))
.cloned())
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/smartcontracts/isi/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ impl ValidQueryRequest {
ValidIterableQuery::execute(q.query, q.predicate, state)?,
&iter_query.params,
)?,
IterableQueryBox::FindAccountsWithAsset(q) => apply_query_postprocessing(
ValidIterableQuery::execute(q.query, q.predicate, state)?,
&iter_query.params,
)?,
IterableQueryBox::FindAllPeers(q) => apply_query_postprocessing(
ValidIterableQuery::execute(q.query, q.predicate, state)?,
&iter_query.params,
Expand Down
20 changes: 11 additions & 9 deletions core/src/smartcontracts/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use iroha_data_model::{
parameter::SmartContractParameters as Config,
prelude::*,
query::{
cursor::QueryId, IterableQueryOutput, QueryBox, QueryRequest, QueryRequest2,
cursor::QueryId, IterableQueryOutput, QueryBox2, QueryRequest, QueryRequest2,
QueryResponse2, SmartContractQuery,
},
smart_contract::payloads::{self, Validate},
Expand Down Expand Up @@ -72,7 +72,7 @@ mod import {
//! Traits which some [Runtime]s should implement to import functions from Iroha to WASM
use iroha_data_model::{
query::{QueryBox, QueryRequest2, QueryResponse2},
query::{QueryRequest2, QueryResponse2},
smart_contract::payloads::Validate,
};

Expand Down Expand Up @@ -105,7 +105,7 @@ mod import {
fn get_validate_instruction_payload(state: &S) -> Validate<InstructionBox>;

#[codec::wrap_trait_fn]
fn get_validate_query_payload(state: &S) -> Validate<QueryBox>;
fn get_validate_query_payload(state: &S) -> Validate<QueryBox2>;
}

pub trait SetDataModel<S> {
Expand Down Expand Up @@ -487,6 +487,8 @@ pub mod state {
pub mod executor {
//! States related to *Executor* execution.
use iroha_data_model::query::QueryBox2;

use super::*;

/// Struct to encapsulate common state kinds for `validate_*` entrypoints
Expand All @@ -499,7 +501,7 @@ pub mod state {
pub type ValidateTransaction = Validate<SignedTransaction>;

/// State kind for executing `validate_query()` entrypoint of executor
pub type ValidateQuery = Validate<QueryBox>;
pub type ValidateQuery = Validate<QueryBox2>;

/// State kind for executing `validate_instruction()` entrypoint of executor
pub type ValidateInstruction = Validate<InstructionBox>;
Expand Down Expand Up @@ -1175,7 +1177,7 @@ impl<'wrld, 'block, 'state>
#[codec::wrap]
fn get_validate_query_payload(
_state: &state::executor::ValidateTransaction<'wrld, 'block, 'state>,
) -> Validate<QueryBox> {
) -> Validate<QueryBox2> {
panic!("Executor `validate_transaction()` entrypoint should not query payload for `validate_query()` entrypoint")
}
}
Expand Down Expand Up @@ -1255,7 +1257,7 @@ impl<'wrld, 'block, 'state>
#[codec::wrap]
fn get_validate_query_payload(
_state: &state::executor::ValidateInstruction<'wrld, 'block, 'state>,
) -> Validate<QueryBox> {
) -> Validate<QueryBox2> {
panic!("Executor `validate_instruction()` entrypoint should not query payload for `validate_query()` entrypoint")
}
}
Expand All @@ -1280,7 +1282,7 @@ impl<'wrld, S: StateReadOnly> Runtime<state::executor::ValidateQuery<'wrld, S>>
state_ro: &'wrld S,
authority: &AccountId,
module: &wasmtime::Module,
query: QueryBox,
query: QueryBox2,
) -> Result<executor::Result> {
let span = wasm_log_span!("Running `validate_query()`");

Expand Down Expand Up @@ -1347,7 +1349,7 @@ impl<'wrld, S: StateReadOnly>
#[codec::wrap]
fn get_validate_query_payload(
state: &state::executor::ValidateQuery<'wrld, S>,
) -> Validate<QueryBox> {
) -> Validate<QueryBox2> {
Validate {
authority: state.authority.clone(),
block_height: state.state.0.height() as u64,
Expand Down Expand Up @@ -1442,7 +1444,7 @@ impl<'wrld, 'block, 'state>
#[codec::wrap]
fn get_validate_query_payload(
_state: &state::executor::Migrate<'wrld, 'block, 'state>,
) -> Validate<QueryBox> {
) -> Validate<QueryBox2> {
panic!("Executor `migrate()` entrypoint should not query payload for `validate_query()` entrypoint")
}
}
Expand Down
10 changes: 8 additions & 2 deletions data_model/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ pub enum QueryRequest2 {
ContinueIterable(ForwardCursor),
}

#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode, Deserialize, Serialize, IntoSchema)]
pub enum QueryBox2 {
Singular(SingularQueryBox),
Iterable(IterableQueryWithParams),
}

impl QueryRequest2 {
pub fn with_authority(self, authority: AccountId) -> QueryRequestWithAuthority {
QueryRequestWithAuthority {
Expand Down Expand Up @@ -1813,7 +1819,7 @@ pub mod prelude {
pub use super::{
account::prelude::*, asset::prelude::*, block::prelude::*, domain::prelude::*,
executor::prelude::*, peer::prelude::*, permission::prelude::*, predicate::PredicateTrait,
role::prelude::*, transaction::prelude::*, trigger::prelude::*, FetchSize, QueryBox,
TransactionQueryOutput,
role::prelude::*, transaction::prelude::*, trigger::prelude::*, FetchSize,
IterableQueryBox, QueryBox, QueryRequest2, SingularQueryBox, TransactionQueryOutput,
};
}
Loading

0 comments on commit ee16955

Please sign in to comment.