diff options
author | Fabio Berger <me@fabioberger.com> | 2018-08-15 05:21:47 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-08-15 05:21:47 +0800 |
commit | 2f2582a0da3095d61a99ef09744dc0995677558e (patch) | |
tree | 54989565919038c42d495dafb7426d4148605e84 /packages/sol-cov/src | |
parent | 8169155a6547fb0283cd0f5362aad3c0b173b00b (diff) | |
parent | fadd292ecf367e42154856509d0ea0c20b23f2f1 (diff) | |
download | dexon-0x-contracts-2f2582a0da3095d61a99ef09744dc0995677558e.tar.gz dexon-0x-contracts-2f2582a0da3095d61a99ef09744dc0995677558e.tar.zst dexon-0x-contracts-2f2582a0da3095d61a99ef09744dc0995677558e.zip |
Merge development
Diffstat (limited to 'packages/sol-cov/src')
-rw-r--r-- | packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts | 60 | ||||
-rw-r--r-- | packages/sol-cov/src/trace_collection_subprovider.ts | 16 |
2 files changed, 61 insertions, 15 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 dc6807cd6..b4d1aa7ba 100644 --- a/packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts +++ b/packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts @@ -1,30 +1,45 @@ import { Compiler, CompilerOptions } from '@0xproject/sol-compiler'; -import * as rimraf from 'rimraf'; +import * as fs from 'fs'; +import * as glob from 'glob'; +import * as path from 'path'; import { ContractData } from '../types'; import { AbstractArtifactAdapter } from './abstract_artifact_adapter'; import { SolCompilerArtifactAdapter } from './sol_compiler_artifact_adapter'; +const DEFAULT_TRUFFLE_ARTIFACTS_DIR = './build/contracts'; + +interface TruffleConfig { + solc?: any; + contracts_build_directory?: string; +} + export class TruffleArtifactAdapter extends AbstractArtifactAdapter { private readonly _solcVersion: string; - private readonly _sourcesPath: string; + private readonly _projectRoot: string; /** * Instantiates a TruffleArtifactAdapter - * @param artifactsPath Path to the truffle project's artifacts directory + * @param projectRoot Path to the truffle project's root directory * @param solcVersion Solidity version with which to compile all the contracts */ - constructor(sourcesPath: string, solcVersion: string) { + constructor(projectRoot: string, solcVersion: string) { super(); this._solcVersion = solcVersion; - this._sourcesPath = sourcesPath; + this._projectRoot = projectRoot; } public async collectContractsDataAsync(): Promise<ContractData[]> { const artifactsDir = '.0x-artifacts'; + const contractsDir = path.join(this._projectRoot, 'contracts'); + const truffleConfig = this._getTruffleConfig(); + const solcConfig = truffleConfig.solc || {}; + const truffleArtifactsDirectory = truffleConfig.contracts_build_directory || DEFAULT_TRUFFLE_ARTIFACTS_DIR; + this._assertSolidityVersionIsCorrect(truffleArtifactsDirectory); const compilerOptions: CompilerOptions = { - contractsDir: this._sourcesPath, + contractsDir, artifactsDir, compilerSettings: { + ...solcConfig, outputSelection: { ['*']: { ['*']: ['abi', 'evm.bytecode.object', 'evm.deployedBytecode.object'], @@ -36,9 +51,38 @@ export class TruffleArtifactAdapter extends AbstractArtifactAdapter { }; const compiler = new Compiler(compilerOptions); await compiler.compileAsync(); - const solCompilerArtifactAdapter = new SolCompilerArtifactAdapter(artifactsDir, this._sourcesPath); + const solCompilerArtifactAdapter = new SolCompilerArtifactAdapter(artifactsDir, contractsDir); const contractsDataFrom0xArtifacts = await solCompilerArtifactAdapter.collectContractsDataAsync(); - rimraf.sync(artifactsDir); return contractsDataFrom0xArtifacts; } + private _getTruffleConfig(): TruffleConfig { + const truffleConfigFileShort = path.resolve(path.join(this._projectRoot, 'truffle.js')); + const truffleConfigFileLong = path.resolve(path.join(this._projectRoot, 'truffle-config.js')); + if (fs.existsSync(truffleConfigFileShort)) { + const truffleConfig = require(truffleConfigFileShort); + return truffleConfig; + } else if (fs.existsSync(truffleConfigFileLong)) { + const truffleConfig = require(truffleConfigFileLong); + return truffleConfig; + } else { + throw new Error( + `Neither ${truffleConfigFileShort} nor ${truffleConfigFileLong} exists. Make sure the project root is correct`, + ); + } + } + private _assertSolidityVersionIsCorrect(truffleArtifactsDirectory: string): void { + const artifactsGlob = `${truffleArtifactsDirectory}/**/*.json`; + const artifactFileNames = glob.sync(artifactsGlob, { absolute: true }); + for (const artifactFileName of artifactFileNames) { + const artifact = JSON.parse(fs.readFileSync(artifactFileName).toString()); + const compilerVersion = artifact.compiler.version; + if (!compilerVersion.startsWith(this._solcVersion)) { + throw new Error( + `${artifact.contractName} was compiled with solidity ${compilerVersion} but specified version is ${ + this._solcVersion + } making it impossible for sol-cov to process traces`, + ); + } + } + } } diff --git a/packages/sol-cov/src/trace_collection_subprovider.ts b/packages/sol-cov/src/trace_collection_subprovider.ts index 91c21fe94..a69bccb07 100644 --- a/packages/sol-cov/src/trace_collection_subprovider.ts +++ b/packages/sol-cov/src/trace_collection_subprovider.ts @@ -1,7 +1,7 @@ import { BlockchainLifecycle } from '@0xproject/dev-utils'; import { Callback, ErrorCallback, NextCallback, Subprovider } from '@0xproject/subproviders'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; -import { CallData, JSONRPCRequestPayload, Provider, TxData } from 'ethereum-types'; +import { CallDataRPC, marshaller, Web3Wrapper } from '@0xproject/web3-wrapper'; +import { JSONRPCRequestPayload, Provider, TxData } from 'ethereum-types'; import * as _ from 'lodash'; import { Lock } from 'semaphore-async-await'; @@ -153,7 +153,7 @@ export abstract class TraceCollectionSubprovider extends Subprovider { cb(); } private async _onCallOrGasEstimateExecutedAsync( - callData: Partial<CallData>, + callData: Partial<CallDataRPC>, _err: Error | null, _callResult: string, cb: Callback, @@ -161,22 +161,24 @@ export abstract class TraceCollectionSubprovider extends Subprovider { await this._recordCallOrGasEstimateTraceAsync(callData); cb(); } - private async _recordCallOrGasEstimateTraceAsync(callData: Partial<CallData>): Promise<void> { + private async _recordCallOrGasEstimateTraceAsync(callData: Partial<CallDataRPC>): Promise<void> { // We don't want other transactions to be exeucted during snashotting period, that's why we lock the // transaction execution for all transactions except our fake ones. await this._lock.acquire(); const blockchainLifecycle = new BlockchainLifecycle(this._web3Wrapper); await blockchainLifecycle.startAsync(); - const fakeTxData: MaybeFakeTxData = { - gas: BLOCK_GAS_LIMIT, + const fakeTxData = { + gas: BLOCK_GAS_LIMIT.toString(16), // tslint:disable-line:custom-no-magic-numbers isFakeTransaction: true, // This transaction (and only it) is allowed to come through when the lock is locked ...callData, from: callData.from || this._defaultFromAddress, }; try { - const txHash = await this._web3Wrapper.sendTransactionAsync(fakeTxData); + const txData = marshaller.unmarshalTxData(fakeTxData); + const txHash = await this._web3Wrapper.sendTransactionAsync(txData); await this._web3Wrapper.awaitTransactionMinedAsync(txHash, 0); } catch (err) { + // TODO(logvinov) Check that transaction failed and not some other exception // Even if this transaction failed - we've already recorded it's trace. _.noop(); } |