Address feedback

This commit is contained in:
Leonid Logvinov
2018-03-09 16:44:44 +01:00
parent 2298a34c37
commit 3b158cb726
6 changed files with 25 additions and 14 deletions

View File

@@ -13,8 +13,10 @@ import { TraceInfoExistingContract, TraceInfoNewContract } from './types';
*/
export class CoverageSubprovider extends Subprovider {
private _coverageManager: CoverageManager;
constructor(artifactsPath: string, sourcesPath: string, networkId: number) {
private _defaultFromAddress: string;
constructor(artifactsPath: string, sourcesPath: string, networkId: number, defaultFromAddress: string) {
super();
this._defaultFromAddress = defaultFromAddress;
this._coverageManager = new CoverageManager(
artifactsPath,
sourcesPath,
@@ -96,7 +98,7 @@ export class CoverageSubprovider extends Subprovider {
const traceInfo: TraceInfoNewContract = {
coveredPcs,
txHash,
address,
address: address as 'NEW_CONTRACT',
bytecode: data as string,
};
this._coverageManager.appendTraceInfo(traceInfo);
@@ -116,9 +118,9 @@ export class CoverageSubprovider extends Subprovider {
const snapshotId = Number((await this.emitPayloadAsync({ method: 'evm_snapshot' })).result);
const txData = callData;
if (_.isUndefined(txData.from)) {
txData.from = '0x5409ed021d9299bf6814279a6a1411a7e866a631'; // TODO
txData.from = this._defaultFromAddress;
}
const txDataWithFromAddress = txData as Web3.TxData & { from: string };
const txDataWithFromAddress = txData as Web3.TxData;
try {
const txHash = (await this.emitPayloadAsync({
method: 'eth_sendTransaction',
@@ -128,7 +130,11 @@ export class CoverageSubprovider extends Subprovider {
} catch (err) {
await this._onTransactionSentAsync(txDataWithFromAddress, err, undefined);
}
const didRevert = (await this.emitPayloadAsync({ method: 'evm_revert', params: [snapshotId] })).result;
const jsonRPCResponse = await this.emitPayloadAsync({ method: 'evm_revert', params: [snapshotId] });
const didRevert = jsonRPCResponse.result;
if (!didRevert) {
throw new Error('Failed to revert the snapshot');
}
}
private async _getContractCodeAsync(address: string): Promise<string> {
const payload = {

View File

@@ -11,7 +11,7 @@ export interface SourceLocation {
fileIndex: number;
}
export const getLocationByOffset = (str: string) => {
export function getLocationByOffset(str: string): LocationByOffset {
const locationByOffset: LocationByOffset = {};
let currentOffset = 0;
for (const char of str.split('')) {
@@ -24,11 +24,16 @@ export const getLocationByOffset = (str: string) => {
currentOffset++;
}
return locationByOffset;
};
}
// Parses a sourcemap string
// The solidity sourcemap format is documented here: https://github.com/ethereum/solidity/blob/develop/docs/miscellaneous.rst#source-mappings
export const parseSourceMap = (sourceCodes: string[], srcMap: string, bytecodeHex: string, sources: string[]) => {
export function parseSourceMap(
sourceCodes: string[],
srcMap: string,
bytecodeHex: string,
sources: string[],
): { [programCounter: number]: SourceRange } {
const bytecode = Uint8Array.from(Buffer.from(bytecodeHex, 'hex'));
const pcToInstructionIndex: { [programCounter: number]: number } = getPcToInstructionIndexMapping(bytecode);
const locationByOffsetByFileIndex = _.map(sourceCodes, getLocationByOffset);
@@ -74,4 +79,4 @@ export const parseSourceMap = (sourceCodes: string[], srcMap: string, bytecodeHe
pcsToSourceRange[pc] = instructionIndexToSourceRange[instructionIndex];
}
return pcsToSourceRange;
};
}