diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-05-15 02:01:18 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-05-23 06:20:34 +0800 |
commit | 974575b695108dd70f4b165f6789f71c3647c2b1 (patch) | |
tree | 3d589c157c51c02066583a08bdec879aa7a18735 /packages/sol-cov/src/coverage_subprovider.ts | |
parent | 60b1fdd367101047d433d4b9c1c47925925296a2 (diff) | |
download | dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar.gz dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar.bz2 dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar.lz dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar.xz dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar.zst dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.zip |
Make sol-cov work with truffle and other artifact adapters
Diffstat (limited to 'packages/sol-cov/src/coverage_subprovider.ts')
-rw-r--r-- | packages/sol-cov/src/coverage_subprovider.ts | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/packages/sol-cov/src/coverage_subprovider.ts b/packages/sol-cov/src/coverage_subprovider.ts index 08efeaa24..f421a17fd 100644 --- a/packages/sol-cov/src/coverage_subprovider.ts +++ b/packages/sol-cov/src/coverage_subprovider.ts @@ -1,10 +1,13 @@ import { Callback, ErrorCallback, NextCallback, Subprovider } from '@0xproject/subproviders'; import { BlockParam, CallData, JSONRPCRequestPayload, TransactionTrace, TxData } from '@0xproject/types'; +import * as fs from 'fs'; import * as _ from 'lodash'; import { Lock } from 'semaphore-async-await'; +import { AbstractArtifactAdapter } from './artifact_adapters/abstract'; import { constants } from './constants'; import { CoverageManager } from './coverage_manager'; +import { getTracesByContractAddress } from './trace'; import { TraceInfoExistingContract, TraceInfoNewContract } from './types'; interface MaybeFakeTxData extends TxData { @@ -26,15 +29,15 @@ export class CoverageSubprovider extends Subprovider { private _defaultFromAddress: string; /** * Instantiates a CoverageSubprovider instance - * @param artifactsPath Path to the smart contract artifacts - * @param sourcesPath Path to the smart contract source files + * @param artifactAdapter Adapter for used artifacts format (0x, truffle, giveth, etc.) * @param defaultFromAddress default from address to use when sending transactions + * @param verbose If true, we will log any unknown transactions. Otherwise we will ignore them */ - constructor(artifactsPath: string, sourcesPath: string, defaultFromAddress: string) { + constructor(artifactAdapter: AbstractArtifactAdapter, defaultFromAddress: string, verbose: boolean = true) { super(); this._lock = new Lock(); this._defaultFromAddress = defaultFromAddress; - this._coverageManager = new CoverageManager(artifactsPath, sourcesPath, this._getContractCodeAsync.bind(this)); + this._coverageManager = new CoverageManager(artifactAdapter, this._getContractCodeAsync.bind(this), verbose); } /** * Write the test coverage results to a file in Istanbul format. @@ -119,8 +122,10 @@ export class CoverageSubprovider extends Subprovider { }; const jsonRPCResponsePayload = await this.emitPayloadAsync(payload); const trace: TransactionTrace = jsonRPCResponsePayload.result; - const coveredPcs = _.map(trace.structLogs, log => log.pc); if (address === constants.NEW_CONTRACT) { + // TODO handle calls to external contracts and contract creations from within the constructor + const structLogsOnDepth0 = _.filter(trace.structLogs, { depth: 0 }); + const coveredPcs = _.map(structLogsOnDepth0, log => log.pc); const traceInfo: TraceInfoNewContract = { coveredPcs, txHash, @@ -129,15 +134,20 @@ export class CoverageSubprovider extends Subprovider { }; this._coverageManager.appendTraceInfo(traceInfo); } else { - payload = { method: 'eth_getCode', params: [address, 'latest'] }; - const runtimeBytecode = (await this.emitPayloadAsync(payload)).result; - const traceInfo: TraceInfoExistingContract = { - coveredPcs, - txHash, - address, - runtimeBytecode, - }; - this._coverageManager.appendTraceInfo(traceInfo); + const tracesByContractAddress = getTracesByContractAddress(trace.structLogs, address); + for (const subcallAddress of _.keys(tracesByContractAddress)) { + payload = { method: 'eth_getCode', params: [subcallAddress, 'latest'] }; + const runtimeBytecode = (await this.emitPayloadAsync(payload)).result; + const traceForThatSubcall = tracesByContractAddress[subcallAddress]; + const coveredPcs = _.map(traceForThatSubcall, log => log.pc); + const traceInfo: TraceInfoExistingContract = { + coveredPcs, + txHash, + address: subcallAddress, + runtimeBytecode, + }; + this._coverageManager.appendTraceInfo(traceInfo); + } } } private async _recordCallTraceAsync(callData: Partial<CallData>, blockNumber: BlockParam): Promise<void> { |