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 ++++++++++++++++++++++++- packages/web3-wrapper/test/web3_wrapper_test.ts | 19 +++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) (limited to 'packages/web3-wrapper') 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; diff --git a/packages/web3-wrapper/test/web3_wrapper_test.ts b/packages/web3-wrapper/test/web3_wrapper_test.ts index 326efe654..1843bcf2c 100644 --- a/packages/web3-wrapper/test/web3_wrapper_test.ts +++ b/packages/web3-wrapper/test/web3_wrapper_test.ts @@ -2,6 +2,7 @@ import * as chai from 'chai'; import * as Ganache from 'ganache-core'; import 'make-promises-safe'; import 'mocha'; +import * as Web3 from 'web3'; import { Web3Wrapper } from '../src'; @@ -37,4 +38,22 @@ describe('Web3Wrapper tests', () => { expect(networkId).to.be.equal(NETWORK_ID); }); }); + describe('mining functions', () => { + it('starts and stops the miner', async () => { + // Note: depending on our provider, the miner may or may not already + // be mining. To account for both conditions, we have what might + // look like too many stops and starts here, but it is necessary. + await web3Wrapper.minerStopAsync(); + let isMining = await web3Wrapper.isMiningAsync(); + expect(isMining).to.be.false(); + await web3Wrapper.minerStartAsync(1); + isMining = await web3Wrapper.isMiningAsync(); + expect(isMining).to.be.true(); + isMining = await web3Wrapper.isMiningAsync(); + expect(isMining).to.be.true(); + await web3Wrapper.minerStopAsync(); + isMining = await web3Wrapper.isMiningAsync(); + expect(isMining).to.be.false(); + }); + }); }); -- cgit 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') 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 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') 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 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') 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 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 ------------------------- packages/web3-wrapper/test/web3_wrapper_test.ts | 19 ------------------ 2 files changed, 45 deletions(-) (limited to 'packages/web3-wrapper') 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 diff --git a/packages/web3-wrapper/test/web3_wrapper_test.ts b/packages/web3-wrapper/test/web3_wrapper_test.ts index 1843bcf2c..326efe654 100644 --- a/packages/web3-wrapper/test/web3_wrapper_test.ts +++ b/packages/web3-wrapper/test/web3_wrapper_test.ts @@ -2,7 +2,6 @@ import * as chai from 'chai'; import * as Ganache from 'ganache-core'; import 'make-promises-safe'; import 'mocha'; -import * as Web3 from 'web3'; import { Web3Wrapper } from '../src'; @@ -38,22 +37,4 @@ describe('Web3Wrapper tests', () => { expect(networkId).to.be.equal(NETWORK_ID); }); }); - describe('mining functions', () => { - it('starts and stops the miner', async () => { - // Note: depending on our provider, the miner may or may not already - // be mining. To account for both conditions, we have what might - // look like too many stops and starts here, but it is necessary. - await web3Wrapper.minerStopAsync(); - let isMining = await web3Wrapper.isMiningAsync(); - expect(isMining).to.be.false(); - await web3Wrapper.minerStartAsync(1); - isMining = await web3Wrapper.isMiningAsync(); - expect(isMining).to.be.true(); - isMining = await web3Wrapper.isMiningAsync(); - expect(isMining).to.be.true(); - await web3Wrapper.minerStopAsync(); - isMining = await web3Wrapper.isMiningAsync(); - expect(isMining).to.be.false(); - }); - }); }); -- cgit 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/index.ts | 2 +- packages/web3-wrapper/src/web3_wrapper.ts | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) (limited to 'packages/web3-wrapper') diff --git a/packages/web3-wrapper/src/index.ts b/packages/web3-wrapper/src/index.ts index 7309e09a8..b14fa7406 100644 --- a/packages/web3-wrapper/src/index.ts +++ b/packages/web3-wrapper/src/index.ts @@ -1,2 +1,2 @@ -export { Web3Wrapper } from './web3_wrapper'; +export { Web3Wrapper, uniqueVersionIds } from './web3_wrapper'; export { Web3WrapperErrors } from './types'; 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 From c57e4ba508d648507cd909a14523d6fee8b09bce Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Tue, 5 Jun 2018 17:10:33 -0700 Subject: Update relevant changelogs --- packages/web3-wrapper/CHANGELOG.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'packages/web3-wrapper') diff --git a/packages/web3-wrapper/CHANGELOG.json b/packages/web3-wrapper/CHANGELOG.json index b753ffbac..54a816e23 100644 --- a/packages/web3-wrapper/CHANGELOG.json +++ b/packages/web3-wrapper/CHANGELOG.json @@ -1,4 +1,25 @@ [ + { + "version": "0.7.0", + "changes": [ + { + "note": "Add exported uniqueVersionIds object", + "pr": 622 + }, + { + "note": "Update increaseTimeAsync to work with Geth", + "pr": 622 + }, + { + "note": "Make callAsync throw if raw call result is 0x (null)", + "pr": 622 + }, + { + "note": "Add new setHeadAsync method", + "pr": 622 + } + ] + }, { "timestamp": 1527009133, "version": "0.6.4", -- cgit 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') 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