From dc956020ef7c6d3f1880263700422b31253c8da3 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Tue, 3 Jul 2018 12:55:05 -0700 Subject: Move NodeType caching out of web3-wrapper and into our internal code --- packages/contracts/test/utils/assertions.ts | 10 ++++++++-- packages/dev-utils/src/blockchain_lifecycle.ts | 12 ++++++++++-- packages/web3-wrapper/src/web3_wrapper.ts | 9 +-------- 3 files changed, 19 insertions(+), 12 deletions(-) (limited to 'packages') diff --git a/packages/contracts/test/utils/assertions.ts b/packages/contracts/test/utils/assertions.ts index c8031c8a1..112a470f6 100644 --- a/packages/contracts/test/utils/assertions.ts +++ b/packages/contracts/test/utils/assertions.ts @@ -9,12 +9,16 @@ import { web3Wrapper } from './web3_wrapper'; const expect = chai.expect; +let nodeType: NodeType | undefined; + // Represents the return value of a `sendTransaction` call. The Promise should // resolve with either a transaction receipt or a transaction hash. export type sendTransactionResult = Promise; async function _getGanacheOrGethError(ganacheError: string, gethError: string): Promise { - const nodeType = await web3Wrapper.getNodeTypeAsync(); + if (_.isUndefined(nodeType)) { + nodeType = await web3Wrapper.getNodeTypeAsync(); + } switch (nodeType) { case NodeType.Ganache: return ganacheError; @@ -68,7 +72,9 @@ export async function expectTransactionFailedAsync(p: sendTransactionResult, rea _.noop(e); }); - const nodeType = await web3Wrapper.getNodeTypeAsync(); + if (_.isUndefined(nodeType)) { + nodeType = await web3Wrapper.getNodeTypeAsync(); + } switch (nodeType) { case NodeType.Ganache: return expect(p).to.be.rejectedWith(reason); diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index abca6d386..9bd65ee5d 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -1,5 +1,6 @@ import { logUtils } from '@0xproject/utils'; import { NodeType, Web3Wrapper } from '@0xproject/web3-wrapper'; +import * as _ from 'lodash'; // HACK(albrow): 🐉 We have to do this so that debug.setHead works correctly. // (Geth does not seem to like debug.setHead(0), so by sending some transactions @@ -12,12 +13,13 @@ export class BlockchainLifecycle { private _web3Wrapper: Web3Wrapper; private _snapshotIdsStack: number[]; private _addresses: string[] = []; + private _nodeType: NodeType | undefined; constructor(web3Wrapper: Web3Wrapper) { this._web3Wrapper = web3Wrapper; this._snapshotIdsStack = []; } public async startAsync(): Promise { - const nodeType = await this._web3Wrapper.getNodeTypeAsync(); + const nodeType = await this._getNodeTypeAsync(); switch (nodeType) { case NodeType.Ganache: const snapshotId = await this._web3Wrapper.takeSnapshotAsync(); @@ -38,7 +40,7 @@ export class BlockchainLifecycle { } } public async revertAsync(): Promise { - const nodeType = await this._web3Wrapper.getNodeTypeAsync(); + const nodeType = await this._getNodeTypeAsync(); switch (nodeType) { case NodeType.Ganache: const snapshotId = this._snapshotIdsStack.pop() as number; @@ -76,4 +78,10 @@ export class BlockchainLifecycle { } logUtils.warn('Done mining the minimum number of blocks.'); } + private async _getNodeTypeAsync(): Promise { + if (_.isUndefined(this._nodeType)) { + this._nodeType = await this._web3Wrapper.getNodeTypeAsync(); + } + return this._nodeType; + } } diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index c2754a13e..b79ade278 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -49,7 +49,6 @@ export class Web3Wrapper { private _web3: Web3; private _txDefaults: Partial; private _jsonRpcRequestId: number; - private _nodeType: NodeType | undefined; /** * Check if an address is a valid Ethereum address * @param address Address to check @@ -498,19 +497,13 @@ export class Web3Wrapper { } /** * Returns either NodeType.Geth or NodeType.Ganache depending on the type of - * the backing Ethereum node. Throws for any other type of node. This - * function caches the result and so subsequent calls are fast. + * the backing Ethereum node. Throws for any other type of node. */ public async getNodeTypeAsync(): Promise { - if (!_.isUndefined(this._nodeType)) { - return this._nodeType; - } const version = await this.getNodeVersionAsync(); if (_.includes(version, uniqueVersionIds.geth)) { - this._nodeType = NodeType.Geth; return NodeType.Geth; } else if (_.includes(version, uniqueVersionIds.ganache)) { - this._nodeType = NodeType.Ganache; return NodeType.Ganache; } else { throw new Error(`Unknown client version: ${version}`); -- cgit v1.2.3