Skip to content

Commit

Permalink
chore: remove call to set_getrandom and make dbg a macro in smart…
Browse files Browse the repository at this point in the history
… contracts

Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Nov 4, 2024
1 parent 2a110d9 commit d35a826
Show file tree
Hide file tree
Showing 49 changed files with 273 additions and 264 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions crates/iroha_core/src/smartcontracts/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,16 +688,15 @@ impl<S> Runtime<S> {
///
/// # Warning
///
/// This function doesn't take ownership of the provided
/// allocation
/// This function doesn't take ownership of the provided allocation
///
/// # Errors
///
/// If string decoding fails
#[allow(clippy::needless_pass_by_value)]
#[codec::wrap(state = "S")]
fn dbg(msg: String) {
println!("{msg}");
eprintln!("{msg}");
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/iroha_executor/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ pub mod role {
}

fn find_account_roles(account_id: AccountId, host: &Iroha) -> impl Iterator<Item = RoleId> {
use iroha_smart_contract::debug::DebugExpectExt as _;
use iroha_smart_contract::DebugExpectExt as _;

host.query(FindRolesByAccountId::new(account_id))
.execute()
Expand Down Expand Up @@ -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() {
Expand Down
97 changes: 53 additions & 44 deletions crates/iroha_executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SignedTransaction> {
// 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<InstructionBox> {
// 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<AnyQueryBox> {
// 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<SignedTransaction> {
// 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<InstructionBox> {
// 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<AnyQueryBox> {
// 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`].
Expand All @@ -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))]
Expand Down Expand Up @@ -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,
};
}
3 changes: 1 addition & 2 deletions crates/iroha_executor/src/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::{
prelude::Context,
smart_contract::{
data_model::{executor::Result, permission::Permission as PermissionObject, prelude::*},
debug::DebugExpectExt as _,
Iroha,
prelude::*,
},
};

Expand Down
6 changes: 3 additions & 3 deletions crates/iroha_executor_derive/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn impl_derive_entrypoints(emitter: &mut Emitter, input: &syn::DeriveInput)

let mut entrypoint_fns: Vec<syn::ItemFn> = vec![
parse_quote! {
#[::iroha_executor::prelude::entrypoint]
#[::iroha_executor::entrypoint]
pub fn execute_transaction(
transaction: ::iroha_executor::prelude::SignedTransaction,
host: ::iroha_executor::prelude::Iroha,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
28 changes: 10 additions & 18 deletions crates/iroha_executor_derive/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
},
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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);
}

Expand Down
34 changes: 32 additions & 2 deletions crates/iroha_executor_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 0 additions & 5 deletions crates/iroha_smart_contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Loading

0 comments on commit d35a826

Please sign in to comment.