Skip to content

Commit

Permalink
Wasm test (#772)
Browse files Browse the repository at this point in the history
* Scaffolding for wasm test

* Fix command to build wasm example

* State os for running job statically

* Run example with wasmtime

* Try different approach for a build with a wasm test

* Add missing comma

* Fix toml formatting

* Adjust path

* Test a failing example

* Return an Err

* Improve wasm example

* Working on example

* Implement functionality

* Improve example

* Cleanup example

* Revert "Implement functionality"

This reverts commit 6900574.

* Add test documentation

* Incorporate review feedback
  • Loading branch information
Niederb authored Jul 4, 2024
1 parent d07bbb1 commit 2a4209e
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
37 changes: 36 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:

# Test for 32 bit and wasm32-unknown-unknown compatibility
cargo build --target wasm32-unknown-unknown --no-default-features --features sync-api,
cargo build --release -p ac-examples-wasm --examples --target wasm32-unknown-unknown,

# Test for 32 bit and wasm32-wasip1 compatibility
cargo build --target wasm32-wasip1 --no-default-features --features std --features staking-xt --features contracts-xt --features sync-api,
Expand Down Expand Up @@ -120,6 +121,14 @@ jobs:
!target/release/examples/*.d
!target/release/examples/*-*
- name: Upload wasm examples
uses: actions/upload-artifact@v4
if: contains(matrix.check, 'examples-wasm')
with:
name: examples-wasm
path: |
target/wasm32-unknown-unknown/release/examples/*.wasm
- name: Upload async testing examples
uses: actions/upload-artifact@v4
if: contains(matrix.check, 'testing-async')
Expand Down Expand Up @@ -235,9 +244,35 @@ jobs:
nc -z -v 127.0.0.1 9944
chmod +x ${{ matrix.example }}
./${{ matrix.example }}
wasm_examples:
runs-on: ${{ matrix.os }}
needs: build
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
example: [
wasm_example,
]
steps:
- uses: actions/checkout@v4

- name: Download examples from previous run
uses: actions/download-artifact@v4
with:
pattern: examples-wasm
merge-multiple: true

- name: Setup `wasmtime`
uses: bytecodealliance/actions/wasmtime/setup@v1

- name: Run wasm example
run: wasmtime --invoke main ${{ matrix.example }}.wasm 0 0

merge:
runs-on: ubuntu-latest
needs: examples
needs: [examples, wasm_examples]
steps:
- name: Merge Artifacts
uses: actions/upload-artifact/merge@v4
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ members = [
"compose-macros",
"examples/async",
"examples/sync",
"examples/wasm",
"node-api",
"test-no-std",
"testing/async",
Expand Down
11 changes: 11 additions & 0 deletions examples/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "ac-examples-wasm"
version = "0.5.0"
license = "Apache-2.0"
edition = "2021"

[dev-dependencies]
sp-core = { default-features = false, features = ["full_crypto", "serde"], git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" }
substrate-api-client = { default-features = false, path = "../..", version = "0.17" }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" }
50 changes: 50 additions & 0 deletions examples/wasm/examples/wasm_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2024 Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//! Example that some basic functions that can be executed in WebAssembly.

pub use pallet_balances::Call as BalancesCall;
use sp_core::{
crypto::{AccountId32, Ss58Codec},
sr25519, Pair,
};
use sp_runtime::MultiAddress;
pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
use std::process::ExitCode;
use substrate_api_client::ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner};

fn main() -> Result<ExitCode, i32> {
// This test is not yet very sophisticated and not exhaustive.
// Still it shows how some basic data structures can be constructed and used.
let alice: sr25519::Pair = Pair::from_string(
"0xe5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a",
None,
)
.unwrap();

let bob_account: AccountId32 =
sr25519::Public::from_ss58check("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty")
.unwrap()
.into();
let _bob: MultiAddress<AccountId32, AccountId32> = MultiAddress::Id(bob_account);
let es_converted: ExtrinsicSigner<AssetRuntimeConfig> = alice.clone().into();
let es_new = ExtrinsicSigner::<AssetRuntimeConfig>::new(alice.clone());
assert_eq!(es_converted.signer().public(), es_new.signer().public());

let extrinsic = UncheckedExtrinsic::from_bytes(&[]);
match extrinsic {
Ok(_) => panic!("Extrinsic should be invalid"),
Err(_) => (),
}
Ok(ExitCode::from(0))
}

0 comments on commit 2a4209e

Please sign in to comment.