Skip to content

Commit

Permalink
[fix] #4082: Remove cloning for query execution in wasm
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Jan 10, 2024
1 parent 4beb873 commit c43a409
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 177 deletions.
Binary file modified configs/peer/executor.wasm
Binary file not shown.
29 changes: 17 additions & 12 deletions core/src/smartcontracts/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ use iroha_data_model::{
isi::InstructionBox,
permission::PermissionTokenSchema,
prelude::*,
query::{QueryBox, QueryId, QueryRequest, QueryWithParameters},
smart_contract::{
payloads::{self, Validate},
SmartContractQueryRequest,
},
query::{QueryBox, QueryRequest, QueryWithParameters},
smart_contract::payloads::{self, Validate},
BatchedResponse, Level as LogLevel, ValidationFail,
};
use iroha_logger::debug;
// NOTE: Using error_span so that span info is logged on every event
use iroha_logger::{error_span as wasm_log_span, prelude::tracing::Span};
use iroha_wasm_codec::{self as codec, WasmUsize};
use parity_scale_codec::Decode;
use wasmtime::{
Caller, Config, Engine, Linker, Module, Store, StoreLimits, StoreLimitsBuilder, TypedFunc,
};
Expand Down Expand Up @@ -76,7 +74,7 @@ mod import {

use super::super::*;

pub trait ExecuteOperations<S> {
pub(crate) trait ExecuteOperations<S> {
/// Execute `query` on host
#[codec::wrap_trait_fn]
fn execute_query(
Expand Down Expand Up @@ -245,6 +243,11 @@ pub mod error {
/// [`Result`] type for this module
pub type Result<T, E = Error> = core::result::Result<T, E>;

#[cfg_attr(test, derive(parity_scale_codec::Encode))]
#[derive(Debug, derive_more::Display, Decode)]
#[repr(transparent)]
pub(crate) struct SmartContractQueryRequest(pub QueryRequest<QueryBox>);

/// Create [`Module`] from bytes.
///
/// # Errors
Expand Down Expand Up @@ -1754,12 +1757,14 @@ mod tests {
let kura = Kura::blank_kura_for_testing();
let query_handle = LiveQueryStore::test().start();
let mut wsv = WorldStateView::new(world_with_test_account(&authority), kura, query_handle);
let query_hex = encode_hex(SmartContractQueryRequest::query(
QueryBox::from(FindAccountById::new(authority.clone())),
Sorting::default(),
Pagination::default(),
FetchSize::default(),
));
let query_hex = encode_hex(SmartContractQueryRequest(QueryRequest::Query(
QueryWithParameters::new(
FindAccountById::new(authority.clone()).into(),
Sorting::default(),
Pagination::default(),
FetchSize::default(),
),
)));

let wat = format!(
r#"
Expand Down
46 changes: 3 additions & 43 deletions data_model/src/isi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub mod model {
/// Note that [`InstructionBox`] is not a self-sufficient instruction,
/// but just a wrapper to pass instructions back and forth.
/// If you are a client SDK user then you likely don't need to use this type directly.
///
/// The order of [`Self`] variants is guaranteed to correspond to the order of [`InstructionType`] variants
#[derive(
DebugCustom,
Display,
Expand Down Expand Up @@ -1690,15 +1692,14 @@ pub mod error {
asset::AssetValueType,
metadata,
query::error::{FindError, QueryExecutionFail},
IdBox, Value,
IdBox,
};

#[model]
pub mod model {
use serde::{Deserialize, Serialize};

use super::*;
use crate::asset::AssetDefinitionId;

/// Instruction execution error type
#[derive(
Expand All @@ -1721,8 +1722,6 @@ pub mod error {
// TODO: Only temporarily opaque because of InstructionExecutionError::Repetition
#[ffi_type(opaque)]
pub enum InstructionExecutionError {
/// Instruction does not adhere to Iroha DSL specification
Evaluate(#[cfg_attr(feature = "std", source)] InstructionEvaluationError),
/// Query failed
Query(#[cfg_attr(feature = "std", source)] QueryExecutionFail),
/// Conversion Error: {0}
Expand Down Expand Up @@ -1759,34 +1758,6 @@ pub mod error {
),
}

/// Evaluation error. This error indicates instruction is not a valid Iroha DSL
#[derive(
Debug,
displaydoc::Display,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
FromVariant,
Deserialize,
Serialize,
Decode,
Encode,
IntoSchema,
)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
// TODO: Only temporarily opaque because of problems with FFI
#[ffi_type(opaque)]
pub enum InstructionEvaluationError {
/// Unsupported parameter type for instruction of type `{0}`
Unsupported(InstructionType),
/// Failed to find parameter in a permission: {0}
PermissionParameter(String),
/// Incorrect value type
Type(#[cfg_attr(feature = "std", source)] TypeError),
}

/// Generic structure used to represent a mismatch
#[derive(
Debug,
Expand Down Expand Up @@ -1833,12 +1804,6 @@ pub mod error {
pub enum TypeError {
/// Asset Ids correspond to assets with different underlying types, {0}
AssetValueType(#[cfg_attr(feature = "std", source)] Mismatch<AssetValueType>),
/// Value passed to the parameter doesn't have the right type, {0}
ParameterValueType(#[cfg_attr(feature = "std", source)] Box<Mismatch<Value>>),
/// AssetDefinition Ids don't match, {0}
AssetDefinitionId(
#[cfg_attr(feature = "std", source)] Box<Mismatch<AssetDefinitionId>>,
),
}

/// Math error, which occurs during instruction execution
Expand Down Expand Up @@ -1959,11 +1924,6 @@ pub mod error {
}
}

impl From<TypeError> for InstructionExecutionError {
fn from(err: TypeError) -> Self {
Self::Evaluate(InstructionEvaluationError::Type(err))
}
}
impl From<FixedPointOperationError> for MathError {
fn from(err: FixedPointOperationError) -> Self {
match err {
Expand Down
Loading

0 comments on commit c43a409

Please sign in to comment.