Move NodeType caching out of web3-wrapper and into our internal code

This commit is contained in:
Alex Browne
2018-07-03 12:55:05 -07:00
parent ce1542da4f
commit dc956020ef
3 changed files with 19 additions and 12 deletions

View File

@@ -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<TransactionReceipt | TransactionReceiptWithDecodedLogs | string>;
async function _getGanacheOrGethError(ganacheError: string, gethError: string): Promise<string> {
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);

View File

@@ -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<void> {
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<void> {
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<NodeType> {
if (_.isUndefined(this._nodeType)) {
this._nodeType = await this._web3Wrapper.getNodeTypeAsync();
}
return this._nodeType;
}
}

View File

@@ -49,7 +49,6 @@ export class Web3Wrapper {
private _web3: Web3;
private _txDefaults: Partial<TxData>;
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<NodeType> {
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}`);