From e6211a8a3e85f0fce4b4918619aea39a9d984fa3 Mon Sep 17 00:00:00 2001 From: Mudit Gupta Date: Sat, 22 Jun 2019 16:14:06 +0530 Subject: [PATCH 1/5] Added Interface sync script --- scripts/ISTRCheck.js | 118 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 scripts/ISTRCheck.js diff --git a/scripts/ISTRCheck.js b/scripts/ISTRCheck.js new file mode 100644 index 000000000..31728a5a5 --- /dev/null +++ b/scripts/ISTRCheck.js @@ -0,0 +1,118 @@ +const fs = require('fs'); +const exec = require('child_process').execSync; +const chalk = require('chalk'); + +// These functions/events are allowed to differ. (These are present in STR but not used and hence not defined in ISTR) +let exceptions = [ + "getBytes32Value", + "getBytesValue", + "getAddressValue", + "getArrayAddress", + "getBoolValue", + "getStringValue", + "getArrayBytes32", + "getUintValue", + "getArrayUint", + "initialize", + "getBytes32Value", + "getBytesValue", + "getAddressValue", + "getArrayAddress", + "getBoolValue", + "getStringValue", + "getArrayBytes32", + "getUintValue", + "getArrayUint", + undefined, // constructor +]; + +async function readFiles() { + if (!fs.existsSync("./build/contracts/ISecurityTokenRegistry.json")) { + console.log(chalk.yellow('Compiling contracts. This may take a while, please wait.')); + exec('truffle compile'); + } + return([ + JSON.parse(fs.readFileSync(`./build/contracts/ISecurityTokenRegistry.json`).toString()).abi, + [ + ...JSON.parse(fs.readFileSync(`./build/contracts/SecurityTokenRegistry.json`).toString()).abi, + ...JSON.parse(fs.readFileSync(`./build/contracts/STRGetter.json`).toString()).abi + ] + ]); +} + +async function checkISTR() { + // Reading ABIs from build files + let ABIs = await readFiles(); + + // Removing functions/events defined in exceptions array. + removeExceptions(ABIs); + + // Removing parameter and return names from ABIs as they can differ. + // Only cleaning ABIs of second file as first one can be cleaned in the next loop efficiently. + for (let i = 0; i < ABIs[1].length; i++) { + cleanABI(ABIs[1][i]); + } + + // This function removed duplicate elements from the ABI. + // i.e If the signature matches in Interface and the contract, it is removed from the ABI. + // This means that the left over elements after this loop are mistakes. + for (let i = 0; i < ABIs[0].length; i++) { + let fna = cleanABI(ABIs[0][i]); + for (let j = 0; j < ABIs[1].length; j++) { + let fnb = ABIs[1][j]; + if (fna.name == fnb.name && fna.type == fnb.type) { + if (JSON.stringify(fna) === JSON.stringify(fnb)) { + ABIs[0].splice(i, 1); + ABIs[1].splice(j, 1); + i--; + break; + } + } + } + } + + // If there is any element remaining in either ABI, it is an error. + if (ABIs[0].length >= 1 || ABIs[1].length > 1) { + for (let i = 0; i < ABIs[0].length; i++) { + console.log(ABIs[0][i].name); + } + for (let i = 0; i < ABIs[1].length; i++) { + console.log(ABIs[1][i].name); + } + console.log(chalk.red('The above Functions/events had no match found.')); + throw("Please synchronize the Interface with the contract."); + } +} + +function cleanABI(element) { + if (element.type === 'event') { + for(let i = 0; i < element.inputs.length; i++) { + element.inputs[i].name = ""; + } + } else if (element.type === 'function') { + for(let i = 0; i < element.inputs.length; i++) { + element.inputs[i].name = ""; + } + for(let i = 0; i < element.outputs.length; i++) { + element.outputs[i].name = ""; + } + } + return element; +} + +function removeExceptions(ABIs) { + for (let i = 0; i < ABIs[0].length; i++) { + if (exceptions.includes(ABIs[0][i].name)) { + ABIs[0].splice(i, 1); + i--; + } + } + for (let i = 0; i < ABIs[1].length; i++) { + if (exceptions.includes(ABIs[1][i].name)) { + ABIs[1].splice(i, 1); + i--; + } + } +} + +checkISTR(); From 6acdb9389e11857f3ade8750224b191ec89b6802 Mon Sep 17 00:00:00 2001 From: Mudit Gupta Date: Sat, 22 Jun 2019 16:18:55 +0530 Subject: [PATCH 2/5] Added interface check script to CI --- .circleci/config.yml | 3 ++- package.json | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2f8945d91..efdf87dd2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -45,7 +45,7 @@ jobs: - run: yarn install - run: node --version - run: truffle version - - run: + - run: command: npm run coverage no_output_timeout: 1h - save_cache: @@ -65,6 +65,7 @@ jobs: - run: node --version - run: truffle version - run: npm run clash-check + - run: npm run istr-check - save_cache: key: dependency-cache-{{ checksum "package.json" }} paths: diff --git a/package.json b/package.json index 8221b6035..fe7227dcb 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "scripts": { "test": "scripts/test.sh 2> /dev/null", "clash-check": "node scripts/clashCheck.js", + "istr-check": "node scripts/ISTRCheck.js", "gas": "scripts/gasUsage.sh", "wintest": "scripts\\wintest.cmd", "wincov": "scripts\\wincov.cmd", From 315ad656e946ce9141c919965bdaac210b318438 Mon Sep 17 00:00:00 2001 From: Mudit Gupta Date: Sat, 22 Jun 2019 16:26:03 +0530 Subject: [PATCH 3/5] return 1 on error --- scripts/ISTRCheck.js | 4 ++-- scripts/clashCheck.js | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/scripts/ISTRCheck.js b/scripts/ISTRCheck.js index 31728a5a5..c4cc9966b 100644 --- a/scripts/ISTRCheck.js +++ b/scripts/ISTRCheck.js @@ -79,8 +79,8 @@ async function checkISTR() { for (let i = 0; i < ABIs[1].length; i++) { console.log(ABIs[1][i].name); } - console.log(chalk.red('The above Functions/events had no match found.')); - throw("Please synchronize the Interface with the contract."); + console.log(chalk.red('The above Functions/events had no match found. Please synchronize the Interface with the contract.')); + process.exit(1); } } diff --git a/scripts/clashCheck.js b/scripts/clashCheck.js index 09f36d31c..181202669 100644 --- a/scripts/clashCheck.js +++ b/scripts/clashCheck.js @@ -47,9 +47,10 @@ async function checkClashes() { }); if (clashesFound) { console.log(chalk.yellow("The clash(es) might be in two different contracts and hence not be am Issue.\nThis script can not detect this (yet) because of proxy contracts")); - throw("Clash(es) found! Please fix."); - } + console.log(chalk.red("Clash(es) found! Please fix.")); + process.exit(1); + } console.log(chalk.green("Clash check finished. No Clashes found.")); } -checkClashes(); \ No newline at end of file +checkClashes(); From 8a57089f6d3a45de054165aaf7f66b78eb05cb2c Mon Sep 17 00:00:00 2001 From: Mudit Gupta Date: Mon, 24 Jun 2019 11:24:50 +0530 Subject: [PATCH 4/5] Use local truffle in scripts --- scripts/ISTRCheck.js | 2 +- scripts/calculateSize.js | 2 +- scripts/clashCheck.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/ISTRCheck.js b/scripts/ISTRCheck.js index c4cc9966b..46015003f 100644 --- a/scripts/ISTRCheck.js +++ b/scripts/ISTRCheck.js @@ -29,7 +29,7 @@ let exceptions = [ async function readFiles() { if (!fs.existsSync("./build/contracts/ISecurityTokenRegistry.json")) { console.log(chalk.yellow('Compiling contracts. This may take a while, please wait.')); - exec('truffle compile'); + exec('npx truffle compile'); } return([ JSON.parse(fs.readFileSync(`./build/contracts/ISecurityTokenRegistry.json`).toString()).abi, diff --git a/scripts/calculateSize.js b/scripts/calculateSize.js index 3f3db87f9..ac5bd1365 100644 --- a/scripts/calculateSize.js +++ b/scripts/calculateSize.js @@ -9,7 +9,7 @@ async function readFiles() { return fs.readdirSync("./build/contracts/"); } else { console.log('Compiling contracts. This may take a while, please wait.'); - exec('truffle compile'); + exec('npx truffle compile'); return fs.readdirSync("./build/contracts/"); } } diff --git a/scripts/clashCheck.js b/scripts/clashCheck.js index 181202669..3a3c0dc85 100644 --- a/scripts/clashCheck.js +++ b/scripts/clashCheck.js @@ -8,7 +8,7 @@ async function readFiles() { return fs.readdirSync("./build/contracts/"); } else { console.log(chalk.yellow('Compiling contracts. This may take a while, please wait.')); - exec('truffle compile'); + exec('npx truffle compile'); return fs.readdirSync("./build/contracts/"); } } From 1493577bbd417fb1da340c5ac1516534dc76b0b6 Mon Sep 17 00:00:00 2001 From: Mudit Gupta Date: Mon, 24 Jun 2019 19:06:56 +0530 Subject: [PATCH 5/5] Removed npx dependency --- scripts/ISTRCheck.js | 2 +- scripts/calculateSize.js | 2 +- scripts/clashCheck.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/ISTRCheck.js b/scripts/ISTRCheck.js index 46015003f..9bbaf7f64 100644 --- a/scripts/ISTRCheck.js +++ b/scripts/ISTRCheck.js @@ -29,7 +29,7 @@ let exceptions = [ async function readFiles() { if (!fs.existsSync("./build/contracts/ISecurityTokenRegistry.json")) { console.log(chalk.yellow('Compiling contracts. This may take a while, please wait.')); - exec('npx truffle compile'); + exec('./node_modules/.bin/truffle compile'); } return([ JSON.parse(fs.readFileSync(`./build/contracts/ISecurityTokenRegistry.json`).toString()).abi, diff --git a/scripts/calculateSize.js b/scripts/calculateSize.js index ac5bd1365..88d1e1d4c 100644 --- a/scripts/calculateSize.js +++ b/scripts/calculateSize.js @@ -9,7 +9,7 @@ async function readFiles() { return fs.readdirSync("./build/contracts/"); } else { console.log('Compiling contracts. This may take a while, please wait.'); - exec('npx truffle compile'); + exec('./node_modules/.bin/truffle compile'); return fs.readdirSync("./build/contracts/"); } } diff --git a/scripts/clashCheck.js b/scripts/clashCheck.js index 3a3c0dc85..6133d4c0b 100644 --- a/scripts/clashCheck.js +++ b/scripts/clashCheck.js @@ -8,7 +8,7 @@ async function readFiles() { return fs.readdirSync("./build/contracts/"); } else { console.log(chalk.yellow('Compiling contracts. This may take a while, please wait.')); - exec('npx truffle compile'); + exec('./node_modules/.bin/truffle compile'); return fs.readdirSync("./build/contracts/"); } }