|
| 1 | +'use strict'; |
| 2 | +const fs = require('fs'); |
| 3 | +const util = require('util'); |
| 4 | +const got = require('got'); |
| 5 | +const yargs = require('yargs'); |
| 6 | + |
| 7 | +let path = 'artifacts'; |
| 8 | + |
| 9 | +const readdir = util.promisify(fs.readdir); |
| 10 | + |
| 11 | +let argv = yargs.default('branch', 'Katalyst').alias('b', 'branch').argv; |
| 12 | + |
| 13 | +async function generateCodeSizeReport() { |
| 14 | + let result = {}; |
| 15 | + let fileNames = await readdir(path); |
| 16 | + for (let i = 0; i < fileNames.length; i++) { |
| 17 | + let fileName = fileNames[i]; |
| 18 | + let rawData = fs.readFileSync(path + '/' + fileName); |
| 19 | + let contractData = JSON.parse(rawData); |
| 20 | + let codeSize = contractData.deployedBytecode.length / 2 - 1; |
| 21 | + if (codeSize > 0) { |
| 22 | + result[fileName] = codeSize; |
| 23 | + } |
| 24 | + } |
| 25 | + return result; |
| 26 | +} |
| 27 | + |
| 28 | +async function writeReport(report) { |
| 29 | + let jsonContent = JSON.stringify(report, null, '\t'); |
| 30 | + let reportDir = 'report'; |
| 31 | + if (process.env.TRAVIS_BRANCH !== undefined) { |
| 32 | + reportDir = `report/${process.env.TRAVIS_BRANCH}`; |
| 33 | + } |
| 34 | + let reportFile = `${reportDir}/contractSize.json`; |
| 35 | + if (!fs.existsSync(reportDir)) { |
| 36 | + fs.mkdirSync(reportDir, {recursive: true}); |
| 37 | + } |
| 38 | + fs.writeFile(reportFile, jsonContent, 'utf8', function (err) { |
| 39 | + if (err) { |
| 40 | + console.log('An error occured while writing JSON Object to File.'); |
| 41 | + return console.log(err); |
| 42 | + } |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +async function getRemoteReport() { |
| 47 | + try { |
| 48 | + const url = `http://katalyst-coverage.knstats.com/report/${argv.branch}/contractSize.json`; |
| 49 | + return await got(url).json(); |
| 50 | + } catch (error) { |
| 51 | + // console.log(error); |
| 52 | + return false; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +async function compareContractSize() { |
| 57 | + let contractSizeReport = await generateCodeSizeReport(); |
| 58 | + await writeReport(contractSizeReport); |
| 59 | + let remoteReport = await getRemoteReport(); |
| 60 | + if (!remoteReport) { |
| 61 | + console.log(`Could not get report for ${argv.branch}`); |
| 62 | + console.log("Current contract size report"); |
| 63 | + console.table(contractSizeReport); |
| 64 | + return false; |
| 65 | + } |
| 66 | + let diffDict = {}; |
| 67 | + for (let contract in contractSizeReport) { |
| 68 | + if (contract in remoteReport) { |
| 69 | + let baseBranchSize = remoteReport[contract]; |
| 70 | + let currentSize = contractSizeReport[contract]; |
| 71 | + let diff = currentSize - baseBranchSize; |
| 72 | + if (diff != 0) { |
| 73 | + diffDict[contract] = { |
| 74 | + [argv.branch]: baseBranchSize, |
| 75 | + current: currentSize, |
| 76 | + diff: diff, |
| 77 | + }; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + for (let contract in remoteReport) { |
| 82 | + if (contract in remoteReport && !(contract in diffDict)) { |
| 83 | + let baseBranchSize = remoteReport[contract]; |
| 84 | + let currentSize = contractSizeReport[contract]; |
| 85 | + let diff = currentSize - baseBranchSize; |
| 86 | + if (diff != 0) { |
| 87 | + diffDict[contract] = { |
| 88 | + [argv.branch]: baseBranchSize, |
| 89 | + current: currentSize, |
| 90 | + diff: diff, |
| 91 | + }; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + if (Object.keys(diffDict).length > 0) { |
| 96 | + console.log(`There is change in following contract size with ${argv.branch}`); |
| 97 | + console.table(diffDict); |
| 98 | + } else { |
| 99 | + console.log("Contract size didn't change"); |
| 100 | + console.table(contractSizeReport); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +compareContractSize(); |
0 commit comments