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/web3-wrapper/src/web3_wrapper.ts | 37 ++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'packages/web3-wrapper/src/web3_wrapper.ts') diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index 3de152df1..d922c80cd 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -281,7 +281,6 @@ export class Web3Wrapper { }; const payload = { jsonrpc: '2.0', - id: this._jsonRpcRequestId++, method: 'eth_getLogs', params: [serializedFilter], }; @@ -403,8 +402,44 @@ export class Web3Wrapper { } return receipt; } + /** + * Start the CPU mining process with the given number of threads and + * generate a new DAG if need be. + * @param threads The number of threads to mine on. + */ + public async minerStartAsync(threads: number = 1): Promise { + await this._sendRawPayloadAsync({ + method: 'miner_start', + params: [threads], + }); + } + /** + * Stop the CPU mining process. + * @param threads The number of threads to mine on. + */ + public async minerStopAsync(): Promise { + await this._sendRawPayloadAsync({ method: 'miner_stop', params: [] }); + } + /** + * Returns true if client is actively mining new blocks. + * @returns A boolean indicating whether the node is currently mining. + */ + public async isMiningAsync(): Promise { + const isMining = await promisify(this._web3.eth.getMining)(); + return isMining; + } + /** + * Sets the current head of the local chain by block number. Note, this is a + * destructive action and may severely damage your chain. Use with extreme + * caution. + * @param blockNumber The block number to reset to. + */ + public async setHeadAsync(blockNumber: number): Promise { + await this._sendRawPayloadAsync({ method: 'debug_setHead', params: [this._web3.toHex(blockNumber)] }); + } private async _sendRawPayloadAsync(payload: Partial): Promise { const sendAsync = this._web3.currentProvider.sendAsync.bind(this._web3.currentProvider); + payload.id = this._jsonRpcRequestId++; const response = await promisify(sendAsync)(payload); const result = response.result; return result; -- 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/web3-wrapper/src/web3_wrapper.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'packages/web3-wrapper/src/web3_wrapper.ts') diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index d922c80cd..b9b0bdbb2 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -258,7 +258,8 @@ export class Web3Wrapper { * @param timeDelta Amount of time to add in seconds */ public async increaseTimeAsync(timeDelta: number): Promise { - await this._sendRawPayloadAsync({ method: 'evm_increaseTime', params: [timeDelta] }); + // TODO(albrow): Detect Geth vs. Ganache and use appropriate endpoint. + await this._sendRawPayloadAsync({ method: 'debug_increaseTime', params: [timeDelta] }); } /** * Retrieve smart contract logs for a given filter -- cgit v1.2.3 From 5900899c0195a851c8d20ca0d4ad85dbbf4c100f Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Mon, 4 Jun 2018 17:47:18 -0700 Subject: Add support for TEST_PROVIDER env var --- packages/web3-wrapper/src/web3_wrapper.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'packages/web3-wrapper/src/web3_wrapper.ts') diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index b9b0bdbb2..ace6a2d61 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -257,9 +257,9 @@ export class Web3Wrapper { * Increase the next blocks timestamp on TestRPC/Ganache local node * @param timeDelta Amount of time to add in seconds */ - public async increaseTimeAsync(timeDelta: number): Promise { + public async increaseTimeAsync(timeDelta: number): Promise { // TODO(albrow): Detect Geth vs. Ganache and use appropriate endpoint. - await this._sendRawPayloadAsync({ method: 'debug_increaseTime', params: [timeDelta] }); + return this._sendRawPayloadAsync({ method: 'debug_increaseTime', params: [timeDelta] }); } /** * Retrieve smart contract logs for a given filter -- cgit v1.2.3 From bca62c813d2e821c56968916615861366402435b Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Mon, 4 Jun 2018 18:10:42 -0700 Subject: Throw in web3-wrapper when rawCallResult is '0x' --- packages/web3-wrapper/src/web3_wrapper.ts | 3 +++ 1 file changed, 3 insertions(+) (limited to 'packages/web3-wrapper/src/web3_wrapper.ts') diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index ace6a2d61..ed2d0a119 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -315,6 +315,9 @@ export class Web3Wrapper { */ public async callAsync(callData: CallData, defaultBlock?: BlockParam): Promise { const rawCallResult = await promisify(this._web3.eth.call)(callData, defaultBlock); + if (rawCallResult === '0x') { + throw new Error('Contract call failed (returned null)'); + } return rawCallResult; } /** -- cgit v1.2.3 From 63caddea62453863de84a4b53e14fe3e61d3008f Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Tue, 5 Jun 2018 15:12:09 -0700 Subject: Small fixes and cleanup --- packages/web3-wrapper/src/web3_wrapper.ts | 26 -------------------------- 1 file changed, 26 deletions(-) (limited to 'packages/web3-wrapper/src/web3_wrapper.ts') diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index ed2d0a119..a19253449 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -406,32 +406,6 @@ export class Web3Wrapper { } return receipt; } - /** - * Start the CPU mining process with the given number of threads and - * generate a new DAG if need be. - * @param threads The number of threads to mine on. - */ - public async minerStartAsync(threads: number = 1): Promise { - await this._sendRawPayloadAsync({ - method: 'miner_start', - params: [threads], - }); - } - /** - * Stop the CPU mining process. - * @param threads The number of threads to mine on. - */ - public async minerStopAsync(): Promise { - await this._sendRawPayloadAsync({ method: 'miner_stop', params: [] }); - } - /** - * Returns true if client is actively mining new blocks. - * @returns A boolean indicating whether the node is currently mining. - */ - public async isMiningAsync(): Promise { - const isMining = await promisify(this._web3.eth.getMining)(); - return isMining; - } /** * Sets the current head of the local chain by block number. Note, this is a * destructive action and may severely damage your chain. Use with extreme -- 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/web3-wrapper/src/web3_wrapper.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'packages/web3-wrapper/src/web3_wrapper.ts') diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index a19253449..6c9fa980e 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -21,6 +21,13 @@ import { Web3WrapperErrors } from './types'; const BASE_TEN = 10; +// These are unique identifiers contained in the response of the +// web3_clientVersion call. +export const uniqueVersionIds = { + geth: 'Geth', + ganache: 'EthereumJS TestRPC', +}; + /** * A wrapper around the Web3.js 0.x library that provides a consistent, clean promise-based interface. */ @@ -254,12 +261,20 @@ export class Web3Wrapper { await this._sendRawPayloadAsync({ method: 'evm_mine', params: [] }); } /** - * Increase the next blocks timestamp on TestRPC/Ganache local node + * Increase the next blocks timestamp on TestRPC/Ganache or Geth local node. + * Will throw if provider is neither TestRPC/Ganache or Geth. * @param timeDelta Amount of time to add in seconds */ public async increaseTimeAsync(timeDelta: number): Promise { - // TODO(albrow): Detect Geth vs. Ganache and use appropriate endpoint. - return this._sendRawPayloadAsync({ method: 'debug_increaseTime', params: [timeDelta] }); + // Detect Geth vs. Ganache and use appropriate endpoint. + const version = await this.getNodeVersionAsync(); + if (_.includes(version, uniqueVersionIds.geth)) { + return this._sendRawPayloadAsync({ method: 'debug_increaseTime', params: [timeDelta] }); + } else if (_.includes(version, uniqueVersionIds.ganache)) { + return this._sendRawPayloadAsync({ method: 'evm_increaseTime', params: [timeDelta] }); + } else { + throw new Error(`Unknown client version: ${version}`); + } } /** * Retrieve smart contract logs for a given filter -- cgit v1.2.3 From dd8727d3aebf1f4b552f8bad921b92107ad22936 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Wed, 6 Jun 2018 11:43:07 -0700 Subject: Apply various fixes based on PR feedback --- packages/web3-wrapper/src/web3_wrapper.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'packages/web3-wrapper/src/web3_wrapper.ts') diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index 6c9fa980e..559bf3ea9 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -422,9 +422,11 @@ export class Web3Wrapper { return receipt; } /** - * Sets the current head of the local chain by block number. Note, this is a - * destructive action and may severely damage your chain. Use with extreme - * caution. + * Calls the 'debug_setHead' JSON RPC method, which sets the current head of + * the local chain by block number. Note, this is a destructive action and + * may severely damage your chain. Use with extreme caution. As of now, this + * is only supported by Geth. It sill throw if the 'debug_setHead' method is + * not supported. * @param blockNumber The block number to reset to. */ public async setHeadAsync(blockNumber: number): Promise { -- cgit v1.2.3