From 577156fe5f63e581b101682d13b7e70e7a9336e5 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Mon, 21 May 2018 13:56:32 -0700 Subject: Use Geth for contract tests --- packages/dev-utils/src/blockchain_lifecycle.ts | 57 ++++++++++++++++++++++---- packages/dev-utils/src/web3_factory.ts | 2 +- 2 files changed, 50 insertions(+), 9 deletions(-) (limited to 'packages/dev-utils/src') diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index 3e35de861..6e7957f10 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -1,6 +1,17 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import * as _ from 'lodash'; import * as Web3 from 'web3'; +enum NodeType { + Geth = 'GETH', + Ganache = 'GANACHE', +} + +// These are unique identifiers contained in the response of the +// web3_clientVersion call. +const GETH_VERSION_ID = 'Geth'; +const GANACHE_VERSION_ID = 'EthereumJS TestRPC'; + export class BlockchainLifecycle { private _web3Wrapper: Web3Wrapper; private _snapshotIdsStack: number[]; @@ -8,17 +19,47 @@ export class BlockchainLifecycle { this._web3Wrapper = web3Wrapper; 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._web3Wrapper.takeSnapshotAsync(); - this._snapshotIdsStack.push(snapshotId); + const nodeType = await this._getNodeTypeAsync(); + switch (nodeType) { + case NodeType.Ganache: + const snapshotId = await this._web3Wrapper.takeSnapshotAsync(); + this._snapshotIdsStack.push(snapshotId); + break; + case NodeType.Geth: + const blockNumber = await this._web3Wrapper.getBlockNumberAsync(); + this._snapshotIdsStack.push(blockNumber); + break; + default: + throw new Error(`Unknown node type: ${nodeType}`); + } } public async revertAsync(): Promise { - const snapshotId = this._snapshotIdsStack.pop() as number; - const didRevert = await this._web3Wrapper.revertSnapshotAsync(snapshotId); - if (!didRevert) { - throw new Error(`Snapshot with id #${snapshotId} failed to revert`); + const nodeType = await this._getNodeTypeAsync(); + switch (nodeType) { + case NodeType.Ganache: + const snapshotId = this._snapshotIdsStack.pop() as number; + const didRevert = await this._web3Wrapper.revertSnapshotAsync(snapshotId); + if (!didRevert) { + throw new Error(`Snapshot with id #${snapshotId} failed to revert`); + } + break; + case NodeType.Geth: + const blockNumber = this._snapshotIdsStack.pop() as number; + await this._web3Wrapper.setHeadAsync(blockNumber); + break; + default: + throw new Error(`Unknown node type: ${nodeType}`); + } + } + private async _getNodeTypeAsync(): Promise { + const version = await this._web3Wrapper.getNodeVersionAsync(); + if (_.includes(version, GETH_VERSION_ID)) { + return NodeType.Geth; + } else if (_.includes(version, GANACHE_VERSION_ID)) { + return NodeType.Ganache; + } else { + throw new Error(`Unknown client version: ${version}`); } } } diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts index 12872c122..d8379825a 100644 --- a/packages/dev-utils/src/web3_factory.ts +++ b/packages/dev-utils/src/web3_factory.ts @@ -28,7 +28,7 @@ export const web3Factory = { if (!hasAddresses) { provider.addProvider(new EmptyWalletSubprovider()); } - provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_LIMIT)); + // provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_LIMIT)); const logger = { log: (arg: any) => { fs.appendFileSync('ganache.log', `${arg}\n`); -- cgit v1.2.3 From 00bf957b53c22f3ccdd6c2e7ad75f0c9e15caa38 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Wed, 23 May 2018 18:13:18 -0700 Subject: Add more transactions to Geth on init. Skip tests that are failing. --- packages/dev-utils/src/blockchain_lifecycle.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'packages/dev-utils/src') diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index 6e7957f10..49ac8c671 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -28,6 +28,7 @@ export class BlockchainLifecycle { break; case NodeType.Geth: const blockNumber = await this._web3Wrapper.getBlockNumberAsync(); + console.log(`block number for snapshot: ${blockNumber}`); this._snapshotIdsStack.push(blockNumber); break; default: @@ -46,6 +47,7 @@ export class BlockchainLifecycle { break; case NodeType.Geth: const blockNumber = this._snapshotIdsStack.pop() as number; + console.log(`setting head: ${blockNumber}`); await this._web3Wrapper.setHeadAsync(blockNumber); break; default: -- cgit v1.2.3 From 2004c0d7398a5e77d08e3b4d8030c0f22cb09cc8 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Fri, 1 Jun 2018 13:19:36 -0700 Subject: Add ability to quickly switch between Geth and Ganache by changing a const --- packages/dev-utils/src/web3_factory.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'packages/dev-utils/src') diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts index d8379825a..25201228d 100644 --- a/packages/dev-utils/src/web3_factory.ts +++ b/packages/dev-utils/src/web3_factory.ts @@ -19,16 +19,22 @@ export interface Web3Config { hasAddresses?: boolean; // default: true shouldUseInProcessGanache?: boolean; // default: false rpcUrl?: string; // default: localhost:8545 + shouldUseFakeGasEstimate?: boolean; // default: true } export const web3Factory = { getRpcProvider(config: Web3Config = {}): ProviderEngine { const provider = new ProviderEngine(); const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses; + config.shouldUseFakeGasEstimate = + _.isUndefined(config.shouldUseFakeGasEstimate) || config.shouldUseFakeGasEstimate; if (!hasAddresses) { provider.addProvider(new EmptyWalletSubprovider()); } - // provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_LIMIT)); + + if (config.shouldUseFakeGasEstimate) { + provider.addProvider(new FakeGasEstimateSubprovider(constants.GAS_LIMIT)); + } const logger = { log: (arg: any) => { fs.appendFileSync('ganache.log', `${arg}\n`); -- cgit v1.2.3 From 45a3d8b75a72fc4104f5070361fb34027b66e7f3 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Mon, 4 Jun 2018 18:41:30 -0700 Subject: Remove extra logs and other small fixes --- packages/dev-utils/src/blockchain_lifecycle.ts | 2 -- 1 file changed, 2 deletions(-) (limited to 'packages/dev-utils/src') diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index 49ac8c671..6e7957f10 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -28,7 +28,6 @@ export class BlockchainLifecycle { break; case NodeType.Geth: const blockNumber = await this._web3Wrapper.getBlockNumberAsync(); - console.log(`block number for snapshot: ${blockNumber}`); this._snapshotIdsStack.push(blockNumber); break; default: @@ -47,7 +46,6 @@ export class BlockchainLifecycle { break; case NodeType.Geth: const blockNumber = this._snapshotIdsStack.pop() as number; - console.log(`setting head: ${blockNumber}`); await this._web3Wrapper.setHeadAsync(blockNumber); break; default: -- cgit v1.2.3 From d6d7f4e875b161aa7284467a61f67989f76ec89e Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Tue, 5 Jun 2018 16:20:38 -0700 Subject: Update more things to work with both Geth and Ganache --- packages/dev-utils/src/blockchain_lifecycle.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'packages/dev-utils/src') diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index 6e7957f10..8e4ad81c7 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -1,4 +1,4 @@ -import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { uniqueVersionIds, Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import * as Web3 from 'web3'; @@ -7,11 +7,6 @@ enum NodeType { Ganache = 'GANACHE', } -// These are unique identifiers contained in the response of the -// web3_clientVersion call. -const GETH_VERSION_ID = 'Geth'; -const GANACHE_VERSION_ID = 'EthereumJS TestRPC'; - export class BlockchainLifecycle { private _web3Wrapper: Web3Wrapper; private _snapshotIdsStack: number[]; @@ -54,9 +49,9 @@ export class BlockchainLifecycle { } private async _getNodeTypeAsync(): Promise { const version = await this._web3Wrapper.getNodeVersionAsync(); - if (_.includes(version, GETH_VERSION_ID)) { + if (_.includes(version, uniqueVersionIds.geth)) { return NodeType.Geth; - } else if (_.includes(version, GANACHE_VERSION_ID)) { + } else if (_.includes(version, uniqueVersionIds.ganache)) { return NodeType.Ganache; } else { throw new Error(`Unknown client version: ${version}`); -- cgit v1.2.3