Skip to content

Commit c1bd4bc

Browse files
committed
Use bytes8 instead of bytes32 as hash key (#590)
1 parent bf77884 commit c1bd4bc

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

lib/collector.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ class DataCollector {
5353
}
5454

5555
/**
56-
* Left-pads zero prefixed bytes 32 hashes to length 66. The '59' in the
56+
* Left-pads zero prefixed bytes8 hashes to length 18. The '11' in the
5757
* comparison below is arbitrary. It provides a margin for recurring zeros
58-
* but prevents left-padding shorter irrelevant hashes (like fn sigs)
58+
* but prevents left-padding shorter irrelevant hashes
5959
*
6060
* @param {String} hash data hash from evm stack.
6161
* @return {String} 0x prefixed hash of length 66.
6262
*/
6363
_normalizeHash(hash){
64-
if (hash.length < 66 && hash.length > 59){
64+
if (hash.length < 18 && hash.length > 11){
6565
hash = hash.slice(2);
66-
while(hash.length < 64) hash = '0' + hash;
66+
while(hash.length < 16) hash = '0' + hash;
6767
hash = '0x' + hash
6868
}
6969
return hash;

lib/coverage.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ class Coverage {
7575
const hits = collectedData[hash].hits;
7676

7777
switch(collectedData[hash].type){
78-
case 'line': this.data[contractPath].l[id] = hits; break;
79-
case 'function': this.data[contractPath].f[id] = hits; break;
80-
case 'statement': this.data[contractPath].s[id] = hits; break;
81-
case 'branch': this.data[contractPath].b[id][data.locationIdx] = hits; break;
82-
case 'and-true': this.data[contractPath].b[id][data.locationIdx] = hits / 2; break;
83-
case 'or-false': this.data[contractPath].b[id][data.locationIdx] = hits / 2; break;
84-
case 'requirePre': this.requireData[contractPath][id].preEvents = hits; break;
85-
case 'requirePost': this.requireData[contractPath][id].postEvents = hits; break;
78+
case 'line': this.data[contractPath].l[id] = hits; break;
79+
case 'function': this.data[contractPath].f[id] = hits; break;
80+
case 'statement': this.data[contractPath].s[id] = hits; break;
81+
case 'branch': this.data[contractPath].b[id][data.locationIdx] = hits; break;
82+
case 'and-true': this.data[contractPath].b[id][data.locationIdx] = hits; break;
83+
case 'or-false': this.data[contractPath].b[id][data.locationIdx] = hits; break;
84+
case 'requirePre': this.requireData[contractPath][id].preEvents = hits; break;
85+
case 'requirePost': this.requireData[contractPath][id].postEvents = hits; break;
8686
}
8787
}
8888

lib/injector.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,26 @@ class Injector {
2828

2929
_getHash(id) {
3030
this.hashCounter++;
31-
return web3Utils.keccak256(`${id}:${this.hashCounter}`);
31+
return web3Utils.keccak256(`${id}:${this.hashCounter}`).slice(0,18);
3232
}
3333

3434
// Method returns void
3535
_getDefaultMethodIdentifier(id){
36-
return `c_${web3Utils.keccak256(id).slice(0,10)}`
36+
return `c_${web3Utils.keccak256(id).slice(2,10)}`
3737
}
3838

3939
// Method returns boolean: true
4040
_getTrueMethodIdentifier(id){
41-
return `c_true${web3Utils.keccak256(id).slice(0,10)}`
41+
return `c_true${web3Utils.keccak256(id).slice(2,10)}`
4242
}
4343

4444
// Method returns boolean: false
4545
_getFalseMethodIdentifier(id){
46-
return `c_false${web3Utils.keccak256(id).slice(0,10)}`
46+
return `c_false${web3Utils.keccak256(id).slice(2,10)}`
4747
}
4848

4949
_getModifierIdentifier(id){
50-
return `c_mod${web3Utils.keccak256(id).slice(0,10)}`
50+
return `c_mod${web3Utils.keccak256(id).slice(2,10)}`
5151
}
5252

5353
_getInjectionComponents(contract, injectionPoint, id, type){
@@ -70,9 +70,9 @@ class Injector {
7070
* @return {String}
7171
*/
7272
_getDefaultMethodDefinition(id){
73-
const hash = web3Utils.keccak256(id).slice(0,10);
73+
const hash = web3Utils.keccak256(id).slice(2,10);
7474
const method = this._getMethodIdentifier(id);
75-
return `\nfunction ${method}(bytes32 c__${hash}) internal pure {}\n`;
75+
return `\nfunction ${method}(bytes8 c__${hash}) internal pure {}\n`;
7676
}
7777

7878
/**
@@ -82,9 +82,9 @@ class Injector {
8282
* @return {String}
8383
*/
8484
_getFileScopedHashMethodDefinition(id, contract){
85-
const hash = web3Utils.keccak256(id).slice(0,10);
85+
const hash = web3Utils.keccak256(id).slice(2,10);
8686
const method = this._getDefaultMethodIdentifier(id);
87-
return `\nfunction ${method}(bytes32 c__${hash}) public pure {}\n`;
87+
return `\nfunction ${method}(bytes8 c__${hash}) public pure {}\n`;
8888
}
8989

9090
/**
@@ -94,9 +94,9 @@ class Injector {
9494
* @return {String} ex: bytes32[1] memory _sc_82e0891
9595
*/
9696
_getTrueMethodDefinition(id){
97-
const hash = web3Utils.keccak256(id).slice(0,10);
97+
const hash = web3Utils.keccak256(id).slice(2,10);
9898
const method = this._getTrueMethodIdentifier(id);
99-
return `function ${method}(bytes32 c__${hash}) public pure returns (bool){ return true; }\n`;
99+
return `function ${method}(bytes8 c__${hash}) public pure returns (bool){ return true; }\n`;
100100
}
101101

102102
/**
@@ -106,9 +106,9 @@ class Injector {
106106
* @return {String} ex: bytes32[1] memory _sc_82e0891
107107
*/
108108
_getFalseMethodDefinition(id){
109-
const hash = web3Utils.keccak256(id).slice(0,10);
109+
const hash = web3Utils.keccak256(id).slice(2,10);
110110
const method = this._getFalseMethodIdentifier(id);
111-
return `function ${method}(bytes32 c__${hash}) public pure returns (bool){ return false; }\n`;
111+
return `function ${method}(bytes8 c__${hash}) public pure returns (bool){ return false; }\n`;
112112
}
113113

114114
_getModifierDefinitions(contractId, instrumentation){

0 commit comments

Comments
 (0)