Use bytes8 instead of bytes32 as hash key (#590)

experimental-options
cgewecke 4 years ago
parent bf77884218
commit c1bd4bcd64
  1. 8
      lib/collector.js
  2. 4
      lib/coverage.js
  3. 26
      lib/injector.js

@ -53,17 +53,17 @@ class DataCollector {
} }
/** /**
* Left-pads zero prefixed bytes 32 hashes to length 66. The '59' in the * Left-pads zero prefixed bytes8 hashes to length 18. The '11' in the
* comparison below is arbitrary. It provides a margin for recurring zeros * comparison below is arbitrary. It provides a margin for recurring zeros
* but prevents left-padding shorter irrelevant hashes (like fn sigs) * but prevents left-padding shorter irrelevant hashes
* *
* @param {String} hash data hash from evm stack. * @param {String} hash data hash from evm stack.
* @return {String} 0x prefixed hash of length 66. * @return {String} 0x prefixed hash of length 66.
*/ */
_normalizeHash(hash){ _normalizeHash(hash){
if (hash.length < 66 && hash.length > 59){ if (hash.length < 18 && hash.length > 11){
hash = hash.slice(2); hash = hash.slice(2);
while(hash.length < 64) hash = '0' + hash; while(hash.length < 16) hash = '0' + hash;
hash = '0x' + hash hash = '0x' + hash
} }
return hash; return hash;

@ -79,8 +79,8 @@ class Coverage {
case 'function': this.data[contractPath].f[id] = hits; break; case 'function': this.data[contractPath].f[id] = hits; break;
case 'statement': this.data[contractPath].s[id] = hits; break; case 'statement': this.data[contractPath].s[id] = hits; break;
case 'branch': this.data[contractPath].b[id][data.locationIdx] = hits; break; case 'branch': this.data[contractPath].b[id][data.locationIdx] = hits; break;
case 'and-true': this.data[contractPath].b[id][data.locationIdx] = hits / 2; break; case 'and-true': this.data[contractPath].b[id][data.locationIdx] = hits; break;
case 'or-false': this.data[contractPath].b[id][data.locationIdx] = hits / 2; break; case 'or-false': this.data[contractPath].b[id][data.locationIdx] = hits; break;
case 'requirePre': this.requireData[contractPath][id].preEvents = hits; break; case 'requirePre': this.requireData[contractPath][id].preEvents = hits; break;
case 'requirePost': this.requireData[contractPath][id].postEvents = hits; break; case 'requirePost': this.requireData[contractPath][id].postEvents = hits; break;
} }

@ -28,26 +28,26 @@ class Injector {
_getHash(id) { _getHash(id) {
this.hashCounter++; this.hashCounter++;
return web3Utils.keccak256(`${id}:${this.hashCounter}`); return web3Utils.keccak256(`${id}:${this.hashCounter}`).slice(0,18);
} }
// Method returns void // Method returns void
_getDefaultMethodIdentifier(id){ _getDefaultMethodIdentifier(id){
return `c_${web3Utils.keccak256(id).slice(0,10)}` return `c_${web3Utils.keccak256(id).slice(2,10)}`
} }
// Method returns boolean: true // Method returns boolean: true
_getTrueMethodIdentifier(id){ _getTrueMethodIdentifier(id){
return `c_true${web3Utils.keccak256(id).slice(0,10)}` return `c_true${web3Utils.keccak256(id).slice(2,10)}`
} }
// Method returns boolean: false // Method returns boolean: false
_getFalseMethodIdentifier(id){ _getFalseMethodIdentifier(id){
return `c_false${web3Utils.keccak256(id).slice(0,10)}` return `c_false${web3Utils.keccak256(id).slice(2,10)}`
} }
_getModifierIdentifier(id){ _getModifierIdentifier(id){
return `c_mod${web3Utils.keccak256(id).slice(0,10)}` return `c_mod${web3Utils.keccak256(id).slice(2,10)}`
} }
_getInjectionComponents(contract, injectionPoint, id, type){ _getInjectionComponents(contract, injectionPoint, id, type){
@ -70,9 +70,9 @@ class Injector {
* @return {String} * @return {String}
*/ */
_getDefaultMethodDefinition(id){ _getDefaultMethodDefinition(id){
const hash = web3Utils.keccak256(id).slice(0,10); const hash = web3Utils.keccak256(id).slice(2,10);
const method = this._getMethodIdentifier(id); const method = this._getMethodIdentifier(id);
return `\nfunction ${method}(bytes32 c__${hash}) internal pure {}\n`; return `\nfunction ${method}(bytes8 c__${hash}) internal pure {}\n`;
} }
/** /**
@ -82,9 +82,9 @@ class Injector {
* @return {String} * @return {String}
*/ */
_getFileScopedHashMethodDefinition(id, contract){ _getFileScopedHashMethodDefinition(id, contract){
const hash = web3Utils.keccak256(id).slice(0,10); const hash = web3Utils.keccak256(id).slice(2,10);
const method = this._getDefaultMethodIdentifier(id); const method = this._getDefaultMethodIdentifier(id);
return `\nfunction ${method}(bytes32 c__${hash}) public pure {}\n`; return `\nfunction ${method}(bytes8 c__${hash}) public pure {}\n`;
} }
/** /**
@ -94,9 +94,9 @@ class Injector {
* @return {String} ex: bytes32[1] memory _sc_82e0891 * @return {String} ex: bytes32[1] memory _sc_82e0891
*/ */
_getTrueMethodDefinition(id){ _getTrueMethodDefinition(id){
const hash = web3Utils.keccak256(id).slice(0,10); const hash = web3Utils.keccak256(id).slice(2,10);
const method = this._getTrueMethodIdentifier(id); const method = this._getTrueMethodIdentifier(id);
return `function ${method}(bytes32 c__${hash}) public pure returns (bool){ return true; }\n`; return `function ${method}(bytes8 c__${hash}) public pure returns (bool){ return true; }\n`;
} }
/** /**
@ -106,9 +106,9 @@ class Injector {
* @return {String} ex: bytes32[1] memory _sc_82e0891 * @return {String} ex: bytes32[1] memory _sc_82e0891
*/ */
_getFalseMethodDefinition(id){ _getFalseMethodDefinition(id){
const hash = web3Utils.keccak256(id).slice(0,10); const hash = web3Utils.keccak256(id).slice(2,10);
const method = this._getFalseMethodIdentifier(id); const method = this._getFalseMethodIdentifier(id);
return `function ${method}(bytes32 c__${hash}) public pure returns (bool){ return false; }\n`; return `function ${method}(bytes8 c__${hash}) public pure returns (bool){ return false; }\n`;
} }
_getModifierDefinitions(contractId, instrumentation){ _getModifierDefinitions(contractId, instrumentation){

Loading…
Cancel
Save