-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathlib.ts
More file actions
184 lines (161 loc) · 6.51 KB
/
lib.ts
File metadata and controls
184 lines (161 loc) · 6.51 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
import { execSync } from "child_process";
import { existsSync, readFileSync } from "fs";
import { join } from "path";
const VERSION_RE = /(?<name>REINITIALIZER_VERSION|MAJOR_VERSION|MINOR_VERSION|PATCH_VERSION)\s*=\s*(?<value>\d+)/g;
const REINITIALIZE_FUNCTION_PREFIX = "reinitializeV";
export interface ContractVersions {
REINITIALIZER_VERSION: number;
MAJOR_VERSION: number;
MINOR_VERSION: number;
PATCH_VERSION: number;
}
export interface ReinitializerInfo {
name: string;
signature: string;
inputs: string[];
}
export interface ContractCheckResult {
name: string;
baselineExists: boolean;
bytecodeChanged: boolean;
semanticVersionChanged: boolean;
reinitializerVersionChanged: boolean;
baselineVersions?: ContractVersions;
targetVersions?: ContractVersions;
reinitializer?: ReinitializerInfo;
errors: string[];
}
function execForgeInspect(contract: string, root: string, field: string): string | null {
try {
return execSync(`forge inspect "contracts/${contract}.sol:${contract}" --root "${root}" ${field}`, {
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
env: { ...process.env, NO_COLOR: "1" },
});
} catch (error: any) {
if (error.stderr) {
console.error(String(error.stderr));
}
return null;
}
}
function forgeInspectBytecode(contract: string, root: string): string | null {
const raw = execForgeInspect(contract, root, "deployedBytecode");
if (raw == null) return null;
const match = raw.match(/0x[0-9a-fA-F]+/);
return match ? match[0] : null;
}
function extractVersions(filePath: string): { versions: Partial<ContractVersions>; source: string } {
const source = readFileSync(filePath, "utf-8");
const versions: Partial<ContractVersions> = {};
for (const { groups } of source.matchAll(VERSION_RE)) {
versions[groups!.name as keyof ContractVersions] = Number(groups!.value);
}
return { versions, source };
}
function getReinitializer(source: string): ReinitializerInfo | undefined {
const uncommented = source.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
const match = uncommented.match(/function\s+(reinitializeV\d+)\s*\(([\s\S]*?)\)\s*(public|external|internal|private)/);
if (!match) return undefined;
const [, name, rawParams] = match;
const normalizedParams = rawParams.replace(/\s+/g, " ").trim();
const inputs = normalizedParams.length > 0 ? normalizedParams.split(",").map((input) => input.trim()) : [];
return {
name,
signature: `${name}(${normalizedParams})`,
inputs,
};
}
export function collectUpgradeVersionResults(baselineDir: string, targetDir: string): ContractCheckResult[] {
const manifestPath = join(targetDir, "upgrade-manifest.json");
if (!existsSync(manifestPath)) {
throw new Error(`upgrade-manifest.json not found in ${targetDir}`);
}
const contracts: string[] = JSON.parse(readFileSync(manifestPath, "utf-8"));
return contracts.map((name) => {
const baseSol = join(baselineDir, "contracts", `${name}.sol`);
const targetSol = join(targetDir, "contracts", `${name}.sol`);
const errors: string[] = [];
if (!existsSync(baseSol)) {
return {
name,
baselineExists: false,
bytecodeChanged: false,
semanticVersionChanged: false,
reinitializerVersionChanged: false,
errors,
};
}
if (!existsSync(targetSol)) {
return {
name,
baselineExists: true,
bytecodeChanged: false,
semanticVersionChanged: false,
reinitializerVersionChanged: false,
errors: [`${name} listed in upgrade-manifest.json but missing in target`],
};
}
const { versions: baseVersions } = extractVersions(baseSol);
const { versions: targetVersions, source: targetSource } = extractVersions(targetSol);
for (const key of ["REINITIALIZER_VERSION", "MAJOR_VERSION", "MINOR_VERSION", "PATCH_VERSION"] as const) {
if (baseVersions[key] == null || targetVersions[key] == null) {
errors.push(`Failed to parse ${key}`);
}
}
const baselineBytecode = errors.length === 0 ? forgeInspectBytecode(name, baselineDir) : null;
const targetBytecode = errors.length === 0 ? forgeInspectBytecode(name, targetDir) : null;
if (errors.length === 0 && baselineBytecode == null) {
errors.push("Failed to compile baseline bytecode");
}
if (errors.length === 0 && targetBytecode == null) {
errors.push("Failed to compile target bytecode");
}
const bytecodeChanged = baselineBytecode != null && targetBytecode != null && baselineBytecode !== targetBytecode;
const reinitializerVersionChanged =
baseVersions.REINITIALIZER_VERSION != null &&
targetVersions.REINITIALIZER_VERSION != null &&
baseVersions.REINITIALIZER_VERSION !== targetVersions.REINITIALIZER_VERSION;
const semanticVersionChanged =
baseVersions.MAJOR_VERSION != null &&
targetVersions.MAJOR_VERSION != null &&
(baseVersions.MAJOR_VERSION !== targetVersions.MAJOR_VERSION ||
baseVersions.MINOR_VERSION !== targetVersions.MINOR_VERSION ||
baseVersions.PATCH_VERSION !== targetVersions.PATCH_VERSION);
if (bytecodeChanged && !reinitializerVersionChanged) {
errors.push(
`${name} bytecode changed but REINITIALIZER_VERSION was not bumped (still ${targetVersions.REINITIALIZER_VERSION})`,
);
}
if (bytecodeChanged && !semanticVersionChanged) {
errors.push(
`${name} bytecode changed but semantic version was not bumped (still v${targetVersions.MAJOR_VERSION}.${targetVersions.MINOR_VERSION}.${targetVersions.PATCH_VERSION})`,
);
}
if (!bytecodeChanged && reinitializerVersionChanged) {
errors.push(
`${name} REINITIALIZER_VERSION bumped (${baseVersions.REINITIALIZER_VERSION} -> ${targetVersions.REINITIALIZER_VERSION}) but bytecode is unchanged`,
);
}
const reinitializer = getReinitializer(targetSource);
if (bytecodeChanged && reinitializerVersionChanged) {
const expectedFn = `${REINITIALIZE_FUNCTION_PREFIX}${targetVersions.REINITIALIZER_VERSION! - 1}`;
if (reinitializer?.name !== expectedFn) {
errors.push(
`${name} has REINITIALIZER_VERSION=${targetVersions.REINITIALIZER_VERSION} but expected ${expectedFn}()`,
);
}
}
return {
name,
baselineExists: true,
bytecodeChanged,
semanticVersionChanged,
reinitializerVersionChanged,
baselineVersions: baseVersions as ContractVersions,
targetVersions: targetVersions as ContractVersions,
reinitializer,
errors,
};
});
}