aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-05-22 08:23:01 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-05-23 06:20:35 +0800
commite4fe4975041f6fd28b84085ce5083dd1c4cb3b22 (patch)
treed43119f687c1d34773b75ccd065b5c1e9b524e65
parent5c9bde203e0d9726f77f910adcf386c5758df072 (diff)
downloaddexon-sol-tools-e4fe4975041f6fd28b84085ce5083dd1c4cb3b22.tar
dexon-sol-tools-e4fe4975041f6fd28b84085ce5083dd1c4cb3b22.tar.gz
dexon-sol-tools-e4fe4975041f6fd28b84085ce5083dd1c4cb3b22.tar.bz2
dexon-sol-tools-e4fe4975041f6fd28b84085ce5083dd1c4cb3b22.tar.lz
dexon-sol-tools-e4fe4975041f6fd28b84085ce5083dd1c4cb3b22.tar.xz
dexon-sol-tools-e4fe4975041f6fd28b84085ce5083dd1c4cb3b22.tar.zst
dexon-sol-tools-e4fe4975041f6fd28b84085ce5083dd1c4cb3b22.zip
Refactor ContractData lookup
-rw-r--r--packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts4
-rw-r--r--packages/sol-cov/src/coverage_manager.ts63
2 files changed, 33 insertions, 34 deletions
diff --git a/packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts b/packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts
index 36a31383d..c7f21b6eb 100644
--- a/packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts
+++ b/packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts
@@ -7,8 +7,8 @@ import * as rimraf from 'rimraf';
import { ContractData } from '../types';
-import { SolCompilerArtifactAdapter } from './0x';
-import { AbstractArtifactAdapter } from './abstract';
+import { AbstractArtifactAdapter } from './abstract_artifact_adapter';
+import { SolCompilerArtifactAdapter } from './sol_compiler_artifact_adapter';
export class TruffleArtifactAdapter extends AbstractArtifactAdapter {
private _solcVersion: string;
diff --git a/packages/sol-cov/src/coverage_manager.ts b/packages/sol-cov/src/coverage_manager.ts
index 1adddbbd6..d7a00f86d 100644
--- a/packages/sol-cov/src/coverage_manager.ts
+++ b/packages/sol-cov/src/coverage_manager.ts
@@ -1,12 +1,12 @@
import { promisify } from '@0xproject/utils';
-import { addHexPrefix } from 'ethereumjs-util';
+import { addHexPrefix, stripHexPrefix } from 'ethereumjs-util';
import * as fs from 'fs';
import { Collector } from 'istanbul';
import * as _ from 'lodash';
import * as mkdirp from 'mkdirp';
import * as path from 'path';
-import { AbstractArtifactAdapter } from './artifact_adapters/abstract';
+import { AbstractArtifactAdapter } from './artifact_adapters/abstract_artifact_adapter';
import { collectCoverageEntries } from './collect_coverage_entries';
import { constants } from './constants';
import { parseSourceMap } from './source_maps';
@@ -112,6 +112,29 @@ export class CoverageManager {
};
return partialCoverage;
}
+ private static _bytecodeToBytecodeRegex(bytecode: string): string {
+ const bytecodeRegex = bytecode
+ // Library linking placeholder: __ConvertLib____________________________
+ .replace(/_.*_/, '.*')
+ // Last 86 characters is solidity compiler metadata that's different between compilations
+ .replace(/.{86}$/, '')
+ // Libraries contain their own address at the beginning of the code and it's impossible to know it in advance
+ .replace(/^0x730000000000000000000000000000000000000000/, '0x73........................................');
+ return bytecodeRegex;
+ }
+ private static _getContractDataIfExists(contractsData: ContractData[], bytecode: string): ContractData | undefined {
+ if (!bytecode.startsWith('0x')) {
+ throw new Error(')x missing');
+ }
+ const contractData = _.find(contractsData, contractDataCandidate => {
+ const bytecodeRegex = CoverageManager._bytecodeToBytecodeRegex(contractDataCandidate.bytecode);
+ const runtimeBytecodeRegex = CoverageManager._bytecodeToBytecodeRegex(
+ contractDataCandidate.runtimeBytecode,
+ );
+ return !_.isNull(bytecode.match(bytecodeRegex)) || !_.isNull(bytecode.match(runtimeBytecodeRegex));
+ });
+ return contractData;
+ }
constructor(
artifactAdapter: AbstractArtifactAdapter,
getContractCodeAsync: (address: string) => Promise<string>,
@@ -136,20 +159,8 @@ export class CoverageManager {
for (const traceInfo of this._traceInfos) {
if (traceInfo.address !== constants.NEW_CONTRACT) {
// Runtime transaction
- let runtimeBytecode = (traceInfo as TraceInfoExistingContract).runtimeBytecode;
- runtimeBytecode = addHexPrefix(runtimeBytecode);
- const contractData = _.find(contractsData, contractDataCandidate => {
- // Library linking placeholder: __ConvertLib____________________________
- let runtimeBytecodeRegex = contractDataCandidate.runtimeBytecode.replace(/_.*_/, '.*');
- // Last 86 characters is solidity compiler metadata that's different between compilations
- runtimeBytecodeRegex = runtimeBytecodeRegex.replace(/.{86}$/, '');
- // Libraries contain their own address at the beginning of the code and it's impossible to know it in advance
- runtimeBytecodeRegex = runtimeBytecodeRegex.replace(
- /^0x730000000000000000000000000000000000000000/,
- '0x73........................................',
- );
- return !_.isNull(runtimeBytecode.match(runtimeBytecodeRegex));
- }) as ContractData;
+ const runtimeBytecode = (traceInfo as TraceInfoExistingContract).runtimeBytecode;
+ const contractData = CoverageManager._getContractDataIfExists(contractsData, runtimeBytecode);
if (_.isUndefined(contractData)) {
if (this._verbose) {
// tslint:disable-next-line:no-console
@@ -157,7 +168,7 @@ export class CoverageManager {
}
continue;
}
- const bytecodeHex = runtimeBytecode.slice(2);
+ const bytecodeHex = stripHexPrefix(runtimeBytecode);
const sourceMap = contractData.sourceMapRuntime;
const pcToSourceRange = parseSourceMap(
contractData.sourceCodes,
@@ -176,20 +187,8 @@ export class CoverageManager {
}
} else {
// Contract creation transaction
- let bytecode = (traceInfo as TraceInfoNewContract).bytecode;
- bytecode = addHexPrefix(bytecode);
- const contractData = _.find(contractsData, contractDataCandidate => {
- // Library linking placeholder: __ConvertLib____________________________
- let bytecodeRegex = contractDataCandidate.bytecode.replace(/_.*_/, '.*');
- // Last 86 characters is solidity compiler metadata that's different between compilations
- bytecodeRegex = bytecodeRegex.replace(/.{86}$/, '');
- // Libraries contain their own address at the beginning of the code and it's impossible to know it in advance
- bytecodeRegex = bytecodeRegex.replace(
- /^0x730000000000000000000000000000000000000000/,
- '0x73........................................',
- );
- return !_.isNull(bytecode.match(bytecodeRegex));
- }) as ContractData;
+ const bytecode = (traceInfo as TraceInfoNewContract).bytecode;
+ const contractData = CoverageManager._getContractDataIfExists(contractsData, bytecode);
if (_.isUndefined(contractData)) {
if (this._verbose) {
// tslint:disable-next-line:no-console
@@ -197,7 +196,7 @@ export class CoverageManager {
}
continue;
}
- const bytecodeHex = bytecode.slice(2);
+ const bytecodeHex = stripHexPrefix(bytecode);
const sourceMap = contractData.sourceMap;
const pcToSourceRange = parseSourceMap(
contractData.sourceCodes,