aboutsummaryrefslogtreecommitdiffstats
path: root/packages/dev-utils/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/dev-utils/src')
-rw-r--r--packages/dev-utils/src/blockchain_lifecycle.ts43
-rw-r--r--packages/dev-utils/src/web3_factory.ts21
2 files changed, 37 insertions, 27 deletions
diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts
index 9bd65ee5d..a48cb0856 100644
--- a/packages/dev-utils/src/blockchain_lifecycle.ts
+++ b/packages/dev-utils/src/blockchain_lifecycle.ts
@@ -10,8 +10,8 @@ import * as _ from 'lodash';
const MINIMUM_BLOCKS = 3;
export class BlockchainLifecycle {
- private _web3Wrapper: Web3Wrapper;
- private _snapshotIdsStack: number[];
+ private readonly _web3Wrapper: Web3Wrapper;
+ private readonly _snapshotIdsStack: number[];
private _addresses: string[] = [];
private _nodeType: NodeType | undefined;
constructor(web3Wrapper: Web3Wrapper) {
@@ -34,6 +34,12 @@ export class BlockchainLifecycle {
blockNumber = await this._web3Wrapper.getBlockNumberAsync();
}
this._snapshotIdsStack.push(blockNumber);
+ // HACK(albrow) It's possible that we applied a time offset but
+ // the transaction we mined to put that time offset into the
+ // blockchain was reverted. As a workaround, we mine a new dummy
+ // block so that the latest block timestamp accounts for any
+ // possible time offsets.
+ await this._mineDummyBlockAsync();
break;
default:
throw new Error(`Unknown node type: ${nodeType}`);
@@ -59,22 +65,9 @@ export class BlockchainLifecycle {
}
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,
- );
+ await this._mineDummyBlockAsync();
}
logUtils.warn('Done mining the minimum number of blocks.');
}
@@ -84,4 +77,22 @@ export class BlockchainLifecycle {
}
return this._nodeType;
}
+ // Sends a transaction that has no real effect on the state and waits for it
+ // to be mined.
+ private async _mineDummyBlockAsync(): Promise<void> {
+ if (this._addresses.length === 0) {
+ this._addresses = await this._web3Wrapper.getAvailableAddressesAsync();
+ if (this._addresses.length === 0) {
+ throw new Error('No accounts found');
+ }
+ }
+ await this._web3Wrapper.awaitTransactionMinedAsync(
+ await this._web3Wrapper.sendTransactionAsync({
+ from: this._addresses[0],
+ to: this._addresses[0],
+ value: '0',
+ }),
+ 0,
+ );
+ }
}
diff --git a/packages/dev-utils/src/web3_factory.ts b/packages/dev-utils/src/web3_factory.ts
index 362a6c3c6..8e713fa68 100644
--- a/packages/dev-utils/src/web3_factory.ts
+++ b/packages/dev-utils/src/web3_factory.ts
@@ -1,7 +1,10 @@
-import ProviderEngine = require('web3-provider-engine');
-import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
-
-import { EmptyWalletSubprovider, FakeGasEstimateSubprovider, GanacheSubprovider } from '@0xproject/subproviders';
+import {
+ EmptyWalletSubprovider,
+ FakeGasEstimateSubprovider,
+ GanacheSubprovider,
+ RPCSubprovider,
+ Web3ProviderEngine,
+} from '@0xproject/subproviders';
import * as fs from 'fs';
import * as _ from 'lodash';
@@ -16,8 +19,8 @@ export interface Web3Config {
}
export const web3Factory = {
- getRpcProvider(config: Web3Config = {}): ProviderEngine {
- const provider = new ProviderEngine();
+ getRpcProvider(config: Web3Config = {}): Web3ProviderEngine {
+ const provider = new Web3ProviderEngine();
const hasAddresses = _.isUndefined(config.hasAddresses) || config.hasAddresses;
config.shouldUseFakeGasEstimate =
_.isUndefined(config.shouldUseFakeGasEstimate) || config.shouldUseFakeGasEstimate;
@@ -49,11 +52,7 @@ export const web3Factory = {
}),
);
} else {
- provider.addProvider(
- new RpcSubprovider({
- rpcUrl: config.rpcUrl || constants.RPC_URL,
- }),
- );
+ provider.addProvider(new RPCSubprovider(config.rpcUrl || constants.RPC_URL));
}
provider.start();
return provider;