Code coverage for Solidity smart-contracts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
solidity-coverage/lib/collector.js

40 lines
935 B

const web3Utils = require('web3-utils')
class DataCollector {
constructor(config={}, instrumentationData={}){
this.instrumentationData = instrumentationData;
this.vm = config.vmResolver
? config.vmResolver(config.provider)
: this._ganacheVMResolver(config.provider);
this._connect();
}
// Subscribes to vm.on('step').
_connect(){
const self = this;
this.vm.on("step", function(info){
if (info.opcode.name.includes("PUSH") && info.stack.length > 0){
const idx = info.stack.length - 1;
const hash = web3Utils.toHex(info.stack[idx]).toString();
if(self.instrumentationData[hash]){
self.instrumentationData[hash].hits++;
}
}
})
}
_ganacheVMResolver(provider){
return provider.engine.manager.state.blockchain.vm;
}
_setInstrumentationData(data){
this.instrumentationData = data;
}
}
module.exports = DataCollector;