Skip to content

Commit 4f06f00

Browse files
authored
fix: add waitForBlockchainSync (#859)
Fixes: #858
1 parent 30093ca commit 4f06f00

File tree

4 files changed

+67
-30
lines changed

4 files changed

+67
-30
lines changed

scripts/check_bitcoind_reset.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@
1010

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

13-
const { btcdClientA }: any = bitcoindClients()
13+
const { bitcoinCoreClientA }: any = bitcoindClients()
1414

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

19-
let count = await btcdClientA.getBlockCount()
19+
let count = await bitcoinCoreClientA.getBlockCount()
2020
console.log(`initial blocksize: ${count}`)
2121

22-
await btcdClientA.generate(101)
22+
await bitcoinCoreClientA.generate(101)
2323

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

2727
console.log('Stopping bitcoind container...')
2828
await resetBitcoinServers()
2929

30-
count = await btcdClientA.getBlockCount()
30+
count = await bitcoinCoreClientA.getBlockCount()
3131
console.log(`block count after stop: ${count}`)
3232

33-
await btcdClientA.generate(11)
33+
await bitcoinCoreClientA.generate(11)
3434

35-
count = await btcdClientA.getBlockCount()
35+
count = await bitcoinCoreClientA.getBlockCount()
3636
console.log(`block count after generating 11 blocks: ${count}`)
3737
}
3838

tests/functional/claim_with_data.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ const createClaim = pipeP(
3434
signVerifiableClaim,
3535
)
3636

37-
const { btcdClientA, btcdClientB }: any = bitcoindClients()
37+
const { bitcoinCoreClientA, bitcoinCoreClientB }: any = bitcoindClients()
3838

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

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

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

6666
// Make sure node A has regtest coins to pay for transactions.
67-
await ensureBitcoinBalance(btcdClientA)
67+
await ensureBitcoinBalance(bitcoinCoreClientA)
6868

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

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

105105
// Wait for claim batches to be read from the blockchain.
106106
await delay(parseInt(process.env.READ_DIRECTORY_INTERVAL_IN_SECONDS || '5', 10) * 1000 * 3)

tests/functional/transaction_timeout.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { allPass, is, isNil, lensPath, not, path, pipe, pipeP, view } from 'ramd
44
import { describe } from 'riteway'
55

66
import { issuer, privateKey } from '../helpers/Keys'
7-
import { ensureBitcoinBalance, bitcoindClients, resetBitcoinServers } from '../helpers/bitcoin'
7+
import {
8+
ensureBitcoinBalance,
9+
bitcoindClients,
10+
resetBitcoinServers,
11+
waitForBlockchainSync,
12+
waitForBlockchainsToSync,
13+
} from '../helpers/bitcoin'
814
import { delayInSeconds, runtimeId, setUpServerAndDb } from '../helpers/utils'
915
import { getWork, postWork } from '../helpers/works'
1016

@@ -32,7 +38,7 @@ const createClaim = pipeP(
3238
signVerifiableClaim,
3339
)
3440

35-
const { btcdClientA, btcdClientB }: any = bitcoindClients()
41+
const { bitcoinCoreClientA, bitcoinCoreClientB }: any = bitcoindClients()
3642

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

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

5359
await delayInSeconds(5)
5460

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

5763
// Make sure node A has regtest coins to pay for transactions.
58-
await ensureBitcoinBalance(btcdClientA)
59-
await btcdClientA.setNetworkActive(false)
64+
const generatedBlockHeight = 101
65+
await ensureBitcoinBalance(bitcoinCoreClientA, generatedBlockHeight)
66+
await waitForBlockchainsToSync(generatedBlockHeight, [bitcoinCoreClientA, bitcoinCoreClientB])
67+
await bitcoinCoreClientA.setNetworkActive(false)
6068

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

102-
await btcdClientB.generate(blockchainSettings.MAXIMUM_TRANSACTION_AGE_IN_BLOCKS + 1)
103-
await btcdClientA.setNetworkActive(true)
110+
await bitcoinCoreClientB.generate(blockchainSettings.MAXIMUM_TRANSACTION_AGE_IN_BLOCKS + 1)
111+
const targetHeight = 102 + blockchainSettings.MAXIMUM_TRANSACTION_AGE_IN_BLOCKS
112+
const waitForTargetHeight = waitForBlockchainSync(targetHeight)
113+
await waitForTargetHeight(bitcoinCoreClientB)
114+
await bitcoinCoreClientA.setNetworkActive(true)
115+
await waitForTargetHeight(bitcoinCoreClientA)
104116

105-
await delayInSeconds((blockchainSettings.PURGE_STALE_TRANSACTIONS_INTERVAL_IN_SECONDS + 5) * 2)
117+
await delayInSeconds(blockchainSettings.PURGE_STALE_TRANSACTIONS_INTERVAL_IN_SECONDS + 5)
106118

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

125-
await btcdClientA.generate(1)
137+
await bitcoinCoreClientA.generate(1)
126138
await delayInSeconds(
127139
blockchainSettings.BATCH_CREATION_INTERVAL_IN_SECONDS +
128140
blockchainSettings.READ_DIRECTORY_INTERVAL_IN_SECONDS,

tests/helpers/bitcoin.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
/* tslint:disable:no-relative-imports */
2+
import BitcoinCore = require('bitcoin-core')
23
const Client = require('bitcoin-core')
3-
import { delay } from '../helpers/utils'
4+
import { LoggingConfiguration, Configuration, loadConfigurationWithDefaults } from 'Configuration'
5+
import { createModuleLogger } from 'Helpers/Logging'
6+
import * as Pino from 'pino'
7+
import {delay, delayInSeconds} from '../helpers/utils'
48

59
const BITCOIND_A = 'bitcoind-1'
610
const BITCOIND_B = 'bitcoind-2'
711

12+
const loggingConfiguration: LoggingConfiguration = loadConfigurationWithDefaults()
13+
const logger: Pino.Logger = createModuleLogger(loggingConfiguration, __dirname)
14+
815
const createClient = (host: string) =>
916
new Client({
1017
host,
@@ -15,22 +22,40 @@ const createClient = (host: string) =>
1522
})
1623

1724
const clientSingleton = (hostA: string = BITCOIND_A, hostB: string = BITCOIND_B) => {
18-
const btcdClientA: any = null
19-
const btcdClientB: any = null
25+
const bitcoinCoreClientA: any = null
26+
const bitcoinCoreClientB: any = null
2027

2128
return () => ({
22-
btcdClientA: btcdClientA || createClient(hostA),
23-
btcdClientB: btcdClientB || createClient(hostB),
29+
bitcoinCoreClientA: bitcoinCoreClientA || createClient(hostA),
30+
bitcoinCoreClientB: bitcoinCoreClientB || createClient(hostB),
2431
})
2532
}
33+
export const waitForBlockchainsToSync = async (
34+
targetBlockHeight: number,
35+
bitcoinClients: ReadonlyArray<BitcoinCore>,
36+
): Promise<void> => {
37+
const waitForTargetHeight = waitForBlockchainSync(targetBlockHeight)
38+
await Promise.all(bitcoinClients.map(waitForTargetHeight))
39+
}
40+
41+
export const waitForBlockchainSync = (targetBlockHeight: number) =>
42+
async (bitcoinClient: BitcoinCore): Promise<void> => {
43+
logger.info(`Waiting for ${targetBlockHeight}`)
44+
let { blocks } = await bitcoinClient.getBlockchainInfo()
45+
while (blocks < targetBlockHeight) {
46+
await delayInSeconds(10)
47+
blocks = (await bitcoinClient.getBlockchainInfo()).blocks
48+
logger.info(`currentHeight: ${blocks}`)
49+
}
50+
}
2651

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

2954
export const resetBitcoinServers = async () => {
30-
const { btcdClientA, btcdClientB }: any = bitcoindClients()
55+
const { bitcoinCoreClientA, bitcoinCoreClientB }: any = bitcoindClients()
3156

32-
await btcdClientA.stop()
33-
await btcdClientB.stop()
57+
await bitcoinCoreClientA.stop()
58+
await bitcoinCoreClientB.stop()
3459
await delay(5 * 1000)
3560
}
3661

0 commit comments

Comments
 (0)