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ć <marin.versic101@gmail.com>
  • Loading branch information
mversic committed Feb 26, 2024
1 parent 46208d0 commit 2b2c6d6
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 360 deletions.
18 changes: 9 additions & 9 deletions client_cli/pytests/test/domains/test_register_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,24 @@ def test_register_one_letter_domain(GIVEN_random_character):
iroha.should(have.domain(GIVEN_random_character))


@allure.label("sdk_test_id", "register_max_lenght_domain")
def test_register_max_lenght_domain(GIVEN_128_lenght_name):
@allure.label("sdk_test_id", "register_max_length_domain")
def test_register_max_length_domain(GIVEN_128_length_name):
with allure.step(
f'WHEN client_cli registers the longest domain "{GIVEN_128_lenght_name}"'
f'WHEN client_cli registers the longest domain "{GIVEN_128_length_name}"'
):
client_cli.register().domain(GIVEN_128_lenght_name)
client_cli.register().domain(GIVEN_128_length_name)
with allure.step(
f'THEN Iroha should have the longest domain "{GIVEN_128_lenght_name}"'
f'THEN Iroha should have the longest domain "{GIVEN_128_length_name}"'
):
iroha.should(have.domain(GIVEN_128_lenght_name))
iroha.should(have.domain(GIVEN_128_length_name))


@allure.label("sdk_test_id", "register_domain_with_too_long_name")
def test_register_domain_with_too_long_name(GIVEN_129_lenght_name):
def test_register_domain_with_too_long_name(GIVEN_129_length_name):
with allure.step(
f'WHEN client_cli registers the domain "{GIVEN_129_lenght_name}" with too long name'
f'WHEN client_cli registers the domain "{GIVEN_129_length_name}" with too long name'
):
client_cli.register().domain(GIVEN_129_lenght_name)
client_cli.register().domain(GIVEN_129_length_name)
with allure.step(
f'THEN client_cli should have the too long domain error: "{Stderr.TOO_LONG}"'
):
Expand Down
16 changes: 8 additions & 8 deletions config/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,20 @@ fn minimal_config_snapshot() -> Result<()> {
max_wasm_size_bytes: 4194304,
},
asset_metadata_limits: Limits {
max_len: 1048576,
max_entry_byte_size: 4096,
capacity: 1048576,
max_entry_len: 4096,
},
asset_definition_metadata_limits: Limits {
max_len: 1048576,
max_entry_byte_size: 4096,
capacity: 1048576,
max_entry_len: 4096,
},
account_metadata_limits: Limits {
max_len: 1048576,
max_entry_byte_size: 4096,
capacity: 1048576,
max_entry_len: 4096,
},
domain_metadata_limits: Limits {
max_len: 1048576,
max_entry_byte_size: 4096,
capacity: 1048576,
max_entry_len: 4096,
},
ident_length_limits: LengthLimits {
min: 1,
Expand Down
8 changes: 4 additions & 4 deletions config/tests/fixtures/full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ max_transactions_in_block = 512
block_time = 2_000
commit_time = 4_000
transaction_limits = {max_instruction_number = 4096, max_wasm_size_bytes = 4194304 }
asset_metadata_limits = { max_len = 1048576, max_entry_byte_size = 4096 }
asset_definition_metadata_limits = { max_len = 1048576, max_entry_byte_size = 4096 }
account_metadata_limits = { max_len = 1048576, max_entry_byte_size = 4096 }
domain_metadata_limits = { max_len = 1048576, max_entry_byte_size = 4096 }
asset_metadata_limits = { capacity = 1048576, max_entry_len = 4096 }
asset_definition_metadata_limits = { capacity = 1048576, max_entry_len = 4096 }
account_metadata_limits = { capacity = 1048576, max_entry_len = 4096 }
domain_metadata_limits = { capacity = 1048576, max_entry_len = 4096 }
ident_length_limits = { min = 1, max = 128 }
wasm_fuel_limit = 55000000
wasm_max_memory = 524288000
Binary file modified configs/swarm/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 @@ -13,17 +13,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 as WasmtimeConfig, Engine, Linker, Module, Store, StoreLimits,
StoreLimitsBuilder, TypedFunc,
Expand Down Expand Up @@ -74,7 +72,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 @@ -243,6 +241,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 @@ -1751,12 +1754,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
2 changes: 1 addition & 1 deletion data_model/derive/src/enum_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl ToTokens for EnumRef {

quote! {
#attrs
pub(super) enum #ident<'a> #impl_generics #where_clause {
pub(crate) enum #ident<'a> #impl_generics #where_clause {
#(#variants),*
}
}
Expand Down
20 changes: 11 additions & 9 deletions data_model/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,34 @@ use proc_macro2::TokenStream;
///
/// # Example
///
/// ```rust
/// ```
/// use iroha_data_model_derive::EnumRef;
/// use parity_scale_codec::Encode;
///
/// #[derive(EnumRef)]
/// enum InnerEnum {
/// #[enum_ref(derive(Encode))]
/// pub enum InnerEnum {
/// A(u32),
/// B(i32)
/// }
///
/// #[derive(EnumRef)]
/// #[enum_ref(derive(Encode))]
/// enum OuterEnum {
/// pub enum OuterEnum {
/// A(String),
/// #[enum_ref(transparent)]
/// B(InnerEnum),
/// }
///
/// /* will produce:
///
/// enum InnerEnumRef<'a> {
/// #[derive(Encode)]
/// pub(crate) enum InnerEnumRef<'a> {
/// A(&'a u32),
/// B(&'a i32),
/// }
///
/// enum OuterEnumRef<'a> {
/// #[derive(Encode)]
/// pub(crate) enum OuterEnumRef<'a> {
/// A(&'a String),
/// B(InnerEnumRef<'a>),
/// }
Expand Down Expand Up @@ -67,7 +69,7 @@ pub fn enum_ref(input: TokenStream) -> Result<TokenStream> {
///
/// # Example
///
/// ```rust
/// ```
/// use iroha_data_model_derive::model;
///
/// #[model]
Expand Down Expand Up @@ -170,7 +172,7 @@ pub fn model_single(input: TokenStream) -> TokenStream {
///
/// The common use-case:
///
/// ```rust
/// ```
/// use iroha_data_model_derive::IdEqOrdHash;
/// use iroha_data_model::{Identifiable, IdBox};
///
Expand Down Expand Up @@ -230,7 +232,7 @@ pub fn model_single(input: TokenStream) -> TokenStream {
///
/// Manual selection of the identifier field:
///
/// ```rust
/// ```
/// use iroha_data_model_derive::IdEqOrdHash;
/// use iroha_data_model::{Identifiable, IdBox};
///
Expand Down
5 changes: 3 additions & 2 deletions data_model/src/isi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,15 +1359,15 @@ 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;
use crate::{asset::AssetDefinitionId, Value};

/// Instruction execution error type
#[derive(
Expand Down Expand Up @@ -1633,6 +1633,7 @@ pub mod error {
Self::Evaluate(InstructionEvaluationError::Type(err))
}
}

impl From<FixedPointOperationError> for MathError {
fn from(err: FixedPointOperationError) -> Self {
match err {
Expand Down
4 changes: 2 additions & 2 deletions data_model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,11 @@ pub mod parameter {
})?;
let lower = lower.parse::<u32>().map_err(|_| ParseError {
reason:
"Failed to parse the `val` part of the `Parameter` as `MetadataLimits`. Invalid `u32` in `max_len` field.",
"Failed to parse the `val` part of the `Parameter` as `MetadataLimits`. Invalid `u32` in `capacity` field.",
})?;
let upper = upper.parse::<u32>().map_err(|_| ParseError {
reason:
"Failed to parse the `val` part of the `Parameter` as `MetadataLimits`. Invalid `u32` in `max_entry_byte_size` field.",
"Failed to parse the `val` part of the `Parameter` as `MetadataLimits`. Invalid `u32` in `max_entry_len` field.",
})?;
Value::MetadataLimits(metadata::Limits::new(lower, upper))
}
Expand Down
30 changes: 15 additions & 15 deletions data_model/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ pub mod model {
Serialize,
IntoSchema,
)]
#[display(fmt = "{max_len},{max_entry_byte_size}_ML")]
#[display(fmt = "{capacity},{max_entry_len}_ML")]
#[ffi_type]
pub struct Limits {
/// Maximum number of entries
pub max_len: u32,
pub capacity: u32,
/// Maximum length of entry
pub max_entry_byte_size: u32,
pub max_entry_len: u32,
}

/// Collection of parameters by their names with checked insertion.
Expand Down Expand Up @@ -88,12 +88,12 @@ pub mod model {
)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum MetadataError {
/// Path specification empty
EmptyPath,
/// Metadata entry is too big
EntryTooBig(#[cfg_attr(feature = "std", source)] SizeError),
/// Metadata exceeds overall length limit
OverallSize(#[cfg_attr(feature = "std", source)] SizeError),
/// Path specification empty
EmptyPath,
MaxCapacity(#[cfg_attr(feature = "std", source)] SizeError),
/// `{0}`: path segment not found, i.e. nothing was found at that key
MissingSegment(Name),
/// `{0}`: path segment not an instance of metadata
Expand Down Expand Up @@ -127,10 +127,10 @@ pub struct SizeError {

impl Limits {
/// Constructor.
pub const fn new(max_len: u32, max_entry_byte_size: u32) -> Limits {
pub const fn new(capacity: u32, max_entry_len: u32) -> Limits {
Limits {
max_len,
max_entry_byte_size,
capacity,
max_entry_len,
}
}
}
Expand Down Expand Up @@ -207,8 +207,8 @@ impl Metadata {
value: Value,
limits: Limits,
) -> Result<Option<Value>, MetadataError> {
if self.0.len() >= limits.max_len as usize {
return Err(MetadataError::OverallSize(SizeError {
if self.0.len() >= limits.capacity as usize {
return Err(MetadataError::MaxCapacity(SizeError {
limits,
actual: self.len_u64(),
}));
Expand All @@ -232,15 +232,15 @@ impl Metadata {
/// if the value was already present, `None` otherwise.
///
/// # Errors
/// Fails if `max_entry_byte_size` or `max_len` from `limits` are exceeded.
/// Fails if `max_entry_len` or `capacity` from `limits` are exceeded.
pub fn insert_with_limits(
&mut self,
key: Name,
value: Value,
limits: Limits,
) -> Result<Option<Value>, MetadataError> {
if self.0.len() >= limits.max_len as usize && !self.0.contains_key(&key) {
return Err(MetadataError::OverallSize(SizeError {
if self.0.len() >= limits.capacity as usize && !self.0.contains_key(&key) {
return Err(MetadataError::MaxCapacity(SizeError {
limits,
actual: self.len_u64(),
}));
Expand Down Expand Up @@ -283,7 +283,7 @@ impl Metadata {
fn check_size_limits(key: &Name, value: Value, limits: Limits) -> Result<(), MetadataError> {
let entry_bytes: Vec<u8> = (key, value).encode();
let byte_size = entry_bytes.len();
if byte_size > limits.max_entry_byte_size as usize {
if byte_size > limits.max_entry_len as usize {
return Err(MetadataError::EntryTooBig(SizeError {
limits,
actual: byte_size
Expand Down
Loading

0 comments on commit 2b2c6d6

Please sign in to comment.