Improve reliability of the minimum block number hack for the devnet/geth

This commit is contained in:
Alex Browne 2018-06-28 15:19:43 -07:00
parent 2474d1d2f4
commit d79994fbb2
No known key found for this signature in database
GPG Key ID: 7974B08A447ABE31
3 changed files with 41 additions and 24 deletions

View File

@ -53,9 +53,9 @@ jobs:
- restore_cache: - restore_cache:
keys: keys:
- repo-{{ .Environment.CIRCLE_SHA1 }} - repo-{{ .Environment.CIRCLE_SHA1 }}
# HACK(albrow): we need to sleep 15 seconds to ensure the devnet is # HACK(albrow): we need to sleep 10 seconds to ensure the devnet is
# initialized # initialized
- run: sleep 15 && TEST_PROVIDER=geth yarn wsrun test contracts - run: sleep 10 && TEST_PROVIDER=geth yarn wsrun test contracts
test-rest: test-rest:
docker: docker:
- image: circleci/node:9 - image: circleci/node:9

View File

@ -1,3 +1,4 @@
import { logUtils } from '@0xproject/utils';
import { uniqueVersionIds, Web3Wrapper } from '@0xproject/web3-wrapper'; import { uniqueVersionIds, Web3Wrapper } from '@0xproject/web3-wrapper';
import { includes } from 'lodash'; import { includes } from 'lodash';
@ -6,9 +7,17 @@ enum NodeType {
Ganache = 'GANACHE', 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 { export class BlockchainLifecycle {
private _web3Wrapper: Web3Wrapper; private _web3Wrapper: Web3Wrapper;
private _snapshotIdsStack: number[]; private _snapshotIdsStack: number[];
private _addresses: string[] = [];
constructor(web3Wrapper: Web3Wrapper) { constructor(web3Wrapper: Web3Wrapper) {
this._web3Wrapper = web3Wrapper; this._web3Wrapper = web3Wrapper;
this._snapshotIdsStack = []; this._snapshotIdsStack = [];
@ -21,7 +30,13 @@ export class BlockchainLifecycle {
this._snapshotIdsStack.push(snapshotId); this._snapshotIdsStack.push(snapshotId);
break; break;
case NodeType.Geth: 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); this._snapshotIdsStack.push(blockNumber);
break; break;
default: default:
@ -56,4 +71,25 @@ export class BlockchainLifecycle {
throw new Error(`Unknown client version: ${version}`); 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.');
}
} }

View File

@ -3,7 +3,7 @@ set -e
# Create log directory for Geth # Create log directory for Geth
mkdir -p /var/log mkdir -p /var/log
# Start Geth in background and redirect output to log file # Start Geth and direct output to stdout
/geth \ /geth \
--verbosity 5 \ --verbosity 5 \
--datadir node0/ \ --datadir node0/ \
@ -22,23 +22,4 @@ mkdir -p /var/log
--mine \ --mine \
--etherbase '0xe8816898d851d5b61b7f950627d04d794c07ca37' \ --etherbase '0xe8816898d851d5b61b7f950627d04d794c07ca37' \
--unlock '0xe8816898d851d5b61b7f950627d04d794c07ca37,0x5409ed021d9299bf6814279a6a1411a7e866a631,0x6ecbe1db9ef729cbe972c83fb886247691fb6beb,0xe36ea790bc9d7ab70c55260c66d52b1eca985f84,0xe834ec434daba538cd1b9fe1582052b880bd7e63,0x78dc5d2d739606d31509c31d654056a45185ecb6,0xa8dda8d7f5310e4a9e24f8eba77e091ac264f872,0x06cef8e666768cc40cc78cf93d9611019ddcb628,0x4404ac8bd8f9618d27ad2f1485aa1b2cfd82482d,0x7457d5e02197480db681d3fdf256c7aca21bdc12,0x91c987bf62d25945db517bdaa840a6c661374402' \ --unlock '0xe8816898d851d5b61b7f950627d04d794c07ca37,0x5409ed021d9299bf6814279a6a1411a7e866a631,0x6ecbe1db9ef729cbe972c83fb886247691fb6beb,0xe36ea790bc9d7ab70c55260c66d52b1eca985f84,0xe834ec434daba538cd1b9fe1582052b880bd7e63,0x78dc5d2d739606d31509c31d654056a45185ecb6,0xa8dda8d7f5310e4a9e24f8eba77e091ac264f872,0x06cef8e666768cc40cc78cf93d9611019ddcb628,0x4404ac8bd8f9618d27ad2f1485aa1b2cfd82482d,0x7457d5e02197480db681d3fdf256c7aca21bdc12,0x91c987bf62d25945db517bdaa840a6c661374402' \
--password=node0/password.txt \ --password=node0/password.txt
> /var/log/geth &
# Wait for Geth to unlock the first account
sleep 10
# Send some transactions.
# 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).
/geth --datadir node0/ attach --exec 'eth.sendTransaction({"from": "0x5409ED021D9299bf6814279A6A1411A7e866A631", "to": "0x84bd1cfa409cb0bb9b23b8b1a33515b4ac00a0af", "value": "0x1"})'
sleep 3
/geth --datadir node0/ attach --exec 'eth.sendTransaction({"from": "0x5409ED021D9299bf6814279A6A1411A7e866A631", "to": "0x84bd1cfa409cb0bb9b23b8b1a33515b4ac00a0af", "value": "0x1"})'
sleep 3
/geth --datadir node0/ attach --exec 'eth.sendTransaction({"from": "0x5409ED021D9299bf6814279A6A1411A7e866A631", "to": "0x84bd1cfa409cb0bb9b23b8b1a33515b4ac00a0af", "value": "0x1"})'
# Use tail to re-attach to the log file and actually see the output.
tail -f /var/log/geth