forked from KyberNetwork/smart-contracts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontractSizeReport.js
104 lines (96 loc) · 2.98 KB
/
contractSizeReport.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
'use strict';
const fs = require('fs');
const util = require('util');
const got = require('got');
const yargs = require('yargs');
let path = 'artifacts';
const readdir = util.promisify(fs.readdir);
let argv = yargs.default('branch', 'Katalyst').alias('b', 'branch').argv;
async function generateCodeSizeReport() {
let result = {};
let fileNames = await readdir(path);
for (let i = 0; i < fileNames.length; i++) {
let fileName = fileNames[i];
let rawData = fs.readFileSync(path + '/' + fileName);
let contractData = JSON.parse(rawData);
let codeSize = contractData.deployedBytecode.length / 2 - 1;
if (codeSize > 0) {
result[fileName] = codeSize;
}
}
return result;
}
async function writeReport(report) {
let jsonContent = JSON.stringify(report, null, '\t');
let reportDir = 'report';
if (process.env.TRAVIS_BRANCH !== undefined) {
reportDir = `report/${process.env.TRAVIS_BRANCH}`;
}
let reportFile = `${reportDir}/contractSize.json`;
if (!fs.existsSync(reportDir)) {
fs.mkdirSync(reportDir, {recursive: true});
}
fs.writeFile(reportFile, jsonContent, 'utf8', function (err) {
if (err) {
console.log('An error occured while writing JSON Object to File.');
return console.log(err);
}
});
}
async function getRemoteReport() {
try {
const url = `http://katalyst-coverage.knstats.com/report/${argv.branch}/contractSize.json`;
return await got(url).json();
} catch (error) {
// console.log(error);
return false;
}
}
async function compareContractSize() {
let contractSizeReport = await generateCodeSizeReport();
await writeReport(contractSizeReport);
let remoteReport = await getRemoteReport();
if (!remoteReport) {
console.log(`Could not get report for ${argv.branch}`);
console.log("Current contract size report");
console.table(contractSizeReport);
return false;
}
let diffDict = {};
for (let contract in contractSizeReport) {
if (contract in remoteReport) {
let baseBranchSize = remoteReport[contract];
let currentSize = contractSizeReport[contract];
let diff = currentSize - baseBranchSize;
if (diff != 0) {
diffDict[contract] = {
[argv.branch]: baseBranchSize,
current: currentSize,
diff: diff,
};
}
}
}
for (let contract in remoteReport) {
if (contract in remoteReport && !(contract in diffDict)) {
let baseBranchSize = remoteReport[contract];
let currentSize = contractSizeReport[contract];
let diff = currentSize - baseBranchSize;
if (diff != 0) {
diffDict[contract] = {
[argv.branch]: baseBranchSize,
current: currentSize,
diff: diff,
};
}
}
}
if (Object.keys(diffDict).length > 0) {
console.log(`There is change in following contract size with ${argv.branch}`);
console.table(diffDict);
} else {
console.log("Contract size didn't change");
console.table(contractSizeReport);
}
}
compareContractSize();