Skip to content

Commit

Permalink
feat: run extra fuzz rounds for new fuzz tests
Browse files Browse the repository at this point in the history
Adds a new script that can run extra fuzz test iterations when
new fuzz tests are added. Can be used in CI or locally to make
sure that new fuzz tests aren't flaky.
  • Loading branch information
smartcontracts committed Sep 5, 2024
1 parent 31f408b commit b6d3b01
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
13 changes: 12 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ jobs:
docker:
- image: <<pipeline.parameters.ci_builder_image>>
resource_class: xlarge
parameters:
test_command:
type: string
default: "just test"
steps:
- checkout
- check-changed:
Expand All @@ -545,7 +549,7 @@ jobs:
working_directory: packages/contracts-bedrock
- run:
name: run tests
command: just test
command: << parameters.test_command >>
environment:
FOUNDRY_PROFILE: ci
working_directory: packages/contracts-bedrock
Expand Down Expand Up @@ -1490,6 +1494,13 @@ workflows:
- pnpm-monorepo:
name: pnpm-monorepo
- contracts-bedrock-tests
name: contracts-bedrock-tests
parameters:
test_command: "just test"
- contracts-bedrock-tests
name: contracts-bedrock-tests-extra-fuzz-runs
parameters:
test_command: "just test-extra-fuzz-runs"
- contracts-bedrock-coverage
- contracts-bedrock-checks:
requires:
Expand Down
4 changes: 4 additions & 0 deletions packages/contracts-bedrock/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ test-kontrol-no-build:
test-rerun: build-go-ffi
forge test --rerun -vvv

# Run extra fuzz iterations for new fuzz tests.
test-extra-fuzz-runs: build-go-ffi
./scripts/testing/test-extra-fuzz-runs.sh

genesis:
forge script scripts/L2Genesis.s.sol:L2Genesis --sig 'runWithStateDump()'

Expand Down
36 changes: 36 additions & 0 deletions packages/contracts-bedrock/scripts/testing/test-extra-fuzz-runs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash
set -euo pipefail

# This script is used to run extra fuzz test iterations on any fuzz tests that
# have been newly added in a PR. We typically want to run extra fuzz iterations
# when new tests are added to make sure that they are not flaky with some small
# percentage of fuzz runs.

# Set the number of fuzz runs to run
# 350000 fuzz runs will guarantee that any test that fails 1% of the time with
# the default 512 fuzz runs will fail 99.9% of the time (on average) inside of
# this script.
FUZZ_RUNS=${1:-350000}

# show where the script failed in this next line!
trap 'echo "Script failed at line $LINENO" && echo "Error: $BASH_COMMAND"' ERR

# Get the contracts base directory
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
CONTRACTS_BASE=$(dirname "$(dirname "$SCRIPT_DIR")")

# Determine if there are any new fuzz tests
NEW_TEST_NAMES=$(git diff origin/develop...HEAD -- '*.sol' | grep -E '^\+.*function testFuzz_' | awk '{print $3}' | sed 's/.*function //; s/(.*//' || true)
NEW_TEST_NAMES=$(echo "$NEW_TEST_NAMES" | tr '\n' ' ' | xargs)

# Exit if no new fuzz tests are found
if [ -z "$NEW_TEST_NAMES" ]; then
echo "No new fuzz tests found"
exit 0
fi

# Navigate to the contracts base directory
cd "$CONTRACTS_BASE"

# Run matching tests with extra fuzz runs
forge test --match-test "${NEW_TEST_NAMES// /|}" --fuzz-runs $FUZZ_RUNS

0 comments on commit b6d3b01

Please sign in to comment.