diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2019-01-14 19:02:32 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2019-01-14 19:04:08 +0800 |
commit | 2581bc93e5893d43642b240d290c141f0d9419bf (patch) | |
tree | 2fa713969c802d0c8c7f6290a789267ca7404d10 | |
parent | 8b62783f4876594ad350175b12a85321e9d1a3fc (diff) | |
download | dexon-sol-tools-2581bc93e5893d43642b240d290c141f0d9419bf.tar dexon-sol-tools-2581bc93e5893d43642b240d290c141f0d9419bf.tar.gz dexon-sol-tools-2581bc93e5893d43642b240d290c141f0d9419bf.tar.bz2 dexon-sol-tools-2581bc93e5893d43642b240d290c141f0d9419bf.tar.lz dexon-sol-tools-2581bc93e5893d43642b240d290c141f0d9419bf.tar.xz dexon-sol-tools-2581bc93e5893d43642b240d290c141f0d9419bf.tar.zst dexon-sol-tools-2581bc93e5893d43642b240d290c141f0d9419bf.zip |
Fix the bug with incorrect source maps parsing by changing contract data from an array to a mapping
5 files changed, 24 insertions, 15 deletions
diff --git a/packages/sol-trace/src/revert_trace_subprovider.ts b/packages/sol-trace/src/revert_trace_subprovider.ts index 31067a402..d36cba08b 100644 --- a/packages/sol-trace/src/revert_trace_subprovider.ts +++ b/packages/sol-trace/src/revert_trace_subprovider.ts @@ -106,7 +106,8 @@ export class RevertTraceSubprovider extends TraceCollectionSubprovider { continue; } - const fileIndex = contractData.sources.indexOf(sourceRange.fileName); + const fileIndexByFileName = _.invert(contractData.sources); + const fileIndex = _.parseInt(fileIndexByFileName[sourceRange.fileName]); const sourceSnippet = getSourceRangeSnippet(sourceRange, contractData.sourceCodes[fileIndex]); if (sourceSnippet !== null) { sourceSnippets.push(sourceSnippet); diff --git a/packages/sol-tracing-utils/src/artifact_adapters/sol_compiler_artifact_adapter.ts b/packages/sol-tracing-utils/src/artifact_adapters/sol_compiler_artifact_adapter.ts index 57391abbe..7d85f6c68 100644 --- a/packages/sol-tracing-utils/src/artifact_adapters/sol_compiler_artifact_adapter.ts +++ b/packages/sol-tracing-utils/src/artifact_adapters/sol_compiler_artifact_adapter.ts @@ -43,9 +43,14 @@ export class SolCompilerArtifactAdapter extends AbstractArtifactAdapter { logUtils.warn(`${artifactFileName} doesn't contain bytecode. Skipping...`); continue; } - let sources = _.keys(artifact.sources); - sources = _.map(sources, relativeFilePath => path.resolve(this._sourcesPath, relativeFilePath)); - const sourceCodes = _.map(sources, (source: string) => fs.readFileSync(source).toString()); + const sources: { [sourceId: number]: string } = {}; + const sourceCodes: { [sourceId: number]: string } = {}; + _.map(artifact.sources, (value: { id: number }, relativeFilePath: string) => { + const filePath = path.resolve(this._sourcesPath, relativeFilePath); + const fileContent = fs.readFileSync(filePath).toString(); + sources[value.id] = filePath; + sourceCodes[value.id] = fileContent; + }); const contractData = { sourceCodes, sources, diff --git a/packages/sol-tracing-utils/src/source_maps.ts b/packages/sol-tracing-utils/src/source_maps.ts index ceb20d843..c674d32a3 100644 --- a/packages/sol-tracing-utils/src/source_maps.ts +++ b/packages/sol-tracing-utils/src/source_maps.ts @@ -33,20 +33,23 @@ export function getLocationByOffset(str: string): LocationByOffset { /** * Parses a sourcemap string. * The solidity sourcemap format is documented here: https://github.com/ethereum/solidity/blob/develop/docs/miscellaneous.rst#source-mappings - * @param sourceCodes sources contents + * @param sourceCodes sources contents by index * @param srcMap source map string * @param bytecodeHex contract bytecode - * @param sources sources file names + * @param sources sources file names by index */ export function parseSourceMap( - sourceCodes: string[], + sourceCodes: { [fileIndex: number]: string }, srcMap: string, bytecodeHex: string, - sources: string[], + sources: { [fileIndex: number]: string }, ): { [programCounter: number]: SourceRange } { const bytecode = Uint8Array.from(Buffer.from(bytecodeHex, 'hex')); const pcToInstructionIndex: { [programCounter: number]: number } = getPcToInstructionIndexMapping(bytecode); - const locationByOffsetByFileIndex = _.map(sourceCodes, s => (_.isUndefined(s) ? {} : getLocationByOffset(s))); + const locationByOffsetByFileIndex: { [fileIndex: number]: LocationByOffset } = {}; + _.map(sourceCodes, (sourceCode: string, fileIndex: number) => { + locationByOffsetByFileIndex[fileIndex] = _.isUndefined(sourceCode) ? {} : getLocationByOffset(sourceCode); + }); const entries = srcMap.split(';'); let lastParsedEntry: SourceLocation = {} as any; const instructionIndexToSourceRange: { [instructionIndex: number]: SourceRange } = {}; diff --git a/packages/sol-tracing-utils/src/trace_collector.ts b/packages/sol-tracing-utils/src/trace_collector.ts index 943e208cf..f5dde8762 100644 --- a/packages/sol-tracing-utils/src/trace_collector.ts +++ b/packages/sol-tracing-utils/src/trace_collector.ts @@ -56,7 +56,7 @@ export class TraceCollector { this._singleFileSubtraceHandler = singleFileSubtraceHandler; } public async writeOutputAsync(): Promise<void> { - const finalCoverage = this._collector.getFinalCoverage(); + const finalCoverage: Coverage = this._collector.getFinalCoverage(); const stringifiedCoverage = JSON.stringify(finalCoverage, null, '\t'); await mkdirpAsync('coverage'); fs.writeFileSync('coverage/coverage.json', stringifiedCoverage); @@ -80,14 +80,14 @@ export class TraceCollector { const bytecodeHex = stripHexPrefix(bytecode); const sourceMap = isContractCreation ? contractData.sourceMap : contractData.sourceMapRuntime; const pcToSourceRange = parseSourceMap(contractData.sourceCodes, sourceMap, bytecodeHex, contractData.sources); - for (let fileIndex = 0; fileIndex < contractData.sources.length; fileIndex++) { + _.map(contractData.sources, (_sourcePath: string, fileIndex: string) => { const singleFileCoverageForTrace = this._singleFileSubtraceHandler( contractData, traceInfo.subtrace, pcToSourceRange, - fileIndex, + _.parseInt(fileIndex), ); this._collector.add(singleFileCoverageForTrace); - } + }); } } diff --git a/packages/sol-tracing-utils/src/types.ts b/packages/sol-tracing-utils/src/types.ts index 54ade0400..fa10a93d6 100644 --- a/packages/sol-tracing-utils/src/types.ts +++ b/packages/sol-tracing-utils/src/types.ts @@ -81,8 +81,8 @@ export interface ContractData { sourceMap: string; runtimeBytecode: string; sourceMapRuntime: string; - sourceCodes: string[]; - sources: string[]; + sourceCodes: { [sourceId: number]: string }; + sources: { [sourceId: number]: string }; } // Part of the trace executed within the same context |