-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathcheck-bytecode-changes.js
More file actions
240 lines (204 loc) · 8.17 KB
/
check-bytecode-changes.js
File metadata and controls
240 lines (204 loc) · 8.17 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
// Contract configuration
// Each contract specifies which other contracts should be bumped when it changes
const CONTRACT_CONFIG = {
"IthacaAccount.sol/IthacaAccount.json": {
name: "IthacaAccount",
bumpsWhenChanged: [], // Account changes don't bump other contracts
},
"Orchestrator.sol/Orchestrator.json": {
name: "Orchestrator",
bumpsWhenChanged: ["IthacaAccount", "SimpleFunder"],
},
"SimpleFunder.sol/SimpleFunder.json": {
name: "SimpleFunder",
bumpsWhenChanged: [], // SimpleFunder changes only bump itself
},
"SimpleSettler.sol/SimpleSettler.json": {
name: "SimpleSettler",
bumpsWhenChanged: [], // SimpleSettler changes only bump itself
},
"Simulator.sol/Simulator.json": {
name: "Simulator",
bumpsWhenChanged: [], // Simulator changes only bump itself
},
"Escrow.sol/Escrow.json": {
name: "Escrow",
bumpsWhenChanged: [], // Escrow changes only bump itself
},
"MultiSigSigner.sol/MultiSigSigner.json": {
name: "MultiSigSigner",
bumpsWhenChanged: [], // MultiSigSigner changes only bump itself
},
"LayerZeroSettler.sol/LayerZeroSettler.json": {
name: "LayerZeroSettler",
bumpsWhenChanged: [], // LayerZeroSettler changes only bump itself
},
};
// All contracts to check for bytecode changes
const CONTRACTS_TO_CHECK = Object.keys(CONTRACT_CONFIG);
function getBytecodeHash(artifactPath) {
try {
const artifact = JSON.parse(fs.readFileSync(artifactPath, "utf8"));
const bytecode = artifact.bytecode?.object || "";
if (!bytecode || bytecode === "0x") {
console.warn(`Warning: No bytecode found in ${artifactPath}`);
return null;
}
// Remove metadata hash from bytecode (last 43 bytes for Solidity)
// This ensures we only compare actual code changes, not metadata
const cleanBytecode = bytecode.slice(0, -86); // 43 bytes = 86 hex chars
return crypto.createHash("sha256").update(cleanBytecode).digest("hex");
} catch (error) {
console.error(`Error reading artifact ${artifactPath}:`, error.message);
return null;
}
}
function compareArtifacts(baseDir, prDir) {
const changes = {};
for (const contract of CONTRACTS_TO_CHECK) {
const basePath = path.join(baseDir, contract);
const prPath = path.join(prDir, contract);
const baseHash = getBytecodeHash(basePath);
const prHash = getBytecodeHash(prPath);
if (baseHash && prHash && baseHash !== prHash) {
changes[contract] = true;
console.log(`Bytecode changed: ${contract}`);
}
}
return changes;
}
function determineContractsToBump(bytecodeChanges) {
const contractsToBump = new Set();
for (const [contractPath, changed] of Object.entries(bytecodeChanges)) {
if (changed) {
const config = CONTRACT_CONFIG[contractPath];
// The contract itself needs to be bumped
contractsToBump.add(config.name);
// Also bump any contracts specified in bumpsWhenChanged
for (const otherContract of config.bumpsWhenChanged) {
contractsToBump.add(otherContract);
}
}
}
return Array.from(contractsToBump);
}
function checkManualVersionBumps(contractsToBump) {
try {
// Check which specific contracts have already had their versions manually bumped
const baseRef = process.env.GITHUB_BASE_REF || 'main';
const alreadyBumpedContracts = [];
// Get the diff for Solidity files
const gitDiff = require("child_process")
.execSync(`git diff origin/${baseRef}...HEAD -- src/*.sol`, {
encoding: "utf8",
});
// Split diff into file sections
const fileSections = gitDiff.split(/^diff --git/m).filter(Boolean);
// Check each contract that needs bumping
for (const contractName of contractsToBump) {
let foundVersionBump = false;
// Find the file section that contains this contract
for (const fileSection of fileSections) {
// Skip if this file section doesn't contain our contract
if (!fileSection.includes(`contract ${contractName}`)) {
continue;
}
// Look for version changes in this file section
const lines = fileSection.split('\n');
let currentContract = null;
let oldVersion = null;
let newVersion = null;
for (const line of lines) {
// Track which contract we're currently in based on context lines or added lines
// Match contract declarations in various diff formats
const contractMatch = line.match(/^([@\s\+\-])?.*\bcontract\s+(\w+)/);
if (contractMatch && !line.startsWith('---') && !line.startsWith('+++')) {
const potentialContract = contractMatch[2];
// Only update currentContract if it's a contract declaration line
if (line.includes('{') || line.match(/\bcontract\s+\w+\s*(is|{)/)) {
currentContract = potentialContract;
}
}
// Only look for version changes if we're in the right contract
if (currentContract === contractName) {
// Check for removed version line
const removedVersionMatch = line.match(/^-\s*version = "(\d+\.\d+\.\d+)";/);
if (removedVersionMatch) {
oldVersion = removedVersionMatch[1];
}
// Check for added version line
const addedVersionMatch = line.match(/^\+\s*version = "(\d+\.\d+\.\d+)";/);
if (addedVersionMatch) {
newVersion = addedVersionMatch[1];
}
}
}
// If we found both old and new versions and they're different, the version was bumped
if (oldVersion && newVersion && oldVersion !== newVersion) {
foundVersionBump = true;
alreadyBumpedContracts.push(contractName);
console.log(`Contract ${contractName} already manually bumped from ${oldVersion} to ${newVersion}`);
break;
}
}
if (!foundVersionBump) {
console.log(`Contract ${contractName} needs automatic version bump`);
}
}
// Return contracts that still need bumping (not manually bumped)
return contractsToBump.filter(c => !alreadyBumpedContracts.includes(c));
} catch (error) {
console.error("Error checking manual version bumps:", error.message);
// If there's an error, assume all contracts need bumping
return contractsToBump;
}
}
function main() {
const args = process.argv.slice(2);
if (args.length !== 2) {
console.error(
"Usage: check-bytecode-changes.js <base-artifacts-dir> <pr-artifacts-dir>"
);
process.exit(1);
}
const [baseDir, prDir] = args;
console.log("Checking bytecode changes...");
const bytecodeChanges = compareArtifacts(baseDir, prDir);
const changedContracts = Object.values(bytecodeChanges).filter(Boolean).length;
if (changedContracts > 0) {
console.log(`\nFound bytecode changes in ${changedContracts} contracts`);
// Determine which contracts need version bumps
const contractsToBump = determineContractsToBump(bytecodeChanges);
console.log(`\nContracts that need version bumps: ${contractsToBump.join(", ")}`);
// Check which contracts have already been manually bumped
const contractsStillNeedingBump = checkManualVersionBumps(contractsToBump);
if (contractsStillNeedingBump.length > 0) {
console.log(`Contracts still needing version bumps: ${contractsStillNeedingBump.join(", ")}`);
console.log("Automatic bump required for remaining contracts");
// Use modern GitHub Actions output syntax
fs.appendFileSync(
process.env.GITHUB_OUTPUT || "/dev/null",
`needs_version_bump=true\ncontracts_to_bump=${contractsStillNeedingBump.join(",")}\n`
);
} else {
console.log("All required contract versions have already been updated");
fs.appendFileSync(
process.env.GITHUB_OUTPUT || "/dev/null",
"needs_version_bump=false\n"
);
}
} else {
console.log("No bytecode changes detected");
fs.appendFileSync(
process.env.GITHUB_OUTPUT || "/dev/null",
"needs_version_bump=false\n"
);
}
}
if (require.main === module) {
main();
}