Skip to content

Commit

Permalink
ci: add job to check contract sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicopernas committed Mar 11, 2024
1 parent 7777f17 commit 43dd83f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/contracts_size.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Contract sizes

on:
pull_request:
branches:
- main
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Check contract sizes
run: forge build --sizes | ./check_contracts_size.sh
28 changes: 28 additions & 0 deletions check_contracts_size.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
set -euo pipefail

# Simple script to parse the output of `forge build --sizes` and error out if a contract's size goes beyond
# the allowed limit.
# Use it with:
# $ forge build --sizes | ./check_contracts_size.sh
# $ ./check_contracts_size.sh < sizes.txt
# $ ./check_contracts_size.sh sizes.txt


# this matches lines like this
# '| Dispatcher | 25.698 | -1.122 |'
pattern='^[ |]+([^ ]+)[ |]+([-]?[0-9\.]+)[ |]+([-]?[0-9\.]+)[ |]+$'

while read -r line; do
if [[ "$line" =~ $pattern ]]; then
name="${BASH_REMATCH[1]}"
size="${BASH_REMATCH[2]}"
margin="${BASH_REMATCH[3]}"

# use bc because bash doesn't like floating point math
if [ "$( bc <<< "$margin < 0" )" = '1' ]; then
echo "Contract '$name' ($size kB) is too large to be deployed"
exit 1
fi
fi
done < "${1:-/dev/stdin}"

0 comments on commit 43dd83f

Please sign in to comment.