Skip to content

Commit

Permalink
fix: add waitForBlockchainSync (#859)
Browse files Browse the repository at this point in the history

Fixes: #858
  • Loading branch information
krobi64 authored Dec 5, 2018
1 parent 30093ca commit 4f06f00
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 30 deletions.
14 changes: 7 additions & 7 deletions scripts/check_bitcoind_reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@

import { bitcoindClients, resetBitcoinServers } from '../tests/helpers/bitcoin'

const { btcdClientA }: any = bitcoindClients()
const { bitcoinCoreClientA }: any = bitcoindClients()

const main = async () => {
console.log('Stopping bitcoind container...')
await resetBitcoinServers()

let count = await btcdClientA.getBlockCount()
let count = await bitcoinCoreClientA.getBlockCount()
console.log(`initial blocksize: ${count}`)

await btcdClientA.generate(101)
await bitcoinCoreClientA.generate(101)

count = await btcdClientA.getBlockCount()
count = await bitcoinCoreClientA.getBlockCount()
console.log(`after generating 101 blocks: ${count}`)

console.log('Stopping bitcoind container...')
await resetBitcoinServers()

count = await btcdClientA.getBlockCount()
count = await bitcoinCoreClientA.getBlockCount()
console.log(`block count after stop: ${count}`)

await btcdClientA.generate(11)
await bitcoinCoreClientA.generate(11)

count = await btcdClientA.getBlockCount()
count = await bitcoinCoreClientA.getBlockCount()
console.log(`block count after generating 11 blocks: ${count}`)
}

Expand Down
12 changes: 6 additions & 6 deletions tests/functional/claim_with_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ const createClaim = pipeP(
signVerifiableClaim,
)

const { btcdClientA, btcdClientB }: any = bitcoindClients()
const { bitcoinCoreClientA, bitcoinCoreClientB }: any = bitcoindClients()

describe('A user can successfully submit a claim into the po.et network', async (assert: any) => {
await resetBitcoinServers()
await btcdClientB.addNode(btcdClientA.host, 'add')
await bitcoinCoreClientB.addNode(bitcoinCoreClientA.host, 'add')
await delay(5 * 1000)

const dbA = await createDatabase(PREFIX_A)
const serverA = await app({
BITCOIN_URL: btcdClientA.host,
BITCOIN_URL: bitcoinCoreClientA.host,
API_PORT: NODE_A_PORT,
MONGODB_DATABASE: dbA.settings.tempDbName,
MONGODB_USER: dbA.settings.tempDbUser,
Expand All @@ -54,7 +54,7 @@ describe('A user can successfully submit a claim into the po.et network', async

const dbB = await createDatabase(PREFIX_B)
const serverB = await app({
BITCOIN_URL: btcdClientB.host,
BITCOIN_URL: bitcoinCoreClientB.host,
API_PORT: NODE_B_PORT,
MONGODB_DATABASE: dbB.settings.tempDbName,
MONGODB_USER: dbB.settings.tempDbUser,
Expand All @@ -64,7 +64,7 @@ describe('A user can successfully submit a claim into the po.et network', async
})

// Make sure node A has regtest coins to pay for transactions.
await ensureBitcoinBalance(btcdClientA)
await ensureBitcoinBalance(bitcoinCoreClientA)

// Allow everything to finish starting.
await delay(5 * 1000)
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('A user can successfully submit a claim into the po.et network', async
await delay(parseInt(process.env.ANCHOR_INTERVAL_IN_SECONDS || '10', 10) * 1000 * 2)

// mine N confirmation blocks on bitcoindA.
await btcdClientA.generate(parseInt(process.env.CONFIRMATION_BLOCKS || '1', 10))
await bitcoinCoreClientA.generate(parseInt(process.env.CONFIRMATION_BLOCKS || '1', 10))

// Wait for claim batches to be read from the blockchain.
await delay(parseInt(process.env.READ_DIRECTORY_INTERVAL_IN_SECONDS || '5', 10) * 1000 * 3)
Expand Down
30 changes: 21 additions & 9 deletions tests/functional/transaction_timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { allPass, is, isNil, lensPath, not, path, pipe, pipeP, view } from 'ramd
import { describe } from 'riteway'

import { issuer, privateKey } from '../helpers/Keys'
import { ensureBitcoinBalance, bitcoindClients, resetBitcoinServers } from '../helpers/bitcoin'
import {
ensureBitcoinBalance,
bitcoindClients,
resetBitcoinServers,
waitForBlockchainSync,
waitForBlockchainsToSync,
} from '../helpers/bitcoin'
import { delayInSeconds, runtimeId, setUpServerAndDb } from '../helpers/utils'
import { getWork, postWork } from '../helpers/works'

Expand Down Expand Up @@ -32,7 +38,7 @@ const createClaim = pipeP(
signVerifiableClaim,
)

const { btcdClientA, btcdClientB }: any = bitcoindClients()
const { bitcoinCoreClientA, bitcoinCoreClientB }: any = bitcoindClients()

const blockHash = lensPath(['anchor', 'blockHash'])
const blockHeight = lensPath(['anchor', 'blockHeight'])
Expand All @@ -48,15 +54,17 @@ const hasValidTxId = allPass([is(String), lengthIsGreaterThan0])

describe('Transaction timout will reset the transaction id for the claim', async assert => {
await resetBitcoinServers()
await btcdClientB.addNode(btcdClientA.host, 'add')
await bitcoinCoreClientB.addNode(bitcoinCoreClientA.host, 'add')

await delayInSeconds(5)

const { db, server } = await setUpServerAndDb({ PREFIX, NODE_PORT, blockchainSettings })

// Make sure node A has regtest coins to pay for transactions.
await ensureBitcoinBalance(btcdClientA)
await btcdClientA.setNetworkActive(false)
const generatedBlockHeight = 101
await ensureBitcoinBalance(bitcoinCoreClientA, generatedBlockHeight)
await waitForBlockchainsToSync(generatedBlockHeight, [bitcoinCoreClientA, bitcoinCoreClientB])
await bitcoinCoreClientA.setNetworkActive(false)

// Allow everything to finish starting.
await delayInSeconds(5)
Expand Down Expand Up @@ -99,10 +107,14 @@ describe('Transaction timout will reset the transaction id for the claim', async
expected: true,
})

await btcdClientB.generate(blockchainSettings.MAXIMUM_TRANSACTION_AGE_IN_BLOCKS + 1)
await btcdClientA.setNetworkActive(true)
await bitcoinCoreClientB.generate(blockchainSettings.MAXIMUM_TRANSACTION_AGE_IN_BLOCKS + 1)
const targetHeight = 102 + blockchainSettings.MAXIMUM_TRANSACTION_AGE_IN_BLOCKS
const waitForTargetHeight = waitForBlockchainSync(targetHeight)
await waitForTargetHeight(bitcoinCoreClientB)
await bitcoinCoreClientA.setNetworkActive(true)
await waitForTargetHeight(bitcoinCoreClientA)

await delayInSeconds((blockchainSettings.PURGE_STALE_TRANSACTIONS_INTERVAL_IN_SECONDS + 5) * 2)
await delayInSeconds(blockchainSettings.PURGE_STALE_TRANSACTIONS_INTERVAL_IN_SECONDS + 5)

const secondResponse = await getWorkFromNode(claim.id)
const secondGet = await secondResponse.json()
Expand All @@ -122,7 +134,7 @@ describe('Transaction timout will reset the transaction id for the claim', async
expected: true,
})

await btcdClientA.generate(1)
await bitcoinCoreClientA.generate(1)
await delayInSeconds(
blockchainSettings.BATCH_CREATION_INTERVAL_IN_SECONDS +
blockchainSettings.READ_DIRECTORY_INTERVAL_IN_SECONDS,
Expand Down
41 changes: 33 additions & 8 deletions tests/helpers/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/* tslint:disable:no-relative-imports */
import BitcoinCore = require('bitcoin-core')
const Client = require('bitcoin-core')
import { delay } from '../helpers/utils'
import { LoggingConfiguration, Configuration, loadConfigurationWithDefaults } from 'Configuration'
import { createModuleLogger } from 'Helpers/Logging'
import * as Pino from 'pino'
import {delay, delayInSeconds} from '../helpers/utils'

const BITCOIND_A = 'bitcoind-1'
const BITCOIND_B = 'bitcoind-2'

const loggingConfiguration: LoggingConfiguration = loadConfigurationWithDefaults()
const logger: Pino.Logger = createModuleLogger(loggingConfiguration, __dirname)

const createClient = (host: string) =>
new Client({
host,
Expand All @@ -15,22 +22,40 @@ const createClient = (host: string) =>
})

const clientSingleton = (hostA: string = BITCOIND_A, hostB: string = BITCOIND_B) => {
const btcdClientA: any = null
const btcdClientB: any = null
const bitcoinCoreClientA: any = null
const bitcoinCoreClientB: any = null

return () => ({
btcdClientA: btcdClientA || createClient(hostA),
btcdClientB: btcdClientB || createClient(hostB),
bitcoinCoreClientA: bitcoinCoreClientA || createClient(hostA),
bitcoinCoreClientB: bitcoinCoreClientB || createClient(hostB),
})
}
export const waitForBlockchainsToSync = async (
targetBlockHeight: number,
bitcoinClients: ReadonlyArray<BitcoinCore>,
): Promise<void> => {
const waitForTargetHeight = waitForBlockchainSync(targetBlockHeight)
await Promise.all(bitcoinClients.map(waitForTargetHeight))
}

export const waitForBlockchainSync = (targetBlockHeight: number) =>
async (bitcoinClient: BitcoinCore): Promise<void> => {
logger.info(`Waiting for ${targetBlockHeight}`)
let { blocks } = await bitcoinClient.getBlockchainInfo()
while (blocks < targetBlockHeight) {
await delayInSeconds(10)
blocks = (await bitcoinClient.getBlockchainInfo()).blocks
logger.info(`currentHeight: ${blocks}`)
}
}

export const bitcoindClients = clientSingleton(process.env.BITCOIN_URL, process.env.BITCOIN_URL_B)

export const resetBitcoinServers = async () => {
const { btcdClientA, btcdClientB }: any = bitcoindClients()
const { bitcoinCoreClientA, bitcoinCoreClientB }: any = bitcoindClients()

await btcdClientA.stop()
await btcdClientB.stop()
await bitcoinCoreClientA.stop()
await bitcoinCoreClientB.stop()
await delay(5 * 1000)
}

Expand Down

0 comments on commit 4f06f00

Please sign in to comment.