Skip to content

Commit

Permalink
add duperfinder
Browse files Browse the repository at this point in the history
  • Loading branch information
tintinweb committed Nov 23, 2021
1 parent db4f725 commit 9730459
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/rules/dupefinder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @author github.com/tintinweb
* @license MIT
* */

const { BaseRule } = require("./builtin");
const utils = require('../utils');
const {AstHashedContractSync, HASH_MODES} = require("solidity-doppelganger");

//["AST_EXACT", "AST_STRUCTURE"];

class DupeFinder extends BaseRule {
constructor(solgrep, selectedModes) {
super(solgrep);
this.selectedModes = selectedModes || HASH_MODES;
this.dupeDb = {}; // {hash: [file, file, ...]}
this.selectedModes.forEach(mode => this.dupeDb[mode] = {});
}

onProcess(sourceUnit) {
Object.values(sourceUnit.contracts).forEach(contract => {
HASH_MODES.forEach(mode => {
let hashAst = new AstHashedContractSync({mode:mode}, sourceUnit.filePath).fromAst(contract.ast)
if(!this.dupeDb[mode][hashAst.hash] || !Array.isArray(this.dupeDb[mode][hashAst.hash])){
this.dupeDb[mode][hashAst.hash] = [];
}
this.dupeDb[mode][hashAst.hash].push(`${sourceUnit.filePath}::${contract.name}`)
})

});
}

onDirAnalyzed() {
this.solgrep.report(undefined, this, "DUPES", this.dupeDb);
}
onClose() {
var uniqueContracts = {};

this.selectedModes.forEach(mode => {
if(typeof uniqueContracts[mode] === "undefined"){
uniqueContracts[mode] = 0;
}
uniqueContracts[mode] += Object.values(this.dupeDb[mode]).filter(v => v.length == 1).length; //number of contracts with unique hashes
this.dupeDb[mode] = utils.filterObjByValue(utils.sortObjByArrayLength(this.dupeDb[mode]), (v) => v.length > 1);
})
console.log("")
console.log("ℹ️ Duplicate Contracts (Hash => SourceUnits):")
console.log(this.dupeDb)
console.log("")
console.log("ℹ️ Number of Unique Contracts per matching method:")
console.log(uniqueContracts)
}
}
DupeFinder.description = "Find Duplicate Contracts! Either 'similar' (AST fuzzy matching) or exact (AST structure) matches.";

module.exports = {
DupeFinder
}

0 comments on commit 9730459

Please sign in to comment.