|
| 1 | +name: WASM Size Budget |
| 2 | + |
| 3 | +# Enforces per-contract WASM size budgets so accidental bloat is caught in CI |
| 4 | +# before it reaches mainnet deployment. Budgets are intentionally conservative |
| 5 | +# and should be updated via a deliberate PR if a contract genuinely needs more |
| 6 | +# space (e.g. a new feature module). |
| 7 | +# |
| 8 | +# Budget table (release build, wasm32v1-none, opt-level=z + LTO): |
| 9 | +# hello-world : 20 KB – toy example, must stay tiny |
| 10 | +# governance-events : 24 KB – pure event types + emitters, no logic |
| 11 | +# predictify-hybrid : 900 KB – full prediction-market contract |
| 12 | +# |
| 13 | +# The governance-events crate was added in FWC26 (Stellar Wave). |
| 14 | + |
| 15 | +on: |
| 16 | + push: |
| 17 | + branches: [ "master", "main" ] |
| 18 | + pull_request: |
| 19 | + branches: [ "master", "main" ] |
| 20 | + |
| 21 | +jobs: |
| 22 | + check-size: |
| 23 | + runs-on: macos-latest |
| 24 | + |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Install Rust |
| 29 | + run: | |
| 30 | + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y |
| 31 | + source $HOME/.cargo/env |
| 32 | + rustup target add wasm32v1-none |
| 33 | +
|
| 34 | + - name: Install Stellar CLI |
| 35 | + run: | |
| 36 | + brew update |
| 37 | + brew install stellar-cli |
| 38 | +
|
| 39 | + - name: Build contracts (release, wasm32v1-none) |
| 40 | + run: | |
| 41 | + source $HOME/.cargo/env |
| 42 | + stellar contract build |
| 43 | +
|
| 44 | + # ── Per-contract size gates ────────────────────────────────────────────── |
| 45 | + # Each WASM is checked against its own budget. Any file that does not |
| 46 | + # match a known contract name triggers a strict 20 KB fallback so unknown |
| 47 | + # contracts cannot sneak through with unbounded size. |
| 48 | + |
| 49 | + - name: Check WASM sizes |
| 50 | + run: | |
| 51 | + set -euo pipefail |
| 52 | + source $HOME/.cargo/env |
| 53 | +
|
| 54 | + # Budgets in bytes (release + opt-level=z + LTO) |
| 55 | + BUDGET_HELLO_WORLD=20480 # 20 KB |
| 56 | + BUDGET_GOVERNANCE_EVENTS=24576 # 24 KB (FWC26 governance-events crate) |
| 57 | + BUDGET_PREDICTIFY_HYBRID=921600 # 900 KB |
| 58 | + BUDGET_FALLBACK=20480 # 20 KB (any unknown contract) |
| 59 | +
|
| 60 | + FAIL=0 |
| 61 | +
|
| 62 | + for wasm_file in target/wasm32v1-none/release/*.wasm; do |
| 63 | + [ -f "$wasm_file" ] || continue |
| 64 | +
|
| 65 | + SIZE=$(stat -f%z "$wasm_file") |
| 66 | + BASE=$(basename "$wasm_file" .wasm) |
| 67 | +
|
| 68 | + case "$BASE" in |
| 69 | + hello_world) |
| 70 | + BUDGET=$BUDGET_HELLO_WORLD ;; |
| 71 | + governance_events) |
| 72 | + BUDGET=$BUDGET_GOVERNANCE_EVENTS ;; |
| 73 | + predictify_hybrid) |
| 74 | + BUDGET=$BUDGET_PREDICTIFY_HYBRID ;; |
| 75 | + *) |
| 76 | + BUDGET=$BUDGET_FALLBACK ;; |
| 77 | + esac |
| 78 | +
|
| 79 | + BUDGET_KB=$(( BUDGET / 1024 )) |
| 80 | + SIZE_KB=$(echo "scale=1; $SIZE / 1024" | bc) |
| 81 | +
|
| 82 | + if [ "$SIZE" -gt "$BUDGET" ]; then |
| 83 | + echo "::error file=$wasm_file::WASM size budget exceeded: $BASE = ${SIZE_KB} KB (budget ${BUDGET_KB} KB)" |
| 84 | + FAIL=1 |
| 85 | + else |
| 86 | + echo "OK $BASE: ${SIZE_KB} KB / ${BUDGET_KB} KB" |
| 87 | + fi |
| 88 | + done |
| 89 | +
|
| 90 | + if [ "$FAIL" -eq 1 ]; then |
| 91 | + echo "" |
| 92 | + echo "One or more contracts exceeded their WASM size budget." |
| 93 | + echo "If the growth is intentional, update the budget table in" |
| 94 | + echo ".github/workflows/wasm-size.yml with a justification comment." |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | +
|
| 98 | + echo "" |
| 99 | + echo "All contracts are within their WASM size budgets." |
| 100 | +
|
| 101 | + # ── Size report (informational, never fails the build) ─────────────────── |
| 102 | + - name: Report WASM sizes |
| 103 | + if: always() |
| 104 | + run: | |
| 105 | + echo "### WASM size report" >> $GITHUB_STEP_SUMMARY |
| 106 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 107 | + echo "| Contract | Size |" >> $GITHUB_STEP_SUMMARY |
| 108 | + echo "|---|---|" >> $GITHUB_STEP_SUMMARY |
| 109 | + for wasm_file in target/wasm32v1-none/release/*.wasm; do |
| 110 | + [ -f "$wasm_file" ] || continue |
| 111 | + SIZE=$(stat -f%z "$wasm_file") |
| 112 | + SIZE_KB=$(echo "scale=1; $SIZE / 1024" | bc) |
| 113 | + BASE=$(basename "$wasm_file" .wasm) |
| 114 | + echo "| \`$BASE\` | ${SIZE_KB} KB |" >> $GITHUB_STEP_SUMMARY |
| 115 | + done |
0 commit comments