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

106 lines
2.8 KiB

/**
* Writes data from the VM step to the in-memory
* coverage map constructed by the Instrumenter.
*/
class DataCollector {
10 months ago
constructor(instrumentationData={}, viaIR){
this.instrumentationData = instrumentationData;
this.validOpcodes = {
"PUSH1": true,
10 months ago
"DUP1": viaIR,
"DUP2": viaIR,
"DUP3": viaIR,
"DUP4": viaIR,
"DUP5": viaIR,
"DUP6": viaIR,
"DUP7": viaIR,
"DUP8": viaIR,
"DUP9": viaIR,
"DUP10": viaIR,
"DUP11": viaIR,
"DUP12": viaIR,
"DUP13": viaIR,
"DUP14": viaIR,
"DUP15": viaIR,
"DUP16": viaIR,
}
10 months ago
this.lastHash = null;
}
/**
* VM step event handler. Detects instrumentation hashes when they are pushed to the
* top of the stack. This runs millions of times - trying to keep it fast.
* @param {Object} info vm step info
*/
step(info){
try {
if (this.validOpcodes[info.opcode.name] && info.stack.length > 0){
const idx = info.stack.length - 1;
let hash = '0x' + info.stack[idx].toString(16);
this._registerHash(hash)
10 months ago
// console.log('hash: ' + info.opcode.name + " " + hash)
}
} catch (err) { /*Ignore*/ };
}
// Temporarily disabled because some relevant traces aren't available
/**
* Converts pushData value to string and registers in instrumentation map.
* @param {HardhatEVMTraceInstruction} instruction
*/
/*trackHardhatEVMInstruction(instruction){
if (instruction && instruction.pushData){
let hash = `0x` + instruction.pushData.toString('hex');
this._registerHash(hash)
}
}*/
/**
* Normalizes has string and marks hit.
* @param {String} hash bytes32 hash
*/
_registerHash(hash){
hash = this._normalizeHash(hash);
10 months ago
//console.log('normalized: ' + hash);
if(this.instrumentationData[hash]){
10 months ago
// abi.encode (used to circumvent viaIR) puts the hash on the stack twice
if (this.lastHash !== hash) {
this.lastHash = hash;
this.instrumentationData[hash].hits++
}
}
}
/**
* Left-pads zero prefixed bytes8 hashes to length 18. The '11' in the
* comparison below is arbitrary. It provides a margin for recurring zeros
* but prevents left-padding shorter irrelevant hashes
*
* @param {String} hash data hash from evm stack.
* @return {String} 0x prefixed hash of length 66.
*/
_normalizeHash(hash){
if (hash.length < 18 && hash.length > 11){
hash = hash.slice(2);
while(hash.length < 16) hash = '0' + hash;
hash = '0x' + hash
}
return hash;
}
/**
* Unit test helper
* @param {Object} data Instrumenter.instrumentationData
*/
_setInstrumentationData(data){
this.instrumentationData = data;
}
}
module.exports = DataCollector;