Skip to content

Commit 3ab0a11

Browse files
authored
Merge pull request #744 from F-OBrien/IST-Check-Script
IST Check script
2 parents 2d1c9fc + c703d10 commit 3ab0a11

File tree

5 files changed

+149
-122
lines changed

5 files changed

+149
-122
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
- run: node --version
6666
- run: truffle version
6767
- run: npm run clash-check
68-
- run: npm run istr-check
68+
- run: npm run i-check
6969
- save_cache:
7070
key: dependency-cache-{{ checksum "package.json" }}
7171
paths:

contracts/interfaces/ISecurityToken.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ interface ISecurityToken {
4242
bytes32 _label,
4343
bool _archived
4444
);
45-
45+
// Emit when Module get upgraded from the securityToken
46+
event ModuleUpgraded(uint8[] _types, address _module);
4647
// Emit when the token details get updated
4748
event UpdateTokenDetails(string _oldDetails, string _newDetails);
4849
// Emit when the token name get updated
@@ -410,7 +411,7 @@ interface ISecurityToken {
410411
* @param _tokenHolder Whom balance need to queried
411412
* @return List of partitions
412413
*/
413-
function partitionsOf(address _tokenHolder) external view returns (bytes32[] memory partitions);
414+
function partitionsOf(address _tokenHolder) external pure returns (bytes32[] memory partitions);
414415

415416
/**
416417
* @notice Gets data store address

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"test": "scripts/test.sh 2> /dev/null",
1111
"clash-check": "node scripts/clashCheck.js",
12-
"istr-check": "node scripts/ISTRCheck.js",
12+
"i-check": "node scripts/ICheck.js",
1313
"gas": "scripts/gasUsage.sh",
1414
"wintest": "scripts\\wintest.cmd",
1515
"wincov": "scripts\\wincov.cmd",

scripts/ICheck.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
const fs = require('fs');
2+
const exec = require('child_process').execSync;
3+
const chalk = require('chalk');
4+
5+
// These functions/events are allowed to differ. (These are present in STR/STRGetter but not used and hence not defined in ISTR)
6+
let strExceptions = [
7+
"initialize",
8+
"getBytes32Value",
9+
"getBytesValue",
10+
"getAddressValue",
11+
"getArrayAddress",
12+
"getBoolValue",
13+
"getStringValue",
14+
"getArrayBytes32",
15+
"getUintValue",
16+
"getArrayUint",
17+
undefined, // constructor
18+
];
19+
20+
// These functions/events are allowed to differ. (These are present in ST/STGetter but not used and hence not defined in IST)
21+
let stExceptions = [
22+
undefined, // constructor
23+
];
24+
25+
async function readFiles() {
26+
if (!fs.existsSync("./build/contracts/ISecurityTokenRegistry.json") || !fs.existsSync("./build/contracts/ISecurityToken.json")) {
27+
console.log(chalk.yellow('Compiling contracts. This may take a while, please wait.'));
28+
exec('./node_modules/.bin/truffle compile');
29+
}
30+
let strABIs = [
31+
JSON.parse(fs.readFileSync(`./build/contracts/ISecurityTokenRegistry.json`).toString()).abi,
32+
JSON.parse(fs.readFileSync(`./build/contracts/SecurityTokenRegistry.json`).toString()).abi,
33+
JSON.parse(fs.readFileSync(`./build/contracts/STRGetter.json`).toString()).abi
34+
];
35+
let stABIs = [
36+
JSON.parse(fs.readFileSync(`./build/contracts/ISecurityToken.json`).toString()).abi,
37+
JSON.parse(fs.readFileSync(`./build/contracts/SecurityToken.json`).toString()).abi,
38+
JSON.parse(fs.readFileSync(`./build/contracts/STGetter.json`).toString()).abi
39+
];
40+
return [strABIs, stABIs];
41+
}
42+
43+
async function checkInterfaces() {
44+
// Reading ABIs from build files
45+
let [strABIs, stABIs] = await readFiles();
46+
// Perform checks on the interface files
47+
let strMismatch = await interfaceCheck(strABIs, strExceptions, "ISecurityTokenRegistry");
48+
let stMismatch = await interfaceCheck(stABIs, stExceptions, "ISecurityToken");
49+
if (strMismatch || stMismatch) {process.exit(1)};
50+
console.log(chalk.green("Interface checks finished. No mismatch was found between interfaces and contacts"));
51+
}
52+
53+
async function interfaceCheck(ABIs, exceptions, interfaceName) {
54+
// Removing functions/events defined in the exceptions arrays.
55+
removeExceptions(ABIs, exceptions);
56+
// Removing parameter and return names from ABIs as they can differ.
57+
// Only cleaning ABIs of third file as other files can be cleaned in the following loops efficiently.
58+
for (let i = 0; i < ABIs[2].length; i++) {
59+
cleanABI(ABIs[2][i]);
60+
}
61+
// Removing one instance of duplicates common to main contract and getter contract
62+
removeDuplicates(ABIs);
63+
// Combine second and third ABI after duplicates were removed
64+
ABIs = [ABIs[0],[...ABIs[1],...ABIs[2]]];
65+
66+
// This function removes elements that match between the interface and contracts
67+
// i.e If the signature matches in Interface and the contract, it is removed from the ABI.
68+
// This means that the left over elements after this loop are mistakes.
69+
for (let i = 0; i < ABIs[0].length; i++) {
70+
let fna = cleanABI(ABIs[0][i]);
71+
for (let j = 0; j < ABIs[1].length; j++) {
72+
let fnb = ABIs[1][j];
73+
if (fna.name == fnb.name && fna.type == fnb.type) {
74+
if (JSON.stringify(fna) === JSON.stringify(fnb)) {
75+
ABIs[0].splice(i, 1);
76+
ABIs[1].splice(j, 1);
77+
i--;
78+
break;
79+
}
80+
}
81+
}
82+
}
83+
84+
// If there is any element remaining in either ABI, it is an error and they should be reported.
85+
if (ABIs[0].length >= 1 || ABIs[1].length >= 1) {
86+
for (let i = 0; i < ABIs[0].length; i++) {
87+
console.log(ABIs[0][i].name);
88+
}
89+
for (let i = 0; i < ABIs[1].length; i++) {
90+
console.log(ABIs[1][i].name);
91+
}
92+
console.log(chalk.red('The above Functions/events had a mismatch with ' + interfaceName + '. Please synchronize the Interface with the contract.\n'));
93+
return true;
94+
}
95+
return false;
96+
}
97+
98+
// This function removes parameter and return names from ABIs as they can differ.
99+
function cleanABI(element) {
100+
if (element.type === 'event') {
101+
for(let i = 0; i < element.inputs.length; i++) {
102+
element.inputs[i].name = "";
103+
}
104+
} else if (element.type === 'function') {
105+
for(let i = 0; i < element.inputs.length; i++) {
106+
element.inputs[i].name = "";
107+
}
108+
for(let i = 0; i < element.outputs.length; i++) {
109+
element.outputs[i].name = "";
110+
}
111+
}
112+
return element;
113+
}
114+
115+
// This function removes one instance of duplicates common to main contract and getter
116+
function removeDuplicates(ABIs) {
117+
for (let i = 0; i < ABIs[1].length; i++) {
118+
let fna = cleanABI(ABIs[1][i]);
119+
for (let j = 0; j < ABIs[2].length; j++) {
120+
let fnb = ABIs[2][j];
121+
if (fna.name == fnb.name && fna.type == fnb.type) {
122+
if (JSON.stringify(fna) === JSON.stringify(fnb)) {
123+
ABIs[1].splice(i, 1);
124+
i--;
125+
break;
126+
}
127+
}
128+
}
129+
}
130+
}
131+
132+
// This function removes functions/events defined in an exceptions arrays.
133+
function removeExceptions(ABIs, exceptions) {
134+
for (let i = 0; i < ABIs.length; i++) {
135+
for (let j = 0; j < ABIs[i].length; j++) {
136+
if (exceptions.includes(ABIs[i][j].name)) {
137+
ABIs[i].splice(j, 1);
138+
j--;
139+
}
140+
}
141+
}
142+
}
143+
144+
checkInterfaces();

scripts/ISTRCheck.js

Lines changed: 0 additions & 118 deletions
This file was deleted.

0 commit comments

Comments
 (0)