From acd644fd29dbfd1a7a14850825b15ae379707090 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 3 Jul 2017 15:00:11 +0200 Subject: [PATCH 1/2] add & use compareByteCode --- src/code/sourceLocationTracker.js | 2 +- src/helpers/util.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/code/sourceLocationTracker.js b/src/code/sourceLocationTracker.js index 1bf434ac76..510df01d5a 100644 --- a/src/code/sourceLocationTracker.js +++ b/src/code/sourceLocationTracker.js @@ -68,7 +68,7 @@ function getSourceMap (address, code, contracts) { var isCreation = helper.isContractCreation(address) var byteProp = isCreation ? 'bytecode' : 'runtimeBytecode' for (var k in contracts) { - if (contracts[k][byteProp] && code.replace(util.swarmHashExtraction(), '').indexOf('0x' + contracts[k][byteProp].replace(util.swarmHashExtraction(), '')) === 0) { + if (util.compareByteCode(code, '0x' + contracts[k][byteProp])) { return isCreation ? contracts[k].srcmap : srcmapRuntime(contracts[k]) } } diff --git a/src/helpers/util.js b/src/helpers/util.js index 8c6aa39ffd..ee9d49ee42 100644 --- a/src/helpers/util.js +++ b/src/helpers/util.js @@ -148,9 +148,34 @@ module.exports = { */ swarmHashExtraction: function () { return /a165627a7a72305820([0-9a-f]{64})0029$/ + }, + + /** + * Compare bytecode. return true if the code is equal (handle swarm hash and library references) + * @param {String} code2 - the bytecode that is actually deployed (contains resolved library reference and a potentially different swarmhash) + * @param {String} code1 - the bytecode generated by the compiler (contains unresolved library reference and a potentially different swarmhash) + * + * @return {bool} + */ + compareByteCode: function (code1, code2) { + var pos = -1 + while ((pos = code2.search(/__(.*)__/)) !== -1) { + code2 = replaceLibReference(code2, pos) + code1 = replaceLibReference(code1, pos) + } + code1 = code1.replace(this.swarmHashExtraction(), '') + code2 = code2.replace(this.swarmHashExtraction(), '') + if (code1 && code2 && code1.indexOf(code2) === 0) { + return true + } + return false } } +function replaceLibReference (code, pos) { + return code.substring(0, pos) + '0000000000000000000000000000000000000000' + code.substring(pos + 40) +} + function buildCallPath (index, rootCall) { var ret = [] findCallInternal(index, rootCall, ret) From f2072054b81b4a8f7dadb868505c99291595da57 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 3 Jul 2017 15:36:50 +0200 Subject: [PATCH 2/2] use compareByteCode in contractNameFromCode --- src/helpers/util.js | 4 ++-- src/solidity/solidityProxy.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/helpers/util.js b/src/helpers/util.js index ee9d49ee42..284fce1198 100644 --- a/src/helpers/util.js +++ b/src/helpers/util.js @@ -152,8 +152,8 @@ module.exports = { /** * Compare bytecode. return true if the code is equal (handle swarm hash and library references) - * @param {String} code2 - the bytecode that is actually deployed (contains resolved library reference and a potentially different swarmhash) - * @param {String} code1 - the bytecode generated by the compiler (contains unresolved library reference and a potentially different swarmhash) + * @param {String} code1 - the bytecode that is actually deployed (contains resolved library reference and a potentially different swarmhash) + * @param {String} code2 - the bytecode generated by the compiler (contains unresolved library reference and a potentially different swarmhash) * * @return {bool} */ diff --git a/src/solidity/solidityProxy.js b/src/solidity/solidityProxy.js index 96895eb0c4..9065742bac 100644 --- a/src/solidity/solidityProxy.js +++ b/src/solidity/solidityProxy.js @@ -137,7 +137,7 @@ function contractNameFromCode (contracts, code, address) { var isCreation = traceHelper.isContractCreation(address) var byteProp = isCreation ? 'bytecode' : 'runtimeBytecode' for (var k in contracts) { - if (code.replace(util.swarmHashExtraction(), '').indexOf('0x' + contracts[k][byteProp].replace(util.swarmHashExtraction(), '')) === 0) { + if (util.compareByteCode(code, '0x' + contracts[k][byteProp])) { return k } }