Skip to content

Commit f8cf817

Browse files
committed
Fix collector for DUP hashes
1 parent 1da9510 commit f8cf817

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

lib/collector.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class DataCollector {
2727
}
2828

2929
this.lastHash = null;
30+
this.viaIR = viaIR;
3031
}
3132

3233
/**
@@ -39,9 +40,9 @@ class DataCollector {
3940
if (this.validOpcodes[info.opcode.name] && info.stack.length > 0){
4041
const idx = info.stack.length - 1;
4142
let hash = '0x' + info.stack[idx].toString(16);
43+
4244
this._registerHash(hash)
4345

44-
// console.log('hash: ' + info.opcode.name + " " + hash)
4546
}
4647
} catch (err) { /*Ignore*/ };
4748
}
@@ -65,15 +66,28 @@ class DataCollector {
6566
_registerHash(hash){
6667
hash = this._normalizeHash(hash);
6768

68-
//console.log('normalized: ' + hash);
69-
7069
if(this.instrumentationData[hash]){
71-
7270
// abi.encode (used to circumvent viaIR) puts the hash on the stack twice
7371
if (this.lastHash !== hash) {
7472
this.lastHash = hash;
7573
this.instrumentationData[hash].hits++
7674
}
75+
return;
76+
}
77+
78+
// Detect and recover from viaIR mangled hashes by left-padding single `0`
79+
// for some cases
80+
if(this.viaIR && hash.length === 18) {
81+
hash = hash.slice(2);
82+
hash = '0' + hash;
83+
hash = hash.slice(0,16);
84+
hash = '0x' + hash;
85+
if(this.instrumentationData[hash]){
86+
if (this.lastHash !== hash) {
87+
this.lastHash = hash;
88+
this.instrumentationData[hash].hits++
89+
}
90+
}
7791
}
7892
}
7993

@@ -83,10 +97,14 @@ class DataCollector {
8397
* but prevents left-padding shorter irrelevant hashes
8498
*
8599
* @param {String} hash data hash from evm stack.
86-
* @return {String} 0x prefixed hash of length 66.
100+
* @return {String} 0x prefixed hash of length 18.
87101
*/
88102
_normalizeHash(hash){
89-
if (hash.length < 18 && hash.length > 11){
103+
// viaIR sometimes zero right-pads the hashes out to 32bytes
104+
// but it doesn't preserve leading zeroes when it does this
105+
if (this.viaIR && hash.length >= 18) {
106+
hash = hash.slice(0,18);
107+
} else if (hash.length < 18 && hash.length > 11){
90108
hash = hash.slice(2);
91109
while(hash.length < 16) hash = '0' + hash;
92110
hash = '0x' + hash

test/integration/standard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ describe('Hardhat Plugin: standard use cases', function() {
364364
verify.lineCoverage(expected);
365365
})
366366

367-
it.only('logicalOR & ternary conditionals', async function(){
367+
it('logicalOR & ternary conditionals', async function(){
368368
mock.installFullProject('ternary-and-logical-or');
369369
mock.hardhatSetupEnv(this);
370370

test/sources/projects/ternary-and-logical-or/test/test_or.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ contract("contract_or", function(accounts) {
55

66
before(async () => instance = await Contract_OR.new())
77

8-
it.only('_if', async function(){
8+
it('_if', async function(){
99
await instance._if(0);
1010
await instance._if(7);
1111
});

0 commit comments

Comments
 (0)