|
| 1 | +#!/usr/bin/env bun |
| 2 | +// Checks that upgradeable contracts have proper version bumps when bytecode changes. |
| 3 | +// Usage: bun ci/check-upgrade-hygiene.ts <main-pkg-dir> <pr-pkg-dir> |
| 4 | + |
| 5 | +import { readFileSync, existsSync } from "fs"; |
| 6 | +import { execSync } from "child_process"; |
| 7 | +import { join } from "path"; |
| 8 | + |
| 9 | +const [mainDir, prDir] = process.argv.slice(2); |
| 10 | +if (!mainDir || !prDir) { |
| 11 | + console.error("Usage: bun ci/check-upgrade-hygiene.ts <main-pkg-dir> <pr-pkg-dir>"); |
| 12 | + process.exit(1); |
| 13 | +} |
| 14 | + |
| 15 | +const manifestPath = join(prDir, "upgrade-manifest.json"); |
| 16 | +if (!existsSync(manifestPath)) { |
| 17 | + console.error(`::error::upgrade-manifest.json not found in ${prDir}`); |
| 18 | + process.exit(1); |
| 19 | +} |
| 20 | + |
| 21 | +const VERSION_RE = /(?<name>REINITIALIZER_VERSION|MAJOR_VERSION|MINOR_VERSION|PATCH_VERSION)\s*=\s*(?<value>\d+)/g; |
| 22 | + |
| 23 | +function extractVersions(filePath: string) { |
| 24 | + const source = readFileSync(filePath, "utf-8"); |
| 25 | + const versions: Record<string, number> = {}; |
| 26 | + for (const { groups } of source.matchAll(VERSION_RE)) { |
| 27 | + versions[groups!.name] = Number(groups!.value); |
| 28 | + } |
| 29 | + return { versions, source }; |
| 30 | +} |
| 31 | + |
| 32 | +function forgeInspect(contract: string, root: string): string | null { |
| 33 | + try { |
| 34 | + const raw = execSync(`forge inspect "contracts/${contract}.sol:${contract}" --root "${root}" deployedBytecode`, { |
| 35 | + encoding: "utf-8", |
| 36 | + stdio: ["pipe", "pipe", "pipe"], |
| 37 | + env: { ...process.env, NO_COLOR: "1" }, |
| 38 | + }); |
| 39 | + // Extract hex bytecode — forge may prepend ANSI codes or compilation progress to stdout |
| 40 | + const match = raw.match(/0x[0-9a-fA-F]+/); |
| 41 | + return match ? match[0] : null; |
| 42 | + } catch (e: any) { |
| 43 | + if (e.stderr) console.error(String(e.stderr)); |
| 44 | + return null; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const contracts: string[] = JSON.parse(readFileSync(manifestPath, "utf-8")); |
| 49 | +let errors = 0; |
| 50 | + |
| 51 | +for (const name of contracts) { |
| 52 | + console.log(`::group::Checking ${name}`); |
| 53 | + try { |
| 54 | + const mainSol = join(mainDir, "contracts", `${name}.sol`); |
| 55 | + const prSol = join(prDir, "contracts", `${name}.sol`); |
| 56 | + |
| 57 | + if (!existsSync(mainSol)) { |
| 58 | + console.log(`Skipping ${name} (new contract, not on main)`); |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + if (!existsSync(prSol)) { |
| 63 | + console.error(`::error::${name} listed in upgrade-manifest.json but missing in PR`); |
| 64 | + errors++; |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + const { versions: mainV } = extractVersions(mainSol); |
| 69 | + const { versions: prV, source: prSrc } = extractVersions(prSol); |
| 70 | + |
| 71 | + let parseFailed = false; |
| 72 | + for (const key of ["REINITIALIZER_VERSION", "MAJOR_VERSION", "MINOR_VERSION", "PATCH_VERSION"]) { |
| 73 | + if (mainV[key] == null || prV[key] == null) { |
| 74 | + console.error(`::error::Failed to parse ${key} for ${name}`); |
| 75 | + errors++; |
| 76 | + parseFailed = true; |
| 77 | + } |
| 78 | + } |
| 79 | + if (parseFailed) continue; |
| 80 | + |
| 81 | + const mainBytecode = forgeInspect(name, mainDir); |
| 82 | + if (mainBytecode == null) { |
| 83 | + console.error(`::error::Failed to compile ${name} on main`); |
| 84 | + errors++; |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + const prBytecode = forgeInspect(name, prDir); |
| 89 | + if (prBytecode == null) { |
| 90 | + console.error(`::error::Failed to compile ${name} on PR`); |
| 91 | + errors++; |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + const bytecodeChanged = mainBytecode !== prBytecode; |
| 96 | + const reinitChanged = mainV.REINITIALIZER_VERSION !== prV.REINITIALIZER_VERSION; |
| 97 | + const versionChanged = |
| 98 | + mainV.MAJOR_VERSION !== prV.MAJOR_VERSION || |
| 99 | + mainV.MINOR_VERSION !== prV.MINOR_VERSION || |
| 100 | + mainV.PATCH_VERSION !== prV.PATCH_VERSION; |
| 101 | + |
| 102 | + if (!bytecodeChanged) { |
| 103 | + console.log(`${name}: bytecode unchanged`); |
| 104 | + if (reinitChanged) { |
| 105 | + console.error( |
| 106 | + `::error::${name} REINITIALIZER_VERSION bumped (${mainV.REINITIALIZER_VERSION} -> ${prV.REINITIALIZER_VERSION}) but bytecode is unchanged`, |
| 107 | + ); |
| 108 | + errors++; |
| 109 | + } |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + console.log(`${name}: bytecode CHANGED`); |
| 114 | + |
| 115 | + if (!reinitChanged) { |
| 116 | + console.error( |
| 117 | + `::error::${name} bytecode changed but REINITIALIZER_VERSION was not bumped (still ${prV.REINITIALIZER_VERSION})`, |
| 118 | + ); |
| 119 | + errors++; |
| 120 | + } else { |
| 121 | + // Convention: reinitializeV{N-1} for REINITIALIZER_VERSION=N |
| 122 | + const expectedFn = `reinitializeV${prV.REINITIALIZER_VERSION - 1}`; |
| 123 | + const uncommented = prSrc.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, ""); |
| 124 | + if (!new RegExp(`function\\s+${expectedFn}\\s*\\(`).test(uncommented)) { |
| 125 | + console.error( |
| 126 | + `::error::${name} has REINITIALIZER_VERSION=${prV.REINITIALIZER_VERSION} but no ${expectedFn}() function found`, |
| 127 | + ); |
| 128 | + errors++; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (!versionChanged) { |
| 133 | + console.error( |
| 134 | + `::error::${name} bytecode changed but semantic version was not bumped (still v${prV.MAJOR_VERSION}.${prV.MINOR_VERSION}.${prV.PATCH_VERSION})`, |
| 135 | + ); |
| 136 | + errors++; |
| 137 | + } |
| 138 | + } finally { |
| 139 | + console.log("::endgroup::"); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +if (errors > 0) { |
| 144 | + console.error(`::error::Upgrade hygiene check failed with ${errors} error(s)`); |
| 145 | + process.exit(1); |
| 146 | +} |
| 147 | + |
| 148 | +console.log("All contracts passed upgrade hygiene checks"); |
0 commit comments