inheritance-ordering.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. if (source.includes('/mocks/')) {
  13. continue;
  14. }
  15. for (const contractDef of findAll('ContractDefinition', solcOutput.sources[source].ast)) {
  16. names[contractDef.id] = contractDef.name;
  17. linearized.push(contractDef.linearizedBaseContracts);
  18. contractDef.linearizedBaseContracts.forEach((c1, i, contracts) => contracts.slice(i + 1).forEach(c2 => {
  19. graph.setEdge(c1, c2);
  20. }));
  21. }
  22. }
  23. /// graphlib.alg.findCycles will not find minimal cycles.
  24. /// We are only interested int cycles of lengths 2 (needs proof)
  25. graph.nodes().forEach((x, i, nodes) => nodes
  26. .slice(i + 1)
  27. .filter(y => graph.hasEdge(x, y) && graph.hasEdge(y, x))
  28. .forEach(y => {
  29. console.log(`Conflict between ${names[x]} and ${names[y]} detected in the following dependency chains:`);
  30. linearized
  31. .filter(chain => chain.includes(parseInt(x)) && chain.includes(parseInt(y)))
  32. .forEach(chain => {
  33. const comp = chain.indexOf(parseInt(x)) < chain.indexOf(parseInt(y)) ? '>' : '<';
  34. console.log(`- ${names[x]} ${comp} ${names[y]} in ${names[chain.find(Boolean)]}`);
  35. // console.log(`- ${names[x]} ${comp} ${names[y]}: ${chain.reverse().map(id => names[id]).join(', ')}`);
  36. });
  37. process.exitCode = 1;
  38. }));
  39. }
  40. if (!process.exitCode) {
  41. console.log('Contract ordering is consistent.');
  42. }