diff --git a/scripts/check_bitcoind_reset.ts b/scripts/check_bitcoind_reset.ts index 7dce0fa4..4b95f467 100755 --- a/scripts/check_bitcoind_reset.ts +++ b/scripts/check_bitcoind_reset.ts @@ -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}`) } diff --git a/tests/functional/claim_with_data.ts b/tests/functional/claim_with_data.ts index 300d8b16..d7bbd5a9 100644 --- a/tests/functional/claim_with_data.ts +++ b/tests/functional/claim_with_data.ts @@ -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, @@ -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, @@ -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) @@ -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) diff --git a/tests/functional/transaction_timeout.ts b/tests/functional/transaction_timeout.ts index 724d0675..acfa32c4 100644 --- a/tests/functional/transaction_timeout.ts +++ b/tests/functional/transaction_timeout.ts @@ -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' @@ -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']) @@ -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) @@ -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() @@ -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, diff --git a/tests/helpers/bitcoin.ts b/tests/helpers/bitcoin.ts index 93e0a058..f2d8557a 100644 --- a/tests/helpers/bitcoin.ts +++ b/tests/helpers/bitcoin.ts @@ -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, @@ -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, +): Promise => { + const waitForTargetHeight = waitForBlockchainSync(targetBlockHeight) + await Promise.all(bitcoinClients.map(waitForTargetHeight)) +} + +export const waitForBlockchainSync = (targetBlockHeight: number) => + async (bitcoinClient: BitcoinCore): Promise => { + 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) }