-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathcheck-upgrade-versions.ts
More file actions
147 lines (127 loc) · 4.89 KB
/
check-upgrade-versions.ts
File metadata and controls
147 lines (127 loc) · 4.89 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
#!/usr/bin/env bun
// Checks that upgradeable contracts have proper version bumps when bytecode changes.
// Usage: bun ci/check-upgrade-versions.ts <baseline-pkg-dir> <pr-pkg-dir>
import { readFileSync, existsSync } from "fs";
import { execSync } from "child_process";
import { join } from "path";
const [baselineDir, prDir] = process.argv.slice(2);
if (!baselineDir || !prDir) {
console.error("Usage: bun ci/check-upgrade-versions.ts <baseline-pkg-dir> <pr-pkg-dir>");
process.exit(1);
}
const manifestPath = join(prDir, "upgrade-manifest.json");
if (!existsSync(manifestPath)) {
console.error(`::error::upgrade-manifest.json not found in ${prDir}`);
process.exit(1);
}
const VERSION_RE = /(?<name>REINITIALIZER_VERSION|MAJOR_VERSION|MINOR_VERSION|PATCH_VERSION)\s*=\s*(?<value>\d+)/g;
function extractVersions(filePath: string) {
const source = readFileSync(filePath, "utf-8");
const versions: Record<string, number> = {};
for (const { groups } of source.matchAll(VERSION_RE)) {
versions[groups!.name] = Number(groups!.value);
}
return { versions, source };
}
function forgeInspect(contract: string, root: string): string | null {
try {
const raw = execSync(`forge inspect "contracts/${contract}.sol:${contract}" --root "${root}" deployedBytecode`, {
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
env: { ...process.env, NO_COLOR: "1" },
});
// Extract hex bytecode — forge may prepend ANSI codes or compilation progress to stdout
const match = raw.match(/0x[0-9a-fA-F]+/);
return match ? match[0] : null;
} catch (e: any) {
if (e.stderr) console.error(String(e.stderr));
return null;
}
}
const contracts: string[] = JSON.parse(readFileSync(manifestPath, "utf-8"));
let errors = 0;
for (const name of contracts) {
console.log(`::group::Checking ${name}`);
try {
const baseSol = join(baselineDir, "contracts", `${name}.sol`);
const prSol = join(prDir, "contracts", `${name}.sol`);
if (!existsSync(baseSol)) {
console.log(`Skipping ${name} (new contract, not in baseline)`);
continue;
}
if (!existsSync(prSol)) {
console.error(`::error::${name} listed in upgrade-manifest.json but missing in PR`);
errors++;
continue;
}
const { versions: baseV } = extractVersions(baseSol);
const { versions: prV, source: prSrc } = extractVersions(prSol);
let parseFailed = false;
for (const key of ["REINITIALIZER_VERSION", "MAJOR_VERSION", "MINOR_VERSION", "PATCH_VERSION"]) {
if (baseV[key] == null || prV[key] == null) {
console.error(`::error::Failed to parse ${key} for ${name}`);
errors++;
parseFailed = true;
}
}
if (parseFailed) continue;
const prBytecode = forgeInspect(name, prDir);
if (prBytecode == null) {
console.error(`::error::Failed to compile ${name} on PR`);
errors++;
continue;
}
const baseBytecode = forgeInspect(name, baselineDir);
if (baseBytecode == null) {
console.error(`::error::Failed to compile ${name} on baseline`);
errors++;
continue;
}
const bytecodeChanged = baseBytecode !== prBytecode;
const reinitChanged = baseV.REINITIALIZER_VERSION !== prV.REINITIALIZER_VERSION;
const versionChanged =
baseV.MAJOR_VERSION !== prV.MAJOR_VERSION ||
baseV.MINOR_VERSION !== prV.MINOR_VERSION ||
baseV.PATCH_VERSION !== prV.PATCH_VERSION;
if (!bytecodeChanged) {
console.log(`${name}: bytecode unchanged`);
if (reinitChanged) {
console.error(
`::error::${name} REINITIALIZER_VERSION bumped (${baseV.REINITIALIZER_VERSION} -> ${prV.REINITIALIZER_VERSION}) but bytecode is unchanged`,
);
errors++;
}
continue;
}
console.log(`${name}: bytecode CHANGED`);
if (!reinitChanged) {
console.error(
`::error::${name} bytecode changed but REINITIALIZER_VERSION was not bumped (still ${prV.REINITIALIZER_VERSION})`,
);
errors++;
} else {
// Convention: reinitializeV{N-1} for REINITIALIZER_VERSION=N
const expectedFn = `reinitializeV${prV.REINITIALIZER_VERSION - 1}`;
const uncommented = prSrc.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
if (!new RegExp(`function\\s+${expectedFn}\\s*\\(`).test(uncommented)) {
console.error(
`::error::${name} has REINITIALIZER_VERSION=${prV.REINITIALIZER_VERSION} but no ${expectedFn}() function found`,
);
errors++;
}
}
if (!versionChanged) {
console.error(
`::error::${name} bytecode changed but semantic version was not bumped (still v${prV.MAJOR_VERSION}.${prV.MINOR_VERSION}.${prV.PATCH_VERSION})`,
);
errors++;
}
} finally {
console.log("::endgroup::");
}
}
if (errors > 0) {
console.error(`::error::Upgrade version check failed with ${errors} error(s)`);
process.exit(1);
}
console.log("All contracts passed upgrade version checks");