inheritanceOrdering.js 1.7 KB

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