Skip to content

Commit

Permalink
chore(packages/jellyfish-api-core): fix descendants test flakiness wi…
Browse files Browse the repository at this point in the history
…th a deterministic setup (#1997)

#### What this PR does / why we need it:

Use a deterministic setup for `getMempoolDescendants` to prevent test
flakiness.

Fixes part of #1771
  • Loading branch information
fuxingloh authored Jan 25, 2023
1 parent 61dd9db commit a8a60da
Showing 1 changed file with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,32 @@ describe('Transactions without descendants', () => {
describe('Transactions with descendants', () => {
const container = new MasterNodeRegTestContainer()
const testing = Testing.create(container)
let txIdWithDescendants: string

beforeAll(async () => {
await testing.container.start()
await testing.container.waitForWalletCoinbaseMaturity()
txIdWithDescendants = await getTxIdWithDescendants()
})

/**
* Reliably create Transaction with descendants by using the same UTXO for another transaction.
*/
async function getTxIdWithDescendants (): Promise<string> {
const txId = await testing.rpc.wallet.sendToAddress('mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU', 0.003)
for (let i = 0; i < 10; i++) {
await testing.rpc.wallet.sendToAddress('mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU', 0.003)
}
const address = await testing.address(1)
const txId = await testing.rpc.wallet.sendToAddress(address, 20100)
const utxos = [{
txid: txId,
vout: 0
}]
await testing.rpc.token.createToken({
collateralAddress: address,
isDAT: false,
mintable: false,
name: 'DESC',
symbol: 'DESC',
tradeable: false
}, utxos)
return txId
}

Expand All @@ -63,11 +78,11 @@ describe('Transactions with descendants', () => {
})

it('should return JSON object if verbose is true', async () => {
const txIdWithDescendants = await getTxIdWithDescendants()
const mempoolDescendants = await testing.rpc.blockchain.getMempoolDescendants(txIdWithDescendants, true)

const keys = Object.keys(mempoolDescendants)
expect(keys.length).toBeGreaterThan(0)
expect(keys.length).toStrictEqual(1)

for (const key of keys) {
expect(mempoolDescendants[key]).toStrictEqual({
fees: expect.any(Object),
Expand All @@ -84,28 +99,28 @@ describe('Transactions with descendants', () => {
ancestorsize: expect.any(BigNumber),
ancestorfees: expect.any(BigNumber),
wtxid: expect.any(String),
depends: expect.any(Array),
spentby: expect.any(Array),
depends: [
txIdWithDescendants
],
spentby: [],
'bip125-replaceable': expect.any(Boolean)
})
}
})

it('should return array of transaction ids if verbose is false', async () => {
const txIdWithDescendants = await getTxIdWithDescendants()
const mempoolDescendants = await testing.rpc.blockchain.getMempoolDescendants(txIdWithDescendants, false)
expect(mempoolDescendants.length).toBeGreaterThan(0)
for (const descendantId of mempoolDescendants) {
expect(descendantId).toStrictEqual(expect.stringMatching(/^[0-9a-f]{64}$/))
}

expect(mempoolDescendants).toStrictEqual([
expect.stringMatching(/^[0-9a-f]{64}$/)
])
})

it('should return array of transaction ids if verbose is undefined', async () => {
const txIdWithDescendants = await getTxIdWithDescendants()
const mempoolDescendants = await testing.rpc.blockchain.getMempoolDescendants(txIdWithDescendants)
expect(mempoolDescendants.length).toBeGreaterThan(0)
for (const descendantId of mempoolDescendants) {
expect(descendantId).toStrictEqual(expect.stringMatching(/^[0-9a-f]{64}$/))
}

expect(mempoolDescendants).toStrictEqual([
expect.stringMatching(/^[0-9a-f]{64}$/)
])
})
})

0 comments on commit a8a60da

Please sign in to comment.