Skip to content

Commit dca7208

Browse files
feat: ci storage check required (#1062)
* feat: only throw for errors - except __gap overwrite * ci: storage check should fail * ci: storage check should fail * perf: optimize storage ci * fix: remove bad variable * chore: forge fmt
1 parent 0bec250 commit dca7208

File tree

2 files changed

+52
-41
lines changed

2 files changed

+52
-41
lines changed

.github/workflows/foundry.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,6 @@ jobs:
122122
with:
123123
version: stable
124124

125-
# Build the project and display contract sizes.
126-
- name: "Forge Build"
127-
run: |
128-
forge --version
129-
forge build --sizes
130-
id: build
131-
132125
# Run storage diff check to detect storage layout incompatibilities.
133126
- name: "Mainnet Storage Diff"
134127
run: |

bin/storage-diff.sh

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ETHERSCAN_API_KEY="1234567890123456789012345678901234567890"
66
INPUT_FILE="contracts.json"
77
QUIET=false
88
TOTAL_ISSUES=0
9+
TOTAL_ERRORS=0
910

1011
# Help message
1112
usage() {
@@ -127,7 +128,8 @@ analyze_storage_changes() {
127128
local onchain_file=$1
128129
local local_file=$2
129130
local contract_name=$3
130-
local issues_found=0
131+
local TOTAL_ERRORS=0 # Changed from issues_found to TOTAL_ERRORS
132+
local warnings_found=0 # New counter for non-critical changes
131133

132134
# Get the storage layouts as arrays
133135
local onchain_slots=$(jq -r '.storage[] | "\(.slot)|\(.label)|\(.offset)|\(.type)"' "$onchain_file")
@@ -160,15 +162,14 @@ analyze_storage_changes() {
160162
while IFS='|' read -r slot local_label local_offset local_type; do
161163
if [[ -z "$slot" ]]; then continue; fi
162164

163-
# Look for matching slot in onchain
164165
onchain_line=$(grep "^${slot}|" "$onchain_map_file")
165166
if [[ -n "$onchain_line" ]]; then
166167
IFS='|' read -r _ onchain_label onchain_offset onchain_type <<< "$onchain_line"
167168

168169
if [[ "$local_label" != "$onchain_label" && "$local_type" == "$onchain_type" && "$local_offset" == "$onchain_offset" ]]; then
169170
echo "${slot}|${onchain_label}|${local_label}|${local_type}" >> "$renamed_vars_file"
170171
echo "$slot" >> "$processed_slots_file"
171-
issues_found=$((issues_found + 1))
172+
warnings_found=$((warnings_found + 1)) # Renames are just warnings
172173
fi
173174
fi
174175
done < "$local_map_file"
@@ -185,45 +186,53 @@ analyze_storage_changes() {
185186
while IFS='|' read -r slot local_label local_offset local_type; do
186187
if [[ -z "$slot" ]]; then continue; fi
187188

188-
# Skip if this slot was processed as a rename
189189
if grep -q "^${slot}$" "$processed_slots_file"; then
190190
continue
191191
fi
192192

193-
# Look for matching slot in onchain
194193
onchain_line=$(grep "^${slot}|" "$onchain_map_file")
195194
if [[ -z "$onchain_line" ]]; then
196-
# New variable added
195+
# New variable added - just a warning
197196
slots_needed=$(calculate_slots "$local_type")
198197
echo -e "\033[32m✨ New variable added: $local_label ($local_type) at slot $slot\033[0m"
199-
issues_found=$((issues_found + 1))
198+
warnings_found=$((warnings_found + 1))
200199
if [ "$slots_needed" -gt 1 ]; then
201200
echo -e "\033[33m 📦 This variable occupies $slots_needed slots\033[0m"
202201
fi
203202
else
204203
IFS='|' read -r _ onchain_label onchain_offset onchain_type <<< "$onchain_line"
205204

206205
if [[ "$local_label" != "$onchain_label" ]]; then
207-
echo -e "\033[31m🚨 Storage slot override detected at slot $slot:\033[0m"
208-
echo -e "\033[31m Previous: $onchain_label ($onchain_type)\033[0m"
209-
echo -e "\033[32m New: $local_label ($local_type)\033[0m"
210-
issues_found=$((issues_found + 1))
211-
212-
# Calculate potential impact
213-
old_slots=$(calculate_slots "$onchain_type")
214-
new_slots=$(calculate_slots "$local_type")
215-
slot_diff=$((new_slots - old_slots))
216-
217-
if [ "$slot_diff" -gt 0 ]; then
218-
echo -e "\033[33m ⚠️ This change will shift subsequent storage slots by +$slot_diff positions\033[0m"
219-
elif [ "$slot_diff" -lt 0 ]; then
220-
echo -e "\033[33m 💡 This change will reduce storage usage by $((slot_diff * -1)) slots\033[0m"
206+
# Only treat as critical error if we're not overriding a gap variable
207+
if [[ "$onchain_label" != "__gap" ]]; then
208+
# Storage slot override is a critical error
209+
echo -e "\033[31m🚨 CRITICAL: Storage slot override detected at slot $slot:\033[0m"
210+
echo -e "\033[31m Previous: $onchain_label ($onchain_type)\033[0m"
211+
echo -e "\033[32m New: $local_label ($local_type)\033[0m"
212+
TOTAL_ERRORS=$((TOTAL_ERRORS + 1))
213+
214+
old_slots=$(calculate_slots "$onchain_type")
215+
new_slots=$(calculate_slots "$local_type")
216+
slot_diff=$((new_slots - old_slots))
217+
218+
if [ "$slot_diff" -gt 0 ]; then
219+
echo -e "\033[31m ⚠️ CRITICAL: This change will shift subsequent storage slots by +$slot_diff positions\033[0m"
220+
elif [ "$slot_diff" -lt 0 ]; then
221+
echo -e "\033[31m ⚠️ CRITICAL: This change will shift subsequent storage slots by $slot_diff positions\033[0m"
222+
fi
223+
else
224+
# Just a warning for gap overrides
225+
echo -e "\033[33m📝 Gap variable override at slot $slot:\033[0m"
226+
echo -e "\033[33m Previous: $onchain_label ($onchain_type)\033[0m"
227+
echo -e "\033[33m New: $local_label ($local_type)\033[0m"
228+
warnings_found=$((warnings_found + 1))
221229
fi
222230
elif [[ "$local_type" != "$onchain_type" ]]; then
223-
echo -e "\033[33m🔄 Type change detected for $local_label at slot $slot:\033[0m"
231+
# Type changes are critical errors
232+
echo -e "\033[31m🔄 CRITICAL: Type change detected for $local_label at slot $slot:\033[0m"
224233
echo -e "\033[31m Previous: $onchain_type\033[0m"
225-
echo -e "\033[32m New: $local_type\033[0m"
226-
issues_found=$((issues_found + 1))
234+
echo -e "\033[31m New: $local_type\033[0m"
235+
TOTAL_ERRORS=$((TOTAL_ERRORS + 1))
227236
fi
228237
fi
229238
echo "$slot" >> "$processed_slots_file"
@@ -233,23 +242,30 @@ analyze_storage_changes() {
233242
while IFS='|' read -r slot onchain_label onchain_offset onchain_type; do
234243
if [[ -z "$slot" ]]; then continue; fi
235244

236-
# Skip if this slot was processed as a rename or already handled
237245
if grep -q "^${slot}$" "$processed_slots_file"; then
238246
continue
239247
fi
240248

241-
# Look for matching slot in local
242249
if ! grep -q "^${slot}|" "$local_map_file"; then
243-
echo -e "\033[31m➖ Variable removed: $onchain_label ($onchain_type) from slot $slot\033[0m"
244-
issues_found=$((issues_found + 1))
250+
# Variable removal is a critical error
251+
echo -e "\033[31m➖ CRITICAL: Variable removed: $onchain_label ($onchain_type) from slot $slot\033[0m"
252+
TOTAL_ERRORS=$((TOTAL_ERRORS + 1))
245253
fi
246254
done < "$onchain_map_file"
247255

248256
# Cleanup temporary files
249257
rm -f "$onchain_map_file" "$local_map_file" "$processed_slots_file" "$renamed_vars_file"
250258

251-
echo "Issues found in $contract_name: $issues_found"
252-
return $issues_found
259+
if [ "$TOTAL_ERRORS" -gt 0 ]; then
260+
echo -e "\033[31mCritical storage layout errors found in $contract_name: $TOTAL_ERRORS\033[0m"
261+
else
262+
echo -e "\033[32mNo critical storage layout errors in $contract_name\033[0m"
263+
fi
264+
if [ "$warnings_found" -gt 0 ]; then
265+
echo "Non-critical changes found: $warnings_found"
266+
fi
267+
268+
return $TOTAL_ERRORS # Only return critical errors
253269
}
254270

255271
# Function to process a single contract
@@ -298,6 +314,7 @@ process_contract() {
298314
# Analyze storage changes
299315
analyze_storage_changes "$onchain_file" "$local_file" "$contract_name"
300316
issues_found=$?
317+
TOTAL_ERRORS=$((TOTAL_ERRORS + issues_found))
301318

302319
return $issues_found
303320
}
@@ -319,10 +336,11 @@ while IFS= read -r contract; do
319336
TOTAL_ISSUES=$((TOTAL_ISSUES + $?))
320337
done <<< "$CONTRACTS"
321338

322-
if [ "$TOTAL_ISSUES" -gt 0 ]; then
323-
echo -e "\n\033[31m🚨 Total storage layout issues found: $TOTAL_ISSUES\033[0m"
339+
if [ "$TOTAL_ERRORS" -gt 0 ]; then
340+
echo -e "\n\033[31m🚨 Total critical storage layout errors found: $TOTAL_ERRORS\033[0m"
324341
exit 1
325342
else
326-
echo -e "\n\033[32m✅ No storage layout issues found\033[0m"
327-
exit 0
343+
echo -e "\n\033[32m✅ No critical storage layout errors found\033[0m"
328344
fi
345+
346+
exit 0

0 commit comments

Comments
 (0)