From 0e2a5964447a31eb4dd067a63ea26f364c3c5bda Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 5 Jul 2020 11:25:39 -0400 Subject: [PATCH 01/20] refactor getLastCallChangeSince --- libs/remix-debug/src/solidity-decoder/solidityProxy.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libs/remix-debug/src/solidity-decoder/solidityProxy.js b/libs/remix-debug/src/solidity-decoder/solidityProxy.js index ee3fd5beac..ba0964caf8 100644 --- a/libs/remix-debug/src/solidity-decoder/solidityProxy.js +++ b/libs/remix-debug/src/solidity-decoder/solidityProxy.js @@ -97,9 +97,12 @@ class SolidityProxy { */ extractStateVariablesAt (vmtraceIndex) { return new Promise((resolve, reject) => { - this.contractNameAt(vmtraceIndex).then((contractName) => { - resolve(this.extractStateVariables(contractName)) - }).catch(reject) + this.contractNameAt(vmtraceIndex, (error, contractName) => { + if (error) { + return reject(error) + } + return resolve(this.extractStateVariables(contractName)) + }) }) } From f1c93833b2e3640c690c1f5f9d7cf8bd4d672ed5 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 17 Jul 2020 15:10:03 -0400 Subject: [PATCH 02/20] refactor getStackAt --- libs/remix-debug/src/solidity-decoder/internalCallTree.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libs/remix-debug/src/solidity-decoder/internalCallTree.js b/libs/remix-debug/src/solidity-decoder/internalCallTree.js index b9c6872b45..f480340b53 100644 --- a/libs/remix-debug/src/solidity-decoder/internalCallTree.js +++ b/libs/remix-debug/src/solidity-decoder/internalCallTree.js @@ -270,6 +270,12 @@ function includeVariableDeclaration (tree, step, sourceLocation, scopeId, newLoc break } } + // input params + if (inputs) { + functionDefinitionAndInputs.inputs = addParams(inputs, tree, scopeId, states, contractName, previousSourceLocation, stack.length, inputs.children.length, -1) + } + // output params + if (outputs) addParams(outputs, tree, scopeId, states, contractName, previousSourceLocation, stack.length, 0, 1) } // input params if (inputs) { From 57f715f939d1709d35fc6d2e6a1db4d95805be40 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 17 Jul 2020 18:47:59 -0400 Subject: [PATCH 03/20] refacto getMemoryAt --- libs/remix-debug/src/solidity-decoder/internalCallTree.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/libs/remix-debug/src/solidity-decoder/internalCallTree.js b/libs/remix-debug/src/solidity-decoder/internalCallTree.js index f480340b53..b9c6872b45 100644 --- a/libs/remix-debug/src/solidity-decoder/internalCallTree.js +++ b/libs/remix-debug/src/solidity-decoder/internalCallTree.js @@ -270,12 +270,6 @@ function includeVariableDeclaration (tree, step, sourceLocation, scopeId, newLoc break } } - // input params - if (inputs) { - functionDefinitionAndInputs.inputs = addParams(inputs, tree, scopeId, states, contractName, previousSourceLocation, stack.length, inputs.children.length, -1) - } - // output params - if (outputs) addParams(outputs, tree, scopeId, states, contractName, previousSourceLocation, stack.length, 0, 1) } // input params if (inputs) { From 8bb7e18bc1186d51611f57b58e2ec46e08d7767e Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 22 Jul 2020 16:36:39 -0400 Subject: [PATCH 04/20] refactor decodeLocalsAt --- libs/remix-debug/src/Ethdebugger.js | 69 ++++++++--------------------- 1 file changed, 18 insertions(+), 51 deletions(-) diff --git a/libs/remix-debug/src/Ethdebugger.js b/libs/remix-debug/src/Ethdebugger.js index 2cebaeb149..2000e05375 100644 --- a/libs/remix-debug/src/Ethdebugger.js +++ b/libs/remix-debug/src/Ethdebugger.js @@ -88,58 +88,25 @@ Ethdebugger.prototype.extractLocalsAt = function (step, callback) { } Ethdebugger.prototype.decodeLocalsAt = function (step, sourceLocation, callback) { - const self = this - this.traceManager.waterfall([ - function getStackAt (stepIndex, callback) { - try { - const result = self.traceManager.getStackAt(stepIndex) - callback(null, result) - } catch (error) { - callback(error) - } - }, - function getMemoryAt (stepIndex, callback) { - try { - const result = self.traceManager.getMemoryAt(stepIndex) - callback(null, result) - } catch (error) { - callback(error) - } - }, - - function getCurrentCalledAddressAt (stepIndex, next) { - try { - const address = self.traceManager.getCurrentCalledAddressAt(stepIndex) - next(null, address) - } catch (error) { - next(error) - } - }], - step, - (error, result) => { - if (!error) { - const stack = result[0].value - const memory = result[1].value - try { - const storageViewer = new StorageViewer({ - stepIndex: step, - tx: this.tx, - address: result[2].value - }, this.storageResolver, this.traceManager) - localDecoder.solidityLocals(step, this.callTree, stack, memory, storageViewer, sourceLocation).then((locals) => { - if (!locals.error) { - callback(null, locals) - } else { - callback(locals.error) - } - }) - } catch (e) { - callback(e.message) + try { + const stack = this.traceManager.getStackAt(step) + const memory = this.traceManager.getMemoryAt(step) + const address = this.traceManager.getCurrentCalledAddressAt(step) + try { + const storageViewer = new StorageViewer({ stepIndex: step, tx: this.tx, address: address }, this.storageResolver, this.traceManager) + localDecoder.solidityLocals(step, this.callTree, stack, memory, storageViewer, sourceLocation).then((locals) => { + if (!locals.error) { + callback(null, locals) + } else { + callback(locals.error) } - } else { - callback(error) - } - }) + }) + } catch (e) { + callback(e.message) + } + } catch (error) { + callback(error) + } } /* decode state */ From 0a9aa84aa339edf9c53a02648d7745d4f49cbd74 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 22 Jul 2020 16:59:26 -0400 Subject: [PATCH 05/20] make sourceLocationFromVMTraceIndex a promise --- libs/remix-debug/src/Ethdebugger.js | 26 ++++++++------------------ libs/remix-debug/test/debugger.js | 11 ++++------- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/libs/remix-debug/src/Ethdebugger.js b/libs/remix-debug/src/Ethdebugger.js index 2000e05375..3b4d9a2f45 100644 --- a/libs/remix-debug/src/Ethdebugger.js +++ b/libs/remix-debug/src/Ethdebugger.js @@ -1,20 +1,16 @@ 'use strict' -const StorageViewer = require('./storage/storageViewer') -const StorageResolver = require('./storage/storageResolver') - -const SolidityDecoder = require('./solidity-decoder') -const SolidityProxy = SolidityDecoder.SolidityProxy -const stateDecoder = SolidityDecoder.stateDecoder -const localDecoder = SolidityDecoder.localDecoder -const InternalCallTree = SolidityDecoder.InternalCallTree - const remixLib = require('@remix-project/remix-lib') const TraceManager = remixLib.trace.TraceManager const CodeManager = remixLib.code.CodeManager const traceHelper = remixLib.helpers.trace const EventManager = remixLib.EventManager +const {SolidityProxy, stateDecoder, localDecoder, InternalCallTree} = require('./solidity-decoder') + +const StorageViewer = require('./storage/storageViewer') +const StorageResolver = require('./storage/storageResolver') + /** * Ethdebugger is a wrapper around a few classes that helps debugging a transaction * @@ -58,17 +54,11 @@ Ethdebugger.prototype.resolveStep = function (index) { } Ethdebugger.prototype.setCompilationResult = function (compilationResult) { - if (compilationResult && compilationResult.data) { - this.solidityProxy.reset(compilationResult.data) - } else { - this.solidityProxy.reset({}) - } + this.solidityProxy.reset((compilationResult && compilationResult.data) || {}) } -Ethdebugger.prototype.sourceLocationFromVMTraceIndex = function (address, stepIndex, callback) { - this.callTree.sourceLocationTracker.getSourceLocationFromVMTraceIndex(address, stepIndex, this.solidityProxy.contracts).then((rawLocation) => { - callback(null, rawLocation) - }).catch(callback) +Ethdebugger.prototype.sourceLocationFromVMTraceIndex = async function (address, stepIndex) { + return this.callTree.sourceLocationTracker.getSourceLocationFromVMTraceIndex(address, stepIndex, this.solidityProxy.contracts) } Ethdebugger.prototype.sourceLocationFromInstructionIndex = function (address, instIndex, callback) { diff --git a/libs/remix-debug/test/debugger.js b/libs/remix-debug/test/debugger.js index 81a800ee09..fb0f75fde7 100644 --- a/libs/remix-debug/test/debugger.js +++ b/libs/remix-debug/test/debugger.js @@ -253,19 +253,16 @@ function testDebugging (debugManager) { }) }) - tape('traceManager.decodeLocalsAt', (t) => { + tape('traceManager.decodeLocalsAt', async (t) => { t.plan(1) const tested = JSON.parse('{"proposalNames":{"value":[{"value":"0x48656C6C6F20576F726C64210000000000000000000000000000000000000000","type":"bytes32"}],"length":"0x1","type":"bytes32[]"},"p":{"value":"45","type":"uint256"},"addressLocal":{"value":"0x4B0897B0513FDC7C541B6D9D7E929C4E5364D2DB","type":"address"},"i":{"value":"2","type":"uint256"},"proposalsLocals":{"value":[{"value":{"name":{"value":"0x48656C6C6F20576F726C64210000000000000000000000000000000000000000","type":"bytes32"},"voteCount":{"value":"0","type":"uint256"}},"type":"struct Ballot.Proposal"}],"length":"0x1","type":"struct Ballot.Proposal[]"}}') try { const address = debugManager.traceManager.getCurrentCalledAddressAt(330) - debugManager.sourceLocationFromVMTraceIndex(address, 330, (error, location) => { + const location = await debugManager.sourceLocationFromVMTraceIndex(address, 330) + debugManager.decodeLocalsAt(330, location, (error, decodedlocals) => { if (error) return t.end(error) - debugManager.decodeLocalsAt(330, location, (error, decodedlocals) => { - if (error) return t.end(error) - t.equal(JSON.stringify(decodedlocals), JSON.stringify(tested)) - }) + t.equal(JSON.stringify(decodedlocals), JSON.stringify(tested)) }) - // }) } catch (error) { return t.end(error) } From 8f6b5100cbd37f8f3cc5a74e0372cf67c01e787b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 22 Jul 2020 17:06:50 -0400 Subject: [PATCH 06/20] make sourceLocationFromInstructionIndex a promise --- libs/remix-debug/src/Ethdebugger.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libs/remix-debug/src/Ethdebugger.js b/libs/remix-debug/src/Ethdebugger.js index 3b4d9a2f45..b087213d41 100644 --- a/libs/remix-debug/src/Ethdebugger.js +++ b/libs/remix-debug/src/Ethdebugger.js @@ -61,10 +61,8 @@ Ethdebugger.prototype.sourceLocationFromVMTraceIndex = async function (address, return this.callTree.sourceLocationTracker.getSourceLocationFromVMTraceIndex(address, stepIndex, this.solidityProxy.contracts) } -Ethdebugger.prototype.sourceLocationFromInstructionIndex = function (address, instIndex, callback) { - this.callTree.sourceLocationTracker.getSourceLocationFromInstructionIndex(address, instIndex, this.solidityProxy.contracts).then((rawLocation) => { - callback(null, rawLocation) - }).catch(callback) +Ethdebugger.prototype.sourceLocationFromInstructionIndex = async function (address, instIndex, callback) { + return this.callTree.sourceLocationTracker.getSourceLocationFromInstructionIndex(address, instIndex, this.solidityProxy.contracts) } /* breakpoint */ From 5ef253a866b17caf06f071353d4c9a63721e7acb Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 22 Jul 2020 17:08:49 -0400 Subject: [PATCH 07/20] remove callback from extractLocalsAt --- libs/remix-debug/src/Ethdebugger.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/remix-debug/src/Ethdebugger.js b/libs/remix-debug/src/Ethdebugger.js index b087213d41..a559ef5ccc 100644 --- a/libs/remix-debug/src/Ethdebugger.js +++ b/libs/remix-debug/src/Ethdebugger.js @@ -71,8 +71,8 @@ Ethdebugger.prototype.setBreakpointManager = function (breakpointManager) { } /* decode locals */ -Ethdebugger.prototype.extractLocalsAt = function (step, callback) { - callback(null, this.callTree.findScope(step)) +Ethdebugger.prototype.extractLocalsAt = function (step) { + return this.callTree.findScope(step) } Ethdebugger.prototype.decodeLocalsAt = function (step, sourceLocation, callback) { From 507d6429c4c836a950e4dbafdbf48e807bf334cd Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 22 Jul 2020 17:14:05 -0400 Subject: [PATCH 08/20] simplify decodeLocalsAt --- libs/remix-debug/src/Ethdebugger.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/libs/remix-debug/src/Ethdebugger.js b/libs/remix-debug/src/Ethdebugger.js index a559ef5ccc..612e6ffabf 100644 --- a/libs/remix-debug/src/Ethdebugger.js +++ b/libs/remix-debug/src/Ethdebugger.js @@ -75,20 +75,18 @@ Ethdebugger.prototype.extractLocalsAt = function (step) { return this.callTree.findScope(step) } -Ethdebugger.prototype.decodeLocalsAt = function (step, sourceLocation, callback) { +Ethdebugger.prototype.decodeLocalsAt = async function (step, sourceLocation, callback) { try { const stack = this.traceManager.getStackAt(step) const memory = this.traceManager.getMemoryAt(step) const address = this.traceManager.getCurrentCalledAddressAt(step) try { const storageViewer = new StorageViewer({ stepIndex: step, tx: this.tx, address: address }, this.storageResolver, this.traceManager) - localDecoder.solidityLocals(step, this.callTree, stack, memory, storageViewer, sourceLocation).then((locals) => { - if (!locals.error) { - callback(null, locals) - } else { - callback(locals.error) - } - }) + const locals = await localDecoder.solidityLocals(step, this.callTree, stack, memory, storageViewer, sourceLocation) + if (locals.error) { + return callback(locals.error) + } + return callback(null, locals) } catch (e) { callback(e.message) } From e847577527d2b8186ee9f50af090a1b2cda5b2cf Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 22 Jul 2020 17:21:15 -0400 Subject: [PATCH 09/20] remove callback from extractStateAt --- libs/remix-debug/src/Ethdebugger.js | 6 ++---- libs/remix-debug/test/debugger.js | 10 ++++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libs/remix-debug/src/Ethdebugger.js b/libs/remix-debug/src/Ethdebugger.js index 612e6ffabf..dfef3a1a59 100644 --- a/libs/remix-debug/src/Ethdebugger.js +++ b/libs/remix-debug/src/Ethdebugger.js @@ -96,10 +96,8 @@ Ethdebugger.prototype.decodeLocalsAt = async function (step, sourceLocation, cal } /* decode state */ -Ethdebugger.prototype.extractStateAt = function (step, callback) { - this.solidityProxy.extractStateVariablesAt(step).then((stateVars) => { - callback(null, stateVars) - }).catch(callback) +Ethdebugger.prototype.extractStateAt = async function (step) { + return this.solidityProxy.extractStateVariablesAt(step) } Ethdebugger.prototype.decodeStateAt = function (step, stateVars, callback) { diff --git a/libs/remix-debug/test/debugger.js b/libs/remix-debug/test/debugger.js index fb0f75fde7..caa4de049f 100644 --- a/libs/remix-debug/test/debugger.js +++ b/libs/remix-debug/test/debugger.js @@ -235,10 +235,10 @@ function testDebugging (debugManager) { } }) - tape('traceManager.decodeStateAt', (t) => { + tape('traceManager.decodeStateAt', async (t) => { t.plan(7) - debugManager.extractStateAt(312, (error, state) => { - if (error) return t.end(error) + try { + const state = await debugManager.extractStateAt(312) debugManager.decodeStateAt(312, state, (error, decodedState) => { if (error) return t.end(error) console.log(decodedState) @@ -250,7 +250,9 @@ function testDebugging (debugManager) { t.equal(decodedState['proposals'].length, '0x1') t.equal(decodedState['proposals'].type, 'struct Ballot.Proposal[]') }) - }) + } catch (error) { + if (error) return t.end(error) + } }) tape('traceManager.decodeLocalsAt', async (t) => { From c7018a7d12962e20c95713b23d180076d927922d Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 22 Jul 2020 17:27:39 -0400 Subject: [PATCH 10/20] refactor decodeStateAt --- libs/remix-debug/src/Ethdebugger.js | 17 ++++------------- libs/remix-debug/test/debugger.js | 20 +++++++++----------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/libs/remix-debug/src/Ethdebugger.js b/libs/remix-debug/src/Ethdebugger.js index dfef3a1a59..0c0c49796a 100644 --- a/libs/remix-debug/src/Ethdebugger.js +++ b/libs/remix-debug/src/Ethdebugger.js @@ -100,21 +100,12 @@ Ethdebugger.prototype.extractStateAt = async function (step) { return this.solidityProxy.extractStateVariablesAt(step) } -Ethdebugger.prototype.decodeStateAt = function (step, stateVars, callback) { +Ethdebugger.prototype.decodeStateAt = async function (step, stateVars, callback) { try { const address = this.traceManager.getCurrentCalledAddressAt(step) - const storageViewer = new StorageViewer({ - stepIndex: step, - tx: this.tx, - address: address - }, this.storageResolver, this.traceManager) - stateDecoder.decodeState(stateVars, storageViewer).then((result) => { - if (!result.error) { - callback(null, result) - } else { - callback(result.error) - } - }) + const storageViewer = new StorageViewer({stepIndex: step, tx: this.tx, address: address}, this.storageResolver, this.traceManager) + const result = await stateDecoder.decodeState(stateVars, storageViewer) + return result } catch (error) { callback(error) } diff --git a/libs/remix-debug/test/debugger.js b/libs/remix-debug/test/debugger.js index caa4de049f..5526e03f3e 100644 --- a/libs/remix-debug/test/debugger.js +++ b/libs/remix-debug/test/debugger.js @@ -239,17 +239,15 @@ function testDebugging (debugManager) { t.plan(7) try { const state = await debugManager.extractStateAt(312) - debugManager.decodeStateAt(312, state, (error, decodedState) => { - if (error) return t.end(error) - console.log(decodedState) - t.equal(decodedState['chairperson'].value, '0x4B0897B0513FDC7C541B6D9D7E929C4E5364D2DB') - t.equal(decodedState['chairperson'].type, 'address') - t.equal(decodedState['proposals'].value[0].value.voteCount.value, '0') - t.equal(decodedState['proposals'].value[0].value.voteCount.type, 'uint256') - t.equal(decodedState['proposals'].value[0].type, 'struct Ballot.Proposal') - t.equal(decodedState['proposals'].length, '0x1') - t.equal(decodedState['proposals'].type, 'struct Ballot.Proposal[]') - }) + const decodedState = await debugManager.decodeStateAt(312, state) + console.log(decodedState) + t.equal(decodedState['chairperson'].value, '0x4B0897B0513FDC7C541B6D9D7E929C4E5364D2DB') + t.equal(decodedState['chairperson'].type, 'address') + t.equal(decodedState['proposals'].value[0].value.voteCount.value, '0') + t.equal(decodedState['proposals'].value[0].value.voteCount.type, 'uint256') + t.equal(decodedState['proposals'].value[0].type, 'struct Ballot.Proposal') + t.equal(decodedState['proposals'].length, '0x1') + t.equal(decodedState['proposals'].type, 'struct Ballot.Proposal[]') } catch (error) { if (error) return t.end(error) } From 78b93513eabf3e84921a8eab9d4bd53d366389b8 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 22 Jul 2020 17:56:02 -0400 Subject: [PATCH 11/20] refactor resolveTrace into a promise --- libs/remix-debug/src/Ethdebugger.js | 29 +++++------- .../test/decoder/localsTests/int.js | 10 ++-- .../test/decoder/localsTests/misc.js | 10 ++-- .../test/decoder/localsTests/misc2.js | 10 ++-- .../test/decoder/localsTests/structArray.js | 10 ++-- .../test/decoder/stateTests/mapping.js | 3 +- libs/remix-lib/src/trace/traceManager.js | 46 ++++++++++--------- libs/remix-lib/test/codeManager.js | 10 ++-- libs/remix-lib/test/traceManager.js | 10 ++-- 9 files changed, 61 insertions(+), 77 deletions(-) diff --git a/libs/remix-debug/src/Ethdebugger.js b/libs/remix-debug/src/Ethdebugger.js index 0c0c49796a..a2b61fd8f5 100644 --- a/libs/remix-debug/src/Ethdebugger.js +++ b/libs/remix-debug/src/Ethdebugger.js @@ -112,11 +112,7 @@ Ethdebugger.prototype.decodeStateAt = async function (step, stateVars, callback) } Ethdebugger.prototype.storageViewAt = function (step, address) { - return new StorageViewer({ - stepIndex: step, - tx: this.tx, - address: address - }, this.storageResolver, this.traceManager) + return new StorageViewer({stepIndex: step, tx: this.tx, address: address}, this.storageResolver, this.traceManager) } Ethdebugger.prototype.updateWeb3 = function (web3) { @@ -134,21 +130,18 @@ Ethdebugger.prototype.debug = function (tx) { if (this.traceManager.isLoading) { return } - if (!tx.to) { - tx.to = traceHelper.contractCreationToken('0') - } + tx.to = tx.to || traceHelper.contractCreationToken('0') this.tx = tx - this.traceManager.resolveTrace(tx, async (error, result) => { - if (result) { - this.setCompilationResult(await this.compilationResult(tx.to)) - this.event.trigger('newTraceLoaded', [this.traceManager.trace]) - if (this.breakpointManager && this.breakpointManager.hasBreakpoint()) { - this.breakpointManager.jumpNextBreakpoint(false) - } - this.storageResolver = new StorageResolver({web3: this.traceManager.web3}) - } else { - this.statusMessage = error ? error.message : 'Trace not loaded' + + this.traceManager.resolveTrace(tx).then(async (result) => { + this.setCompilationResult(await this.compilationResult(tx.to)) + this.event.trigger('newTraceLoaded', [this.traceManager.trace]) + if (this.breakpointManager && this.breakpointManager.hasBreakpoint()) { + this.breakpointManager.jumpNextBreakpoint(false) } + this.storageResolver = new StorageResolver({web3: this.traceManager.web3}) + }).catch((error) => { + this.statusMessage = error ? error.message : 'Trace not loaded' }) } diff --git a/libs/remix-debug/test/decoder/localsTests/int.js b/libs/remix-debug/test/decoder/localsTests/int.js index dc1cd74375..2e7f5036e3 100644 --- a/libs/remix-debug/test/decoder/localsTests/int.js +++ b/libs/remix-debug/test/decoder/localsTests/int.js @@ -119,12 +119,10 @@ module.exports = function (st, vm, privateKey, contractBytecode, compilationResu cb() }) }) - traceManager.resolveTrace(tx, (error, result) => { - if (error) { - st.fail(error) - } else { - debuggerEvent.trigger('newTraceLoaded', [traceManager.trace]) - } + traceManager.resolveTrace(tx).then(() => { + debuggerEvent.trigger('newTraceLoaded', [traceManager.trace]) + }).catch((error) => { + st.fail(error) }) } }) diff --git a/libs/remix-debug/test/decoder/localsTests/misc.js b/libs/remix-debug/test/decoder/localsTests/misc.js index 48c727689a..be40d3de6e 100644 --- a/libs/remix-debug/test/decoder/localsTests/misc.js +++ b/libs/remix-debug/test/decoder/localsTests/misc.js @@ -65,12 +65,10 @@ module.exports = function (st, vm, privateKey, contractBytecode, compilationResu cb() }) }) - traceManager.resolveTrace(tx, (error, result) => { - if (error) { - st.fail(error) - } else { - debuggerEvent.trigger('newTraceLoaded', [traceManager.trace]) - } + traceManager.resolveTrace(tx).then(() => { + debuggerEvent.trigger('newTraceLoaded', [traceManager.trace]) + }).catch((error) => { + st.fail(error) }) } }) diff --git a/libs/remix-debug/test/decoder/localsTests/misc2.js b/libs/remix-debug/test/decoder/localsTests/misc2.js index e9ca0a4d74..3bdff3a259 100644 --- a/libs/remix-debug/test/decoder/localsTests/misc2.js +++ b/libs/remix-debug/test/decoder/localsTests/misc2.js @@ -51,12 +51,10 @@ module.exports = function (st, vm, privateKey, contractBytecode, compilationResu cb() }) }) - traceManager.resolveTrace(tx, (error, result) => { - if (error) { - st.fail(error) - } else { - debuggerEvent.trigger('newTraceLoaded', [traceManager.trace]) - } + traceManager.resolveTrace(tx).then(() => { + debuggerEvent.trigger('newTraceLoaded', [traceManager.trace]) + }).catch((error) => { + st.fail(error) }) } }) diff --git a/libs/remix-debug/test/decoder/localsTests/structArray.js b/libs/remix-debug/test/decoder/localsTests/structArray.js index c424727138..16235d9453 100644 --- a/libs/remix-debug/test/decoder/localsTests/structArray.js +++ b/libs/remix-debug/test/decoder/localsTests/structArray.js @@ -109,12 +109,10 @@ module.exports = function (st, vm, privateKey, contractBytecode, compilationResu cb() }) }) - traceManager.resolveTrace(tx, (error, result) => { - if (error) { - st.fail(error) - } else { - debuggerEvent.trigger('newTraceLoaded', [traceManager.trace]) - } + traceManager.resolveTrace(tx).then(() => { + debuggerEvent.trigger('newTraceLoaded', [traceManager.trace]) + }).catch((error) => { + st.fail(error) }) } }) diff --git a/libs/remix-debug/test/decoder/stateTests/mapping.js b/libs/remix-debug/test/decoder/stateTests/mapping.js index 97520367df..2fa355f7f8 100644 --- a/libs/remix-debug/test/decoder/stateTests/mapping.js +++ b/libs/remix-debug/test/decoder/stateTests/mapping.js @@ -46,7 +46,8 @@ function testMapping (st, vm, privateKey, contractAddress, output, cb) { st.end(error) } else { var traceManager = new TraceManager({web3: vm.web3}) - traceManager.resolveTrace(tx, () => { + + traceManager.resolveTrace(tx).then(() => { var storageViewer = new StorageViewer({ stepIndex: 268, tx: tx, diff --git a/libs/remix-lib/src/trace/traceManager.js b/libs/remix-lib/src/trace/traceManager.js index 386ec78e25..11ce5caa43 100644 --- a/libs/remix-lib/src/trace/traceManager.js +++ b/libs/remix-lib/src/trace/traceManager.js @@ -17,30 +17,32 @@ function TraceManager (options) { } // init section -TraceManager.prototype.resolveTrace = async function (tx, callback) { - this.tx = tx - this.init() - if (!this.web3) callback('web3 not loaded', false) - this.isLoading = true - try { - const result = await this.getTrace(tx.hash) - - if (result.structLogs.length > 0) { - this.trace = result.structLogs - - this.traceAnalyser.analyse(result.structLogs, tx) +TraceManager.prototype.resolveTrace = async function (tx) { + return new Promise(async (resolve, reject) => { + this.tx = tx + this.init() + if (!this.web3) reject('web3 not loaded') + this.isLoading = true + try { + const result = await this.getTrace(tx.hash) + + if (result.structLogs.length > 0) { + this.trace = result.structLogs + + this.traceAnalyser.analyse(result.structLogs, tx) + this.isLoading = false + return resolve(true) + } + var mes = tx.hash + ' is not a contract invocation or contract creation.' + console.log(mes) + this.isLoading = false + reject(mes) + } catch (error) { + console.log(error) this.isLoading = false - return callback(null, true) + reject(error) } - var mes = tx.hash + ' is not a contract invocation or contract creation.' - console.log(mes) - this.isLoading = false - callback(mes, false) - } catch (error) { - console.log(error) - this.isLoading = false - callback(error, false) - } + }) } TraceManager.prototype.getTrace = function (txHash) { diff --git a/libs/remix-lib/test/codeManager.js b/libs/remix-lib/test/codeManager.js index 52f0ea3768..4a25225e1d 100644 --- a/libs/remix-lib/test/codeManager.js +++ b/libs/remix-lib/test/codeManager.js @@ -23,12 +23,10 @@ tape('CodeManager', function (t) { const contractCode = web3.eth.getCode('0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5') codeManager.codeResolver.cacheExecutingCode('0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5', contractCode) // so a call to web3 is not necessary const tx = web3.eth.getTransaction('0x20ef65b8b186ca942fcccd634f37074dde49b541c27994fc7596740ef44cfd51') - traceManager.resolveTrace(tx, function (error, result) { - if (error) { - t.fail(' - traceManager.resolveTrace - failed ' + result) - } else { - continueTesting(t, codeManager) - } + traceManager.resolveTrace(tx).then(() => { + continueTesting(t, codeManager) + }).catch(() => { + t.fail(' - traceManager.resolveTrace - failed ') }) } }) diff --git a/libs/remix-lib/test/traceManager.js b/libs/remix-lib/test/traceManager.js index 83f12b31d6..ee65b82ab9 100644 --- a/libs/remix-lib/test/traceManager.js +++ b/libs/remix-lib/test/traceManager.js @@ -27,12 +27,10 @@ tape('TraceManager', function (t) { t.test('TraceManager.resolveTrace', function (st) { const tx = web3.eth.getTransaction('0x20ef65b8b186ca942fcccd634f37074dde49b541c27994fc7596740ef44cfd51') - traceManager.resolveTrace(tx, function (error, result) { - if (error) { - st.fail(' - traceManager.resolveTrace - failed ' + result) - } else { - st.end() - } + traceManager.resolveTrace(tx).then(() => { + st.end() + }).catch(() => { + st.fail(' - traceManager.resolveTrace - failed ') }) }) From 9c77e3d2f1c584364cb0251e5642a73885e808e6 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 24 Jul 2020 14:47:41 -0400 Subject: [PATCH 12/20] refactor getCode --- .../src/solidity-decoder/solidityProxy.js | 16 ++++++---------- libs/remix-lib/src/code/codeManager.js | 18 ++++++++---------- libs/remix-lib/src/sourceLocationTracker.js | 18 +++++++----------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/libs/remix-debug/src/solidity-decoder/solidityProxy.js b/libs/remix-debug/src/solidity-decoder/solidityProxy.js index ba0964caf8..d5405dd99b 100644 --- a/libs/remix-debug/src/solidity-decoder/solidityProxy.js +++ b/libs/remix-debug/src/solidity-decoder/solidityProxy.js @@ -39,21 +39,17 @@ class SolidityProxy { * @param {Int} vmTraceIndex - index in the vm trave where to resolve the executed contract name * @param {Function} cb - callback returns (error, contractName) */ - contractNameAt (vmTraceIndex) { - return new Promise((resolve, reject) => { + async contractNameAt (vmTraceIndex) { + return new Promise(async (resolve, reject) => { try { const address = this.traceManager.getCurrentCalledAddressAt(vmTraceIndex) if (this.cache.contractNameByAddress[address]) { return resolve(this.cache.contractNameByAddress[address]) } - this.codeManager.getCode(address, (error, code) => { - if (error) { - return reject(error) - } - const contractName = contractNameFromCode(this.contracts, code.bytecode, address) - this.cache.contractNameByAddress[address] = contractName - resolve(contractName) - }) + const code = await this.codeManager.getCode(address) + const contractName = contractNameFromCode(this.contracts, code.bytecode, address) + this.cache.contractNameByAddress[address] = contractName + resolve(contractName) } catch (error) { reject(error) } diff --git a/libs/remix-lib/src/code/codeManager.js b/libs/remix-lib/src/code/codeManager.js index d665192a28..fb13172d11 100644 --- a/libs/remix-lib/src/code/codeManager.js +++ b/libs/remix-lib/src/code/codeManager.js @@ -53,19 +53,18 @@ CodeManager.prototype.resolveStep = function (stepIndex, tx) { * @param {String} address - address of the contract to get the code from * @param {Function} cb - callback function, return the bytecode */ -CodeManager.prototype.getCode = function (address, cb) { +CodeManager.prototype.getCode = async function (address) { if (!traceHelper.isContractCreation(address)) { - return this.codeResolver.resolveCode(address).then((code) => { - cb(null, code) - }) + const code = await this.codeResolver.resolveCode(address) + return code } var codes = this.codeResolver.getExecutingCodeFromCache(address) if (codes) { - return cb(null, codes) + return codes } const hexCode = this.traceManager.getContractCreationCode(address) codes = this.codeResolver.cacheExecutingCode(address, hexCode) - cb(null, codes) + return codes } /** @@ -120,11 +119,10 @@ CodeManager.prototype.getFunctionFromPC = function (address, pc, sourceMap, ast) } function retrieveCodeAndTrigger (codeMananger, address, stepIndex, tx) { - codeMananger.getCode(address, (error, result) => { - if (error) { - return console.log(error) - } + codeMananger.getCode(address).then((result) => { retrieveIndexAndTrigger(codeMananger, address, stepIndex, result.instructions) + }).catch((error) => { + return console.log(error) }) } diff --git a/libs/remix-lib/src/sourceLocationTracker.js b/libs/remix-lib/src/sourceLocationTracker.js index 79c5a82357..7755e1dd8d 100644 --- a/libs/remix-lib/src/sourceLocationTracker.js +++ b/libs/remix-lib/src/sourceLocationTracker.js @@ -78,19 +78,15 @@ function extractSourceMap (self, codeManager, address, contracts) { return new Promise((resolve, reject) => { if (self.sourceMapByAddress[address]) return resolve(self.sourceMapByAddress[address]) - codeManager.getCode(address, (error, result) => { - if (!error) { - const sourceMap = getSourceMap(address, result.bytecode, contracts) - if (sourceMap) { - if (!helper.isContractCreation(address)) self.sourceMapByAddress[address] = sourceMap - resolve(sourceMap) - } else { - reject('no sourcemap associated with the code ' + address) - } + codeManager.getCode(address).then((result) => { + const sourceMap = getSourceMap(address, result.bytecode, contracts) + if (sourceMap) { + if (!helper.isContractCreation(address)) self.sourceMapByAddress[address] = sourceMap + resolve(sourceMap) } else { - reject(error) + reject('no sourcemap associated with the code ' + address) } - }) + }).catch(reject) }) } From 5ae4763b8b5fea3f991cbb23b4e2bbebb63aab3d Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 24 Jul 2020 15:08:57 -0400 Subject: [PATCH 13/20] refactor extractStateVariablesAt --- .../src/solidity-decoder/solidityProxy.js | 38 ++++++------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/libs/remix-debug/src/solidity-decoder/solidityProxy.js b/libs/remix-debug/src/solidity-decoder/solidityProxy.js index d5405dd99b..e4f10a53e6 100644 --- a/libs/remix-debug/src/solidity-decoder/solidityProxy.js +++ b/libs/remix-debug/src/solidity-decoder/solidityProxy.js @@ -40,20 +40,14 @@ class SolidityProxy { * @param {Function} cb - callback returns (error, contractName) */ async contractNameAt (vmTraceIndex) { - return new Promise(async (resolve, reject) => { - try { - const address = this.traceManager.getCurrentCalledAddressAt(vmTraceIndex) - if (this.cache.contractNameByAddress[address]) { - return resolve(this.cache.contractNameByAddress[address]) - } - const code = await this.codeManager.getCode(address) - const contractName = contractNameFromCode(this.contracts, code.bytecode, address) - this.cache.contractNameByAddress[address] = contractName - resolve(contractName) - } catch (error) { - reject(error) - } - }) + const address = this.traceManager.getCurrentCalledAddressAt(vmTraceIndex) + if (this.cache.contractNameByAddress[address]) { + return this.cache.contractNameByAddress[address] + } + const code = await this.codeManager.getCode(address) + const contractName = contractNameFromCode(this.contracts, code.bytecode, address) + this.cache.contractNameByAddress[address] = contractName + return contractName } /** @@ -91,15 +85,9 @@ class SolidityProxy { * @param {Int} vmTraceIndex - index in the vm trave where to resolve the state variables * @return {Object} - returns state variables of @args vmTraceIndex */ - extractStateVariablesAt (vmtraceIndex) { - return new Promise((resolve, reject) => { - this.contractNameAt(vmtraceIndex, (error, contractName) => { - if (error) { - return reject(error) - } - return resolve(this.extractStateVariables(contractName)) - }) - }) + async extractStateVariablesAt (vmtraceIndex) { + const contractName = await this.contractNameAt(vmtraceIndex) + return this.extractStateVariables(contractName) } /** @@ -112,10 +100,8 @@ class SolidityProxy { const file = this.fileNameFromIndex(sourceLocation.file) if (this.sources[file]) { return this.sources[file].legacyAST - } else { - // console.log('AST not found for file id ' + sourceLocation.file) - return null } + return null } /** From 7feb9edb3e003caebd036e31ece2a2f785e6dc58 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 24 Jul 2020 15:13:37 -0400 Subject: [PATCH 14/20] refactor extractSourceLocation --- .../src/solidity-decoder/internalCallTree.js | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/libs/remix-debug/src/solidity-decoder/internalCallTree.js b/libs/remix-debug/src/solidity-decoder/internalCallTree.js index b9c6872b45..56d41d4faa 100644 --- a/libs/remix-debug/src/solidity-decoder/internalCallTree.js +++ b/libs/remix-debug/src/solidity-decoder/internalCallTree.js @@ -124,23 +124,15 @@ class InternalCallTree { return functions } - extractSourceLocation (step) { - return new Promise((resolve, reject) => { - try { - const address = this.traceManager.getCurrentCalledAddressAt(step) - try { - this.sourceLocationTracker.getSourceLocationFromVMTraceIndex(address, step, this.solidityProxy.contracts).then(resolve).catch((error) => { - return reject('InternalCallTree - Cannot retrieve sourcelocation for step ' + step + ' ' + error) - }) - } catch (error) { - return reject('InternalCallTree - Cannot retrieve address for step ' + step + ' ' + error) - } - } catch (error) { - return reject('InternalCallTree - Cannot retrieve address for step ' + step + ' ' + error) - } - }) + async extractSourceLocation (step) { + try { + const address = this.traceManager.getCurrentCalledAddressAt(step) + const location = await this.sourceLocationTracker.getSourceLocationFromVMTraceIndex(address, step, this.solidityProxy.contracts) + return location + } catch (error) { + throw new Error('InternalCallTree - Cannot retrieve sourcelocation for step ' + step + ' ' + error) + } } - } async function buildTree (tree, step, scopeId, isExternalCall) { From 6698706e98f735f8ac238bc83088addfc302a66b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 24 Jul 2020 15:31:30 -0400 Subject: [PATCH 15/20] refactor storageResolver --- .../src/storage/storageResolver.js | 64 ++++++++----------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/libs/remix-debug/src/storage/storageResolver.js b/libs/remix-debug/src/storage/storageResolver.js index 0de8fbd161..8186e87368 100644 --- a/libs/remix-debug/src/storage/storageResolver.js +++ b/libs/remix-debug/src/storage/storageResolver.js @@ -39,17 +39,14 @@ class StorageResolver { * @param {Array} corrections - used in case the calculated sha3 has been modifyed before SSTORE (notably used for struct in mapping). * @return {Function} - callback */ - initialPreimagesMappings (tx, stepIndex, address, corrections) { - return new Promise((resolve, reject) => { - if (this.preimagesMappingByAddress[address]) { - return resolve(this.preimagesMappingByAddress[address]) - } - this.storageRange(tx, stepIndex, address).then((storage) => { - const mappings = mappingPreimages.decodeMappingsKeys(this.web3, storage, corrections) - this.preimagesMappingByAddress[address] = mappings - resolve(mappings) - }).catch(reject) - }) + async initialPreimagesMappings (tx, stepIndex, address, corrections) { + if (this.preimagesMappingByAddress[address]) { + return this.preimagesMappingByAddress[address] + } + const storage = await this.storageRange(tx, stepIndex, address) + const mappings = mappingPreimages.decodeMappingsKeys(this.web3, storage, corrections) + this.preimagesMappingByAddress[address] = mappings + return mappings } /** @@ -61,12 +58,9 @@ class StorageResolver { * @param {String} - address - lookup address * @param {Function} - callback - {key, hashedKey, value} - */ - storageSlot (slot, tx, stepIndex, address) { - return new Promise((resolve, reject) => { - this.storageRangeInternal(this, slot, tx, stepIndex, address).then((storage) => { - resolve(storage[slot] !== undefined ? storage[slot] : null) - }).catch(reject) - }) + async storageSlot (slot, tx, stepIndex, address) { + const storage = await this.storageRangeInternal(this, slot, tx, stepIndex, address) + return (storage[slot] !== undefined ? storage[slot] : null) } /** @@ -85,27 +79,21 @@ class StorageResolver { * even if the next 1000 items are not in the cache. * - If @arg slot is not cached, the corresponding value will be resolved and the next 1000 slots. */ - storageRangeInternal (self, slotKey, tx, stepIndex, address) { - return new Promise((resolve, reject) => { - var cached = this.fromCache(self, address) - if (cached && cached.storage[slotKey]) { // we have the current slot in the cache and maybe the next 1000... - return resolve(cached.storage) - } - this.storageRangeWeb3Call(tx, address, slotKey, self.maxSize).then((result) => { - const [storage, nextKey] = result - if (!storage[slotKey] && slotKey !== self.zeroSlot) { // we don't cache the zero slot (could lead to inconsistency) - storage[slotKey] = { - key: slotKey, - value: self.zeroSlot - } - } - self.toCache(self, address, storage) - if (slotKey === self.zeroSlot && !nextKey) { // only working if keys are sorted !! - self.storageByAddress[address].complete = true - } - return resolve(storage) - }).catch(reject) - }) + async storageRangeInternal (self, slotKey, tx, stepIndex, address) { + var cached = this.fromCache(self, address) + if (cached && cached.storage[slotKey]) { // we have the current slot in the cache and maybe the next 1000... + return cached.storage + } + const result = await this.storageRangeWeb3Call(tx, address, slotKey, self.maxSize) + const [storage, nextKey] = result + if (!storage[slotKey] && slotKey !== self.zeroSlot) { // we don't cache the zero slot (could lead to inconsistency) + storage[slotKey] = {key: slotKey, value: self.zeroSlot} + } + self.toCache(self, address, storage) + if (slotKey === self.zeroSlot && !nextKey) { // only working if keys are sorted !! + self.storageByAddress[address].complete = true + } + return storage } /** From d19e441f3b381d13f85650d116f217d450acde20 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 24 Jul 2020 15:46:55 -0400 Subject: [PATCH 16/20] refactor extractMappingsLocationChanges --- libs/remix-debug/src/storage/storageViewer.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/libs/remix-debug/src/storage/storageViewer.js b/libs/remix-debug/src/storage/storageViewer.js index 206701a637..0661dd3f9c 100644 --- a/libs/remix-debug/src/storage/storageViewer.js +++ b/libs/remix-debug/src/storage/storageViewer.js @@ -77,13 +77,8 @@ class StorageViewer { async mappingsLocation (corrections) { if (!this.currentMappingsLocationPromise) { this.currentMappingsLocationPromise = new Promise((resolve, reject) => { - this.extractMappingsLocationChanges(this.storageChanges, corrections, (error, mappingsLocationChanges) => { - if (error) { - reject(error) - } else { - resolve(mappingsLocationChanges) - } - }) + const mappingsLocationChanges = this.extractMappingsLocationChanges(this.storageChanges, corrections) + return resolve(mappingsLocationChanges) }) } return this.currentMappingsLocationPromise @@ -94,13 +89,13 @@ class StorageViewer { * @param {Map} storageChanges * @param {Array} corrections - used in case the calculated sha3 has been modifyed before SSTORE (notably used for struct in mapping). */ - extractMappingsLocationChanges (storageChanges, corrections, callback) { + extractMappingsLocationChanges (storageChanges, corrections) { if (this.mappingsLocationChanges) { - return callback(null, this.mappingsLocationChanges) + return this.mappingsLocationChanges } const mappings = mappingPreimages.decodeMappingsKeys(this.web3, storageChanges, corrections) this.mappingsLocationChanges = mappings - return callback(null, this.mappingsLocationChanges) + return this.mappingsLocationChanges } } From 245c48d2ece1c851c601da6f8368a2973e796a17 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 24 Jul 2020 15:52:42 -0400 Subject: [PATCH 17/20] refactor getSourceLocationFromInstructionIndex --- libs/remix-lib/src/sourceLocationTracker.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/libs/remix-lib/src/sourceLocationTracker.js b/libs/remix-lib/src/sourceLocationTracker.js index 7755e1dd8d..f4b27b269e 100644 --- a/libs/remix-lib/src/sourceLocationTracker.js +++ b/libs/remix-lib/src/sourceLocationTracker.js @@ -22,12 +22,9 @@ function SourceLocationTracker (_codeManager) { * @param {Object} contractDetails - AST of compiled contracts * @param {Function} cb - callback function */ -SourceLocationTracker.prototype.getSourceLocationFromInstructionIndex = function (address, index, contracts) { - return new Promise((resolve, reject) => { - extractSourceMap(this, this.codeManager, address, contracts).then((sourceMap) => { - resolve(this.sourceMappingDecoder.atIndex(index, sourceMap)) - }).catch(reject) - }) +SourceLocationTracker.prototype.getSourceLocationFromInstructionIndex = async function (address, index, contracts) { + const sourceMap = await extractSourceMap(this, this.codeManager, address, contracts) + return this.sourceMappingDecoder.atIndex(index, sourceMap) } /** From 31ba1e8f3727481d2a51306bb4316551c560f99a Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 24 Jul 2020 16:23:16 -0400 Subject: [PATCH 18/20] refactor GetInstructionIndex --- libs/remix-lib/src/code/codeManager.js | 24 ++++++++++++++----- libs/remix-lib/src/sourceLocationTracker.js | 16 ++++--------- libs/remix-lib/test/codeManager.js | 26 ++++++++++----------- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/libs/remix-lib/src/code/codeManager.js b/libs/remix-lib/src/code/codeManager.js index fb13172d11..1897d64b69 100644 --- a/libs/remix-lib/src/code/codeManager.js +++ b/libs/remix-lib/src/code/codeManager.js @@ -104,6 +104,17 @@ CodeManager.prototype.getInstructionIndex = function (address, step, callback) { } } +CodeManager.prototype.newGetInstructionIndex = function (address, step) { + try { + const pc = this.traceManager.getCurrentPC(step) + const itemIndex = this.codeResolver.getInstructionIndex(address, pc) + return itemIndex + } catch (error) { + console.log(error) + throw new Error('Cannot retrieve current PC for ' + step) + } +} + /** * Retrieve the called function for the given @arg pc and @arg address * @@ -127,12 +138,13 @@ function retrieveCodeAndTrigger (codeMananger, address, stepIndex, tx) { } function retrieveIndexAndTrigger (codeMananger, address, step, code) { - codeMananger.getInstructionIndex(address, step, (error, result) => { - if (error) { - return console.log(error) - } - codeMananger.event.trigger('changed', [code, address, result]) - }) + let result + try { + result = codeMananger.newGetInstructionIndex(address, step) + } catch (error) { + return console.log(error) + } + codeMananger.event.trigger('changed', [code, address, result]) } module.exports = CodeManager diff --git a/libs/remix-lib/src/sourceLocationTracker.js b/libs/remix-lib/src/sourceLocationTracker.js index f4b27b269e..5a2d133895 100644 --- a/libs/remix-lib/src/sourceLocationTracker.js +++ b/libs/remix-lib/src/sourceLocationTracker.js @@ -35,18 +35,10 @@ SourceLocationTracker.prototype.getSourceLocationFromInstructionIndex = async fu * @param {Object} contractDetails - AST of compiled contracts * @param {Function} cb - callback function */ -SourceLocationTracker.prototype.getSourceLocationFromVMTraceIndex = function (address, vmtraceStepIndex, contracts) { - return new Promise((resolve, reject) => { - extractSourceMap(this, this.codeManager, address, contracts).then((sourceMap) => { - this.codeManager.getInstructionIndex(address, vmtraceStepIndex, (error, index) => { - if (error) { - reject(error) - } else { - resolve(this.sourceMappingDecoder.atIndex(index, sourceMap)) - } - }) - }).catch(reject) - }) +SourceLocationTracker.prototype.getSourceLocationFromVMTraceIndex = async function (address, vmtraceStepIndex, contracts) { + const sourceMap = await extractSourceMap(this, this.codeManager, address, contracts) + const index = this.codeManager.newGetInstructionIndex(address, vmtraceStepIndex) + return this.sourceMappingDecoder.atIndex(index, sourceMap) } SourceLocationTracker.prototype.clearCache = function () { diff --git a/libs/remix-lib/test/codeManager.js b/libs/remix-lib/test/codeManager.js index 4a25225e1d..06d7e78efa 100644 --- a/libs/remix-lib/test/codeManager.js +++ b/libs/remix-lib/test/codeManager.js @@ -68,22 +68,20 @@ function continueTesting (t, codeManager) { t.test('CodeManager.getInstructionIndex', function (st) { st.plan(2) - codeManager.getInstructionIndex('0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5', 16, function (error, result) { + try { + const result = codeManager.newGetInstructionIndex('0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5', 16) console.log(result) - if (error) { - st.fail(error) - } else { - st.ok(result === 25) - } - }) + st.ok(result === 25) + } catch (error) { + st.fail(error) + } - codeManager.getInstructionIndex('(Contract Creation - Step 63)', 70, function (error, result) { + try { + const result = codeManager.newGetInstructionIndex('(Contract Creation - Step 63)', 70) console.log(result) - if (error) { - st.fail(error) - } else { - st.ok(result === 6) - } - }) + st.ok(result === 6) + } catch (error) { + st.fail(error) + } }) } From d3207060e148fcd94ad6ece155a0a2161d18303b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 24 Jul 2020 16:30:21 -0400 Subject: [PATCH 19/20] refactor GetInstructionIndex --- libs/remix-lib/src/code/codeManager.js | 15 ++------------- libs/remix-lib/src/sourceLocationTracker.js | 2 +- libs/remix-lib/test/codeManager.js | 4 ++-- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/libs/remix-lib/src/code/codeManager.js b/libs/remix-lib/src/code/codeManager.js index 1897d64b69..3f4df37b6f 100644 --- a/libs/remix-lib/src/code/codeManager.js +++ b/libs/remix-lib/src/code/codeManager.js @@ -93,18 +93,7 @@ CodeManager.prototype.getFunctionFromStep = function (stepIndex, sourceMap, ast) * @param {String} step - vm trace step * @param {Function} callback - instruction index */ -CodeManager.prototype.getInstructionIndex = function (address, step, callback) { - try { - const pc = this.traceManager.getCurrentPC(step) - const itemIndex = this.codeResolver.getInstructionIndex(address, pc) - callback(null, itemIndex) - } catch (error) { - console.log(error) - return callback('Cannot retrieve current PC for ' + step, null) - } -} - -CodeManager.prototype.newGetInstructionIndex = function (address, step) { +CodeManager.prototype.getInstructionIndex = function (address, step) { try { const pc = this.traceManager.getCurrentPC(step) const itemIndex = this.codeResolver.getInstructionIndex(address, pc) @@ -140,7 +129,7 @@ function retrieveCodeAndTrigger (codeMananger, address, stepIndex, tx) { function retrieveIndexAndTrigger (codeMananger, address, step, code) { let result try { - result = codeMananger.newGetInstructionIndex(address, step) + result = codeMananger.getInstructionIndex(address, step) } catch (error) { return console.log(error) } diff --git a/libs/remix-lib/src/sourceLocationTracker.js b/libs/remix-lib/src/sourceLocationTracker.js index 5a2d133895..2ce73865c2 100644 --- a/libs/remix-lib/src/sourceLocationTracker.js +++ b/libs/remix-lib/src/sourceLocationTracker.js @@ -37,7 +37,7 @@ SourceLocationTracker.prototype.getSourceLocationFromInstructionIndex = async fu */ SourceLocationTracker.prototype.getSourceLocationFromVMTraceIndex = async function (address, vmtraceStepIndex, contracts) { const sourceMap = await extractSourceMap(this, this.codeManager, address, contracts) - const index = this.codeManager.newGetInstructionIndex(address, vmtraceStepIndex) + const index = this.codeManager.getInstructionIndex(address, vmtraceStepIndex) return this.sourceMappingDecoder.atIndex(index, sourceMap) } diff --git a/libs/remix-lib/test/codeManager.js b/libs/remix-lib/test/codeManager.js index 06d7e78efa..5ced6c6a74 100644 --- a/libs/remix-lib/test/codeManager.js +++ b/libs/remix-lib/test/codeManager.js @@ -69,7 +69,7 @@ function continueTesting (t, codeManager) { t.test('CodeManager.getInstructionIndex', function (st) { st.plan(2) try { - const result = codeManager.newGetInstructionIndex('0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5', 16) + const result = codeManager.getInstructionIndex('0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5', 16) console.log(result) st.ok(result === 25) } catch (error) { @@ -77,7 +77,7 @@ function continueTesting (t, codeManager) { } try { - const result = codeManager.newGetInstructionIndex('(Contract Creation - Step 63)', 70) + const result = codeManager.getInstructionIndex('(Contract Creation - Step 63)', 70) console.log(result) st.ok(result === 6) } catch (error) { From f8f965ec64f9e9ba17a29c64769d3109697dca28 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 27 Aug 2020 16:01:32 -0400 Subject: [PATCH 20/20] fix resolveTrace method --- libs/remix-lib/src/trace/traceManager.js | 44 +++++++++++------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/libs/remix-lib/src/trace/traceManager.js b/libs/remix-lib/src/trace/traceManager.js index 11ce5caa43..7109c5c744 100644 --- a/libs/remix-lib/src/trace/traceManager.js +++ b/libs/remix-lib/src/trace/traceManager.js @@ -18,31 +18,29 @@ function TraceManager (options) { // init section TraceManager.prototype.resolveTrace = async function (tx) { - return new Promise(async (resolve, reject) => { - this.tx = tx - this.init() - if (!this.web3) reject('web3 not loaded') - this.isLoading = true - try { - const result = await this.getTrace(tx.hash) - - if (result.structLogs.length > 0) { - this.trace = result.structLogs - - this.traceAnalyser.analyse(result.structLogs, tx) - this.isLoading = false - return resolve(true) - } - var mes = tx.hash + ' is not a contract invocation or contract creation.' - console.log(mes) - this.isLoading = false - reject(mes) - } catch (error) { - console.log(error) + this.tx = tx + this.init() + if (!this.web3) throw new Error('web3 not loaded') + this.isLoading = true + try { + const result = await this.getTrace(tx.hash) + + if (result.structLogs.length > 0) { + this.trace = result.structLogs + + this.traceAnalyser.analyse(result.structLogs, tx) this.isLoading = false - reject(error) + return true } - }) + var mes = tx.hash + ' is not a contract invocation or contract creation.' + console.log(mes) + this.isLoading = false + throw new Error(mes) + } catch (error) { + console.log(error) + this.isLoading = false + throw new Error(error) + } } TraceManager.prototype.getTrace = function (txHash) {