-
Notifications
You must be signed in to change notification settings - Fork 4
/
_reporter.js
349 lines (297 loc) · 10.2 KB
/
_reporter.js
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const fs = require("fs");
const path = require("path");
// ========== INPUTS ==========
const oldData = JSON.parse(process.argv[2]);
const newData = JSON.parse(process.argv[3]);
const contractPath = path.parse(process.argv[4]);
const omitNew = process.argv[5];
// Omit if same
if (JSON.stringify(oldData) === JSON.stringify(newData)) process.exit(0);
// ========== VISUALIZE LAYOUTS ==========
const oldVisualized = createLayout(oldData);
//console.log(JSON.stringify(oldVisualized, null, 2));
const newVisualized = createLayout(newData);
//console.log(JSON.stringify(newVisualized, null, 2));
const overlayedVisualized = overlayLayouts(oldVisualized, newVisualized);
//console.log(JSON.stringify(overlayedVisualized, null, 2));
const [alignedOverlayedVisualized, alignedOldVisualized] = alignLayouts(overlayedVisualized, oldVisualized);
//console.log(JSON.stringify(alignedOverlayedVisualized, null, 2));
//console.log(JSON.stringify(alignedOldVisualized, null, 2));
if (alignedOldVisualized.length !== alignedOverlayedVisualized.length) {
console.warn(
"Error: Lengths do not match.\nThis is a bug. Please, submit a new issue: https://github.com/0xPolygon/storage-delta/issues.",
);
process.exit(1);
}
// ========== ANALYZE ==========
let reportOld = "";
let reportNew = "";
let i = 0;
for (; i < alignedOldVisualized.length; i++) {
const oldItem = alignedOldVisualized[i];
const overlayedItem = alignedOverlayedVisualized[i];
// overlayedItem can be undefined, dirty, or a storage item
// oldItem can be undefined or a storage item
// Emojis:
// 🏴 - Problematic
// 🏳️ - Moved
// 🏁 - Moved & problematic
// 🪦 - Removed
// 🌱 - New
// Same start
if (checkStart(overlayedItem, oldItem) === 1) {
// auto
if (overlayedItem.label === "@dirty") {
printNew(false, "🪦");
printOld(true);
continue;
}
if (!isSame(overlayedItem, oldItem)) {
if (!hasExisted(overlayedItem, oldVisualized)) {
printNew(true, "🏴");
printOld(true);
} else {
printNew(true, "🏁");
printOld(true);
}
} else {
printNew(true, " ");
printOld(true);
}
} else if (checkStart(overlayedItem, oldItem) === 0) {
if (overlayedItem.label === "@dirty") {
const diff = overlayedItem.end - overlayedItem.start;
const s = diff === 1 ? "" : "s";
const itemLike = {
...overlayedItem,
label: diff + " dirty byte" + s,
type: {
label: "",
},
offset: overlayedItem.start % 32,
slot: Math.floor(overlayedItem.start / 32),
};
reportNew += formatLine(" ", itemLike);
printOld(false);
continue;
}
if (!isClean(overlayedItem, oldVisualized)) {
if (!hasExisted(overlayedItem, oldVisualized)) {
printNew(true, "🏴");
printOld(false);
} else {
printNew(true, "🏁");
printOld(false);
}
} else {
if (!hasExisted(overlayedItem, oldVisualized)) {
printNew(true, "🌱");
printOld(false);
} else {
printNew(true, "🏳️");
printOld(false);
}
}
} else if (checkStart(overlayedItem, oldItem) === -1) {
// auto
printNew(false, "🪦");
printOld(true);
}
}
// ========== OPTIONS ==========
// --omit report if only New findings
if (!["🏴", "🏳️", "🏁", "🪦"].some((emoji) => reportNew.includes(emoji)) && omitNew) process.exit(1);
// ========== REPORT FINDINGS ==========
// remove \n from end of report
reportOld = reportOld.slice(0, -1);
reportNew = reportNew.slice(0, -1);
const directoryPath = path.join("./storage_delta", contractPath.dir);
// Create directories recursively
try {
fs.mkdirSync(directoryPath, { recursive: true });
} catch (err) {
if (err.code !== "EEXIST") throw err;
}
const reportOldPath = path.join(directoryPath, contractPath.name + "-OLD");
const reportNewPath = path.join(directoryPath, contractPath.name + "-NEW");
// Write files
fs.writeFileSync(reportOldPath, reportOld);
fs.writeFileSync(reportNewPath, reportNew);
// ========== HELPERS ==========
// IN: Storage layout JSON
// OUT: Easy to read storage layout JSON
function createLayout(data) {
const result = [];
function calcStart(item) {
let slot = parseInt(item.slot);
let offset = parseInt(item.offset);
let result = slot * 32 + offset;
if (slot + offset === 0) result = 0;
return result;
}
for (const item of data.storage) {
const start = calcStart(item);
const end = start + parseInt(data.types[item.type].numberOfBytes);
const json = {
label: item.label,
start: start,
end: end,
type: processType(data.types[item.type]),
slot: item.slot,
offset: item.offset,
};
result.push(json);
}
return result;
}
// IN: JSON for "type"
// OUT: JSON of "type" with "astId" and "contract" removed from each "member"
// Note: For shallow comparison. TODO: Deep comparison
function processType(type) {
if (type && type.members && Array.isArray(type.members)) {
// Iterate through each member of the type
type.members.forEach((member) => {
// Delete the "astId" and "contract" properties from the member
delete member.astId;
delete member.contract;
});
}
return type;
}
// IN: Old storage layout JSON, new storage layout JSON
// OUT: New storage layout JSON with dirty bytes visible
function overlayLayouts(oldArray, newArray) {
let result = [];
// Create a copy of oldArray to avoid modifying the original
let oldArrayCopy = oldArray.map((item) => ({ ...item }));
let oldIndex = 0,
newIndex = 0;
while (oldIndex < oldArrayCopy.length || newIndex < newArray.length) {
let oldItem = oldArrayCopy[oldIndex];
let newItem = newArray[newIndex];
if (newItem && (!oldItem || newItem.start < oldItem.start)) {
// Add the new item and advance the new index
result.push(newItem);
newIndex++;
// Skip over any old items that are completely overlapped by the new item
while (oldItem && newItem.end >= oldItem.end) {
oldIndex++;
oldItem = oldArrayCopy[oldIndex];
}
} else if (oldItem) {
// Handle the case where the old item has no corresponding new item
if (!newItem || oldItem.end <= newItem.start) {
result.push({ label: "@dirty", start: oldItem.start, end: oldItem.end });
oldIndex++;
} else {
// Handle partial overlap or complete overlap of the old item by the new item
if (oldItem.start < newItem.start) {
result.push({ label: "@dirty", start: oldItem.start, end: newItem.start });
}
// Update the old item's start if the new item ends within it
if (newItem.end < oldItem.end) {
oldArrayCopy[oldIndex] = { ...oldItem, start: newItem.end };
} else {
oldIndex++;
}
}
}
}
return result.filter((item) => item.start < item.end);
}
// IN: Overlayed layout, old layout
// OUT: Aligned overlayed layout, aligned old layout
function alignLayouts(layout1, layout2) {
// Function to insert missing starts from one layout into another
function insertMissingStarts(baseLayout, referenceLayout) {
let result = [...baseLayout];
const referenceStarts = new Set(referenceLayout.map((item) => item.start));
for (const start of referenceStarts) {
if (!baseLayout.some((item) => item.start === start)) {
result.push({ label: "@undefined", start: start });
}
}
// Sort by start to maintain order
result.sort((a, b) => a.start - b.start);
return result;
}
// Align layout1 with layout2
let alignedLayout1 = insertMissingStarts(layout1, layout2);
// Align layout2 with the updated alignedLayout1
let alignedLayout2 = insertMissingStarts(layout2, alignedLayout1);
return [alignedLayout1, alignedLayout2];
}
// IN: Storage item, storage item
// OUT: True or false
function isSame(item1, item2) {
// Compare items by label and type
if (item1.label === item2.label && JSON.stringify(item1.type) === JSON.stringify(item2.type)) {
return true; // Items are equal
} else {
return false; // Items are not equal
}
}
// IN: Item from aligned overlayed storage layout, old storage layout
// OUT: True or false
// Note: Check for dirty bytes.
function isClean(item, oldLayout) {
for (const oldItem of oldLayout) {
// Check for overlap between the given item and oldItem
if (item.start < oldItem.end && item.end > oldItem.start) {
return false; // There is an overlap, not clean
}
}
return true; // No overlaps found, clean
}
// IN: Item from aligned overlayed storage layout, old storage layout
// OUT: True or false
function hasExisted(item, oldLayout) {
for (const oldItem of oldLayout) {
if (isSame(item, oldItem)) {
return true; // The item exists in oldLayout
}
}
return false; // The item does not exist in oldLayout
}
// IN: Item from aligned overlayed storage layout, old storage layout
// OUT: -1 if no start, 0 diff/gt start, 1 if same start
function checkStart(newItem, oldItem) {
if (newItem.label !== "@undefined" && oldItem.label !== "@undefined") {
return 1;
} else if (oldItem.label === "@undefined") {
return 0;
} else if (newItem.label === "@undefined") {
return -1;
}
console.warn(
"Error: Cannot check start.\nThis is a bug. Please, submit a new issue: https://github.com/0xPolygon/storage-delta/issues.",
);
process.exit(1);
}
// IN: Whether line should be empty
// Notes: Adds line to old report.
function printOld(notEmpty) {
if (!notEmpty) reportOld += "\n";
else reportOld += formatLine(" ", alignedOldVisualized[i]);
}
// IN: Whether line should be empty, finding indicator
// Notes: Adds line to new report.
function printNew(notEmpty, emoji) {
if (!notEmpty) reportNew += emoji + "\n";
else reportNew += formatLine(emoji, alignedOverlayedVisualized[i]);
}
// IN: Finding indicator, storage item
function formatLine(emoji, item) {
emoji = emoji.padEnd(1, " ");
let slot = item.slot;
let offset = item.offset.toString().padEnd(5, " ");
let label = item.label.padEnd(25, " ");
let type = item.type.label;
if (item.offset == 0) {
slot = slot.padEnd(8, " ");
offset = "";
} else {
slot += ": ";
}
return `${emoji} ${slot}${offset} ${label} ${type}\n`;
}