From 0d78f1a1563e02cdda9e3b9567c4089f16769c58 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 22 Jul 2020 17:56:02 -0400 Subject: [PATCH] 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 ') }) })