aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-cov/src/profiler_subprovider.ts
blob: 62ed1b47207c172005003aca49e7882e4885d99b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import * as _ from 'lodash';

import { AbstractArtifactAdapter } from './artifact_adapters/abstract_artifact_adapter';
import { collectCoverageEntries } from './collect_coverage_entries';
import { TraceCollectionSubprovider } from './trace_collection_subprovider';
import { SingleFileSubtraceHandler, TraceCollector } from './trace_collector';
import { ContractData, Coverage, SourceRange, Subtrace, TraceInfo } from './types';
import { utils } from './utils';

/**
 * This class implements the [web3-provider-engine](https://github.com/MetaMask/provider-engine) subprovider interface.
 * ProfilerSubprovider is used to profile Solidity code while running tests.
 */
export class ProfilerSubprovider extends TraceCollectionSubprovider {
    private _profilerCollector: TraceCollector;
    /**
     * Instantiates a ProfilerSubprovider instance
     * @param artifactAdapter Adapter for used artifacts format (0x, truffle, giveth, etc.)
     * @param defaultFromAddress default from address to use when sending transactions
     * @param isVerbose If true, we will log any unknown transactions. Otherwise we will ignore them
     */
    constructor(artifactAdapter: AbstractArtifactAdapter, defaultFromAddress: string, isVerbose: boolean = true) {
        const traceCollectionSubproviderConfig = {
            shouldCollectTransactionTraces: true,
            shouldCollectGasEstimateTraces: false,
            shouldCollectCallTraces: false,
        };
        super(defaultFromAddress, traceCollectionSubproviderConfig);
        this._profilerCollector = new TraceCollector(artifactAdapter, isVerbose, profilerHandler);
    }
    public async handleTraceInfoAsync(traceInfo: TraceInfo): Promise<void> {
        await this._profilerCollector.computeSingleTraceCoverageAsync(traceInfo);
    }
    /**
     * Write the test profiler results to a file in Istanbul format.
     */
    public async writeProfilerOutputAsync(): Promise<void> {
        await this._profilerCollector.writeOutputAsync();
    }
}

/**
 * Computed partial coverage for a single file & subtrace for the purposes of
 * gas profiling.
 * @param contractData      Contract metadata (source, srcMap, bytecode)
 * @param subtrace          A subset of a transcation/call trace that was executed within that contract
 * @param pcToSourceRange   A mapping from program counters to source ranges
 * @param fileIndex         Index of a file to compute coverage for
 * @return Partial istanbul coverage for that file & subtrace
 */
export const profilerHandler: SingleFileSubtraceHandler = (
    contractData: ContractData,
    subtrace: Subtrace,
    pcToSourceRange: { [programCounter: number]: SourceRange },
    fileIndex: number,
): Coverage => {
    const absoluteFileName = contractData.sources[fileIndex];
    const profilerEntriesDescription = collectCoverageEntries(contractData.sourceCodes[fileIndex]);
    const gasConsumedByStatement: { [statementId: string]: number } = {};
    const statementIds = _.keys(profilerEntriesDescription.statementMap);
    for (const statementId of statementIds) {
        const statementDescription = profilerEntriesDescription.statementMap[statementId];
        const totalGasCost = _.sum(
            _.map(subtrace, structLog => {
                const sourceRange = pcToSourceRange[structLog.pc];
                if (_.isUndefined(sourceRange)) {
                    return 0;
                }
                if (sourceRange.fileName !== absoluteFileName) {
                    return 0;
                }
                if (utils.isRangeInside(sourceRange.location, statementDescription)) {
                    return structLog.gasCost;
                } else {
                    return 0;
                }
            }),
        );
        gasConsumedByStatement[statementId] = totalGasCost;
    }
    const partialProfilerOutput = {
        [absoluteFileName]: {
            ...profilerEntriesDescription,
            path: absoluteFileName,
            f: {}, // I's meaningless in profiling context
            s: gasConsumedByStatement,
            b: {}, // I's meaningless in profiling context
        },
    };
    return partialProfilerOutput;
};