aboutsummaryrefslogtreecommitdiffstats
path: root/packages/dev-utils/src
diff options
context:
space:
mode:
authorAlex Browne <stephenalexbrowne@gmail.com>2018-05-22 04:56:32 +0800
committerAlex Browne <stephenalexbrowne@gmail.com>2018-06-07 03:39:39 +0800
commit577156fe5f63e581b101682d13b7e70e7a9336e5 (patch)
tree202d928d4888825a4ea1f24e2fd03368643c2966 /packages/dev-utils/src
parentda3f783a9ff69b059b1a98f502d980660d6bacab (diff)
downloaddexon-sol-tools-577156fe5f63e581b101682d13b7e70e7a9336e5.tar
dexon-sol-tools-577156fe5f63e581b101682d13b7e70e7a9336e5.tar.gz
dexon-sol-tools-577156fe5f63e581b101682d13b7e70e7a9336e5.tar.bz2
dexon-sol-tools-577156fe5f63e581b101682d13b7e70e7a9336e5.tar.lz
dexon-sol-tools-577156fe5f63e581b101682d13b7e70e7a9336e5.tar.xz
dexon-sol-tools-577156fe5f63e581b101682d13b7e70e7a9336e5.tar.zst
dexon-sol-tools-577156fe5f63e581b101682d13b7e70e7a9336e5.zip
Use Geth for contract tests
Diffstat (limited to 'packages/dev-utils/src')
-rw-r--r--packages/dev-utils/src/blockchain_lifecycle.ts57
-rw-r--r--packages/dev-utils/src/web3_factory.ts2
2 files changed, 50 insertions, 9 deletions
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<void> {
- 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<void> {
- 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<NodeType> {
+ 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`);