diff options
author | Jacob Evans <dekz@dekz.net> | 2018-07-02 09:21:16 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-02 09:21:16 +0800 |
commit | ad570b8ae162a213b4b88c417ecd64d4661df18b (patch) | |
tree | d9acfb9e2459c4dfcac191061fefebe015ff5771 /packages/dev-utils | |
parent | b9165c03af40983d885af2b18e729f11746de91d (diff) | |
parent | b9b00e10d39c3c84bc72892ef37f1313e904414d (diff) | |
download | dexon-sol-tools-ad570b8ae162a213b4b88c417ecd64d4661df18b.tar dexon-sol-tools-ad570b8ae162a213b4b88c417ecd64d4661df18b.tar.gz dexon-sol-tools-ad570b8ae162a213b4b88c417ecd64d4661df18b.tar.bz2 dexon-sol-tools-ad570b8ae162a213b4b88c417ecd64d4661df18b.tar.lz dexon-sol-tools-ad570b8ae162a213b4b88c417ecd64d4661df18b.tar.xz dexon-sol-tools-ad570b8ae162a213b4b88c417ecd64d4661df18b.tar.zst dexon-sol-tools-ad570b8ae162a213b4b88c417ecd64d4661df18b.zip |
Merge branch 'v2-prototype' into eth-lightwallet-subprovider-final
Diffstat (limited to 'packages/dev-utils')
-rw-r--r-- | packages/dev-utils/package.json | 1 | ||||
-rw-r--r-- | packages/dev-utils/src/blockchain_lifecycle.ts | 38 |
2 files changed, 38 insertions, 1 deletions
diff --git a/packages/dev-utils/package.json b/packages/dev-utils/package.json index cb83cedf2..e62075054 100644 --- a/packages/dev-utils/package.json +++ b/packages/dev-utils/package.json @@ -47,6 +47,7 @@ "dependencies": { "@0xproject/subproviders": "^0.10.4", "@0xproject/types": "^0.8.1", + "@0xproject/utils": "^0.7.1", "ethereum-types": "^0.0.1", "@0xproject/typescript-typings": "^0.4.1", "@0xproject/web3-wrapper": "^0.7.1", diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index 4bb136097..587332f1a 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -1,3 +1,4 @@ +import { logUtils } from '@0xproject/utils'; import { uniqueVersionIds, Web3Wrapper } from '@0xproject/web3-wrapper'; import { includes } from 'lodash'; @@ -6,9 +7,17 @@ enum NodeType { Ganache = 'GANACHE', } +// 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 +// we increase the current block number beyond 0). Additionally, some tests seem +// to break when there are fewer than 3 blocks in the chain. (We have no idea +// why, but it was consistently reproducible). +const MINIMUM_BLOCKS = 3; + export class BlockchainLifecycle { private _web3Wrapper: Web3Wrapper; private _snapshotIdsStack: number[]; + private _addresses: string[] = []; constructor(web3Wrapper: Web3Wrapper) { this._web3Wrapper = web3Wrapper; this._snapshotIdsStack = []; @@ -21,7 +30,13 @@ export class BlockchainLifecycle { this._snapshotIdsStack.push(snapshotId); break; case NodeType.Geth: - const blockNumber = await this._web3Wrapper.getBlockNumberAsync(); + let blockNumber = await this._web3Wrapper.getBlockNumberAsync(); + if (blockNumber < MINIMUM_BLOCKS) { + // If the minimum block number is not met, force Geth to + // mine some blocks by sending some dummy transactions. + await this._mineMinimumBlocksAsync(); + blockNumber = await this._web3Wrapper.getBlockNumberAsync(); + } this._snapshotIdsStack.push(blockNumber); break; default: @@ -56,4 +71,25 @@ export class BlockchainLifecycle { throw new Error(`Unknown client version: ${version}`); } } + private async _mineMinimumBlocksAsync(): Promise<void> { + logUtils.warn('WARNING: minimum block number for tests not met. Mining additional blocks...'); + if (this._addresses.length === 0) { + this._addresses = await this._web3Wrapper.getAvailableAddressesAsync(); + if (this._addresses.length === 0) { + throw new Error('No accounts found'); + } + } + while ((await this._web3Wrapper.getBlockNumberAsync()) < MINIMUM_BLOCKS) { + logUtils.warn('Mining block...'); + await this._web3Wrapper.awaitTransactionMinedAsync( + await this._web3Wrapper.sendTransactionAsync({ + from: this._addresses[0], + to: this._addresses[0], + value: '0', + }), + 0, + ); + } + logUtils.warn('Done mining the minimum number of blocks.'); + } } |