-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #625 from PolymathNetwork/clash-script
Added script to detect function selector clashes
- Loading branch information
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
contract functionSigClash1 { | ||
// function clash550254402() public { | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
contract functionSigClash2 { | ||
// function proxyOwner() public { | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const fs = require('fs'); | ||
const exec = require('child_process').execSync; | ||
const chalk = require('chalk'); | ||
const Web3 = require('web3'); | ||
|
||
async function readFiles() { | ||
if (fs.existsSync("./build/contracts/")) { | ||
return fs.readdirSync("./build/contracts/"); | ||
} else { | ||
console.log(chalk.yellow('Compiling contracts. This may take a while, please wait.')); | ||
exec('truffle compile'); | ||
return fs.readdirSync("./build/contracts/"); | ||
} | ||
} | ||
|
||
async function checkClashes() { | ||
console.log(chalk.green("Starting function selector clash check")); | ||
let files = await readFiles(); | ||
let contractFunctions = new Set(); | ||
let functionSelectors = new Map(); | ||
files.forEach(item => { | ||
let ABI = JSON.parse(fs.readFileSync(`./build/contracts/${item}`).toString()).abi; | ||
ABI.forEach(element => { | ||
if (element['type'] == 'function') { | ||
let functionSig = element['name'] + '('; | ||
element['inputs'].forEach(input => { | ||
functionSig = functionSig + input['type'] + ','; | ||
}); | ||
if(functionSig[functionSig.length - 1] == ',') | ||
functionSig = functionSig.slice(0, -1) + ')'; | ||
else | ||
functionSig = functionSig + ')'; | ||
contractFunctions.add(functionSig); | ||
} | ||
}); | ||
}); | ||
let clashesFound = false; | ||
contractFunctions.forEach(functionSig => { | ||
let fnSelector = Web3.utils.sha3(functionSig).slice(0, 10); | ||
if(functionSelectors.has(fnSelector)) { | ||
clashesFound = true; | ||
console.log(chalk.red('Function selector clash found!', functionSelectors.get(fnSelector), 'and', functionSig, 'have the same function selector:', fnSelector)); | ||
functionSelectors.set(fnSelector, functionSelectors.get(fnSelector) + ', ' + functionSig); | ||
} else { | ||
functionSelectors.set(fnSelector, functionSig); | ||
} | ||
}); | ||
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.green("Clash check finished. No Clashes found.")); | ||
} | ||
|
||
checkClashes(); |