Skip to content

Commit 1a396dc

Browse files
Merge pull request #100 from Code-Paragon/feature/issue-51-wasm-compression
build: Automate Contract WASM Compression Pipeline (#51)
2 parents c298e92 + 0a12134 commit 1a396dc

5 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: WASM Build & Compress
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
build_and_compress:
11+
runs-on: ubuntu-latest
12+
13+
defaults:
14+
run:
15+
working-directory: contracts/substream_contracts
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Set up Rust
21+
uses: dtolnay/rust-toolchain@stable
22+
with:
23+
targets: wasm32-unknown-unknown
24+
25+
- name: Install wasm-opt
26+
run: npm install -g wasm-opt
27+
28+
- name: Make script executable
29+
run: chmod +x scripts/compress_wasm.sh
30+
31+
- name: Build and Compress WASM
32+
run: make build-compressed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,13 @@ lto = true
2222
[profile.release-with-logs]
2323
inherits = "release"
2424
debug-assertions = true
25+
26+
# ==========================================
27+
# Issue #51: WASM Size Optimization Profile
28+
# ==========================================
29+
[profile.release]
30+
opt-level = "z" # Optimize for size
31+
lto = true # Enable Link Time Optimization (removes dead code)
32+
codegen-units = 1 # Compile crate as a single unit for maximum optimization
33+
panic = "abort" # Remove panic unwinding (saves significant space)
34+
strip = "debuginfo" # Strip debug info but keep symbols for error tracing
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
# Exit immediately if a command exits with a non-zero status
3+
set -e
4+
5+
echo "🔨 Building release WASM..."
6+
cargo build --target wasm32-unknown-unknown --release
7+
8+
# Find the generated WASM file
9+
WASM_FILE=$(find target/wasm32-unknown-unknown/release -maxdepth 1 -name "*.wasm" | grep -v "_opt" | head -n 1)
10+
if [ -z "$WASM_FILE" ]; then
11+
echo "❌ Error: Could not find compiled WASM file in target directory."
12+
exit 1
13+
fi
14+
15+
# Define the output optimized file name
16+
OPT_WASM_FILE="${WASM_FILE%.wasm}_opt.wasm"
17+
18+
# Check for wasm-opt dependency
19+
if ! command -v wasm-opt &> /dev/null; then
20+
echo "⚠️ wasm-opt could not be found."
21+
echo "Installing via npm (requires Node.js)..."
22+
npm install -g wasm-opt
23+
fi
24+
25+
echo "🗜️ Optimizing WASM with wasm-opt..."
26+
# -Oz: aggressive size optimization
27+
# --signext-lowering: required for Soroban compatibility
28+
wasm-opt -Oz "$WASM_FILE" -o "$OPT_WASM_FILE"
29+
30+
# Calculate and display savings
31+
ORIGINAL_SIZE=$(wc -c <"$WASM_FILE")
32+
OPT_SIZE=$(wc -c <"$OPT_WASM_FILE")
33+
SAVINGS=$(( ORIGINAL_SIZE - OPT_SIZE ))
34+
PERCENTAGE=$(( SAVINGS * 100 / ORIGINAL_SIZE ))
35+
36+
echo "=========================================="
37+
echo "✅ WASM Compression Complete!"
38+
echo "Original Size: $ORIGINAL_SIZE bytes"
39+
echo "Optimized Size: $OPT_SIZE bytes"
40+
echo "Space Saved: $SAVINGS bytes ($PERCENTAGE%)"
41+
echo "Output File: $OPT_WASM_FILE"
42+
echo "=========================================="
43+
44+
# Check 64KB Soroban limit guardrail
45+
if [ "$OPT_SIZE" -gt 65536 ]; then
46+
echo "⚠️ WARNING: Optimized WASM is still over the 64KB Soroban deployment limit!"
47+
fi

0 commit comments

Comments
 (0)