-
Notifications
You must be signed in to change notification settings - Fork 492
Expand file tree
/
Copy pathzksync-compat-analysis.js
More file actions
63 lines (55 loc) · 1.95 KB
/
Copy pathzksync-compat-analysis.js
File metadata and controls
63 lines (55 loc) · 1.95 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
#!/usr/bin/env node
const { readSync, forEachWalkSync } = require('./common.js');
const { execSync } = require('child_process');
async function main() {
const getLastModifiedGitTimestamp = (filePath) => {
try {
const output = execSync(`git log -1 --format=%ct -- ${filePath}`, { encoding: 'utf-8' });
return ~~output.trim();
} catch (error) {
return null;
}
};
let zkSyncIncompatOpcodes = [
'codecopy', 'extcodecopy'
];
let specialPatterns = [
{name: 'precompile4', reStr: 'staticcall\\([^,]*?,\\s*?(0x0*)?4'}
]
let flattenedPathsAndScores = [];
forEachWalkSync(['src'], srcPath => {
if (!srcPath.match(/\.sol$/i) || srcPath.match(/\/(g|legacy)\//)) return;
const src = readSync(srcPath);
const numMatches = reStr => (src.match(new RegExp(reStr, 'img')) || []).length;
let totalScore = 0;
let scores = {};
let redundantGasCount = 0;
zkSyncIncompatOpcodes.forEach(opcode => {
const score = numMatches('[^a-zA-z]' + opcode + '\\(');
if (opcode.match(/call$/)) {
redundantGasCount += numMatches('[^a-zA-z]' + opcode + '\\([\\S\\s]*?gas\\s*?\\(');
}
totalScore += score;
scores[opcode] = score;
});
specialPatterns.forEach(c => {
const score = numMatches(c.reStr);
totalScore += score;
scores[c.name] = score;
});
if (redundantGasCount) scores['gas'] -= redundantGasCount;
for (const key in scores) if (scores[key] === 0) delete scores[key];
const lastModifiedGitTimestamp = getLastModifiedGitTimestamp(srcPath);
flattenedPathsAndScores.push({srcPath, scores, totalScore, lastModifiedGitTimestamp});
});
flattenedPathsAndScores.sort((a, b) => a.totalScore - b.totalScore);
flattenedPathsAndScores.forEach(x => {
if (x.totalScore === 0) delete x.scores;
delete x.totalScore;
});
console.log(JSON.stringify(flattenedPathsAndScores, null, 4));
};
main().catch(e => {
console.error(e);
process.exit(1);
});