forked from hyperledger-iroha/iroha
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] hyperledger-iroha#2099: Add WASM integration test based on …
…Orillion use-case (hyperledger-iroha#2122) Signed-off-by: Daniil Polyakov <arjentix@gmail.com>
- Loading branch information
Showing
19 changed files
with
392 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,49 @@ | ||
//! Build script which builds smartcontract for test | ||
//! | ||
//! Technically this script is used only for testing purposes, but current cargo implementation | ||
//! doesn't allow to run build script only for tests or get info about current profile from it. | ||
//! See [cargo issue #4001](https://github.com/rust-lang/cargo/issues/4001) | ||
|
||
use std::{env, path::Path, process::Command}; | ||
|
||
#[allow(clippy::expect_used)] | ||
fn main() { | ||
let manifest_dir = | ||
env::var("CARGO_MANIFEST_DIR").expect("Expected `CARGO_MANIFEST_DIR` environment variable"); | ||
let smartcontract_path = | ||
Path::new(&manifest_dir).join("tests/integration/create_nft_for_every_user_smartcontract"); | ||
let out_dir = env::var_os("OUT_DIR").expect("Expected `OUT_DIR` environment variable"); | ||
|
||
println!("cargo:rerun-if-changed=.."); | ||
|
||
let fmt = Command::new("cargo") | ||
// Removing environment variable to avoid | ||
// `error: infinite recursion detected` when running `cargo lints` | ||
.env_remove("RUST_RECURSION_COUNT") | ||
.current_dir(smartcontract_path.clone()) | ||
.args(&["+nightly-2022-04-20", "fmt", "--all"]) | ||
.status() | ||
.expect("Failed to run `cargo fmt` on smartcontract"); | ||
assert!(fmt.success(), "Can't format smartcontract"); | ||
|
||
let build = Command::new("cargo") | ||
// Removing environment variable to avoid | ||
// `error: infinite recursion detected` when running `cargo lints` | ||
.env_remove("RUST_RECURSION_COUNT") | ||
.env("CARGO_TARGET_DIR", out_dir) | ||
.current_dir(smartcontract_path) | ||
.args(&[ | ||
"+nightly-2022-04-20", | ||
"build", | ||
"--release", | ||
"-Z", | ||
"build-std", | ||
"-Z", | ||
"build-std-features=panic_immediate_abort", | ||
"--target", | ||
"wasm32-unknown-unknown", | ||
]) | ||
.status() | ||
.expect("Failed to run `cargo build` on smartcontract"); | ||
assert!(build.success(), "Can't build smartcontract") | ||
} |
26 changes: 26 additions & 0 deletions
26
client/tests/integration/create_nft_for_every_user_smartcontract/Cargo.toml
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,26 @@ | ||
[package] | ||
name = "create_nft_for_every_user_smartcontract" | ||
version = "2.0.0-pre-rc.3" | ||
authors = ["Iroha 2 team <https://github.com/orgs/soramitsu/teams/iroha2>"] | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
# Smartcontract should be linked dynamically so that it may link to functions exported | ||
# from the host environment. Also, host environment executes the smartcontract by | ||
# calling the function which smartcontract exports(entry point of execution) | ||
crate-type = ['cdylib'] | ||
|
||
# Empty workspace to fix "current package believes it's in a workspace when it's not" | ||
[workspace] | ||
|
||
[profile.release] | ||
strip = "debuginfo" # Remove debugging info from the binary | ||
panic = "abort" # Panics are transcribed to Traps when compiling for wasm anyways | ||
lto = true # Link-time-optimization produces notable decrease in binary size | ||
opt-level = "z" # Optimize for size vs speed with "s"/"z"(removes vectorization) | ||
codegen-units = 1 # Further reduces binary size but increases compilation time | ||
|
||
[dependencies] | ||
iroha_wasm = { path = "../../../../wasm", features = ["debug"]} |
70 changes: 70 additions & 0 deletions
70
client/tests/integration/create_nft_for_every_user_smartcontract/src/lib.rs
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 @@ | ||
//! Smartcontract which creates new nft for every user | ||
//! | ||
//! This module isn't included in the build-tree, | ||
//! but instead it is being built by a `client/build.rs` | ||
|
||
#![no_std] | ||
#![no_main] | ||
#![allow(clippy::all)] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::{format, string::ToString, vec::Vec}; | ||
use core::str::FromStr; | ||
|
||
use iroha_wasm::{data_model::prelude::*, DebugUnwrapExt, Execute}; | ||
|
||
#[iroha_wasm::iroha_wasm] | ||
fn smartcontract_entry_point(_account_id: AccountId) { | ||
let query = QueryBox::FindAllAccounts(FindAllAccounts {}); | ||
let accounts: Vec<Account> = query.execute().try_into().dbg_unwrap(); | ||
|
||
let limits = MetadataLimits::new(256, 256); | ||
|
||
for account in accounts { | ||
let mut metadata = Metadata::new(); | ||
let name = format!( | ||
"nft_for_{}_in_{}", | ||
account.id().name, | ||
account.id().domain_id | ||
) | ||
.parse() | ||
.dbg_unwrap(); | ||
metadata | ||
.insert_with_limits(name, true.into(), limits) | ||
.dbg_unwrap(); | ||
|
||
let nft_id = generate_new_nft_id(account.id()); | ||
let nft_definition = AssetDefinition::store(nft_id.clone()) | ||
.mintable_once() | ||
.with_metadata(metadata) | ||
.build(); | ||
let account_nft_id = <Asset as Identifiable>::Id::new(nft_id, account.id().clone()); | ||
|
||
Instruction::Register(RegisterBox::new(nft_definition)).execute(); | ||
Instruction::SetKeyValue(SetKeyValueBox::new( | ||
account_nft_id, | ||
Name::from_str("has_this_nft").dbg_unwrap(), | ||
Value::Bool(true), | ||
)) | ||
.execute(); | ||
} | ||
} | ||
|
||
fn generate_new_nft_id(account_id: &<Account as Identifiable>::Id) -> AssetDefinitionId { | ||
let query = QueryBox::FindAssetsByAccountId(FindAssetsByAccountId::new(account_id.clone())); | ||
let assets: Vec<Asset> = query.execute().try_into().dbg_unwrap(); | ||
|
||
let new_number = assets | ||
.into_iter() | ||
.filter(|asset| asset.id().definition_id.to_string().starts_with("nft_")) | ||
.count() | ||
+ 1; | ||
|
||
format!( | ||
"nft_number_{}_for_{}#{}", | ||
new_number, account_id.name, account_id.domain_id | ||
) | ||
.parse() | ||
.dbg_unwrap() | ||
} |
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
Oops, something went wrong.