Skip to content

Commit

Permalink
Check inheritance tree consistency (#2727)
Browse files Browse the repository at this point in the history
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
  • Loading branch information
Amxx and frangio authored Jun 18, 2021
1 parent 27e0900 commit 4d0f8c1
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
env:
FORCE_COLOR: 1
ENABLE_GAS_REPORT: true
- run: npm run test:inheritance
- name: Print gas report
run: cat gas-report.txt

Expand Down
2 changes: 1 addition & 1 deletion contracts/mocks/ERC1155ReceiverMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pragma solidity ^0.8.0;
import "../token/ERC1155/IERC1155Receiver.sol";
import "../utils/introspection/ERC165.sol";

contract ERC1155ReceiverMock is IERC1155Receiver, ERC165 {
contract ERC1155ReceiverMock is ERC165, IERC1155Receiver {
bytes4 private _recRetval;
bool private _recReverts;
bytes4 private _batRetval;
Expand Down
132 changes: 130 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"release": "scripts/release/release.sh",
"version": "scripts/release/version.sh",
"test": "hardhat test",
"test:inheritance": "node scripts/inheritanceOrdering artifacts/build-info/*",
"gas-report": "env ENABLE_GAS_REPORT=true npm run test"
},
"repository": {
Expand Down Expand Up @@ -64,6 +65,7 @@
"eth-sig-util": "^3.0.0",
"ethereumjs-util": "^7.0.7",
"ethereumjs-wallet": "^1.0.1",
"graphlib": "^2.1.8",
"hardhat": "^2.0.6",
"hardhat-gas-reporter": "^1.0.4",
"keccak256": "^1.0.2",
Expand All @@ -75,6 +77,7 @@
"prettier-plugin-solidity": "^1.0.0-beta.13",
"rimraf": "^3.0.2",
"solhint": "^3.3.6",
"solidity-ast": "^0.4.25",
"solidity-coverage": "^0.7.11",
"solidity-docgen": "^0.5.3",
"web3": "^1.3.0",
Expand Down
38 changes: 38 additions & 0 deletions scripts/inheritanceOrdering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const path = require('path');
const graphlib = require('graphlib');
const { findAll } = require('solidity-ast/utils');
const { _: artifacts } = require('yargs').argv;

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

const graph = new graphlib.Graph({ directed: true });
const names = {};
const linearized = [];

for (const source in solcOutput.contracts) {
for (const contractDef of findAll('ContractDefinition', solcOutput.sources[source].ast)) {
names[contractDef.id] = contractDef.name;
linearized.push(contractDef.linearizedBaseContracts);

contractDef.linearizedBaseContracts.forEach((c1, i, contracts) => contracts.slice(i + 1).forEach(c2 => {
graph.setEdge(c1, c2);
}));
}
}

graphlib.alg.findCycles(graph).forEach(([ c1, c2 ]) => {
console.log(`Conflict between ${names[c1]} and ${names[c2]} detected in the following dependency chains:`);
linearized
.filter(chain => chain.includes(parseInt(c1)) && chain.includes(parseInt(c2)))
.forEach(chain => {
const comp = chain.indexOf(c1) < chain.indexOf(c2) ? '>' : '<';
console.log(`- ${names[c1]} ${comp} ${names[c2]}: ${chain.reverse().map(id => names[id]).join(', ')}`);
});
process.exitCode = 1;
});
}

if (!process.exitCode) {
console.log('Contract ordering is consistent.');
}

0 comments on commit 4d0f8c1

Please sign in to comment.