-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add helper crate for writing wasm smartcontracts
Signed-off-by: Marin Veršić <[email protected]>
- Loading branch information
Showing
9 changed files
with
331 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "iroha_wasm" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[features] | ||
default = ["panic_handler"] | ||
panic_handler = [] | ||
|
||
[profile.dev] | ||
panic = "abort" | ||
|
||
[profile.release] | ||
panic = "abort" | ||
|
||
[dependencies] | ||
iroha_data_model = { version = "=2.0.0-pre.1", path = "../data_model", default-features = false } | ||
iroha_wasm_derive = { path = "derive" } | ||
|
||
parity-scale-codec = { version = "2.3.1", default-features = false } | ||
wee_alloc = "0.4.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "iroha_wasm_derive" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = {version = "1", default-features = false } | ||
quote = "1.0" | ||
proc-macro-error = "1.0" | ||
|
||
[dev-dependencies] | ||
iroha_wasm = { path = "../" } | ||
|
||
trybuild = "1.0.53" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//! Exposes macros which facilitate writing smartcontracts | ||
#![allow(clippy::str_to_string)] | ||
|
||
use proc_macro::TokenStream; | ||
use proc_macro_error::{abort, proc_macro_error}; | ||
use quote::quote; | ||
use syn::{parse_macro_input, ItemFn, Path, ReturnType, Signature, Type}; | ||
|
||
/// Used to annotate user-defined function which starts the execution of smartcontract | ||
#[proc_macro_error] | ||
#[proc_macro_attribute] | ||
pub fn iroha_wasm(_: TokenStream, item: TokenStream) -> TokenStream { | ||
let ItemFn { | ||
attrs, | ||
vis, | ||
sig, | ||
block, | ||
}: ItemFn = parse_macro_input!(item as ItemFn); | ||
|
||
verify_function_signature(&sig); | ||
let fn_name = &sig.ident; | ||
|
||
quote! { | ||
#[no_mangle] | ||
unsafe extern "C" fn _iroha_wasm_main(ptr: u32, len: u32) { | ||
#fn_name(iroha_wasm::_decode_from_raw::<AccountId>(ptr, len)) | ||
} | ||
|
||
#(#attrs)* | ||
#vis #sig | ||
#block | ||
} | ||
.into() | ||
} | ||
|
||
fn verify_function_signature(sig: &Signature) -> bool { | ||
if ReturnType::Default != sig.output { | ||
abort!(sig.output, "Exported function must not have a return type"); | ||
} | ||
|
||
if sig.inputs.len() != 1 { | ||
abort!( | ||
sig.inputs, | ||
"Exported function must have exactly 1 input argument of type `AccountId`" | ||
); | ||
} | ||
|
||
if let Some(syn::FnArg::Typed(pat)) = sig.inputs.iter().next() { | ||
if let syn::Type::Reference(ty) = &*pat.ty { | ||
return type_is_account_id(&ty.elem); | ||
} | ||
} | ||
|
||
false | ||
} | ||
|
||
fn type_is_account_id(account_id_ty: &Type) -> bool { | ||
const ACCOUNT_ID_IDENT: &str = "AccountId"; | ||
|
||
if let Type::Path(path) = account_id_ty { | ||
let Path { segments, .. } = &path.path; | ||
|
||
if let Some(type_name) = segments.iter().last().map(|ty| &ty.ident) { | ||
return *type_name == ACCOUNT_ID_IDENT; | ||
} | ||
} | ||
|
||
false | ||
} |
Oops, something went wrong.