2018-03-22 14:43:29 +00:00
|
|
|
const Viz = require('viz.js');
|
2018-03-22 20:18:13 +00:00
|
|
|
const fs = require('fs');
|
2018-03-22 14:43:29 +00:00
|
|
|
|
|
|
|
class GraphGenerator {
|
2018-03-22 20:18:13 +00:00
|
|
|
constructor(engine) {
|
|
|
|
this.engine = engine;
|
2018-03-22 14:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
2018-03-22 20:18:13 +00:00
|
|
|
let id = 0;
|
|
|
|
let contractString = "";
|
2018-03-22 21:11:11 +00:00
|
|
|
let relationshipString = "";
|
|
|
|
let idMapping = {};
|
|
|
|
let contractInheritance = {};
|
2018-03-22 20:18:13 +00:00
|
|
|
|
2018-03-22 21:11:11 +00:00
|
|
|
|
2018-03-22 20:18:13 +00:00
|
|
|
for (let contract in this.engine.contractsManager.contracts) {
|
|
|
|
id++;
|
2018-03-22 21:11:11 +00:00
|
|
|
|
|
|
|
idMapping[contract] = id;
|
|
|
|
|
2018-03-22 20:18:13 +00:00
|
|
|
let contractLabel = "";
|
|
|
|
|
|
|
|
contractLabel += `${contract}`;
|
|
|
|
|
2018-03-22 21:11:11 +00:00
|
|
|
if(this.engine.contractsManager.contracts[contract].instanceOf !== undefined &&
|
|
|
|
this.engine.contractsManager.contracts[this.engine.contractsManager.contracts[contract].instanceOf] !== undefined){
|
|
|
|
contractInheritance[contract] = this.engine.contractsManager.contracts[contract].instanceOf;
|
|
|
|
}
|
|
|
|
|
|
|
|
contractLabel += "|";
|
|
|
|
|
|
|
|
for(let i = 0; i < this.engine.contractsManager.contracts[contract].abiDefinition.length; i++){
|
|
|
|
switch(this.engine.contractsManager.contracts[contract].abiDefinition[i].type){
|
|
|
|
case 'fallback':
|
|
|
|
contractLabel += "«fallback»()" + '\\l';
|
|
|
|
break;
|
|
|
|
case 'constructor':
|
|
|
|
contractLabel += "«constructor»(";
|
|
|
|
for(let j = 0; j < this.engine.contractsManager.contracts[contract].abiDefinition[i].inputs.length; j++){
|
|
|
|
contractLabel += (j == 0 ? "" : ", ") + this.engine.contractsManager.contracts[contract].abiDefinition[i].inputs[j].type;
|
|
|
|
}
|
|
|
|
contractLabel += ")\\l";
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 20:18:13 +00:00
|
|
|
let fHashes = this.engine.contractsManager.contracts[contract].functionHashes;
|
|
|
|
if(fHashes != {} && fHashes != undefined){
|
|
|
|
for(let method in this.engine.contractsManager.contracts[contract].functionHashes){
|
|
|
|
contractLabel += method + '\\l';
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 21:11:11 +00:00
|
|
|
|
2018-03-22 20:18:13 +00:00
|
|
|
|
|
|
|
contractString += `${id}[label = "{${contractLabel}}"]`
|
|
|
|
contractString += "\n";
|
|
|
|
}
|
|
|
|
|
2018-03-22 21:11:11 +00:00
|
|
|
for (let c in this.engine.contractsManager.contractDependencies){
|
|
|
|
let contractDependencies = Array.from(new Set(this.engine.contractsManager.contractDependencies[c]));
|
|
|
|
contractDependencies.forEach(function(d){
|
|
|
|
if(idMapping[c] !== undefined && idMapping[d] !== undefined){
|
|
|
|
relationshipString += `${idMapping[d]}->${idMapping[c]}[constraint=true, arrowtail=diamond]`
|
|
|
|
relationshipString += "\n";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let c in contractInheritance){
|
|
|
|
relationshipString += `${idMapping[contractInheritance[c]]}->${idMapping[c]}`
|
|
|
|
relationshipString += "\n";
|
|
|
|
}
|
|
|
|
|
2018-03-22 20:18:13 +00:00
|
|
|
|
|
|
|
let dot = `
|
|
|
|
digraph hierarchy {
|
|
|
|
node[shape=record,style=filled,fillcolor=gray95]
|
|
|
|
edge[dir=back, arrowtail=empty]
|
|
|
|
${contractString}
|
2018-03-22 21:11:11 +00:00
|
|
|
${relationshipString}
|
2018-03-22 20:18:13 +00:00
|
|
|
}`;
|
|
|
|
|
|
|
|
let svg = Viz(dot);
|
|
|
|
|
|
|
|
let filename = "diagram.svg";
|
|
|
|
|
|
|
|
fs.writeFileSync(filename, svg, (err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-03-22 14:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = GraphGenerator;
|