Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ test/config.ini

# wasm
wasm-wrappers/pkg/
wasm-wrappers/js-bindings-test/dist/

# 'mintlayer-data' will be mapped to home directories of docker containers, so everything
# inside it will be generated by the containers.
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,22 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install TypeScript & Knip
run: npm install typescript knip
- name: Install wasm-pack
run: cargo install wasm-pack
- name: Build the wasm module
working-directory: ./wasm-wrappers
run: wasm-pack build --target nodejs
- name: Compile the tests
working-directory: ./wasm-wrappers
run: tsc --project js-bindings-test/tsconfig.json
- name: Run the tests
working-directory: ./wasm-wrappers
run: node js-bindings/node-entry.js
run: node --enable-source-maps js-bindings-test/node-entry.js
- name: Run Knip
working-directory: ./wasm-wrappers/js-bindings-test
run: npx knip

wasm_artifacts:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ test/config.ini

# wasm
wasm-wrappers/pkg/
wasm-wrappers/js-bindings-test/dist/

# 'mintlayer-data' will be mapped to home directories of docker containers, so everything
# inside it will be generated by the containers.
Expand Down
3 changes: 2 additions & 1 deletion build-tools/codecheck/codecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
'.git',
'build-tools/docker/example-mainnet/mintlayer-data',
'build-tools/docker/example-mainnet-dns-server/mintlayer-data',
'wasm-wrappers/pkg'
'wasm-wrappers/pkg',
'wasm-wrappers/js-bindings-test/dist',
]


Expand Down
4 changes: 3 additions & 1 deletion do_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ set -e
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
PYTHON=$(which python || which python3)

cd "$SCRIPT_DIR"

cargo fmt --check -- --config newline_style=Unix

# Install cargo deny first with: cargo install cargo-deny.
Expand Down Expand Up @@ -36,7 +38,7 @@ cargo clippy --all-features --workspace --lib --bins --examples -- \
-D clippy::string_slice

# Install requirements with: pip install -r ./build-tools/codecheck/requirements.txt
"$PYTHON" "$SCRIPT_DIR/build-tools/codecheck/codecheck.py"
"$PYTHON" "build-tools/codecheck/codecheck.py"

# Ensure that wasm documentation is up-to-date
cargo run -p wasm-doc-gen -- -o wasm-wrappers/WASM-API.md --check
69 changes: 47 additions & 22 deletions wasm-wrappers/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
## Basic wasm bindings for mintlayer
# Basic wasm bindings for mintlayer

This module has different basic functionalities of mintlayer compiled into wasm for various purposes, primarily interfacing with other systems and languages without having to rewrite code.

##### Note: This was tested on x86_64 Linux, and may not work on other platforms. It didn't work on M1 Mac directly (particularly the build. A pre-built wasm binary works fine on a browser, see below for more information).

## Running the tests

### To run in a web browser
### Preparation

Make sure you have wasm-pack and the wasm32-unknown-unknown target installed:

Expand All @@ -16,61 +17,85 @@ cargo install wasm-pack

Also make sure you have `clang` installed. It's required.

To build the wasm package from the crate, run (in the wasm Cargo.toml directory):

```
wasm-pack build --target web
```

**Note for mac users**: `llvm` installed by Xcode doesn't support wasm targets, but the homebrew version does, these commands may make it possible to compile to wasm targets. Note that using these commands could have other side effects on your toolchain. Please consider researching the clang toolchain and how it works before using them. We do not recommend copying and pasting commands without fully understanding the side-effects.
```
brew install llvm
AR=/opt/homebrew/opt/llvm/bin/llvm-ar CC=/opt/homebrew/opt/llvm/bin/clang wasm-pack build --target web
```

To test the wasm binary. First, install `http-server` web server (feel free to use any other web-server of your choosing):
Also, install TypeScript:
```
npm install -g typescript
```

### Compile the tests via `tsc`

In the wasm Cargo.toml directory, run:
```
cargo install http-server
tsc --project js-bindings-test/tsconfig.json
```

Then run the http server, and then choose the file `js-bindings/index.html`:
### Run the tests in a web browser

To build the wasm package from the crate, run (in the wasm Cargo.toml directory):

```
http-server --port 8080 --verbose
wasm-pack build --target web
```

If you're using a remote server, either tunnel to port 8080, or expose that port and run this (assuming you understand the security risks):
To test the wasm binary, first install `http-server` web server (feel free to use any other web-server of your choosing):

```
http-server --port 8080 --host 0.0.0.0 --verbose
cargo install http-server
```

The ported wasm functions are exported to the file `js-bindings/index.js` and used in the file `js-bindings/index.html` with a basic test/example in them using JavaScript. Use your browser's console to see the output.
Then run the http server:

### To run in Node.js
```
http-server --port 8080
```

Make sure you have wasm-pack and the wasm32-unknown-unknown target installed:
If you're using a remote server, either tunnel to port 8080, or expose that port and run this (assuming you understand the security risks):

```
rustup target add wasm32-unknown-unknown
cargo install wasm-pack
http-server --port 8080 --host 0.0.0.0
```

Also make sure you have `clang` installed. It's required.
To run test tests, choose the file `js-bindings-test/index.html` in the browser. Use browser's console to see the output.

### Run the tests in Node.js

To build the wasm package from the crate, run (in the wasm Cargo.toml directory):

```
wasm-pack build --target nodejs
```

Finally, to run the example, run:
Finally, to run the tests, run:

```
node --enable-source-maps js-bindings-test/node-entry.js
```

### Run `knip`

We use `knip` to make sure that there are no unused exports in `js-bindings-test/tests` (which could
mean that some of the tests are never run).

**Note: unused local definitions are caught by the TypeScript compiler itself, via the `noUnusedLocals` setting.**

To run `knip` locally, first install it:
```
node js-bindings/node-entry.js
npm install -g knip
```

And then run (in the wasm Cargo.toml directory):
```
(cd js-bindings-test && npx knip)
```

**Note: to explicitly exclude an export from `knip`'s report, annotate it with `/** @public */`.**

### Further documentation on wasm

- https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_wasm
Expand Down
Loading