Skip to content

Commit

Permalink
feat: optimize bitseed_generator
Browse files Browse the repository at this point in the history
  • Loading branch information
yubing744 committed Sep 3, 2024
1 parent df9a1a2 commit 6079059
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/bitseed_generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/artifacts
18 changes: 18 additions & 0 deletions examples/bitseed_generator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.PHONY: all clean build test optimize

all: build

build:
cargo wasm --target-dir="./target"

test:
cargo test

optimize: build
docker run -u 1000 --rm -v "$(CURDIR)":/code \
--entrypoint /code/scripts/optimize.sh \
cosmwasm/optimizer:0.16.0 .

clean:
rm -rf ./artifacts && rm -rf ./target

59 changes: 59 additions & 0 deletions examples/bitseed_generator/scripts/optimize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/ash
# shellcheck shell=dash
# See https://www.shellcheck.net/wiki/SC2187
set -o errexit -o nounset -o pipefail
command -v shellcheck >/dev/null && shellcheck "$0"

export PATH="$PATH:/root/.cargo/bin"

# Debug toolchain and default Rust version
rustup toolchain list
cargo --version

# Prepare artifacts directory for later use
mkdir -p artifacts

# Delete previously built artifacts. Those can exist if the image is called
# with a cache mounted to /target. In cases where contracts are removed over time,
# old builds in cache should not be contained in the result of the next build.
rm -f /target/wasm32-unknown-unknown/release/*.wasm

# There are two cases here
# 1. The contract is included in the root workspace (eg. `cosmwasm-template`)
# In this case, we pass no argument, just mount the proper directory.
# 2. Contracts are excluded from the root workspace, but import relative paths from other packages (only `cosmwasm`).
# In this case, we mount root workspace and pass in a path `docker run <repo> ./contracts/hackatom`

# This parameter allows us to mount a folder into docker container's "/code"
# and build "/code/contracts/mycontract".
# The default value for $1 is "." (see CMD in the Dockerfile).

# Ensure we get exactly one argument and this is a directory (the path to the Cargo project to be built)
if [ "$#" -ne 1 ] || ! [ -d "$1" ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
PROJECTDIR="$1"

echo "Optimizing artifacts ..."
for WASM in /code/target/wasm32-unknown-unknown/release/*.wasm; do
[ -e "$WASM" ] || continue # https://superuser.com/a/519493

OUT_FILENAME=$(basename "$WASM")
echo "Optimizing $OUT_FILENAME ..."
# --signext-lowering is needed to support blockchains runnning CosmWasm < 1.3. It can be removed eventually
wasm-opt -Os --signext-lowering "$WASM" -o "artifacts/$OUT_FILENAME"
done

echo "Post-processing artifacts..."
(
cd artifacts

if test -n "$(find . -maxdepth 1 -name '*.wasm' -print -quit)"; then
sha256sum -- *.wasm | tee checksums.txt
else
echo "Warn: No .wasm file built. Check your build configuration in Cargo.toml."
fi
)

echo "Done."

0 comments on commit 6079059

Please sign in to comment.