Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to check custom errors for duplicates in repo (tooling) #4549

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ jobs:
run: npm run test:inheritance
- name: Check proceduraly generated contracts are up-to-date
run: npm run test:generation
- name: Check custom errors have unique names
run: npm run test:errors-uniqueness
- name: Compare gas costs
uses: ./.github/actions/gas-compare
with:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"version": "scripts/release/version.sh",
"test": "hardhat test",
"test:inheritance": "scripts/checks/inheritance-ordering.js artifacts/build-info/*",
"test:errors-uniqueness": "scripts/checks/custom-errors-duplication.js artifacts/build-info/*",
"test:generation": "scripts/checks/generation.sh",
"gas-report": "env ENABLE_GAS_REPORT=true npm run test",
"slither": "npm run clean && slither ."
Expand Down
71 changes: 71 additions & 0 deletions scripts/checks/custom-errors-duplication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const path = require('path');
const { findAll } = require('solidity-ast/utils');
const { _: artifacts } = require('yargs').argv;

const excludedDirs = ['contracts/mocks/', 'test/', 'contracts-exposed'];
const ROOT_PATH = path.resolve(__dirname, '../..');

function main() {
const reports = processArtifacts(artifacts);

if (reports.length > 0) {
console.log('Found duplicates in custom errors:');
for (const report of reports) {
let output = '';
for (const file of report.files) {
output += `${report.name}: declared in ${file.file}\n`;
}
console.log(output, '\n');
}
}

if (reports.length > 0) process.exitCode = 1;

if (!process.exitCode) {
console.log('No duplicate custom errors found.');
}
}

function processArtifacts(artifacts) {
const filesContainingCustomError = {};

for (const artifact of artifacts) {
const { output: solcOutput } = require(path.resolve(ROOT_PATH, artifact));

for (const source in solcOutput.contracts) {
if (excludedDirs.some(pattern => source.startsWith(pattern))) {
continue;
}

const customErrors = processSource(source, solcOutput);
if (customErrors.length > 0) {
for (const customError of customErrors) {
if (!filesContainingCustomError[customError.name]) {
filesContainingCustomError[customError.name] = [{ file: source, node: customError }];
} else {
filesContainingCustomError[customError.name].push({ file: source, node: customError });
}
}
}
}
}

const duplicates = [];
for (const customError of Object.keys(filesContainingCustomError)) {
if (filesContainingCustomError[customError].length > 1) {
duplicates.push({ name: customError, files: filesContainingCustomError[customError] });
}
}

return duplicates;
}

function processSource(source, solcOutput) {
const customErrors = [];
for (const customError of findAll('ErrorDefinition', solcOutput.sources[source].ast)) {
customErrors.push(customError);
}
return customErrors;
}

main();
Loading