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 15 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
run: cache-util restore cargo_git cargo_registry yarn_cache rocksdb:/root/rocksdb
- name: Preparing rocksdb library
run: scripts/ci/build_rocksdb.sh
- name: Install wasm-opt
run: scripts/ci/install-wasm-opt.sh
- name: Build contracts
run: cargo make build-contracts
- name: Test contracts
Expand Down Expand Up @@ -55,6 +57,8 @@ jobs:
cache-util restore aurora-engine-target@modexp@${{ hashFiles('**/Cargo.lock') }}:target
- name: Preparing rocksdb library
run: scripts/ci/build_rocksdb.sh
- name: Install wasm-opt
run: scripts/ci/install-wasm-opt.sh
- name: Test mainnet bench-modexp
run: cargo make --profile mainnet bench-modexp
- name: Test testnet bench-modexp
Expand Down
12 changes: 12 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ dependencies = [
"build-engine",
"make-bin-directory",
"copy-engine-build",
"wasm-opt",
"post-engine-build-env",
]

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

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

[tasks.wasm-opt]
condition = { profiles = ["mainnet", "testnet", "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", "testnet", "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 @@ -125,6 +147,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

To clean up the workspace, the following tasks are available:
Expand Down
12 changes: 6 additions & 6 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: 121,
near_gas_used: 116,
});
}

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: 219,
near_gas_used: 212,
});
}

Expand All @@ -71,7 +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,
near_gas_used: 177,
near_gas_used: 171,
});
}

Expand All @@ -88,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: 648,
near_gas_used: 604,
});
}

Expand All @@ -106,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: 179,
near_gas_used: 173,
});
}

Expand All @@ -125,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: 282,
near_gas_used: 277,
});
}

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

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

assert_eq!(114, profile.all_gas() / 1_000_000_000_000);
assert_eq!(111, profile.all_gas() / 1_000_000_000_000);
}

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

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

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