Skip to content

Commit

Permalink
optimize and fix known block gen flaky race conditions (#226)
Browse files Browse the repository at this point in the history
* optimize and fix known block gen flaky race conditions

* optimized waitForWalletBalanceGTE
  • Loading branch information
fuxingloh authored May 16, 2021
1 parent e609025 commit c329eb7
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 27 deletions.
16 changes: 9 additions & 7 deletions packages/jellyfish-api-core/__tests__/category/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('masternode', () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
await container.waitForWalletBalanceGTE(300)

await setup()
})
Expand All @@ -21,8 +20,6 @@ describe('masternode', () => {
})

async function setup (): Promise<void> {
await container.generate(100)

const from = await container.call('getnewaddress')
await createToken(from, 'DBTC', 200)

Expand All @@ -42,18 +39,19 @@ describe('masternode', () => {
tradeable: true,
collateralAddress: address
}
await container.waitForWalletBalanceGTE(101)
await container.call('createtoken', [metadata])
await container.generate(25)
await container.generate(1)

await container.call('minttokens', [`${amount.toString()}@${symbol}`])
await container.generate(25)
await container.generate(1)
}

async function accountToAccount (symbol: string, amount: number, from: string, _to = ''): Promise<string> {
const to = _to !== '' ? _to : await container.call('getnewaddress')

await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }])
await container.generate(25)
await container.generate(1)

return to
}
Expand Down Expand Up @@ -414,8 +412,12 @@ describe('masternode', () => {
it('should listAccountHistory with options depth', async () => {
await waitForExpect(async () => {
const depth = 10
const height = await container.getBlockCount()
const accountHistories = await client.account.listAccountHistory('mine', { depth })
expect(accountHistories.length).toBe(depth + 1) // plus 1 to include zero index

for (const accountHistory of accountHistories) {
expect(accountHistory.blockHeight).toBeGreaterThanOrEqual(height - depth)
}
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('masternode', () => {
await container.call('utxostoaccount', [payload])
await container.call('minttokens', [`2000@${symbol}`])

await container.generate(25)
await container.generate(1)
}

describe('addPoolLiquidity', () => {
Expand All @@ -80,7 +80,7 @@ describe('masternode', () => {
const tokenBAddress = await container.call('getnewaddress')
await container.call('sendtokenstoaddress', [{}, { [tokenAAddress]: ['10@DFI'] }])
await container.call('sendtokenstoaddress', [{}, { [tokenBAddress]: ['200@DDAI'] }])
await container.generate(25)
await container.generate(1)

const shareAddress = await container.call('getnewaddress')
const data = await client.poolpair.addPoolLiquidity({
Expand All @@ -97,13 +97,13 @@ describe('masternode', () => {
const tokenBAddress = await container.call('getnewaddress')
await container.call('sendtokenstoaddress', [{}, { [tokenAAddress]: ['10@DFI'] }])
await container.call('sendtokenstoaddress', [{}, { [tokenBAddress]: ['200@DDAI'] }])
await container.generate(25)
await container.generate(1)

const txid = await container.call('sendmany', ['', {
[tokenAAddress]: 10,
[tokenBAddress]: 20
}])
await container.generate(2)
await container.generate(1)

const utxos = await container.call('listunspent')
const inputs = utxos.filter((utxo: any) => utxo.txid === txid).map((utxo: any) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/jellyfish-api-core/__tests__/category/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ describe('masternode', () => {
beforeAll(async () => {
await createToken('DTEST')
await createToken('DABC')
await container.generate(2)
await container.generate(1)

const tokens = await client.token.listTokens()
for (const k in tokens) {
Expand Down Expand Up @@ -334,7 +334,7 @@ describe('masternode', () => {
await createToken('DBTC', { isDAT: true })
await createToken('DNOTMINT', { mintable: false })
await createToken('DNOTTRAD', { tradeable: false })
await container.generate(3)
await container.generate(1)
})

it('should listTokens', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/jellyfish-api-core/src/category/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class Account {
*
* @param {OwnerType} [owner='mine'] single account ID (CScript or address) or reserved words 'mine' to list history for all owned accounts or 'all' to list whole DB
* @param {AccountHistoryOptions} [options]
* @param {number} [options.maxBlockHeight] Optional height to iterate from (downto genesis block), (default = chaintip).
* @param {number} [options.maxBlockHeight] Optional height to iterate from (down to genesis block), (default = chaintip).
* @param {number} [options.depth] Maximum depth, from the genesis block is the default
* @param {boolean} [options.no_rewards] Filter out rewards
* @param {string} [options.token] Filter by token
Expand Down
2 changes: 1 addition & 1 deletion packages/testcontainers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('master node pos minting', () => {
})

it('should wait until coinbase maturity with spendable balance', async () => {
await container.generate(100)
await container.waitForWalletCoinbaseMaturity()

await waitForExpect(async () => {
const info = await container.getMintingInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,27 @@ export class MasterNodeRegTestContainer extends RegTestContainer {
/**
* Wait for master node wallet coin to be mature for spending.
*
* A coinbase transaction must be 100 blocks deep before you can spend its outputs.
* This is a safeguard to prevent outputs that originate
* from the coinbase transaction from becoming un-spendable
* (in the event the mined block moves out of the active chain due to a fork).
* A coinbase transaction must be 100 blocks deep before you can spend its outputs. This is a
* safeguard to prevent outputs that originate from the coinbase transaction from becoming
* un-spendable (in the event the mined block moves out of the active chain due to a fork).
*
* @param {number} [timeout=90000] in ms
*/
async waitForWalletCoinbaseMaturity (timeout = 90000): Promise<void> {
return await this.waitForCondition(async () => {
await this.generate(1)
const count = await this.getBlockCount()
return count > 100
if (count > 100) {
return true
}
await this.generate(1)
return false
}, timeout, 1)
}

/**
* Wait for in wallet balance to be greater than an amount.
* This allow test that require fund to wait for fund to be filled up before running the tests.
* This method will trigger block generate to get to the required balance faster.
*
* @param {number} balance to wait for in wallet to be greater than or equal
* @param {number} [timeout=30000] in ms
Expand All @@ -109,8 +114,12 @@ export class MasterNodeRegTestContainer extends RegTestContainer {
async waitForWalletBalanceGTE (balance: number, timeout = 30000): Promise<void> {
return await this.waitForCondition(async () => {
const getbalance = await this.call('getbalance')
return getbalance >= balance
}, timeout)
if (getbalance >= balance) {
return true
}
await this.generate(1)
return false
}, timeout, 1)
}

/**
Expand Down
4 changes: 0 additions & 4 deletions packages/testing/__tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ describe('utxosToAccount', () => {
})

describe('accountToAccount', () => {
beforeAll(async () => {
await container.generate(100)
})

it('should accountToAccount', async () => {
const symbol = 'DAD'
const from = await container.call('getnewaddress')
Expand Down

0 comments on commit c329eb7

Please sign in to comment.