Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: added wasm optimization #835

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
etc/tests/uniswap
- name: Install dependencies
run: cargo +stable make -V || cargo +stable install cargo-make
- name: Install wasm-opt
run: scripts/ci/install-wasm-opt.sh
- name: Build main contract
run: |
case ${{ matrix.profile }} in
Expand Down Expand Up @@ -81,6 +83,8 @@ jobs:
key: ${{ matrix.profile }}-cargo-modexp-test
- name: Install dependencies
run: cargo +stable make -V || cargo +stable install cargo-make
- name: Install wasm-opt
run: scripts/ci/install-wasm-opt.sh
- name: Test ${{ matrix.profile }} bench-modexp
run: cargo make --profile ${{ matrix.profile }} bench-modexp

Expand Down
12 changes: 12 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ dependencies = [
"build-engine",
"make-bin-directory",
"copy-engine-build",
"wasm-opt",
"post-engine-build-env",
]

Expand All @@ -270,6 +271,7 @@ dependencies = [
"build-engine",
"make-bin-directory",
"copy-engine-build",
"wasm-opt",
"post-engine-build-env",
]

Expand All @@ -288,6 +290,16 @@ env = { "RUSTFLAGS" = "${RUSTC_FLAGS_BUILD}", "CARGO_FEATURES" = "${CARGO_FEATUR
category = "Build"
run_task = "build-engine-flow"

[tasks.wasm-opt]
condition = { profiles = ["mainnet", "testnet", "mainnet-silo", "testnet-silo", "custom"] }
category = "Build"
script = '''
filesize_before=$(wc -c bin/${WASM_FILE} | awk '{print $1}')
wasm-opt -O4 bin/${WASM_FILE} -o bin/${WASM_FILE} --strip-debug --vacuum
filesize_after=$(wc -c bin/${WASM_FILE} | awk '{print $1}')
echo "File size before: [$filesize_before] and after: [$filesize_after]"
'''

[tasks.build]
condition = { profiles = ["mainnet", "mainnet-silo", "testnet", "testnet-silo", "localnet", "development", "custom"] }
env = { "RUSTFLAGS" = "-C strip=symbols --remap-path-prefix ${HOME}=/path/to/home/ --remap-path-prefix ${PWD}=/path/to/source/", "CARGO_FEATURES" = "${CARGO_FEATURES_BUILD}", "RELEASE" = "--release", "TARGET_DIR" = "release" }
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ documentation.

- Node.js (v14+)
- cargo-make
- wasm-opt (<= v110)

```sh
cargo install --force cargo-make
Expand All @@ -37,6 +38,27 @@ cargo install --force cargo-make
- Node.js (v14+)
- Docker
- cargo-make
- wasm-opt (<= v110)

### Prerequisite wasm-opt

For WebAssembly optimization we use `wasm-opt`.
The supported version of [Binaryen toolchain for WebAssembly](https://github.com/WebAssembly/binaryen)
is `<= v110`. A higher version is not supported.

We recommend installing the release:
https://github.com/WebAssembly/binaryen/releases/tag/version_110

`wasm-opt` command should be available for the build process.

Verify version:

```bash
$ wasm-opt --version
wasm-opt version 110 (version_110)
```

Please keep in mind, that `wasm-opt` runs automatically when you run `cargo make build-*` and `cargo make test` commands.

## Development

Expand Down Expand Up @@ -130,6 +152,20 @@ here:
cargo make check
```

#### Running WebAssembly optimization

In common cases, you don't need to run `wasm-opt` manually, because
it's part of builds and tests.

But for development reasons only you can run:
- `wasm-opt` , runs WebAssembly optimization for pre-build wasm files for specific profile. Requires a
`--profile` argument.

For example, the following will run wasm-opt for pre-build mainnet binary:
```sh
cargo make --profile mainnet wasm-opt
```

#### Cleanup

The following tasks are available to clean up the workspace:
Expand Down
15 changes: 6 additions & 9 deletions engine-tests/src/tests/repro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn repro_GdASJ3KESs() {
block_timestamp: 1_645_717_564_644_206_730,
input_path: "src/tests/res/input_GdASJ3KESs.hex",
evm_gas_used: 706_713,
near_gas_used: 115,
near_gas_used: 112,
});
}

Expand All @@ -51,7 +51,7 @@ fn repro_8ru7VEA() {
block_timestamp: 1_648_829_935_343_349_589,
input_path: "src/tests/res/input_8ru7VEA.hex",
evm_gas_used: 1_732_181,
near_gas_used: 206,
near_gas_used: 201,
});
}

Expand All @@ -71,10 +71,7 @@ fn repro_FRcorNv() {
block_timestamp: 1_650_960_438_774_745_116,
input_path: "src/tests/res/input_FRcorNv.hex",
evm_gas_used: 1_239_721,
#[cfg(not(feature = "ext-connector"))]
near_gas_used: 168,
#[cfg(feature = "ext-connector")]
near_gas_used: 167,
near_gas_used: 163,
});
}

Expand All @@ -91,7 +88,7 @@ fn repro_5bEgfRQ() {
block_timestamp: 1_651_073_772_931_594_646,
input_path: "src/tests/res/input_5bEgfRQ.hex",
evm_gas_used: 6_414_105,
near_gas_used: 650,
near_gas_used: 602,
});
}

Expand All @@ -109,7 +106,7 @@ fn repro_D98vwmi() {
block_timestamp: 1_651_753_443_421_003_245,
input_path: "src/tests/res/input_D98vwmi.hex",
evm_gas_used: 1_035_348,
near_gas_used: 169,
near_gas_used: 164,
});
}

Expand All @@ -128,7 +125,7 @@ fn repro_Emufid2() {
block_timestamp: 1_662_118_048_636_713_538,
input_path: "src/tests/res/input_Emufid2.hex",
evm_gas_used: 1_156_364,
near_gas_used: 294,
near_gas_used: 292,
});
}

Expand Down
10 changes: 5 additions & 5 deletions engine-tests/src/tests/uniswap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn test_uniswap_input_multihop() {

let (_amount_out, _evm_gas, profile) = context.exact_input(&tokens, INPUT_AMOUNT.into());

assert_eq!(108, profile.all_gas() / 1_000_000_000_000);
assert_eq!(106, profile.all_gas() / 1_000_000_000_000);
}

#[test]
Expand All @@ -53,17 +53,17 @@ fn test_uniswap_exact_output() {
utils::assert_gas_bound(profile.all_gas(), 32);
let wasm_fraction = 100 * profile.wasm_gas() / profile.all_gas();
assert!(
(40..=50).contains(&wasm_fraction),
"{wasm_fraction}% is not between 40% and 50%",
(30..=40).contains(&wasm_fraction),
"{wasm_fraction}% is not between 30% and 40%",
);

let (_amount_in, profile) =
context.exact_output_single(&token_a, &token_b, OUTPUT_AMOUNT.into());
utils::assert_gas_bound(profile.all_gas(), 17);
let wasm_fraction = 100 * profile.wasm_gas() / profile.all_gas();
assert!(
(40..=50).contains(&wasm_fraction),
"{wasm_fraction}% is not between 40% and 50%",
(30..=40).contains(&wasm_fraction),
"{wasm_fraction}% is not between 30% and 40%",
);
}

Expand Down
10 changes: 10 additions & 0 deletions scripts/ci/install-wasm-opt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

VERSION=119

if [[ ! -f wasm-opt ]]; then
mkdir binaryen
curl -sL https://github.com/WebAssembly/binaryen/releases/download/version_$VERSION/binaryen-version_$VERSION-x86_64-linux.tar.gz | tar -xz -C binaryen
cp binaryen/binaryen-version_$VERSION/bin/* /usr/local/bin
rm -rf binaryen
fi
Loading