forked from bgd-labs/protocol-v3.6-upgrade
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiff.ts
More file actions
181 lines (166 loc) · 5.16 KB
/
diff.ts
File metadata and controls
181 lines (166 loc) · 5.16 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
import {
BlockscoutStyleSourceCode,
diffCode,
getSourceCode,
parseBlockscoutStyleSourceCode,
parseEtherscanStyleSourceCode,
StandardJsonInput,
} from "@bgd-labs/toolbox";
import {
mkdirSync,
readdirSync,
readFileSync,
statSync,
unlinkSync,
writeFileSync,
} from "fs";
import path from "path";
import { Hex, getAddress, slice } from "viem";
function bytes32ToAddress(bytes32: Hex) {
return getAddress(slice(bytes32, 12, 32));
}
// Set the target directory
const directoryPath = path.join(__dirname, "reports");
const erc1967ImplSlot =
"0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc";
// diff all the networks
const files = readdirSync(directoryPath);
// Filter files ending with '_after'
const filteredFiles = files.filter((file) => file.endsWith("_after.json"));
async function diff({
address1,
address2,
chainId1,
chainId2,
flatten,
output,
path = "./diffs/code",
}) {
const sources = await Promise.all([
getSourceCode({
chainId: Number(chainId1),
address: address1 as any,
apiKey: process.env.ETHERSCAN_API_KEY,
apiUrl: process.env.EXPLORER_PROXY,
}),
getSourceCode({
chainId: Number(chainId2),
address: address2 as any,
apiKey: process.env.ETHERSCAN_API_KEY,
apiUrl: process.env.EXPLORER_PROXY,
}),
]);
const source1: StandardJsonInput = (sources[0] as BlockscoutStyleSourceCode)
.AdditionalSources
? parseBlockscoutStyleSourceCode(sources[0] as BlockscoutStyleSourceCode)
: parseEtherscanStyleSourceCode(sources[0].SourceCode);
const source2: StandardJsonInput = (sources[1] as BlockscoutStyleSourceCode)
.AdditionalSources
? parseBlockscoutStyleSourceCode(sources[1] as BlockscoutStyleSourceCode)
: parseEtherscanStyleSourceCode(sources[1].SourceCode);
const diff = await diffCode(source1, source2);
if (flatten || output === "stdout") {
const flat = Object.keys(diff).reduce((acc, key) => {
acc += diff[key];
return acc;
}, "");
if (output === "stdout") {
console.log(flat);
} else {
const filePath = `${path}/${chainId1}`;
mkdirSync(filePath, { recursive: true });
writeFileSync(`${filePath}/${address1}_${address2}.patch`, flat);
}
} else {
const filePath = `${path}/${chainId1}/${address1}_${address2}`;
mkdirSync(filePath, { recursive: true });
Object.keys(diff).map((file) => {
writeFileSync(`${filePath}/${file}.patch`, diff[file]);
});
}
}
for (const file of filteredFiles) {
const contentBefore = JSON.parse(
readFileSync(`${directoryPath}/${file.replace("_after", "_before")}`, {
encoding: "utf8",
}),
);
const contentAfter = JSON.parse(
readFileSync(`${directoryPath}/${file}`, { encoding: "utf8" }),
);
console.log("starting ", contentBefore.chainId);
// diff slots that are not pure implementation slots (e.g. things on addresses provider)
await diff({
address1: contentBefore.poolConfig.protocolDataProvider,
chainId1: contentBefore.chainId,
address2: contentAfter.poolConfig.protocolDataProvider,
chainId2: contentAfter.chainId,
output: "file",
});
for (const contract in contentAfter.raw) {
const implSlot = contentAfter.raw[contract].stateDiff[erc1967ImplSlot];
if (implSlot) {
await diff({
address1: bytes32ToAddress(implSlot.previousValue),
chainId1: contentBefore.chainId,
address2: bytes32ToAddress(implSlot.newValue),
chainId2: contentAfter.chainId,
output: "file",
});
}
}
}
// now as the diffing is done, let's remove duplicates and generate a report
// Function to read files recursively
function getFiles(
dir,
fileList: { path: string; name: string; content: string }[] = [],
) {
const files = readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
if (statSync(filePath).isDirectory()) {
getFiles(filePath, fileList);
} else {
fileList.push({
path: filePath,
name: file,
content: readFileSync(filePath, { encoding: "utf8" }),
});
}
});
return fileList;
}
// Get all files including subdirectories
// Normalize patch content by trimming paths in --- / +++ lines to just the filename
function normalizePatch(content: string): string {
return content
.split("\n")
.map((line) => {
if (line.startsWith("--- ") || line.startsWith("+++ ")) {
const prefix = line.slice(0, 4);
const filePath = line.slice(4);
return prefix + filePath.split("/").pop();
}
return line;
})
.join("\n")
.trim();
}
const allFiles = getFiles(path.join(__dirname, "diffs", "code"));
const uniqueArray = allFiles
.sort((a, b) => {
const extractNumber = (str) => {
const match = str.match(/\/diffs\/code\/(\d+)\//);
return match ? parseInt(match[1], 10) : Infinity;
};
return extractNumber(a.path) - extractNumber(b.path);
})
.filter(
(obj, index, self) =>
index === self.findIndex((o) => normalizePatch(o.content) === normalizePatch(obj.content)),
);
for (const file of allFiles) {
const isUnique = uniqueArray.find((uniq) => uniq.path === file.path);
if (!isUnique) unlinkSync(file.path);
}