From 13299158d1e22d1af1cd36434fc403a74743ecb1 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Sun, 4 Mar 2018 19:05:26 -0800 Subject: Add sol-cover implementation --- packages/dev-utils/CHANGELOG.md | 6 +++ packages/dev-utils/package.json | 1 + packages/dev-utils/src/blockchain_lifecycle.ts | 15 +++--- packages/dev-utils/src/constants.ts | 2 +- packages/dev-utils/src/coverage.ts | 14 +++++ packages/dev-utils/src/index.ts | 2 +- packages/dev-utils/src/rpc.ts | 62 ---------------------- packages/dev-utils/src/web3_factory.ts | 49 +++++++++++++---- .../dev-utils/test/blockchain_lifecycle_test.ts | 11 ++-- packages/dev-utils/test/rpc_test.ts | 19 ++++--- packages/dev-utils/tslint.json | 5 +- 11 files changed, 90 insertions(+), 96 deletions(-) create mode 100644 packages/dev-utils/src/coverage.ts delete mode 100644 packages/dev-utils/src/rpc.ts (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/CHANGELOG.md b/packages/dev-utils/CHANGELOG.md index ecc5546ae..3e5708402 100644 --- a/packages/dev-utils/CHANGELOG.md +++ b/packages/dev-utils/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## v0.3.0 - _TBD, 2018_ + + * Add coverage subprovider if enabled (#426) + * Refactor `BlockchainLifecycle` to work with in-process ganache (#426) + * Remove `RPC` class and move it's logic to `Web3Wrapper` (#426) + ## v0.2.0 - _February 16, 2018_ * Remove subproviders (#392) diff --git a/packages/dev-utils/package.json b/packages/dev-utils/package.json index fbbcb182f..dc594daaf 100644 --- a/packages/dev-utils/package.json +++ b/packages/dev-utils/package.json @@ -38,6 +38,7 @@ "typescript": "2.7.1" }, "dependencies": { + "@0xproject/sol-cov": "^0.0.1", "@0xproject/subproviders": "^0.7.0", "@0xproject/types": "^0.3.1", "@0xproject/utils": "^0.4.1", diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index c46902f76..48746f8c1 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -1,21 +1,24 @@ -import { RPC } from './rpc'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import * as Web3 from 'web3'; export class BlockchainLifecycle { - private _rpc: RPC; + private _web3Wrapper: Web3Wrapper; private _snapshotIdsStack: number[]; - constructor() { - this._rpc = new RPC(); + constructor(web3Orweb3Wrapper: Web3Wrapper | Web3) { + this._web3Wrapper = (web3Orweb3Wrapper as Web3Wrapper).isZeroExWeb3Wrapper + ? (web3Orweb3Wrapper as Web3Wrapper) + : new Web3Wrapper((web3Orweb3Wrapper as Web3).currentProvider); this._snapshotIdsStack = []; } // TODO: In order to run these tests on an actual node, we should check if we are running against // TestRPC, if so, use snapshots, otherwise re-deploy contracts before every test public async startAsync(): Promise { - const snapshotId = await this._rpc.takeSnapshotAsync(); + const snapshotId = await this._web3Wrapper.takeSnapshotAsync(); this._snapshotIdsStack.push(snapshotId); } public async revertAsync(): Promise { const snapshotId = this._snapshotIdsStack.pop() as number; - const didRevert = await this._rpc.revertSnapshotAsync(snapshotId); + const didRevert = await this._web3Wrapper.revertSnapshotAsync(snapshotId); if (!didRevert) { throw new Error(`Snapshot with id #${snapshotId} failed to revert`); } diff --git a/packages/dev-utils/src/constants.ts b/packages/dev-utils/src/constants.ts index 4f7b4202a..b018ba791 100644 --- a/packages/dev-utils/src/constants.ts +++ b/packages/dev-utils/src/constants.ts @@ -1,5 +1,5 @@ export const constants = { RPC_URL: 'http://localhost:8545', RPC_PORT: 8545, - GAS_ESTIMATE: 1000000, + GAS_ESTIMATE: 5000000, }; diff --git a/packages/dev-utils/src/coverage.ts b/packages/dev-utils/src/coverage.ts new file mode 100644 index 000000000..1750d19ce --- /dev/null +++ b/packages/dev-utils/src/coverage.ts @@ -0,0 +1,14 @@ +import { CoverageSubprovider } from '@0xproject/sol-cov'; +import * as _ from 'lodash'; + +let coverageSubprovider: CoverageSubprovider; + +export function getCoverageSubprovider(): CoverageSubprovider { + if (_.isUndefined(coverageSubprovider)) { + const artifactsPath = './src/artifacts'; + const contractsPath = './src/contracts'; + const networkId = 50; + coverageSubprovider = new CoverageSubprovider(artifactsPath, contractsPath, networkId); + } + return coverageSubprovider; +} diff --git a/packages/dev-utils/src/index.ts b/packages/dev-utils/src/index.ts index e899ac206..c8009158f 100644 --- a/packages/dev-utils/src/index.ts +++ b/packages/dev-utils/src/index.ts @@ -1,4 +1,4 @@ -export { RPC } from './rpc'; export { BlockchainLifecycle } from './blockchain_lifecycle'; export { web3Factory } from './web3_factory'; export { constants as devConstants } from './constants'; +export { getCoverageSubprovider } from './coverage'; diff --git a/packages/dev-utils/src/rpc.ts b/packages/dev-utils/src/rpc.ts deleted file mode 100644 index 47a359263..000000000 --- a/packages/dev-utils/src/rpc.ts +++ /dev/null @@ -1,62 +0,0 @@ -import * as ethUtil from 'ethereumjs-util'; -import * as request from 'request-promise-native'; - -import { constants } from './constants'; - -export class RPC { - private _url: string; - private _id: number; - constructor() { - this._url = constants.RPC_URL; - this._id = 0; - } - public async takeSnapshotAsync(): Promise { - const method = 'evm_snapshot'; - const params: any[] = []; - const payload = this._toPayload(method, params); - const snapshotIdHex = await this._sendAsync(payload); - const snapshotId = ethUtil.bufferToInt(ethUtil.toBuffer(snapshotIdHex)); - return snapshotId; - } - public async revertSnapshotAsync(snapshotId: number): Promise { - const method = 'evm_revert'; - const params = [snapshotId]; - const payload = this._toPayload(method, params); - const didRevert = await this._sendAsync(payload); - return didRevert; - } - public async increaseTimeAsync(time: number) { - const method = 'evm_increaseTime'; - const params = [time]; - const payload = this._toPayload(method, params); - return this._sendAsync(payload); - } - public async mineBlockAsync(): Promise { - const method = 'evm_mine'; - const params: any[] = []; - const payload = this._toPayload(method, params); - await this._sendAsync(payload); - } - private _toPayload(method: string, params: any[] = []): string { - const payload = JSON.stringify({ - id: this._id, - method, - params, - }); - this._id += 1; - return payload; - } - private async _sendAsync(payload: string): Promise { - const opts = { - method: 'POST', - uri: this._url, - body: payload, - headers: { - 'content-type': 'application/json', - }, - }; - const bodyString = await request(opts); - const body = JSON.parse(bodyString); - return body.result; - } -} diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts index b0e0e4d3f..74359d4f6 100644 --- a/packages/dev-utils/src/web3_factory.ts +++ b/packages/dev-utils/src/web3_factory.ts @@ -6,9 +6,13 @@ import ProviderEngine = require('web3-provider-engine'); import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); -import { EmptyWalletSubprovider, FakeGasEstimateSubprovider } from '@0xproject/subproviders'; +import { EmptyWalletSubprovider, FakeGasEstimateSubprovider, GanacheSubprovider } from '@0xproject/subproviders'; +import * as fs from 'fs'; +import * as _ from 'lodash'; +import * as process from 'process'; import { constants } from './constants'; +import { getCoverageSubprovider } from './coverage'; // HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang // because they are using the wrong XHR package. @@ -17,24 +21,51 @@ import { constants } from './constants'; // tslint:disable-next-line:ordered-imports import * as Web3 from 'web3'; +export interface Web3Config { + hasAddresses?: boolean; // default: true + useInProcessGanache?: boolean; // default: false +} + export const web3Factory = { - create(hasAddresses: boolean = true): Web3 { - const provider = this.getRpcProvider(hasAddresses); + create(config: Web3Config = {}): Web3 { + const provider = this.getRpcProvider(config); const web3 = new Web3(); web3.setProvider(provider); return web3; }, - getRpcProvider(hasAddresses: boolean = true): Web3.Provider { + getRpcProvider(config: Web3Config = {}): Web3.Provider { const provider = new ProviderEngine(); + if (process.env.COVERAGE) { + provider.addProvider(getCoverageSubprovider()); + } + const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses; if (!hasAddresses) { provider.addProvider(new EmptyWalletSubprovider()); } provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_ESTIMATE)); - provider.addProvider( - new RpcSubprovider({ - rpcUrl: constants.RPC_URL, - }), - ); + const logger = { + log: (arg: any) => { + fs.appendFileSync('ganache.log', `${arg}\n`); + }, + }; + const useInProcessGanache = config.useInProcessGanache; + if (useInProcessGanache) { + provider.addProvider( + new GanacheSubprovider({ + logger, + verbose: process.env.VERBOSE_GANACHE, + port: 8545, + networkId: 50, + mnemonic: 'concert load couple harbor equip island argue ramp clarify fence smart topic', + }), + ); + } else { + provider.addProvider( + new RpcSubprovider({ + rpcUrl: constants.RPC_URL, + }), + ); + } provider.start(); return provider; }, diff --git a/packages/dev-utils/test/blockchain_lifecycle_test.ts b/packages/dev-utils/test/blockchain_lifecycle_test.ts index 4fdc53c87..5ed67e012 100644 --- a/packages/dev-utils/test/blockchain_lifecycle_test.ts +++ b/packages/dev-utils/test/blockchain_lifecycle_test.ts @@ -3,20 +3,19 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; import 'mocha'; -import { BlockchainLifecycle, RPC, web3Factory } from '../src'; +import { BlockchainLifecycle, web3Factory } from '../src'; const expect = chai.expect; describe('BlockchainLifecycle tests', () => { - const web3 = web3Factory.create(); - const web3Wrapper = new Web3Wrapper(web3.currentProvider); - const rpc = new RPC(); - const blockchainLifecycle = new BlockchainLifecycle(); + const web3Provider = web3Factory.getRpcProvider(); + const web3Wrapper = new Web3Wrapper(web3Provider); + const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); describe('#startAsync/revertAsync', () => { it('reverts changes in between', async () => { const blockNumberBefore = await web3Wrapper.getBlockNumberAsync(); await blockchainLifecycle.startAsync(); - await rpc.mineBlockAsync(); + await web3Wrapper.mineBlockAsync(); const blockNumberAfter = await web3Wrapper.getBlockNumberAsync(); expect(blockNumberAfter).to.be.equal(blockNumberBefore + 1); await blockchainLifecycle.revertAsync(); diff --git a/packages/dev-utils/test/rpc_test.ts b/packages/dev-utils/test/rpc_test.ts index 2f0e5ff7f..1bdea3613 100644 --- a/packages/dev-utils/test/rpc_test.ts +++ b/packages/dev-utils/test/rpc_test.ts @@ -3,18 +3,17 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; import 'mocha'; -import { RPC, web3Factory } from '../src'; +import { web3Factory } from '../src'; const expect = chai.expect; describe('RPC tests', () => { - const web3 = web3Factory.create(); - const web3Wrapper = new Web3Wrapper(web3.currentProvider); - const rpc = new RPC(); + const web3Provider = web3Factory.getRpcProvider(); + const web3Wrapper = new Web3Wrapper(web3Provider); describe('#mineBlockAsync', () => { it('increases block number when called', async () => { const blockNumberBefore = await web3Wrapper.getBlockNumberAsync(); - await rpc.mineBlockAsync(); + await web3Wrapper.mineBlockAsync(); const blockNumberAfter = await web3Wrapper.getBlockNumberAsync(); expect(blockNumberAfter).to.be.equal(blockNumberBefore + 1); }); @@ -23,8 +22,8 @@ describe('RPC tests', () => { it('increases time when called', async () => { const TIME_DELTA = 1000; const blockTimestampBefore = await web3Wrapper.getBlockTimestampAsync(BlockParamLiteral.Latest); - await rpc.increaseTimeAsync(TIME_DELTA); - await rpc.mineBlockAsync(); + await web3Wrapper.increaseTimeAsync(TIME_DELTA); + await web3Wrapper.mineBlockAsync(); const blockTimestampAfter = await web3Wrapper.getBlockTimestampAsync(BlockParamLiteral.Latest); expect(blockTimestampAfter).to.be.at.least(blockTimestampBefore + TIME_DELTA); }); @@ -32,9 +31,9 @@ describe('RPC tests', () => { describe('#takeSnapshotAsync/revertSnapshotAsync', () => { it('reverts changes in between', async () => { const blockNumberBefore = await web3Wrapper.getBlockNumberAsync(); - const snapshotId = await rpc.takeSnapshotAsync(); - await rpc.mineBlockAsync(); - await rpc.revertSnapshotAsync(snapshotId); + const snapshotId = await web3Wrapper.takeSnapshotAsync(); + await web3Wrapper.mineBlockAsync(); + await web3Wrapper.revertSnapshotAsync(snapshotId); const blockNumberAfter = await web3Wrapper.getBlockNumberAsync(); expect(blockNumberAfter).to.be.equal(blockNumberBefore); }); diff --git a/packages/dev-utils/tslint.json b/packages/dev-utils/tslint.json index ffaefe83a..015dce851 100644 --- a/packages/dev-utils/tslint.json +++ b/packages/dev-utils/tslint.json @@ -1,3 +1,6 @@ { - "extends": ["@0xproject/tslint-config"] + "extends": ["@0xproject/tslint-config"], + "rules": { + "completed-docs": false + } } -- cgit v1.2.3 From 3b77e4ebf1d875ec1c1fdd91dec4e03e1ce5d391 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Mar 2018 15:17:00 +0100 Subject: Rename getCoverageSubprovider to getCoverageSubproviderSingleton --- packages/dev-utils/src/coverage.ts | 2 +- packages/dev-utils/src/index.ts | 2 +- packages/dev-utils/src/web3_factory.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/coverage.ts b/packages/dev-utils/src/coverage.ts index 1750d19ce..b39fa5de4 100644 --- a/packages/dev-utils/src/coverage.ts +++ b/packages/dev-utils/src/coverage.ts @@ -3,7 +3,7 @@ import * as _ from 'lodash'; let coverageSubprovider: CoverageSubprovider; -export function getCoverageSubprovider(): CoverageSubprovider { +export function getCoverageSubproviderSingleton(): CoverageSubprovider { if (_.isUndefined(coverageSubprovider)) { const artifactsPath = './src/artifacts'; const contractsPath = './src/contracts'; diff --git a/packages/dev-utils/src/index.ts b/packages/dev-utils/src/index.ts index c8009158f..61e9bcfa2 100644 --- a/packages/dev-utils/src/index.ts +++ b/packages/dev-utils/src/index.ts @@ -1,4 +1,4 @@ export { BlockchainLifecycle } from './blockchain_lifecycle'; export { web3Factory } from './web3_factory'; export { constants as devConstants } from './constants'; -export { getCoverageSubprovider } from './coverage'; +export { getCoverageSubproviderSingleton } from './coverage'; diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts index 74359d4f6..4077dc13b 100644 --- a/packages/dev-utils/src/web3_factory.ts +++ b/packages/dev-utils/src/web3_factory.ts @@ -12,7 +12,7 @@ import * as _ from 'lodash'; import * as process from 'process'; import { constants } from './constants'; -import { getCoverageSubprovider } from './coverage'; +import { getCoverageSubproviderSingleton } from './coverage'; // HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang // because they are using the wrong XHR package. @@ -36,7 +36,7 @@ export const web3Factory = { getRpcProvider(config: Web3Config = {}): Web3.Provider { const provider = new ProviderEngine(); if (process.env.COVERAGE) { - provider.addProvider(getCoverageSubprovider()); + provider.addProvider(getCoverageSubproviderSingleton()); } const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses; if (!hasAddresses) { -- cgit v1.2.3 From c5afca53a4e94b51956f10f564c2e9db00012c8f Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Mar 2018 15:39:50 +0100 Subject: Rename COVERAGE to SOLIDITY_COVERAGS --- packages/dev-utils/CHANGELOG.md | 2 +- packages/dev-utils/src/web3_factory.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/CHANGELOG.md b/packages/dev-utils/CHANGELOG.md index 3e5708402..8193b78ac 100644 --- a/packages/dev-utils/CHANGELOG.md +++ b/packages/dev-utils/CHANGELOG.md @@ -2,7 +2,7 @@ ## v0.3.0 - _TBD, 2018_ - * Add coverage subprovider if enabled (#426) + * Add coverage subprovider if SOLIDITY_COVERAGE env variable is true (#426) * Refactor `BlockchainLifecycle` to work with in-process ganache (#426) * Remove `RPC` class and move it's logic to `Web3Wrapper` (#426) diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts index 4077dc13b..200670c3b 100644 --- a/packages/dev-utils/src/web3_factory.ts +++ b/packages/dev-utils/src/web3_factory.ts @@ -35,7 +35,7 @@ export const web3Factory = { }, getRpcProvider(config: Web3Config = {}): Web3.Provider { const provider = new ProviderEngine(); - if (process.env.COVERAGE) { + if (process.env.SOLIDITY_COVERAGE) { provider.addProvider(getCoverageSubproviderSingleton()); } const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses; -- cgit v1.2.3 From a9479b3c015961e93f396fb1e23dc61e261c6aef Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Mar 2018 15:45:45 +0100 Subject: Make coverage a module instead of a function --- packages/dev-utils/src/coverage.ts | 17 +++++++++++------ packages/dev-utils/src/index.ts | 2 +- packages/dev-utils/src/web3_factory.ts | 4 ++-- 3 files changed, 14 insertions(+), 9 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/coverage.ts b/packages/dev-utils/src/coverage.ts index b39fa5de4..db87e2285 100644 --- a/packages/dev-utils/src/coverage.ts +++ b/packages/dev-utils/src/coverage.ts @@ -3,12 +3,17 @@ import * as _ from 'lodash'; let coverageSubprovider: CoverageSubprovider; -export function getCoverageSubproviderSingleton(): CoverageSubprovider { - if (_.isUndefined(coverageSubprovider)) { +export const coverage = { + getCoverageSubproviderSingleton(): CoverageSubprovider { + if (_.isUndefined(coverageSubprovider)) { + coverageSubprovider = coverage._getCoverageSubprovider(); + } + return coverageSubprovider; + }, + _getCoverageSubprovider(): CoverageSubprovider { const artifactsPath = './src/artifacts'; const contractsPath = './src/contracts'; const networkId = 50; - coverageSubprovider = new CoverageSubprovider(artifactsPath, contractsPath, networkId); - } - return coverageSubprovider; -} + return new CoverageSubprovider(artifactsPath, contractsPath, networkId); + }, +}; diff --git a/packages/dev-utils/src/index.ts b/packages/dev-utils/src/index.ts index 61e9bcfa2..72d538e5d 100644 --- a/packages/dev-utils/src/index.ts +++ b/packages/dev-utils/src/index.ts @@ -1,4 +1,4 @@ export { BlockchainLifecycle } from './blockchain_lifecycle'; export { web3Factory } from './web3_factory'; export { constants as devConstants } from './constants'; -export { getCoverageSubproviderSingleton } from './coverage'; +export { coverage } from './coverage'; diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts index 200670c3b..74deec7b1 100644 --- a/packages/dev-utils/src/web3_factory.ts +++ b/packages/dev-utils/src/web3_factory.ts @@ -12,7 +12,7 @@ import * as _ from 'lodash'; import * as process from 'process'; import { constants } from './constants'; -import { getCoverageSubproviderSingleton } from './coverage'; +import { coverage } from './coverage'; // HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang // because they are using the wrong XHR package. @@ -36,7 +36,7 @@ export const web3Factory = { getRpcProvider(config: Web3Config = {}): Web3.Provider { const provider = new ProviderEngine(); if (process.env.SOLIDITY_COVERAGE) { - provider.addProvider(getCoverageSubproviderSingleton()); + provider.addProvider(coverage.getCoverageSubproviderSingleton()); } const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses; if (!hasAddresses) { -- cgit v1.2.3 From 6c87ebac012953f214501e57f29318c03c174b3d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Mar 2018 15:46:34 +0100 Subject: Rename useInProcessGanache to shouldUseInProcessGanache --- packages/dev-utils/src/web3_factory.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts index 74deec7b1..285c253bc 100644 --- a/packages/dev-utils/src/web3_factory.ts +++ b/packages/dev-utils/src/web3_factory.ts @@ -23,7 +23,7 @@ import * as Web3 from 'web3'; export interface Web3Config { hasAddresses?: boolean; // default: true - useInProcessGanache?: boolean; // default: false + shouldUseInProcessGanache?: boolean; // default: false } export const web3Factory = { @@ -48,8 +48,8 @@ export const web3Factory = { fs.appendFileSync('ganache.log', `${arg}\n`); }, }; - const useInProcessGanache = config.useInProcessGanache; - if (useInProcessGanache) { + const shouldUseInProcessGanache = config.shouldUseInProcessGanache; + if (shouldUseInProcessGanache) { provider.addProvider( new GanacheSubprovider({ logger, -- cgit v1.2.3 From 981752059cfddc0aac798850e30eaad5094adceb Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Mar 2018 15:48:12 +0100 Subject: Add double negation before using shouldUseInProcessGanache --- packages/dev-utils/src/web3_factory.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts index 285c253bc..1bbdad81e 100644 --- a/packages/dev-utils/src/web3_factory.ts +++ b/packages/dev-utils/src/web3_factory.ts @@ -48,7 +48,7 @@ export const web3Factory = { fs.appendFileSync('ganache.log', `${arg}\n`); }, }; - const shouldUseInProcessGanache = config.shouldUseInProcessGanache; + const shouldUseInProcessGanache = !!config.shouldUseInProcessGanache; if (shouldUseInProcessGanache) { provider.addProvider( new GanacheSubprovider({ -- cgit v1.2.3 From f02d3f689d34aaa9d5df7fb4df4651909bf6265c Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Mar 2018 15:52:14 +0100 Subject: Document env variables --- packages/dev-utils/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/README.md b/packages/dev-utils/README.md index f7e30df1e..1d2c49884 100644 --- a/packages/dev-utils/README.md +++ b/packages/dev-utils/README.md @@ -2,6 +2,15 @@ Dev utils to be shared across 0x projects and packages +## Configuration + +Some env variables might be set to change the behaviour of created web3 providers/instances. + +``` +VERBOSE_GANACHE: boolean. Enables verbose Ganache logging. Every request/response payload. Slightly slower, but usefull for testing. +SOLIDITY_COVERAGE: boolean. If set - adds coverage subprovider which intercepts all calls/transactions and can be later used to compute code coverage. +``` + ## Install ```bash -- cgit v1.2.3 From 3b158cb72649c19a6b569821d23672ea7454b45b Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Mar 2018 16:44:44 +0100 Subject: Address feedback --- packages/dev-utils/src/coverage.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/coverage.ts b/packages/dev-utils/src/coverage.ts index db87e2285..a29acf5da 100644 --- a/packages/dev-utils/src/coverage.ts +++ b/packages/dev-utils/src/coverage.ts @@ -14,6 +14,7 @@ export const coverage = { const artifactsPath = './src/artifacts'; const contractsPath = './src/contracts'; const networkId = 50; - return new CoverageSubprovider(artifactsPath, contractsPath, networkId); + const defaultFromAddress = '0x5409ed021d9299bf6814279a6a1411a7e866a631'; + return new CoverageSubprovider(artifactsPath, contractsPath, networkId, defaultFromAddress); }, }; -- cgit v1.2.3 From e2b2bf1e0ddad1b665c68f76b8088b517ed6c537 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 12 Mar 2018 03:21:19 +0100 Subject: Fix a typo --- packages/dev-utils/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/README.md b/packages/dev-utils/README.md index 1d2c49884..59db0abbe 100644 --- a/packages/dev-utils/README.md +++ b/packages/dev-utils/README.md @@ -7,7 +7,7 @@ Dev utils to be shared across 0x projects and packages Some env variables might be set to change the behaviour of created web3 providers/instances. ``` -VERBOSE_GANACHE: boolean. Enables verbose Ganache logging. Every request/response payload. Slightly slower, but usefull for testing. +VERBOSE_GANACHE: boolean. Enables verbose Ganache logging. Every request/response payload. Slightly slower, but useful for testing. SOLIDITY_COVERAGE: boolean. If set - adds coverage subprovider which intercepts all calls/transactions and can be later used to compute code coverage. ``` -- cgit v1.2.3 From d6c2e47bbd099f380c8fd4790d6d7be17d3532e5 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 12 Mar 2018 03:33:24 +0100 Subject: Make env variables parsing more strict and add docs --- packages/dev-utils/README.md | 2 ++ packages/dev-utils/src/web3_factory.ts | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/README.md b/packages/dev-utils/README.md index 59db0abbe..0c4175e35 100644 --- a/packages/dev-utils/README.md +++ b/packages/dev-utils/README.md @@ -11,6 +11,8 @@ VERBOSE_GANACHE: boolean. Enables verbose Ganache logging. Every request/respons SOLIDITY_COVERAGE: boolean. If set - adds coverage subprovider which intercepts all calls/transactions and can be later used to compute code coverage. ``` +Boolean env variables should be either `true` or `false`. Defaults to `false` if not set. + ## Install ```bash diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts index 1bbdad81e..3e4b39686 100644 --- a/packages/dev-utils/src/web3_factory.ts +++ b/packages/dev-utils/src/web3_factory.ts @@ -13,6 +13,7 @@ import * as process from 'process'; import { constants } from './constants'; import { coverage } from './coverage'; +import { env } from './env'; // HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang // because they are using the wrong XHR package. @@ -26,6 +27,11 @@ export interface Web3Config { shouldUseInProcessGanache?: boolean; // default: false } +enum EnvVars { + SolidityCoverage = 'SOLIDITY_COVERAGE', + VerboseGanache = 'VERBOSE_GANACHE', +} + export const web3Factory = { create(config: Web3Config = {}): Web3 { const provider = this.getRpcProvider(config); @@ -35,7 +41,8 @@ export const web3Factory = { }, getRpcProvider(config: Web3Config = {}): Web3.Provider { const provider = new ProviderEngine(); - if (process.env.SOLIDITY_COVERAGE) { + const isCoverageEnabled = env.parseBoolean(EnvVars.SolidityCoverage); + if (isCoverageEnabled) { provider.addProvider(coverage.getCoverageSubproviderSingleton()); } const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses; @@ -53,7 +60,7 @@ export const web3Factory = { provider.addProvider( new GanacheSubprovider({ logger, - verbose: process.env.VERBOSE_GANACHE, + verbose: env.parseBoolean(EnvVars.SolidityCoverage), port: 8545, networkId: 50, mnemonic: 'concert load couple harbor equip island argue ramp clarify fence smart topic', -- cgit v1.2.3 From 870995933a60b67af03b3f42a2aed169d33bd87f Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 12 Mar 2018 03:35:11 +0100 Subject: Remove redundant Date.now() --- packages/dev-utils/src/env.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 packages/dev-utils/src/env.ts (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/env.ts b/packages/dev-utils/src/env.ts new file mode 100644 index 000000000..7e46a5f89 --- /dev/null +++ b/packages/dev-utils/src/env.ts @@ -0,0 +1,19 @@ +import * as _ from 'lodash'; +import * as process from 'process'; + +export const env = { + parseBoolean(key: string): boolean { + let isTrue: boolean; + const envVarvalue = process.env[key]; + if (process.env.SOLIDITY_COVERAGE === 'true') { + isTrue = true; + } else if (process.env.SOLIDITY_COVERAGE === 'false' || _.isUndefined(process.env.SOLIDITY_COVERAGE)) { + isTrue = false; + } else { + throw new Error( + `Failed to parse ENV variable ${key} as boolean. Please make sure it's either true or false. Defaults to false`, + ); + } + return isTrue; + }, +}; -- cgit v1.2.3 From 32e3cab116eedb940c5e1be2a11daeb31260ce77 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 12 Mar 2018 12:51:37 +0100 Subject: Make BlockchainLifecycle accept only web3Wrapper --- packages/dev-utils/src/blockchain_lifecycle.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index 48746f8c1..3e35de861 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -4,10 +4,8 @@ import * as Web3 from 'web3'; export class BlockchainLifecycle { private _web3Wrapper: Web3Wrapper; private _snapshotIdsStack: number[]; - constructor(web3Orweb3Wrapper: Web3Wrapper | Web3) { - this._web3Wrapper = (web3Orweb3Wrapper as Web3Wrapper).isZeroExWeb3Wrapper - ? (web3Orweb3Wrapper as Web3Wrapper) - : new Web3Wrapper((web3Orweb3Wrapper as Web3).currentProvider); + constructor(web3Wrapper: Web3Wrapper) { + this._web3Wrapper = web3Wrapper; this._snapshotIdsStack = []; } // TODO: In order to run these tests on an actual node, we should check if we are running against -- cgit v1.2.3 From 075f286130742351edc6f6609d9d4738a3b53f61 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 12 Mar 2018 12:53:07 +0100 Subject: Introduce TESTRPC_FIRST_ADDRESS --- packages/dev-utils/src/constants.ts | 1 + packages/dev-utils/src/coverage.ts | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/constants.ts b/packages/dev-utils/src/constants.ts index b018ba791..05bcc1638 100644 --- a/packages/dev-utils/src/constants.ts +++ b/packages/dev-utils/src/constants.ts @@ -2,4 +2,5 @@ export const constants = { RPC_URL: 'http://localhost:8545', RPC_PORT: 8545, GAS_ESTIMATE: 5000000, + TESTRPC_FIRST_ADDRESS: '0x5409ed021d9299bf6814279a6a1411a7e866a631', }; diff --git a/packages/dev-utils/src/coverage.ts b/packages/dev-utils/src/coverage.ts index a29acf5da..40bb47e2d 100644 --- a/packages/dev-utils/src/coverage.ts +++ b/packages/dev-utils/src/coverage.ts @@ -1,6 +1,8 @@ import { CoverageSubprovider } from '@0xproject/sol-cov'; import * as _ from 'lodash'; +import { constants } from './constants'; + let coverageSubprovider: CoverageSubprovider; export const coverage = { @@ -14,7 +16,7 @@ export const coverage = { const artifactsPath = './src/artifacts'; const contractsPath = './src/contracts'; const networkId = 50; - const defaultFromAddress = '0x5409ed021d9299bf6814279a6a1411a7e866a631'; + const defaultFromAddress = constants.TESTRPC_FIRST_ADDRESS; return new CoverageSubprovider(artifactsPath, contractsPath, networkId, defaultFromAddress); }, }; -- cgit v1.2.3 From e4740961199ee6b34ad32dd242e0081afbb41a69 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 12 Mar 2018 12:53:51 +0100 Subject: Fix a typo in handling env variables --- packages/dev-utils/src/env.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/env.ts b/packages/dev-utils/src/env.ts index 7e46a5f89..6f8b1c5f8 100644 --- a/packages/dev-utils/src/env.ts +++ b/packages/dev-utils/src/env.ts @@ -4,10 +4,10 @@ import * as process from 'process'; export const env = { parseBoolean(key: string): boolean { let isTrue: boolean; - const envVarvalue = process.env[key]; - if (process.env.SOLIDITY_COVERAGE === 'true') { + const envVarValue = process.env[key]; + if (envVarValue === 'true') { isTrue = true; - } else if (process.env.SOLIDITY_COVERAGE === 'false' || _.isUndefined(process.env.SOLIDITY_COVERAGE)) { + } else if (envVarValue === 'false' || _.isUndefined(envVarValue)) { isTrue = false; } else { throw new Error( -- cgit v1.2.3 From a9db0e8ebe1360cf521302691998ef1f98e2dc51 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 13 Mar 2018 15:19:31 +0100 Subject: Properly and consistently parse ENV vars --- packages/dev-utils/src/env.ts | 5 +++++ packages/dev-utils/src/index.ts | 1 + packages/dev-utils/src/web3_factory.ts | 7 +------ 3 files changed, 7 insertions(+), 6 deletions(-) (limited to 'packages/dev-utils') diff --git a/packages/dev-utils/src/env.ts b/packages/dev-utils/src/env.ts index 6f8b1c5f8..114dbf4a8 100644 --- a/packages/dev-utils/src/env.ts +++ b/packages/dev-utils/src/env.ts @@ -1,6 +1,11 @@ import * as _ from 'lodash'; import * as process from 'process'; +export enum EnvVars { + SolidityCoverage = 'SOLIDITY_COVERAGE', + VerboseGanache = 'VERBOSE_GANACHE', +} + export const env = { parseBoolean(key: string): boolean { let isTrue: boolean; diff --git a/packages/dev-utils/src/index.ts b/packages/dev-utils/src/index.ts index 72d538e5d..2ff2a2238 100644 --- a/packages/dev-utils/src/index.ts +++ b/packages/dev-utils/src/index.ts @@ -2,3 +2,4 @@ export { BlockchainLifecycle } from './blockchain_lifecycle'; export { web3Factory } from './web3_factory'; export { constants as devConstants } from './constants'; export { coverage } from './coverage'; +export { env, EnvVars } from './env'; diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts index 3e4b39686..6435f0f9a 100644 --- a/packages/dev-utils/src/web3_factory.ts +++ b/packages/dev-utils/src/web3_factory.ts @@ -13,7 +13,7 @@ import * as process from 'process'; import { constants } from './constants'; import { coverage } from './coverage'; -import { env } from './env'; +import { env, EnvVars } from './env'; // HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang // because they are using the wrong XHR package. @@ -27,11 +27,6 @@ export interface Web3Config { shouldUseInProcessGanache?: boolean; // default: false } -enum EnvVars { - SolidityCoverage = 'SOLIDITY_COVERAGE', - VerboseGanache = 'VERBOSE_GANACHE', -} - export const web3Factory = { create(config: Web3Config = {}): Web3 { const provider = this.getRpcProvider(config); -- cgit v1.2.3