inheritanceOrdering.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env node
  2. const path = require('path');
  3. const graphlib = require('graphlib');
  4. const { findAll } = require('solidity-ast/utils');
  5. const { _: artifacts } = require('yargs').argv;
  6. for (const artifact of artifacts) {
  7. const { output: solcOutput } = require(path.resolve(__dirname, '../..', artifact));
  8. const graph = new graphlib.Graph({ directed: true });
  9. const names = {};
  10. const linearized = [];
  11. for (const source in solcOutput.contracts) {
  12. for (const contractDef of findAll('ContractDefinition', solcOutput.sources[source].ast)) {
  13. names[contractDef.id] = contractDef.name;
  14. linearized.push(contractDef.linearizedBaseContracts);
  15. contractDef.linearizedBaseContracts.forEach((c1, i, contracts) => contracts.slice(i + 1).forEach(c2 => {
  16. graph.setEdge(c1, c2);
  17. }));
  18. }
  19. }
  20. /// graphlib.alg.findCycles will not find minimal cycles.
  21. /// We are only interested int cycles of lengths 2 (needs proof)
  22. graph.nodes().forEach((x, i, nodes) => nodes
  23. .slice(i + 1)
  24. .filter(y => graph.hasEdge(x, y) && graph.hasEdge(y, x))
  25. .forEach(y => {
  26. console.log(`Conflict between ${names[x]} and ${names[y]} detected in the following dependency chains:`);
  27. linearized
  28. .filter(chain => chain.includes(parseInt(x)) && chain.includes(parseInt(y)))
  29. .forEach(chain => {
  30. const comp = chain.indexOf(parseInt(x)) < chain.indexOf(parseInt(y)) ? '>' : '<';
  31. console.log(`- ${names[x]} ${comp} ${names[y]} in ${names[chain.find(Boolean)]}`);
  32. // console.log(`- ${names[x]} ${comp} ${names[y]}: ${chain.reverse().map(id => names[id]).join(', ')}`);
  33. });
  34. process.exitCode = 1;
  35. }));
  36. }
  37. if (!process.exitCode) {
  38. console.log('Contract ordering is consistent.');
  39. }