From b39f707fe1a84cd2a1b939f92d31701aea106366 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 1 Feb 2021 17:22:04 +0100 Subject: [PATCH] case where generated sources are not to be considered --- .../src/source/sourceLocationTracker.ts | 21 ++++++++++++- .../remix-debug/test/sourceLocationTracker.ts | 30 ++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/libs/remix-debug/src/source/sourceLocationTracker.ts b/libs/remix-debug/src/source/sourceLocationTracker.ts index bc6efd1bf8..f9967b5e98 100644 --- a/libs/remix-debug/src/source/sourceLocationTracker.ts +++ b/libs/remix-debug/src/source/sourceLocationTracker.ts @@ -59,6 +59,19 @@ export class SourceLocationTracker { return null } + /** + * Returns the total amount of sources from a specific @arg address and @arg contracts + * + * @param {String} address - contract address from which has generated sources + * @param {Object} contracts - AST of compiled contracts + */ + getTotalAmountOfSources (address, contracts) { + let sourcesLength = Object.keys(contracts).length + const generatedSources = this.getGeneratedSourcesFromAddress(address) + if (generatedSources) sourcesLength = sourcesLength + Object.keys(generatedSources).length + return sourcesLength + } + /** * Return a valid source location associated with the given @arg vmTraceIndex * @@ -67,8 +80,14 @@ export class SourceLocationTracker { * @param {Object} contractDetails - AST of compiled contracts */ async getValidSourceLocationFromVMTraceIndex (address, vmtraceStepIndex, contracts) { + const amountOfSources = this.getTotalAmountOfSources(address, contracts) let map: Record = { file: -1 } - while (vmtraceStepIndex >= 0 && map.file === -1) { + /* + (map.file === -1) this indicates that it isn't associated with a known source code + (map.file > amountOfSources - 1) this indicates the current file index exceed the total number of files. + this happens when generated sources should not be considered. + */ + while (vmtraceStepIndex >= 0 && (map.file === -1 || map.file > amountOfSources - 1)) { map = await this.getSourceLocationFromVMTraceIndex(address, vmtraceStepIndex, contracts) vmtraceStepIndex = vmtraceStepIndex - 1 } diff --git a/libs/remix-debug/test/sourceLocationTracker.ts b/libs/remix-debug/test/sourceLocationTracker.ts index 989f6ae336..8fb289824a 100644 --- a/libs/remix-debug/test/sourceLocationTracker.ts +++ b/libs/remix-debug/test/sourceLocationTracker.ts @@ -54,9 +54,37 @@ tape('SourceLocationTracker', function (t) { traceManager.resolveTrace(tx).then(async () => { - const sourceLocationTracker = new SourceLocationTracker(codeManager, { debugWithGeneratedSources: false }) + + try { + // with debugWithGeneratedSources: false + const sourceLocationTracker = new SourceLocationTracker(codeManager, { debugWithGeneratedSources: false }) + + let map = await sourceLocationTracker.getSourceLocationFromVMTraceIndex('0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5', 0, output.contracts) + console.log(map) + st.equal(map['file'], 0) + st.equal(map['start'], 35) + + map = await sourceLocationTracker.getSourceLocationFromVMTraceIndex('0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5', 45, output.contracts) + st.equal(map['file'], 1) // 1 refers to the generated source (pragma experimental ABIEncoderV2) + + map = await sourceLocationTracker.getValidSourceLocationFromVMTraceIndex('0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5', 45, output.contracts) + st.equal(map['file'], 0) // 1 refers to the generated source (pragma experimental ABIEncoderV2) + st.equal(map['start'], 303) + st.equal(map['length'], 448) + + map = await sourceLocationTracker.getValidSourceLocationFromVMTraceIndex('0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5', 36, output.contracts) + st.equal(map['file'], 0) // 0 refers to the initial solidity code. see source below (ABIEncoderV2) + st.equal(map['start'], 303) + st.equal(map['length'], 448) + } catch (e) { + console.log(e) + } + try { + // with debugWithGeneratedSources: true + const sourceLocationTracker = new SourceLocationTracker(codeManager, { debugWithGeneratedSources: true }) + let map = await sourceLocationTracker.getSourceLocationFromVMTraceIndex('0x0d3a18d64dfe4f927832ab58d6451cecc4e517c5', 0, output.contracts) console.log(map) st.equal(map['file'], 0)