From d35a82634acf8b3d5b2bd17a83ce0496e873846c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Ver=C5=A1i=C4=87?= Date: Sat, 2 Nov 2024 21:25:46 +0100 Subject: [PATCH] chore: remove call to `set_getrandom` and make `dbg` a macro in smart contracts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marin Veršić --- Cargo.lock | 2 +- crates/iroha_core/src/smartcontracts/wasm.rs | 5 +- crates/iroha_executor/src/default.rs | 4 +- crates/iroha_executor/src/lib.rs | 97 ++++++++++--------- crates/iroha_executor/src/permission.rs | 3 +- crates/iroha_executor_derive/src/default.rs | 6 +- .../iroha_executor_derive/src/entrypoint.rs | 28 ++---- crates/iroha_executor_derive/src/lib.rs | 34 ++++++- crates/iroha_smart_contract/Cargo.toml | 5 - crates/iroha_smart_contract/src/lib.rs | 70 ++++--------- .../src/entrypoint.rs | 2 + crates/iroha_smart_contract_utils/Cargo.toml | 3 +- .../src/{debug.rs => dbg.rs} | 84 ++++++++++++---- crates/iroha_smart_contract_utils/src/lib.rs | 50 +++++++--- crates/iroha_smart_contract_utils/src/log.rs | 5 +- crates/iroha_trigger/src/lib.rs | 23 +++-- crates/iroha_trigger_derive/src/entrypoint.rs | 4 +- .../src/complex_isi.rs | 1 + wasm/Cargo.toml | 1 - wasm/libs/default_executor/Cargo.toml | 1 - wasm/libs/default_executor/src/lib.rs | 10 +- wasm/libs/multisig_accounts/Cargo.toml | 1 - wasm/libs/multisig_accounts/src/lib.rs | 9 +- wasm/libs/multisig_domains/Cargo.toml | 1 - wasm/libs/multisig_domains/src/lib.rs | 13 +-- wasm/libs/multisig_transactions/Cargo.toml | 1 - wasm/libs/multisig_transactions/src/lib.rs | 9 +- .../Cargo.toml | 1 - .../src/lib.rs | 6 +- .../Cargo.toml | 1 - .../src/lib.rs | 4 +- .../Cargo.toml | 1 - .../src/lib.rs | 6 +- .../executor_remove_permission/Cargo.toml | 1 - .../executor_remove_permission/src/lib.rs | 4 +- wasm/samples/executor_with_admin/Cargo.toml | 1 - wasm/samples/executor_with_admin/src/lib.rs | 4 +- .../executor_with_custom_parameter/Cargo.toml | 1 - .../executor_with_custom_parameter/src/lib.rs | 4 +- .../Cargo.toml | 1 - .../src/lib.rs | 4 +- .../executor_with_migration_fail/Cargo.toml | 1 - .../executor_with_migration_fail/src/lib.rs | 8 +- wasm/samples/mint_rose_trigger/Cargo.toml | 1 - wasm/samples/mint_rose_trigger/src/lib.rs | 9 +- .../query_assets_and_save_cursor/Cargo.toml | 1 - .../query_assets_and_save_cursor/src/lib.rs | 3 - .../Cargo.toml | 1 - .../src/lib.rs | 2 - 49 files changed, 273 insertions(+), 264 deletions(-) rename crates/iroha_smart_contract_utils/src/{debug.rs => dbg.rs} (68%) diff --git a/Cargo.lock b/Cargo.lock index 1eea68b0d8a..f273833c325 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3494,7 +3494,6 @@ version = "2.0.0-rc.1.0" dependencies = [ "derive_more", "displaydoc", - "getrandom", "iroha_data_model", "iroha_macro", "iroha_smart_contract_derive", @@ -3520,6 +3519,7 @@ name = "iroha_smart_contract_utils" version = "2.0.0-rc.1.0" dependencies = [ "cfg-if", + "getrandom", "iroha_data_model", "parity-scale-codec", "webassembly-test", diff --git a/crates/iroha_core/src/smartcontracts/wasm.rs b/crates/iroha_core/src/smartcontracts/wasm.rs index 450621f5fdc..899409b330b 100644 --- a/crates/iroha_core/src/smartcontracts/wasm.rs +++ b/crates/iroha_core/src/smartcontracts/wasm.rs @@ -688,8 +688,7 @@ impl Runtime { /// /// # Warning /// - /// This function doesn't take ownership of the provided - /// allocation + /// This function doesn't take ownership of the provided allocation /// /// # Errors /// @@ -697,7 +696,7 @@ impl Runtime { #[allow(clippy::needless_pass_by_value)] #[codec::wrap(state = "S")] fn dbg(msg: String) { - println!("{msg}"); + eprintln!("{msg}"); } } diff --git a/crates/iroha_executor/src/default.rs b/crates/iroha_executor/src/default.rs index d3a3fa20029..19fe09bb666 100644 --- a/crates/iroha_executor/src/default.rs +++ b/crates/iroha_executor/src/default.rs @@ -1222,7 +1222,7 @@ pub mod role { } fn find_account_roles(account_id: AccountId, host: &Iroha) -> impl Iterator { - use iroha_smart_contract::debug::DebugExpectExt as _; + use iroha_smart_contract::DebugExpectExt as _; host.query(FindRolesByAccountId::new(account_id)) .execute() @@ -1273,7 +1273,7 @@ pub mod role { } for permission in role.inner().permissions() { - iroha_smart_contract::debug!(&format!("Checking `{permission:?}`")); + iroha_smart_contract::log::debug!(&format!("Checking `{permission:?}`")); if let Ok(any_permission) = AnyPermission::try_from(permission) { if !executor.context().curr_block.is_genesis() { diff --git a/crates/iroha_executor/src/lib.rs b/crates/iroha_executor/src/lib.rs index a35f01f0ec2..098d64b4760 100644 --- a/crates/iroha_executor/src/lib.rs +++ b/crates/iroha_executor/src/lib.rs @@ -10,56 +10,65 @@ use data_model::{executor::Result, ValidationFail}; #[cfg(not(test))] use data_model::{prelude::*, query::AnyQueryBox, smart_contract::payloads}; use iroha_executor_data_model::{parameter::Parameter, permission::Permission}; +pub use iroha_executor_derive::{entrypoint, migrate}; use iroha_schema::{Ident, MetaMap}; pub use iroha_smart_contract as smart_contract; -pub use iroha_smart_contract_utils::{debug, encode_with_length_prefix}; -#[cfg(not(test))] -use iroha_smart_contract_utils::{decode_with_length_prefix_from_raw, encode_and_execute}; -pub use smart_contract::{data_model, stub_getrandom, Iroha}; +pub use iroha_smart_contract_utils::{dbg, dbg_panic, DebugExpectExt, DebugUnwrapExt}; +pub use smart_contract::{data_model, Iroha}; pub mod default; pub mod permission; -pub mod utils { - //! Crate with utilities for implementing smart contract FFI - pub use iroha_smart_contract_utils::encode_with_length_prefix; -} - pub mod log { //! WASM logging utilities - pub use iroha_smart_contract_utils::{debug, error, event, info, log::*, trace, warn}; + pub use iroha_smart_contract_utils::{debug, error, event, info, trace, warn}; } -/// Get context for `validate_transaction()` entrypoint. -#[cfg(not(test))] -pub fn decode_execute_transaction_context( - context: *const u8, -) -> payloads::Validate { - // Safety: ownership of the provided context is transferred into `_decode_from_raw` - unsafe { decode_with_length_prefix_from_raw(context) } -} +#[doc(hidden)] +pub mod utils { + //! Crate with utilities -/// Get context for `validate_instruction()` entrypoint. -#[cfg(not(test))] -pub fn decode_execute_instruction_context( - context: *const u8, -) -> payloads::Validate { - // Safety: ownership of the provided context is transferred into `_decode_from_raw` - unsafe { decode_with_length_prefix_from_raw(context) } -} + #[cfg(not(test))] + use iroha_smart_contract_utils::decode_with_length_prefix_from_raw; + pub use iroha_smart_contract_utils::{ + encode_with_length_prefix, register_getrandom_err_callback, + }; -/// Get context for `validate_query()` entrypoint. -#[cfg(not(test))] -pub fn decode_validate_query_context(context: *const u8) -> payloads::Validate { - // Safety: ownership of the provided context is transferred into `_decode_from_raw` - unsafe { decode_with_length_prefix_from_raw(context) } -} + #[cfg(not(test))] + use super::*; -/// Get context for `migrate()` entrypoint. -#[cfg(not(test))] -pub fn decode_migrate_context(context: *const u8) -> payloads::ExecutorContext { - // Safety: ownership of the provided context is transferred into `_decode_from_raw` - unsafe { decode_with_length_prefix_from_raw(context) } + /// Get context for `validate_transaction()` entrypoint. + #[cfg(not(test))] + pub fn decode_execute_transaction_context( + context: *const u8, + ) -> payloads::Validate { + // Safety: ownership of the provided context is transferred into `_decode_from_raw` + + unsafe { decode_with_length_prefix_from_raw(context) } + } + + /// Get context for `validate_instruction()` entrypoint. + #[cfg(not(test))] + pub fn decode_execute_instruction_context( + context: *const u8, + ) -> payloads::Validate { + // Safety: ownership of the provided context is transferred into `_decode_from_raw` + unsafe { decode_with_length_prefix_from_raw(context) } + } + + /// Get context for `validate_query()` entrypoint. + #[cfg(not(test))] + pub fn decode_validate_query_context(context: *const u8) -> payloads::Validate { + // Safety: ownership of the provided context is transferred into `_decode_from_raw` + unsafe { decode_with_length_prefix_from_raw(context) } + } + + /// Get context for `migrate()` entrypoint. + #[cfg(not(test))] + pub fn decode_migrate_context(context: *const u8) -> payloads::ExecutorContext { + // Safety: ownership of the provided context is transferred into `_decode_from_raw` + unsafe { decode_with_length_prefix_from_raw(context) } + } } /// Set new [`ExecutorDataModel`]. @@ -75,7 +84,7 @@ pub fn decode_migrate_context(context: *const u8) -> payloads::ExecutorContext { #[cfg(not(test))] pub fn set_data_model(data_model: &ExecutorDataModel) { // Safety: - ownership of the returned result is transferred into `_decode_from_raw` - unsafe { encode_and_execute(&data_model, host::set_data_model) } + unsafe { iroha_smart_contract_utils::encode_and_execute(&data_model, host::set_data_model) } } #[cfg(not(test))] @@ -277,14 +286,14 @@ pub mod prelude { pub use alloc::vec::Vec; - pub use iroha_executor_derive::{entrypoint, Entrypoints, Execute, Visit}; - pub use iroha_smart_contract::prelude::*; + pub use iroha_executor_derive::{Entrypoints, Execute, Visit}; - pub use super::{ + pub use crate::{ data_model::{ - executor::Result, smart_contract::payloads::ExecutorContext as Context, visit::Visit, - ValidationFail, + executor::Result, prelude::*, smart_contract::payloads::ExecutorContext as Context, + visit::Visit, }, - deny, execute, DataModelBuilder, Execute, + dbg, dbg_panic, deny, execute, DataModelBuilder, DebugExpectExt, DebugUnwrapExt, Execute, + Iroha, }; } diff --git a/crates/iroha_executor/src/permission.rs b/crates/iroha_executor/src/permission.rs index ed65e8185a0..f39cc199d71 100644 --- a/crates/iroha_executor/src/permission.rs +++ b/crates/iroha_executor/src/permission.rs @@ -8,8 +8,7 @@ use crate::{ prelude::Context, smart_contract::{ data_model::{executor::Result, permission::Permission as PermissionObject, prelude::*}, - debug::DebugExpectExt as _, - Iroha, + prelude::*, }, }; diff --git a/crates/iroha_executor_derive/src/default.rs b/crates/iroha_executor_derive/src/default.rs index c5def5c00b6..ca9e0abc80a 100644 --- a/crates/iroha_executor_derive/src/default.rs +++ b/crates/iroha_executor_derive/src/default.rs @@ -51,7 +51,7 @@ pub fn impl_derive_entrypoints(emitter: &mut Emitter, input: &syn::DeriveInput) let mut entrypoint_fns: Vec = vec![ parse_quote! { - #[::iroha_executor::prelude::entrypoint] + #[::iroha_executor::entrypoint] pub fn execute_transaction( transaction: ::iroha_executor::prelude::SignedTransaction, host: ::iroha_executor::prelude::Iroha, @@ -64,7 +64,7 @@ pub fn impl_derive_entrypoints(emitter: &mut Emitter, input: &syn::DeriveInput) } }, parse_quote! { - #[::iroha_executor::prelude::entrypoint] + #[::iroha_executor::entrypoint] pub fn execute_instruction( instruction: ::iroha_executor::prelude::InstructionBox, host: ::iroha_executor::prelude::Iroha, @@ -77,7 +77,7 @@ pub fn impl_derive_entrypoints(emitter: &mut Emitter, input: &syn::DeriveInput) } }, parse_quote! { - #[::iroha_executor::prelude::entrypoint] + #[::iroha_executor::entrypoint] pub fn validate_query( query: ::iroha_executor::data_model::query::AnyQueryBox, host: ::iroha_executor::prelude::Iroha, diff --git a/crates/iroha_executor_derive/src/entrypoint.rs b/crates/iroha_executor_derive/src/entrypoint.rs index 2527b8b20be..5f75d2f789e 100644 --- a/crates/iroha_executor_derive/src/entrypoint.rs +++ b/crates/iroha_executor_derive/src/entrypoint.rs @@ -20,33 +20,26 @@ mod import { /// [`executor_entrypoint`](crate::executor_entrypoint()) macro implementation #[allow(clippy::needless_pass_by_value)] -pub fn impl_entrypoint(emitter: &mut Emitter, item: syn::ItemFn) -> TokenStream { +pub fn impl_validate_entrypoint(emitter: &mut Emitter, item: syn::ItemFn) -> TokenStream { macro_rules! match_entrypoints { (validate: { $($user_entrypoint_name:ident => $generated_entrypoint_name:ident ($decode_validation_context_fn_name:ident)),* $(,)? - } - other: { - $($other_user_entrypoint_name:ident => $branch:block),* $(,)? }) => { match &item.sig.ident { $(fn_name if fn_name == stringify!($user_entrypoint_name) => { - impl_validate_entrypoint( + impl_validate_entrypoint_priv( item, stringify!($user_entrypoint_name), export::$generated_entrypoint_name, import::$decode_validation_context_fn_name, ) })* - $(fn_name if fn_name == stringify!($other_user_entrypoint_name) => $branch),* _ => { emit!( emitter, "Executor entrypoint name must be one of: {:?}", - [ - $(stringify!($user_entrypoint_name),)* - $(stringify!($other_user_entrypoint_name),)* - ] + [$(stringify!($user_entrypoint_name),)*] ); return quote!(); }, @@ -60,13 +53,10 @@ pub fn impl_entrypoint(emitter: &mut Emitter, item: syn::ItemFn) -> TokenStream execute_instruction => EXECUTOR_EXECUTE_INSTRUCTION(DECODE_EXECUTE_INSTRUCTION_CONTEXT), validate_query => EXECUTOR_VALIDATE_QUERY(DECODE_VALIDATE_QUERY_CONTEXT), } - other: { - migrate => { impl_migrate_entrypoint(item) } - } } } -fn impl_validate_entrypoint( +fn impl_validate_entrypoint_priv( fn_item: syn::ItemFn, user_entrypoint_name: &'static str, generated_entrypoint_name: &'static str, @@ -103,9 +93,9 @@ fn impl_validate_entrypoint( #[no_mangle] #[doc(hidden)] unsafe extern "C" fn #generated_entrypoint_ident(context: *const u8) -> *const u8 { - let host = ::iroha_executor::smart_contract::Iroha; + let host = ::iroha_executor::Iroha; - let context = ::iroha_executor::#decode_validation_context_fn_ident(context); + let context = ::iroha_executor::utils::#decode_validation_context_fn_ident(context); let verdict = #fn_name(context.target, host, context.context); let bytes_box = ::core::mem::ManuallyDrop::new( @@ -124,7 +114,7 @@ fn impl_validate_entrypoint( } } -fn impl_migrate_entrypoint(fn_item: syn::ItemFn) -> TokenStream { +pub fn impl_migrate_entrypoint(fn_item: syn::ItemFn) -> TokenStream { let syn::ItemFn { attrs, vis, @@ -139,6 +129,8 @@ fn impl_migrate_entrypoint(fn_item: syn::ItemFn) -> TokenStream { ); quote! { + iroha_executor::utils::register_getrandom_err_callback!(); + /// Executor `migrate` entrypoint /// /// # Memory safety @@ -148,7 +140,7 @@ fn impl_migrate_entrypoint(fn_item: syn::ItemFn) -> TokenStream { #[doc(hidden)] unsafe extern "C" fn #migrate_fn_name(context: *const u8) { let host = ::iroha_executor::smart_contract::Iroha; - let context = ::iroha_executor::decode_migrate_context(context); + let context = ::iroha_executor::utils::decode_migrate_context(context); #fn_name(host, context); } diff --git a/crates/iroha_executor_derive/src/lib.rs b/crates/iroha_executor_derive/src/lib.rs index e89b95f2817..b5d401e05dc 100644 --- a/crates/iroha_executor_derive/src/lib.rs +++ b/crates/iroha_executor_derive/src/lib.rs @@ -16,10 +16,40 @@ mod entrypoint; /// ```ignore /// use iroha_executor::prelude::*; /// -/// #[entrypoint] +/// #[migrate] /// fn migrate(host: Iroha, context: Context) { /// todo!() /// } +/// ``` +#[manyhow] +#[proc_macro_attribute] +pub fn migrate(attr: TokenStream, item: TokenStream) -> TokenStream { + let mut emitter = Emitter::new(); + + if !attr.is_empty() { + emit!( + emitter, + "`#[migrate]` macro for Executor accepts no attributes" + ); + } + + let Some(item) = emitter.handle(syn::parse2(item)) else { + return emitter.finish_token_stream(); + }; + + let result = entrypoint::impl_migrate_entrypoint(item); + + emitter.finish_token_stream_with(result) +} + +/// Annotate the user-defined function that starts the execution of a executor. +/// +/// There are 4 acceptable forms of this macro usage. See examples. +/// +/// # Examples +/// +/// ```ignore +/// use iroha_executor::prelude::*; /// /// #[entrypoint] /// fn execute_transaction(transaction: SignedTransaction, host: Iroha, context: Context) -> Result { @@ -52,7 +82,7 @@ pub fn entrypoint(attr: TokenStream, item: TokenStream) -> TokenStream { return emitter.finish_token_stream(); }; - let result = entrypoint::impl_entrypoint(&mut emitter, item); + let result = entrypoint::impl_validate_entrypoint(&mut emitter, item); emitter.finish_token_stream_with(result) } diff --git a/crates/iroha_smart_contract/Cargo.toml b/crates/iroha_smart_contract/Cargo.toml index 7aaf2b04d6b..684bd81c806 100644 --- a/crates/iroha_smart_contract/Cargo.toml +++ b/crates/iroha_smart_contract/Cargo.toml @@ -26,11 +26,6 @@ derive_more.workspace = true displaydoc.workspace = true -getrandom = "0.2" - [dev-dependencies] webassembly-test = "0.1.0" -# Not used directly but required for compilation -getrandom = { version = "0.2", features = ["custom"] } - trybuild = { workspace = true } diff --git a/crates/iroha_smart_contract/src/lib.rs b/crates/iroha_smart_contract/src/lib.rs index 08131cde857..cf3de235de3 100644 --- a/crates/iroha_smart_contract/src/lib.rs +++ b/crates/iroha_smart_contract/src/lib.rs @@ -4,7 +4,7 @@ extern crate alloc; -use alloc::boxed::Box; +use alloc::{boxed::Box, format}; use core::fmt::Debug; use data_model::{ @@ -20,13 +20,22 @@ use iroha_data_model::query::{ SingularQueryBox, SingularQueryOutputBox, }; pub use iroha_smart_contract_derive::main; -pub use iroha_smart_contract_utils::{debug, error, info, log, warn}; -use iroha_smart_contract_utils::{ - debug::{dbg_panic, DebugExpectExt as _}, - decode_with_length_prefix_from_raw, encode_and_execute, -}; +pub use iroha_smart_contract_utils::{dbg, dbg_panic, DebugExpectExt, DebugUnwrapExt}; +use iroha_smart_contract_utils::{decode_with_length_prefix_from_raw, encode_and_execute}; use parity_scale_codec::{Decode, Encode}; +#[doc(hidden)] +pub mod utils { + //! Crate with utilities + + pub use iroha_smart_contract_utils::register_getrandom_err_callback; +} + +pub mod log { + //! WASM logging utilities + pub use iroha_smart_contract_utils::{debug, error, event, info, trace, warn}; +} + /// An iterable query cursor for use in smart contracts. #[derive(Debug, Clone, Encode, Decode)] pub struct QueryCursor { @@ -130,7 +139,7 @@ impl QueryExecutor for Iroha { ) -> Result { let QueryResponse::Singular(output) = Self::execute_query(&QueryRequest::Singular(query))? else { - dbg_panic("BUG: iroha returned unexpected type in singular query"); + dbg_panic!("BUG: iroha returned unexpected type in singular query"); }; Ok(output) @@ -142,7 +151,7 @@ impl QueryExecutor for Iroha { ) -> Result<(QueryOutputBatchBox, u64, Option), Self::Error> { let QueryResponse::Iterable(output) = Self::execute_query(&QueryRequest::Start(query))? else { - dbg_panic("BUG: iroha returned unexpected type in iterable query"); + dbg_panic!("BUG: iroha returned unexpected type in iterable query"); }; let (batch, remaining_items, cursor) = output.into_parts(); @@ -160,7 +169,7 @@ impl QueryExecutor for Iroha { let QueryResponse::Iterable(output) = Self::execute_query(&QueryRequest::Continue(cursor.cursor))? else { - dbg_panic("BUG: iroha returned unexpected type in iterable query"); + dbg_panic!("BUG: iroha returned unexpected type in iterable query"); }; let (batch, remaining_items, cursor) = output.into_parts(); @@ -176,7 +185,7 @@ impl QueryExecutor for Iroha { #[no_mangle] extern "C" fn _iroha_smart_contract_alloc(len: usize) -> *const u8 { if len == 0 { - iroha_smart_contract_utils::debug::dbg_panic("Cannot allocate 0 bytes"); + dbg_panic!("Cannot allocate 0 bytes"); } let layout = core::alloc::Layout::array::(len).dbg_expect("Cannot allocate layout"); // Safety: safe because `layout` is guaranteed to have non-zero size @@ -191,40 +200,6 @@ unsafe extern "C" fn _iroha_smart_contract_dealloc(offset: *mut u8, len: usize) let _box = Box::from_raw(core::slice::from_raw_parts_mut(offset, len)); } -/// Stub for [`getrandom::getrandom()`] for Iroha smart contracts. -/// Prints a log message with [`error!`] and panics. -/// -/// Required in order to crates like `iroha_crypto` to compile. Should never be called. -/// -/// # Panics -/// -/// Always Panics with [`unimplemented!()`]; -/// -/// # Errors -/// -/// No errors, always panics. -/// -/// # Example -/// -/// ``` -/// // Cargo.toml -/// // getrandom = { version = "0.2", features = ["custom"] } -/// -/// getrandom::register_custom_getrandom!(iroha_smart_contract::stub_getrandom); -/// ``` -pub fn stub_getrandom(_dest: &mut [u8]) -> Result<(), getrandom::Error> { - const ERROR_MESSAGE: &str = - "`getrandom()` is not implemented. To provide your custom function \ - see https://docs.rs/getrandom/latest/getrandom/macro.register_custom_getrandom.html. \ - Be aware that your function must give the same result on different peers at the same execution round, - and keep in mind the consequences of purely implemented random function."; - - // we don't support logging in our current wasm test runner implementation - #[cfg(not(test))] - error!(ERROR_MESSAGE); - unimplemented!("{ERROR_MESSAGE}") -} - /// Get context for smart contract `main()` entrypoint. #[cfg(not(test))] pub fn get_smart_contract_context() -> data_model::smart_contract::payloads::SmartContractContext { @@ -264,12 +239,9 @@ mod host { /// Most used items pub mod prelude { - pub use iroha_smart_contract_derive::main; - pub use iroha_smart_contract_utils::debug::DebugUnwrapExt; - pub use crate::{ data_model::{prelude::*, smart_contract::payloads::SmartContractContext as Context}, - Iroha, + dbg, dbg_panic, DebugExpectExt, DebugUnwrapExt, Iroha, }; } @@ -283,8 +255,6 @@ mod tests { use super::*; - getrandom::register_custom_getrandom!(super::stub_getrandom); - const QUERY_RESULT: Result = Ok(numeric!(1234)); const ISI_RESULT: Result<(), ValidationFail> = Ok(()); diff --git a/crates/iroha_smart_contract_derive/src/entrypoint.rs b/crates/iroha_smart_contract_derive/src/entrypoint.rs index 4184c4b2c3f..76b75279f97 100644 --- a/crates/iroha_smart_contract_derive/src/entrypoint.rs +++ b/crates/iroha_smart_contract_derive/src/entrypoint.rs @@ -31,6 +31,8 @@ pub fn impl_entrypoint(emitter: &mut Emitter, item: syn::ItemFn) -> TokenStream let main_fn_name = syn::Ident::new(export::SMART_CONTRACT_MAIN, proc_macro2::Span::call_site()); quote! { + iroha_smart_contract::utils::register_getrandom_err_callback!(); + /// Smart contract entrypoint #[no_mangle] #[doc(hidden)] diff --git a/crates/iroha_smart_contract_utils/Cargo.toml b/crates/iroha_smart_contract_utils/Cargo.toml index 349839eea2d..4daa75c1bea 100644 --- a/crates/iroha_smart_contract_utils/Cargo.toml +++ b/crates/iroha_smart_contract_utils/Cargo.toml @@ -14,10 +14,11 @@ workspace = true debug = [] [dependencies] -cfg-if.workspace = true iroha_data_model.workspace = true +getrandom = { version = "0.2", features = ["custom"] } parity-scale-codec.workspace = true +cfg-if.workspace = true [dev-dependencies] webassembly-test = "0.1.0" diff --git a/crates/iroha_smart_contract_utils/src/debug.rs b/crates/iroha_smart_contract_utils/src/dbg.rs similarity index 68% rename from crates/iroha_smart_contract_utils/src/debug.rs rename to crates/iroha_smart_contract_utils/src/dbg.rs index ef1780accd3..7418efa47ba 100644 --- a/crates/iroha_smart_contract_utils/src/debug.rs +++ b/crates/iroha_smart_contract_utils/src/dbg.rs @@ -9,7 +9,6 @@ use cfg_if::cfg_if; #[cfg(target_family = "wasm")] #[cfg(not(test))] mod host { - #[cfg(feature = "debug")] #[link(wasm_import_module = "iroha")] extern "C" { /// Prints string to the standard output by providing offset and length @@ -29,7 +28,7 @@ mod host { /// Does nothing unless `debug` feature is enabled. /// /// When running outside of wasm, always prints the output to stderr -#[allow(unused_variables)] +#[doc(hidden)] pub fn dbg(obj: &T) { cfg_if! { if #[cfg(not(target_family = "wasm"))] { @@ -44,20 +43,57 @@ pub fn dbg(obj: &T) { let s = format!("{obj:?}"); // Safety: `host_dbg` doesn't take ownership of it's pointer parameter - unsafe { crate::encode_and_execute(&s, host_dbg) } + unsafe { crate::encode_and_execute(&s, host_dbg); } } } } -/// Print `msg` and call [`panic!`]. +/// Print `obj` in debug representation. Does nothing unless `debug` feature is enabled. /// -/// Only call [`panic!`] if `debug` feature is not specified. +/// When running as a wasm smart contract, prints to host's stdout. +/// When running outside of wasm, always prints the output to stderr +#[macro_export] +macro_rules! dbg { + () => { + #[cfg(feature = "debug")] + $crate::debug::dbg(concat!("[{}:{}:{}]", core::file!(), core::line!(), core::column!())); + }; + ($val:expr $(,)?) => {{ + #[cfg(feature = "debug")] + match $val { + tmp => { + let location = concat!("[{}:{}:{}]", core::file!(), core::line!(), core::column!()); + let location = format!("{location} {} = {tmp:#?}", stringify!($val)); + $crate::dbg(&location); + } + } + }}; + ($($val:expr),+ $(,)?) => { + ($($crate::dbg!($val)),+,) + }; +} + +/// Print `obj` in debug representation. Does nothing unless `debug` feature is enabled. /// -/// # Panics -/// Always -pub fn dbg_panic(msg: &str) -> ! { - dbg(msg); - panic!("{msg}") +/// When running as a wasm smart contract, prints to host's stderr. +/// When running outside of wasm, always prints the output to stderr +#[macro_export] +macro_rules! dbg_panic { + () => { + dbg!(); + panic(); + }; + ($val:expr $(,)?) => {{ + match $val { + tmp => { + dbg!(tmp); + panic!("{tmp:?}"); + } + } + }}; + ($($val:expr),+ $(,)?) => { + ($($crate::dbg!($val)),+,) + }; } /// Extension implemented for `Result` and `Option` to provide unwrapping with error message, @@ -84,10 +120,10 @@ impl DebugUnwrapExt for Result { match self { Ok(out) => out, Err(err) => { - dbg(&format!( - "WASM execution panicked at `called Result::dbg_unwrap()` on an `Err` value: {err:?}", - )); - panic!(""); + let msg = format!("WASM execution panicked at `called Result::dbg_unwrap()` on an `Err` value: {err:?}"); + + dbg!(&msg); + panic!("{msg}"); } } } @@ -107,8 +143,10 @@ impl DebugUnwrapExt for Option { match self { Some(out) => out, None => { - dbg("WASM execution panicked at 'called `Option::dbg_unwrap()` on a `None` value'"); - panic!(""); + let msg = "WASM execution panicked at 'called `Option::dbg_unwrap()` on a `None` value'"; + + dbg!(msg); + panic!("{msg}"); } } } @@ -140,8 +178,10 @@ impl DebugExpectExt for Result { match self { Ok(out) => out, Err(err) => { - dbg(&format!("WASM execution panicked at `{msg}: {err:?}`",)); - panic!(""); + let msg = format!("WASM execution panicked at `{msg}: {err:?}`"); + + dbg!(&msg); + panic!("{msg}"); } } } @@ -161,8 +201,10 @@ impl DebugExpectExt for Option { match self { Some(out) => out, None => { - dbg(&format!("WASM execution panicked at `{msg}`",)); - panic!(""); + let msg = format!("WASM execution panicked at `{msg}`",); + + dbg!(&msg); + panic!("{msg}"); } } } @@ -190,6 +232,6 @@ mod tests { #[webassembly_test] fn dbg_call() { - super::dbg(get_dbg_message()); + dbg!(get_dbg_message()); } } diff --git a/crates/iroha_smart_contract_utils/src/lib.rs b/crates/iroha_smart_contract_utils/src/lib.rs index 971177c09fe..d5b266a7e4c 100644 --- a/crates/iroha_smart_contract_utils/src/lib.rs +++ b/crates/iroha_smart_contract_utils/src/lib.rs @@ -9,23 +9,45 @@ extern crate alloc; use alloc::{boxed::Box, format, vec::Vec}; use core::ops::RangeFrom; +pub use dbg::*; +pub use getrandom; pub use parity_scale_codec::{DecodeAll, Encode}; -pub mod debug; +mod dbg; pub mod log; -/// Decode the object from given pointer and length -/// -/// # Warning -/// -/// This method takes ownership of the given pointer -/// -/// # Safety -/// -/// It's safe to call this function as long as it's safe to construct, from the given -/// pointer, `Box<[u8]>` containing the encoded object -unsafe fn _decode_from_raw(ptr: *const u8, len: usize) -> T { - _decode_from_raw_in_range(ptr, len, 0..) +/// Registers a custom `getrandom` function that: +/// +/// 1. prints `error` message to the log +/// 2. panics with the same message +#[doc(hidden)] +#[macro_export] +macro_rules! register_getrandom_err_callback { + () => { + /// Prints a log message with [`error!`] and panics. + /// + /// # Panics + /// + /// Always Panics with [`unimplemented!()`]; + /// + /// # Errors + /// + /// No errors, always panics. + fn stub_getrandom(_dest: &mut [u8]) -> Result<(), $crate::getrandom::Error> { + const ERROR_MESSAGE: &str = + "`getrandom()` is not implemented. To provide your custom function \ + see https://docs.rs/getrandom/latest/getrandom/macro.register_custom_getrandom.html. \ + Be aware that your function must give the same result on different peers at the same execution round, + and keep in mind the consequences of purely implemented random function."; + + // we don't support logging in our current wasm test runner implementation + #[cfg(not(test))] + $crate::error!(ERROR_MESSAGE); + unimplemented!("{ERROR_MESSAGE}") + } + + $crate::getrandom::register_custom_getrandom!(stub_getrandom); + }; } /// Decode the object from given pointer and length in the given range @@ -66,6 +88,7 @@ unsafe fn _decode_from_raw_in_range( /// /// It's safe to call this function as long as it's safe to construct, from the given /// pointer, byte array of prefix length and `Box<[u8]>` containing the encoded object +#[doc(hidden)] pub unsafe fn decode_with_length_prefix_from_raw(ptr: *const u8) -> T { let len_size_bytes = core::mem::size_of::(); @@ -87,6 +110,7 @@ pub unsafe fn decode_with_length_prefix_from_raw(ptr: *const u8) - /// # Safety /// /// The given function must not take ownership of the pointer argument +#[doc(hidden)] pub unsafe fn encode_and_execute( obj: &T, fun: unsafe extern "C" fn(*const u8, usize) -> O, diff --git a/crates/iroha_smart_contract_utils/src/log.rs b/crates/iroha_smart_contract_utils/src/log.rs index d42255aee72..4acf81f975b 100644 --- a/crates/iroha_smart_contract_utils/src/log.rs +++ b/crates/iroha_smart_contract_utils/src/log.rs @@ -21,10 +21,9 @@ mod host { /// Log `obj` with desired log level /// -/// When running as a wasm smart contract, -/// prints to the host logging system with the corresponding level. -/// +/// When running as a wasm smart contract, prints to the host logging system with the corresponding level. /// When running outside of wasm, prints the output along with its level to stderr +#[doc(hidden)] pub fn log(log_level: Level, obj: &T) { cfg_if! { if #[cfg(not(target_family = "wasm"))] { diff --git a/crates/iroha_trigger/src/lib.rs b/crates/iroha_trigger/src/lib.rs index ffa36a49186..a6916b9afce 100644 --- a/crates/iroha_trigger/src/lib.rs +++ b/crates/iroha_trigger/src/lib.rs @@ -3,13 +3,20 @@ #![allow(unsafe_code)] pub use iroha_smart_contract as smart_contract; -pub use iroha_smart_contract_utils::debug; +pub use iroha_smart_contract_utils::{dbg, dbg_panic, DebugExpectExt, DebugUnwrapExt}; pub use iroha_trigger_derive::main; -pub use smart_contract::{data_model, stub_getrandom}; +pub use smart_contract::{data_model, Iroha}; + +#[doc(hidden)] +pub mod utils { + //! Crate with utilities + + pub use iroha_smart_contract_utils::register_getrandom_err_callback; +} pub mod log { //! WASM logging utilities - pub use iroha_smart_contract_utils::{debug, error, event, info, log::*, trace, warn}; + pub use iroha_smart_contract_utils::{debug, error, event, info, trace, warn}; } #[cfg(not(test))] @@ -36,10 +43,8 @@ pub fn get_trigger_context() -> data_model::smart_contract::payloads::TriggerCon pub mod prelude { //! Common imports used by triggers - - pub use iroha_smart_contract::prelude::*; - pub use iroha_smart_contract_utils::debug::DebugUnwrapExt; - pub use iroha_trigger_derive::main; - - pub use crate::data_model::smart_contract::payloads::TriggerContext as Context; + pub use crate::{ + data_model::{prelude::*, smart_contract::payloads::TriggerContext as Context}, + dbg, dbg_panic, DebugExpectExt, DebugUnwrapExt, Iroha, + }; } diff --git a/crates/iroha_trigger_derive/src/entrypoint.rs b/crates/iroha_trigger_derive/src/entrypoint.rs index 2337268151d..a7bad47e1f1 100644 --- a/crates/iroha_trigger_derive/src/entrypoint.rs +++ b/crates/iroha_trigger_derive/src/entrypoint.rs @@ -31,11 +31,13 @@ pub fn impl_entrypoint(emitter: &mut Emitter, item: syn::ItemFn) -> TokenStream let main_fn_name = syn::Ident::new(export::TRIGGER_MAIN, proc_macro2::Span::call_site()); quote! { + iroha_trigger::utils::register_getrandom_err_callback!(); + /// Smart contract entrypoint #[no_mangle] #[doc(hidden)] unsafe extern "C" fn #main_fn_name() { - let host = ::iroha_trigger::smart_contract::Iroha; + let host = ::iroha_trigger::Iroha; let context = ::iroha_trigger::get_trigger_context(); #fn_name(host, context) } diff --git a/data_model/samples/executor_custom_data_model/src/complex_isi.rs b/data_model/samples/executor_custom_data_model/src/complex_isi.rs index f293c256f39..afbb8571c48 100644 --- a/data_model/samples/executor_custom_data_model/src/complex_isi.rs +++ b/data_model/samples/executor_custom_data_model/src/complex_isi.rs @@ -1,4 +1,5 @@ //! Example of custom expression system. +//! //! Only few expressions are implemented to show proof-of-concept. //! See `wasm/samples/executor_custom_instructions_complex`. //! This is simplified version of expression system from Iroha v2.0.0-pre-rc.20 diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index a6b1f37da04..ad54dafcb30 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -39,7 +39,6 @@ parity-scale-codec = { version = "3.2.1", default-features = false } anyhow = { version = "1.0.71", default-features = false } serde = { version = "1.0.151", default-features = false } serde_json = { version = "1.0.91", default-features = false } -getrandom = { version = "0.2", features = ["custom"] } nonzero_ext = { version = "0.3.0", default-features = false } dlmalloc = { version = "0.2.6", features = ["global"] } diff --git a/wasm/libs/default_executor/Cargo.toml b/wasm/libs/default_executor/Cargo.toml index 3efbe8de352..c62989cb24f 100644 --- a/wasm/libs/default_executor/Cargo.toml +++ b/wasm/libs/default_executor/Cargo.toml @@ -14,4 +14,3 @@ iroha_executor.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true diff --git a/wasm/libs/default_executor/src/lib.rs b/wasm/libs/default_executor/src/lib.rs index 7b1fb13d3e8..f8475b05d1b 100644 --- a/wasm/libs/default_executor/src/lib.rs +++ b/wasm/libs/default_executor/src/lib.rs @@ -6,15 +6,11 @@ extern crate panic_halt; use dlmalloc::GlobalDlmalloc; -use iroha_executor::{ - data_model::block::BlockHeader, debug::dbg_panic, prelude::*, DataModelBuilder, -}; +use iroha_executor::{data_model::block::BlockHeader, prelude::*}; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_executor::stub_getrandom); - /// Executor that replaces some of [`Execute`]'s methods with sensible defaults /// /// # Warning @@ -30,7 +26,7 @@ struct Executor { impl Executor { fn ensure_genesis(curr_block: BlockHeader) { if !curr_block.is_genesis() { - dbg_panic( + dbg_panic!( "Default Executor is intended to be used only in genesis. \ Write your own executor if you need to upgrade executor on existing chain.", ); @@ -47,7 +43,7 @@ impl Executor { /// /// If `migrate()` entrypoint fails then the whole `Upgrade` instruction /// will be denied and previous executor will stay unchanged. -#[entrypoint] +#[iroha_executor::migrate] fn migrate(host: Iroha, context: Context) { Executor::ensure_genesis(context.curr_block); DataModelBuilder::with_default_permissions().build_and_set(&host); diff --git a/wasm/libs/multisig_accounts/Cargo.toml b/wasm/libs/multisig_accounts/Cargo.toml index 476b201367d..d8162aa48c9 100644 --- a/wasm/libs/multisig_accounts/Cargo.toml +++ b/wasm/libs/multisig_accounts/Cargo.toml @@ -17,7 +17,6 @@ iroha_multisig_data_model.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true, default-features = false } diff --git a/wasm/libs/multisig_accounts/src/lib.rs b/wasm/libs/multisig_accounts/src/lib.rs index 7ad18e3a2f7..24401b85916 100644 --- a/wasm/libs/multisig_accounts/src/lib.rs +++ b/wasm/libs/multisig_accounts/src/lib.rs @@ -11,16 +11,11 @@ use alloc::format; use dlmalloc::GlobalDlmalloc; use iroha_executor_data_model::permission::trigger::CanExecuteTrigger; use iroha_multisig_data_model::MultisigAccountArgs; -use iroha_trigger::{ - debug::{dbg_panic, DebugExpectExt as _}, - prelude::*, -}; +use iroha_trigger::prelude::*; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_trigger::stub_getrandom); - // Binary containing common logic to each multisig account for handling multisig transactions const MULTISIG_TRANSACTIONS_WASM: &[u8] = core::include_bytes!(concat!( core::env!("CARGO_MANIFEST_DIR"), @@ -30,7 +25,7 @@ const MULTISIG_TRANSACTIONS_WASM: &[u8] = core::include_bytes!(concat!( #[iroha_trigger::main] fn main(host: Iroha, context: Context) { let EventBox::ExecuteTrigger(event) = context.event else { - dbg_panic("trigger misused: must be triggered only by a call"); + dbg_panic!("trigger misused: must be triggered only by a call"); }; let args: MultisigAccountArgs = event .args() diff --git a/wasm/libs/multisig_domains/Cargo.toml b/wasm/libs/multisig_domains/Cargo.toml index ae37e394289..efbab17b923 100644 --- a/wasm/libs/multisig_domains/Cargo.toml +++ b/wasm/libs/multisig_domains/Cargo.toml @@ -16,7 +16,6 @@ iroha_executor_data_model.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true, default-features = false } diff --git a/wasm/libs/multisig_domains/src/lib.rs b/wasm/libs/multisig_domains/src/lib.rs index c60e3a9e802..9b93d096d7f 100644 --- a/wasm/libs/multisig_domains/src/lib.rs +++ b/wasm/libs/multisig_domains/src/lib.rs @@ -9,16 +9,11 @@ extern crate panic_halt; use alloc::format; use dlmalloc::GlobalDlmalloc; -use iroha_trigger::{ - debug::{dbg_panic, DebugExpectExt as _}, - prelude::*, -}; +use iroha_trigger::prelude::*; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_trigger::stub_getrandom); - // Binary containing common logic to each domain for handling multisig accounts const MULTISIG_ACCOUNTS_WASM: &[u8] = core::include_bytes!(concat!( core::env!("CARGO_MANIFEST_DIR"), @@ -28,7 +23,7 @@ const MULTISIG_ACCOUNTS_WASM: &[u8] = core::include_bytes!(concat!( #[iroha_trigger::main] fn main(host: Iroha, context: Context) { let EventBox::Data(DataEvent::Domain(event)) = context.event else { - dbg_panic("trigger misused: must be triggered only by a domain event"); + dbg_panic!("trigger misused: must be triggered only by a domain event"); }; let (domain_id, domain_owner, owner_changed) = match event { DomainEvent::Created(domain) => (domain.id().clone(), domain.owned_by().clone(), false), @@ -37,8 +32,8 @@ fn main(host: Iroha, context: Context) { owner_changed.new_owner().clone(), true, ), - _ => dbg_panic( - "trigger misused: must be triggered only when domain created or owner changed", + _ => dbg_panic!( + "trigger misused: must be triggered only when domain created or owner changed" ), }; diff --git a/wasm/libs/multisig_transactions/Cargo.toml b/wasm/libs/multisig_transactions/Cargo.toml index ab208ce239b..dd676e46c23 100644 --- a/wasm/libs/multisig_transactions/Cargo.toml +++ b/wasm/libs/multisig_transactions/Cargo.toml @@ -16,7 +16,6 @@ iroha_multisig_data_model.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true, default-features = false } diff --git a/wasm/libs/multisig_transactions/src/lib.rs b/wasm/libs/multisig_transactions/src/lib.rs index f4ac819cce4..d70c7fc870c 100644 --- a/wasm/libs/multisig_transactions/src/lib.rs +++ b/wasm/libs/multisig_transactions/src/lib.rs @@ -14,20 +14,15 @@ use alloc::{ use dlmalloc::GlobalDlmalloc; use iroha_multisig_data_model::MultisigTransactionArgs; -use iroha_trigger::{ - debug::{dbg_panic, DebugExpectExt as _}, - prelude::*, -}; +use iroha_trigger::prelude::*; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_trigger::stub_getrandom); - #[iroha_trigger::main] fn main(host: Iroha, context: Context) { let EventBox::ExecuteTrigger(event) = context.event else { - dbg_panic("trigger misused: must be triggered only by a call"); + dbg_panic!("trigger misused: must be triggered only by a call"); }; let trigger_id = context.id; let args: MultisigTransactionArgs = event diff --git a/wasm/samples/create_nft_for_every_user_trigger/Cargo.toml b/wasm/samples/create_nft_for_every_user_trigger/Cargo.toml index 58f3433a861..44d2f3908c8 100644 --- a/wasm/samples/create_nft_for_every_user_trigger/Cargo.toml +++ b/wasm/samples/create_nft_for_every_user_trigger/Cargo.toml @@ -15,4 +15,3 @@ iroha_trigger.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true diff --git a/wasm/samples/create_nft_for_every_user_trigger/src/lib.rs b/wasm/samples/create_nft_for_every_user_trigger/src/lib.rs index 0d9113e0fd7..c345eb4c389 100644 --- a/wasm/samples/create_nft_for_every_user_trigger/src/lib.rs +++ b/wasm/samples/create_nft_for_every_user_trigger/src/lib.rs @@ -8,19 +8,17 @@ extern crate panic_halt; use alloc::{format, string::ToString}; use dlmalloc::GlobalDlmalloc; -use iroha_trigger::{debug::dbg_panic, prelude::*}; +use iroha_trigger::prelude::*; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_trigger::stub_getrandom); - #[iroha_trigger::main] fn main(host: Iroha, context: Context) { iroha_trigger::log::info!("Executing trigger"); if !matches!(context.event, EventBox::Time(_)) { - dbg_panic("Only work as a by call trigger"); + dbg_panic!("Only work as a by call trigger"); }; let bad_domain_ids: [DomainId; 3] = [ diff --git a/wasm/samples/executor_custom_instructions_complex/Cargo.toml b/wasm/samples/executor_custom_instructions_complex/Cargo.toml index fc67ea62b04..35f8e3701fa 100644 --- a/wasm/samples/executor_custom_instructions_complex/Cargo.toml +++ b/wasm/samples/executor_custom_instructions_complex/Cargo.toml @@ -20,4 +20,3 @@ serde.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true diff --git a/wasm/samples/executor_custom_instructions_complex/src/lib.rs b/wasm/samples/executor_custom_instructions_complex/src/lib.rs index 549c7e3c162..f75bc03538c 100644 --- a/wasm/samples/executor_custom_instructions_complex/src/lib.rs +++ b/wasm/samples/executor_custom_instructions_complex/src/lib.rs @@ -22,8 +22,6 @@ use iroha_executor::{ #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_executor::stub_getrandom); - #[derive(Visit, Execute, Entrypoints)] #[visit(custom(visit_custom))] struct Executor { @@ -95,7 +93,7 @@ impl executor_custom_data_model::complex_isi::Context for Context<'_> { } } -#[entrypoint] +#[iroha_executor::migrate] fn migrate(host: Iroha, _context: iroha_executor::prelude::Context) { DataModelBuilder::with_default_permissions() .add_instruction::() diff --git a/wasm/samples/executor_custom_instructions_simple/Cargo.toml b/wasm/samples/executor_custom_instructions_simple/Cargo.toml index 7dd316c7878..6cea868cb8e 100644 --- a/wasm/samples/executor_custom_instructions_simple/Cargo.toml +++ b/wasm/samples/executor_custom_instructions_simple/Cargo.toml @@ -20,4 +20,3 @@ serde.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true diff --git a/wasm/samples/executor_custom_instructions_simple/src/lib.rs b/wasm/samples/executor_custom_instructions_simple/src/lib.rs index 88afdac4eed..9a5db335edc 100644 --- a/wasm/samples/executor_custom_instructions_simple/src/lib.rs +++ b/wasm/samples/executor_custom_instructions_simple/src/lib.rs @@ -10,13 +10,11 @@ extern crate panic_halt; use dlmalloc::GlobalDlmalloc; use executor_custom_data_model::simple_isi::{CustomInstructionBox, MintAssetForAllAccounts}; -use iroha_executor::{data_model::isi::CustomInstruction, debug::DebugExpectExt, prelude::*}; +use iroha_executor::{data_model::isi::CustomInstruction, prelude::*}; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_executor::stub_getrandom); - #[derive(Visit, Execute, Entrypoints)] #[visit(custom(visit_custom))] struct Executor { @@ -64,7 +62,7 @@ fn execute_mint_asset_for_all_accounts( Ok(()) } -#[entrypoint] +#[iroha_executor::migrate] fn migrate(host: Iroha, _context: Context) { DataModelBuilder::with_default_permissions() .add_instruction::() diff --git a/wasm/samples/executor_remove_permission/Cargo.toml b/wasm/samples/executor_remove_permission/Cargo.toml index 6c5a58306f4..458249ad4fc 100644 --- a/wasm/samples/executor_remove_permission/Cargo.toml +++ b/wasm/samples/executor_remove_permission/Cargo.toml @@ -22,4 +22,3 @@ serde.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true diff --git a/wasm/samples/executor_remove_permission/src/lib.rs b/wasm/samples/executor_remove_permission/src/lib.rs index 06196c32428..ec9272ae786 100644 --- a/wasm/samples/executor_remove_permission/src/lib.rs +++ b/wasm/samples/executor_remove_permission/src/lib.rs @@ -13,8 +13,6 @@ use iroha_executor_data_model::permission::domain::CanUnregisterDomain; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_executor::stub_getrandom); - #[derive(Visit, Execute, Entrypoints)] struct Executor { host: Iroha, @@ -22,7 +20,7 @@ struct Executor { verdict: Result, } -#[entrypoint] +#[iroha_executor::migrate] fn migrate(host: Iroha, _context: Context) { // Note that actually migration will reset token schema to default (minus `CanUnregisterDomain`) // So any added custom permission tokens will be also removed diff --git a/wasm/samples/executor_with_admin/Cargo.toml b/wasm/samples/executor_with_admin/Cargo.toml index 808cb9d5594..a86fcf97703 100644 --- a/wasm/samples/executor_with_admin/Cargo.toml +++ b/wasm/samples/executor_with_admin/Cargo.toml @@ -16,4 +16,3 @@ iroha_schema.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true diff --git a/wasm/samples/executor_with_admin/src/lib.rs b/wasm/samples/executor_with_admin/src/lib.rs index c41c063d401..857a9a950ca 100644 --- a/wasm/samples/executor_with_admin/src/lib.rs +++ b/wasm/samples/executor_with_admin/src/lib.rs @@ -12,8 +12,6 @@ use iroha_executor::prelude::*; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_executor::stub_getrandom); - #[derive(Visit, Execute, Entrypoints)] #[visit(custom(visit_instruction))] struct Executor { @@ -34,5 +32,5 @@ fn visit_instruction(executor: &mut Executor, isi: &InstructionBox) { iroha_executor::default::visit_instruction(executor, isi); } -#[entrypoint] +#[iroha_executor::migrate] fn migrate(_host: Iroha, _context: Context) {} diff --git a/wasm/samples/executor_with_custom_parameter/Cargo.toml b/wasm/samples/executor_with_custom_parameter/Cargo.toml index 4fc114974f1..92888a254f6 100644 --- a/wasm/samples/executor_with_custom_parameter/Cargo.toml +++ b/wasm/samples/executor_with_custom_parameter/Cargo.toml @@ -23,4 +23,3 @@ serde.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true diff --git a/wasm/samples/executor_with_custom_parameter/src/lib.rs b/wasm/samples/executor_with_custom_parameter/src/lib.rs index 7edb4412553..4a343bfa4b9 100644 --- a/wasm/samples/executor_with_custom_parameter/src/lib.rs +++ b/wasm/samples/executor_with_custom_parameter/src/lib.rs @@ -15,8 +15,6 @@ use iroha_executor_data_model::parameter::Parameter; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_executor::stub_getrandom); - #[derive(Visit, Execute, Entrypoints)] #[visit(custom(visit_register_domain))] struct Executor { @@ -43,7 +41,7 @@ fn visit_register_domain(executor: &mut Executor, isi: &Register) { execute!(executor, isi); } -#[entrypoint] +#[iroha_executor::migrate] fn migrate(host: Iroha, _context: Context) { DataModelBuilder::with_default_permissions() .add_parameter(DomainLimits::default()) diff --git a/wasm/samples/executor_with_custom_permission/Cargo.toml b/wasm/samples/executor_with_custom_permission/Cargo.toml index 32cf35963c6..5dc5651d128 100644 --- a/wasm/samples/executor_with_custom_permission/Cargo.toml +++ b/wasm/samples/executor_with_custom_permission/Cargo.toml @@ -23,4 +23,3 @@ serde.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true diff --git a/wasm/samples/executor_with_custom_permission/src/lib.rs b/wasm/samples/executor_with_custom_permission/src/lib.rs index 5ae332a8094..a55a985610f 100644 --- a/wasm/samples/executor_with_custom_permission/src/lib.rs +++ b/wasm/samples/executor_with_custom_permission/src/lib.rs @@ -25,8 +25,6 @@ use iroha_executor_data_model::permission::domain::CanUnregisterDomain; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_executor::stub_getrandom); - #[derive(Visit, Execute, Entrypoints)] #[visit(custom( visit_register_domain, @@ -164,7 +162,7 @@ pub fn visit_revoke_account_permission( iroha_executor::default::visit_revoke_account_permission(executor, isi) } -#[entrypoint] +#[iroha_executor::migrate] pub fn migrate(host: Iroha, _context: Context) { let accounts = Executor::get_all_accounts_with_can_unregister_domain_permission(&host).collect::>(); diff --git a/wasm/samples/executor_with_migration_fail/Cargo.toml b/wasm/samples/executor_with_migration_fail/Cargo.toml index b6326eed261..36ee081b0b6 100644 --- a/wasm/samples/executor_with_migration_fail/Cargo.toml +++ b/wasm/samples/executor_with_migration_fail/Cargo.toml @@ -16,4 +16,3 @@ anyhow.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true diff --git a/wasm/samples/executor_with_migration_fail/src/lib.rs b/wasm/samples/executor_with_migration_fail/src/lib.rs index 1bab3082384..9ef1296a6e9 100644 --- a/wasm/samples/executor_with_migration_fail/src/lib.rs +++ b/wasm/samples/executor_with_migration_fail/src/lib.rs @@ -6,13 +6,11 @@ extern crate panic_halt; use dlmalloc::GlobalDlmalloc; -use iroha_executor::{debug::dbg_panic, prelude::*}; +use iroha_executor::prelude::*; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_executor::stub_getrandom); - #[derive(Visit, Execute, Entrypoints)] struct Executor { host: Iroha, @@ -20,7 +18,7 @@ struct Executor { verdict: Result, } -#[entrypoint] +#[iroha_executor::migrate] fn migrate(host: Iroha, _context: Context) { // Performing side-effects to check in the test that it won't be applied after failure @@ -29,5 +27,5 @@ fn migrate(host: Iroha, _context: Context) { host.submit(&Register::domain(Domain::new(domain_id))) .unwrap(); - dbg_panic("This executor always fails to migrate"); + dbg_panic!("This executor always fails to migrate"); } diff --git a/wasm/samples/mint_rose_trigger/Cargo.toml b/wasm/samples/mint_rose_trigger/Cargo.toml index 9a4fda69c6e..2ceb93a8810 100644 --- a/wasm/samples/mint_rose_trigger/Cargo.toml +++ b/wasm/samples/mint_rose_trigger/Cargo.toml @@ -16,4 +16,3 @@ iroha_trigger.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true diff --git a/wasm/samples/mint_rose_trigger/src/lib.rs b/wasm/samples/mint_rose_trigger/src/lib.rs index e05a9725c40..ef1fe988d29 100644 --- a/wasm/samples/mint_rose_trigger/src/lib.rs +++ b/wasm/samples/mint_rose_trigger/src/lib.rs @@ -6,22 +6,17 @@ extern crate panic_halt; use dlmalloc::GlobalDlmalloc; -use iroha_trigger::{ - debug::{dbg_panic, DebugExpectExt as _}, - prelude::*, -}; +use iroha_trigger::prelude::*; use mint_rose_trigger_data_model::MintRoseArgs; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_trigger::stub_getrandom); - /// Mint 1 rose for owner #[iroha_trigger::main] fn main(host: Iroha, context: Context) { let EventBox::ExecuteTrigger(event) = context.event else { - dbg_panic("Only work as a by call trigger"); + dbg_panic!("Only work as a by call trigger"); }; let val = event diff --git a/wasm/samples/query_assets_and_save_cursor/Cargo.toml b/wasm/samples/query_assets_and_save_cursor/Cargo.toml index 41c9da685c8..61d3d1f8233 100644 --- a/wasm/samples/query_assets_and_save_cursor/Cargo.toml +++ b/wasm/samples/query_assets_and_save_cursor/Cargo.toml @@ -15,7 +15,6 @@ iroha_smart_contract.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true parity-scale-codec.workspace = true nonzero_ext.workspace = true serde_json = { version = "1.0.108", default-features = false } diff --git a/wasm/samples/query_assets_and_save_cursor/src/lib.rs b/wasm/samples/query_assets_and_save_cursor/src/lib.rs index 81ed71a94fe..e7261d9b9b4 100644 --- a/wasm/samples/query_assets_and_save_cursor/src/lib.rs +++ b/wasm/samples/query_assets_and_save_cursor/src/lib.rs @@ -13,7 +13,6 @@ use iroha_smart_contract::{ predicate::CompoundPredicate, QueryWithFilter, QueryWithParams, }, - debug::DebugExpectExt as _, prelude::*, }; use nonzero_ext::nonzero; @@ -22,8 +21,6 @@ use parity_scale_codec::{Decode, DecodeAll, Encode}; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_smart_contract::stub_getrandom); - /// Execute [`FindAssets`] and save cursor to the owner's metadata. /// NOTE: DON'T TAKE THIS AS AN EXAMPLE, THIS IS ONLY FOR TESTING INTERNALS OF IROHA #[iroha_smart_contract::main] diff --git a/wasm/samples/smart_contract_can_filter_queries/Cargo.toml b/wasm/samples/smart_contract_can_filter_queries/Cargo.toml index 33a478ddfad..69c95d6a026 100644 --- a/wasm/samples/smart_contract_can_filter_queries/Cargo.toml +++ b/wasm/samples/smart_contract_can_filter_queries/Cargo.toml @@ -15,7 +15,6 @@ iroha_smart_contract.workspace = true panic-halt.workspace = true dlmalloc.workspace = true -getrandom.workspace = true parity-scale-codec.workspace = true nonzero_ext.workspace = true serde_json = { workspace = true, default-features = false } diff --git a/wasm/samples/smart_contract_can_filter_queries/src/lib.rs b/wasm/samples/smart_contract_can_filter_queries/src/lib.rs index 2501f27fac0..798f719629c 100644 --- a/wasm/samples/smart_contract_can_filter_queries/src/lib.rs +++ b/wasm/samples/smart_contract_can_filter_queries/src/lib.rs @@ -15,8 +15,6 @@ use iroha_smart_contract::{prelude::*, Iroha}; #[global_allocator] static ALLOC: GlobalDlmalloc = GlobalDlmalloc; -getrandom::register_custom_getrandom!(iroha_smart_contract::stub_getrandom); - /// Create two asset definitions in the looking_glass domain, query all asset definitions, filter them to only be in the looking_glass domain, check that the results are consistent #[iroha_smart_contract::main] fn main(host: Iroha, _context: Context) {