Skip to content

Commit

Permalink
Explicitly wait for nodes to sync in end-to-end tests (#1752)
Browse files Browse the repository at this point in the history
  • Loading branch information
Asa Oines authored and celo-ci-bot-user committed Nov 19, 2019
1 parent 45f29ee commit 3b15c1d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
8 changes: 5 additions & 3 deletions packages/celotool/src/e2e-tests/governance_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
importGenesis,
initAndStartGeth,
sleep,
waitToFinishSyncing,
} from './utils'

interface MemberSwapper {
Expand Down Expand Up @@ -290,17 +291,17 @@ describe('governance tests', () => {
epoch = new BigNumber(await validators.methods.getEpochSize().call()).toNumber()
assert.equal(epoch, 10)

// Give the nodes time to sync, and time for an epoch transition so we can activate our vote.
// Wait for an epoch transition so we can activate our vote.
let blockNumber: number
do {
blockNumber = await web3.eth.getBlockNumber()
await sleep(0.1)
} while (blockNumber % epoch !== 1)

await activate(validatorAccounts[0])

// Prepare for member swapping.
const groupWeb3 = new Web3('ws://localhost:8555')
await waitToFinishSyncing(groupWeb3)
const groupKit = newKitFromWeb3(groupWeb3)
validators = await groupKit._web3Contracts.getValidators()
const membersToSwap = [validatorAccounts[0], validatorAccounts[1]]
Expand All @@ -309,6 +310,7 @@ describe('governance tests', () => {
// Prepare for key rotation.
const validatorWeb3 = new Web3('http://localhost:8549')
const authorizedWeb3s = [new Web3('ws://localhost:8559'), new Web3('ws://localhost:8561')]
await Promise.all(authorizedWeb3s.map((w) => waitToFinishSyncing(w)))
const authorizedPrivateKeys = [rotation0PrivateKey, rotation1PrivateKey]
const keyRotator = await newKeyRotator(
newKitFromWeb3(validatorWeb3),
Expand Down Expand Up @@ -439,7 +441,7 @@ describe('governance tests', () => {
const expectedScore = adjustmentSpeed
.times(uptime)
.plus(new BigNumber(1).minus(adjustmentSpeed).times(fromFixed(previousScore)))
assert.equal(score.toFixed(), toFixed(expectedScore).toFixed())
assertAlmostEqual(score, toFixed(expectedScore))
}

for (const blockNumber of blockNumbers) {
Expand Down
24 changes: 13 additions & 11 deletions packages/celotool/src/e2e-tests/sync_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
initAndStartGeth,
killInstance,
sleep,
waitToFinishSyncing,
} from './utils'

describe('sync tests', function(this: any) {
Expand All @@ -28,8 +29,6 @@ describe('sync tests', function(this: any) {
await hooks.before()
// Restart validator nodes.
await hooks.restart()
// Give validators time to connect to eachother.
await sleep(40)
const fullInstance = {
name: 'full',
validating: false,
Expand All @@ -40,7 +39,8 @@ describe('sync tests', function(this: any) {
peers: [await getEnode(8545)],
}
await initAndStartGeth(hooks.gethBinaryPath, fullInstance)
await sleep(3)
const web3 = new Web3('http://localhost:8553')
await waitToFinishSyncing(web3)
})

after(hooks.after)
Expand All @@ -66,17 +66,19 @@ describe('sync tests', function(this: any) {

it('should sync the latest block', async () => {
const validatingWeb3 = new Web3(`http://localhost:8545`)
const validatingFirstBlock = await validatingWeb3.eth.getBlock('latest')
await sleep(20)
const validatingLatestBlock = await validatingWeb3.eth.getBlock('latest')
await sleep(3)
const validatingFirstBlock = await validatingWeb3.eth.getBlockNumber()
const syncWeb3 = new Web3(`http://localhost:8555`)
const syncLatestBlock = await syncWeb3.eth.getBlock('latest')
assert.isAbove(validatingLatestBlock.number, 1)
await waitToFinishSyncing(syncWeb3)
// Give the validators time to create more blocks.
await sleep(2)
const validatingLatestBlock = await validatingWeb3.eth.getBlockNumber()
await sleep(1)
const syncLatestBlock = await syncWeb3.eth.getBlockNumber()
assert.isAbove(validatingLatestBlock, 1)
// Assert that the validator is still producing blocks.
assert.isAbove(validatingLatestBlock.number, validatingFirstBlock.number)
assert.isAbove(validatingLatestBlock, validatingFirstBlock)
// Assert that the syncing node has synced with the validator.
assert.isAtLeast(syncLatestBlock.number, validatingLatestBlock.number)
assert.isAtLeast(syncLatestBlock, validatingLatestBlock)
})
})
}
Expand Down
6 changes: 6 additions & 0 deletions packages/celotool/src/e2e-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ const GENESIS_PATH = `${TEST_DIR}/genesis.json`
const NetworkId = 1101
const MonorepoRoot = resolvePath(joinPath(__dirname, '../..', '../..'))

export async function waitToFinishSyncing(web3: any) {
while ((await web3.eth.isSyncing()) || (await web3.eth.getBlockNumber()) === 0) {
await sleep(0.1)
}
}

export function assertAlmostEqual(
actual: BigNumber,
expected: BigNumber,
Expand Down

0 comments on commit 3b15c1d

Please sign in to comment.