-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathrun-fork-tests.sh
More file actions
executable file
·215 lines (186 loc) · 10.5 KB
/
Copy pathrun-fork-tests.sh
File metadata and controls
executable file
·215 lines (186 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env bash
# run-fork-tests.sh — run the base-std unit suite against a local anvil that
# dispatches Base's Rust precompiles, validating the Solidity reference against
# the live Rust impl from base/base.
#
# Both binaries (anvil + forge) come from the base-anvil fork of foundry-rs,
# which adds a single `--base` flag to NetworkConfigs that installs the B-20
# precompile suite into the EVM. Stock foundry binaries will NOT work.
#
# Workflow:
# 1. Launch anvil on $PORT with --base (registers Base precompiles).
# 2. Fund + impersonate the activation admin, activate the gated features.
# 3. Run forge --fork-url against anvil + the `fork` profile (which sets
# base = true so forge's own EVM also dispatches to Base precompiles).
# 4. Tear down anvil regardless of success / failure.
#
# Any extra arguments to this script are forwarded to `forge test`. Use them
# to scope the run (e.g. --match-contract, --match-test, -vvvv).
#
# Env vars (with defaults):
# ANVIL_BIN path to the patched anvil binary
# (default: ../base-anvil/target/release/anvil, falling
# back to debug if release is missing)
# FORGE_BIN path to the patched forge binary
# (default: `forge` next to ANVIL_BIN)
# BASE_ANVIL_IMAGE prebuilt base-anvil package image to consume instead of a
# local build, e.g. ghcr.io/base/base-anvil:main (or a
# :sha-<base/base-sha> tag). When set (and ANVIL_BIN is not
# explicitly overridden), the image's anvil + forge binaries
# are extracted and used, skipping the ~30-min local build.
# Linux-only: the image ships linux binaries (no darwin build
# is published). Requires docker. See FORK_TESTING.md.
# PORT local RPC port for anvil (default: 8546)
# ACTIVATION_ADMIN address authorized to activate features
# (default: 0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc, the
# canonical local-dev admin)
# ANVIL_LOG anvil stdout/stderr log path (default: /tmp/anvil.log)
#
# Exit codes:
# 0 forge test exit 0 (all targeted tests pass)
# 1 forge test exit non-zero (at least one targeted test fails — the
# output is the cross-validation signal)
# 2 environment problem (missing binary, port in use, anvil failed to
# start, activation tx failed)
set -euo pipefail
# ── Layout ────────────────────────────────────────────────────────────────────
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DEFAULT_ANVIL_RELEASE="$REPO_ROOT/../base-anvil/target/release/anvil"
DEFAULT_ANVIL_DEBUG="$REPO_ROOT/../base-anvil/target/debug/anvil"
# Binary resolution precedence:
# 1. Explicit ANVIL_BIN / FORGE_BIN env vars (full override).
# 2. BASE_ANVIL_IMAGE — pull the prebuilt base-anvil package image published by
# base/base on every merge to main and extract its anvil + forge binaries.
# Skips the ~30-min local build. The published image ships LINUX binaries
# only, so this path requires a Linux host (CI / Linux dev box / container);
# on macOS, build base-anvil locally instead (see FORK_TESTING.md).
# Tags: ghcr.io/base/base-anvil:main | :sha-<base/base-sha> | :main-<date>-<sha>
# 3. A local ../base-anvil release (then debug) build — the default for local dev.
if [[ -z "${ANVIL_BIN:-}" && -n "${BASE_ANVIL_IMAGE:-}" ]]; then
if [[ "$(uname -s)" != "Linux" ]]; then
echo "ERROR: BASE_ANVIL_IMAGE ($BASE_ANVIL_IMAGE) ships linux binaries that cannot" >&2
echo "run natively on $(uname -s). Build base-anvil locally (see FORK_TESTING.md)," >&2
echo "or run this script on a linux host / inside a linux container." >&2
exit 2
fi
command -v docker >/dev/null 2>&1 || { echo "ERROR: docker is required to use BASE_ANVIL_IMAGE." >&2; exit 2; }
img_dir="$(mktemp -d)"
echo "[run-fork-tests] pulling $BASE_ANVIL_IMAGE ..." >&2
docker pull "$BASE_ANVIL_IMAGE" >&2 \
|| { echo "ERROR: failed to pull $BASE_ANVIL_IMAGE (private package? try 'docker login ghcr.io')." >&2; exit 2; }
cid="$(docker create "$BASE_ANVIL_IMAGE")"
docker cp "$cid:/usr/local/bin/anvil" "$img_dir/anvil" >/dev/null
docker cp "$cid:/usr/local/bin/forge" "$img_dir/forge" >/dev/null
docker cp "$cid:/usr/local/share/base-anvil/MANIFEST.json" "$img_dir/MANIFEST.json" >/dev/null 2>&1 || true
docker rm "$cid" >/dev/null
chmod +x "$img_dir/anvil" "$img_dir/forge"
ANVIL_BIN="$img_dir/anvil"
: "${FORGE_BIN:="$img_dir/forge"}"
# Surface provenance so a green run is never mistaken for "current base/base".
if [[ -f "$img_dir/MANIFEST.json" ]] && command -v jq >/dev/null 2>&1; then
echo "[run-fork-tests] image built against base/base $(jq -r '.base.sha // "?"' "$img_dir/MANIFEST.json") (base-std smoke-tested: $(jq -r '.base_std.smoke_tested // "?"' "$img_dir/MANIFEST.json"))" >&2
fi
fi
if [[ -z "${ANVIL_BIN:-}" ]]; then
if [[ -x "$DEFAULT_ANVIL_RELEASE" ]]; then
ANVIL_BIN="$DEFAULT_ANVIL_RELEASE"
elif [[ -x "$DEFAULT_ANVIL_DEBUG" ]]; then
ANVIL_BIN="$DEFAULT_ANVIL_DEBUG"
else
echo "ERROR: anvil binary not found. Expected at:" >&2
echo " $DEFAULT_ANVIL_RELEASE" >&2
echo " $DEFAULT_ANVIL_DEBUG" >&2
echo "Build with: cd ../base-anvil && cargo build --release -p anvil -p forge" >&2
echo "Or set ANVIL_BIN=/path/to/anvil." >&2
exit 2
fi
fi
if [[ -z "${FORGE_BIN:-}" ]]; then
FORGE_BIN="$(dirname "$ANVIL_BIN")/forge"
if [[ ! -x "$FORGE_BIN" ]]; then
echo "ERROR: patched forge binary not found at $FORGE_BIN." >&2
echo "Build with: cd $(dirname "$ANVIL_BIN")/../.. && cargo build --release -p forge" >&2
echo "Or set FORGE_BIN=/path/to/forge." >&2
echo "(System forge will NOT work — it lacks the --base injection." >&2
echo " forge must come from the base-anvil fork of foundry-rs.)" >&2
exit 2
fi
fi
PORT="${PORT:-8546}"
ACTIVATION_ADMIN="${ACTIVATION_ADMIN:-0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc}"
REGISTRY=0x8453000000000000000000000000000000000001
LOG_FILE="${ANVIL_LOG:-/tmp/anvil.log}"
# Feature IDs mirror the canonical set in test/lib/mocks/ActivationRegistryFeatureList.sol
# (the Solidity reference is the source of truth). If a feature is added there, append its ID here.
FEATURE_IDS=(
0xcdcc772fe4cbdb1029f822861176d09e646db96723d4c1e82ddfdeb8163ef54c # B20_ASSET
0xb582ebae03f16fee49a6763f78df482fb11ae73f103ed0d330bbe556aa90a43f # POLICY_REGISTRY
0xecfa0def2c10020caaf65e6155aa69c84b24892aaef76eeac52e0e2b3a0b8601 # B20_STABLECOIN
)
# ── Helpers ───────────────────────────────────────────────────────────────────
log() { echo "[run-fork-tests] $*" >&2; }
die() { echo "[run-fork-tests] ERROR: $*" >&2; exit 2; }
rpc() {
local method="$1"; shift
local params="$1"; shift
curl -s -X POST -H "Content-Type: application/json" \
--data "{\"jsonrpc\":\"2.0\",\"method\":\"$method\",\"params\":$params,\"id\":1}" \
"http://localhost:$PORT"
}
# ── Pre-flight ────────────────────────────────────────────────────────────────
command -v cast >/dev/null 2>&1 || die "cast not found (install foundry: https://getfoundry.sh)"
command -v curl >/dev/null 2>&1 || die "curl not found"
if lsof -i ":$PORT" >/dev/null 2>&1; then
die "port $PORT is already in use. Set PORT=<other> or kill the existing listener."
fi
log "anvil: $ANVIL_BIN"
log "forge: $FORGE_BIN"
log "port: $PORT"
log "activation admin: $ACTIVATION_ADMIN"
log "log file: $LOG_FILE"
# ── Launch anvil ──────────────────────────────────────────────────────────────
log "starting anvil…"
"$ANVIL_BIN" --base --base-activation-admin "$ACTIVATION_ADMIN" --port "$PORT" \
> "$LOG_FILE" 2>&1 &
ANVIL_PID=$!
trap 'kill $ANVIL_PID 2>/dev/null; wait $ANVIL_PID 2>/dev/null; true' EXIT
# Poll for the RPC port to come up (up to 10s).
for i in $(seq 1 20); do
if rpc eth_chainId '[]' 2>/dev/null | grep -q '"result"'; then
break
fi
sleep 0.5
if ! kill -0 $ANVIL_PID 2>/dev/null; then
echo "--- last 20 lines of $LOG_FILE ---" >&2
tail -20 "$LOG_FILE" >&2
die "anvil exited during startup; see $LOG_FILE"
fi
done
log "anvil up (pid=$ANVIL_PID)"
# ── Activate features ────────────────────────────────────────────────────────
# Anvil's --unlocked + anvil_impersonateAccount lets us send activate() calls
# from the admin address without needing its private key. Real-chain forks
# would substitute --private-key + a funded signer.
log "funding + impersonating activation admin…"
rpc anvil_setBalance "[\"$ACTIVATION_ADMIN\", \"0xffffffffffffffff\"]" > /dev/null
rpc anvil_impersonateAccount "[\"$ACTIVATION_ADMIN\"]" > /dev/null
for fid in "${FEATURE_IDS[@]}"; do
log "activating feature $fid"
out=$(cast send --rpc-url "http://localhost:$PORT" --from "$ACTIVATION_ADMIN" \
--unlocked "$REGISTRY" "activate(bytes32)" "$fid" 2>&1) || \
die "activation tx failed for $fid:\n$out"
# status==1 line confirms inclusion + success
echo "$out" | grep -E "^status\b" | head -1 >&2 || die "no status in cast send output for $fid"
done
# ── Run the test suite ────────────────────────────────────────────────────────
log "running forge test --fork-url http://localhost:$PORT $*"
cd "$REPO_ROOT"
# LIVE_PRECOMPILES skips BaseTest's vm.etch of the mocks at the precompile
# addresses (so calls dispatch to the real Rust impls). FOUNDRY_PROFILE=fork
# enables the [profile.fork] base=true setting (so forge's EVM installs the
# Base precompile set).
LIVE_PRECOMPILES=true FOUNDRY_PROFILE=fork \
"$FORGE_BIN" test --fork-url "http://localhost:$PORT" "$@"
forge_exit=$?
log "forge test exited $forge_exit"
exit $forge_exit