From 933c829c0b2bb2217000291a7aa33b28cabb0bd0 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Thu, 2 Nov 2023 16:15:05 -0300 Subject: [PATCH] tests: always wait for tx received when injecting funds --- __tests__/integration/atomic_swap.test.js | 6 +- .../integration/hathorwallet_facade.test.js | 164 +++++++----------- .../integration/hathorwallet_others.test.js | 36 ++-- .../helpers/genesis-wallet.helper.js | 9 +- .../integration/helpers/wallet.helper.js | 1 - __tests__/integration/storage/leveldb.test.js | 3 +- __tests__/integration/storage/storage.test.js | 5 +- 7 files changed, 92 insertions(+), 132 deletions(-) diff --git a/__tests__/integration/atomic_swap.test.js b/__tests__/integration/atomic_swap.test.js index aac23951b..f1307e2bc 100644 --- a/__tests__/integration/atomic_swap.test.js +++ b/__tests__/integration/atomic_swap.test.js @@ -24,7 +24,7 @@ describe('partial tx proposal', () => { const hWallet2 = await generateWalletHelper(); // Injecting funds and creating a new custom token - const txI = await GenesisWalletHelper.injectFunds(await hWallet1.getAddressAtIndex(0), 103); + const txI = await GenesisWalletHelper.injectFunds(hWallet1, await hWallet1.getAddressAtIndex(0), 103); const { hash: token1Uid } = await createTokenHelper( hWallet1, 'Token1', @@ -33,7 +33,7 @@ describe('partial tx proposal', () => { ); // Injecting funds and creating a new custom token - await GenesisWalletHelper.injectFunds(await hWallet2.getAddressAtIndex(0), 10); + await GenesisWalletHelper.injectFunds(hWallet2, await hWallet2.getAddressAtIndex(0), 10); const { hash: token2Uid } = await createTokenHelper( hWallet2, 'Token2', @@ -99,7 +99,7 @@ describe('partial tx proposal', () => { expect(tx.hash).toBeDefined(); await waitForTxReceived(hWallet1, tx.hash); - await delay(1000); // This transaction seems to take longer than usual to complete + await waitForTxReceived(hWallet2, tx.hash); // Get the balance states before the exchange const w1HTRAfter = await hWallet1.getBalance(HATHOR_TOKEN_CONFIG.uid); diff --git a/__tests__/integration/hathorwallet_facade.test.js b/__tests__/integration/hathorwallet_facade.test.js index 22f232946..15bda2a7e 100644 --- a/__tests__/integration/hathorwallet_facade.test.js +++ b/__tests__/integration/hathorwallet_facade.test.js @@ -35,7 +35,6 @@ const sampleNftData = 'ipfs://bafybeiccfclkdtucu6y4yc5cpr6y3yuinr67svmii46v5cfcr describe('getTxById', () => { afterEach(async () => { - console.log('A'); await stopAllWallets(); await GenesisWalletHelper.clearListeners(); }); @@ -48,8 +47,7 @@ describe('getTxById', () => { // Injecting some funds on this wallet const fundDestinationAddress = await hWallet.getAddressAtIndex(0); - const tx1 = await GenesisWalletHelper.injectFunds(fundDestinationAddress, 10); - await waitForTxReceived(hWallet, tx1.hash); + const tx1 = await GenesisWalletHelper.injectFunds(hWallet, fundDestinationAddress, 10); // Validating the full history increased in one expect(Object.keys(await hWallet.getFullHistory())).toHaveLength(1); @@ -134,8 +132,7 @@ describe('getTxById', () => { // Test case: custom token with funds const address = await hWallet.getAddressAtIndex(0); // Inject 10 HTR into the wallet - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(address, 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, address, 10); // Generate a random amount of new tokens const newTokenAmount = getRandomInt(1000, 10); // Create a new custom token with the generated amount @@ -308,9 +305,6 @@ describe('start', () => { it('should start a wallet with a transaction history', async () => { // Send a transaction to one of the wallet's addresses const walletData = precalculationHelpers.test.getPrecalculatedWallet(); - const injectAddress = walletData.addresses[0]; - const injectValue = getRandomInt(10, 1); - const injectionTx = await GenesisWalletHelper.injectFunds(injectAddress, injectValue); // Start the wallet const walletConfig = { @@ -324,6 +318,10 @@ describe('start', () => { await hWallet.start(); await waitForWalletReady(hWallet); + const injectAddress = walletData.addresses[0]; + const injectValue = getRandomInt(10, 1); + const injectionTx = await GenesisWalletHelper.injectFunds(hWallet, injectAddress, injectValue); + // Validate that it has transactions const txHistory = await hWallet.getTxHistory(); expect(txHistory).toHaveLength(1); @@ -403,8 +401,7 @@ describe('start', () => { seed: walletData.words, preCalculatedAddresses: walletData.addresses, }); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 2); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 2); const { hash: tokenUid } = await createTokenHelper( hWallet, 'Dedicated Wallet Token', @@ -513,8 +510,7 @@ describe('start', () => { await expect(hWallet.getUtxos()).resolves.toHaveProperty('total_utxos_available', 0); // Generating a transaction and validating it shows correctly - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(1), 1); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(1), 1); await expect(hWallet.getBalance(HATHOR_TOKEN_CONFIG.uid)).resolves.toMatchObject([ expect.objectContaining({ @@ -536,7 +532,7 @@ describe('start', () => { }); // Adding funds to it - await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); /* * XXX: The code branches that require a PIN would not be achievable without this hack that @@ -660,8 +656,7 @@ describe('addresses methods', () => { // Expect the "current address" to change when a transaction arrives at the current one currentAddress = await hWallet.getCurrentAddress(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(currentAddress.address, 1); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, currentAddress.address, 1); const currentAfterTx = await hWallet.getCurrentAddress(); expect(currentAfterTx).toMatchObject({ index: currentAddress.index + 1, @@ -738,8 +733,7 @@ describe('getBalance', () => { // Generating one transaction to validate its effects const injectedValue = getRandomInt(10, 2); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), injectedValue); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), injectedValue); // Validating the transaction effects const balance1 = await hWallet.getBalance(HATHOR_TOKEN_CONFIG.uid); @@ -769,8 +763,7 @@ describe('getBalance', () => { }); // Creating a new custom token - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); const newTokenAmount = getRandomInt(1000, 10); const { hash: tokenUid } = await createTokenHelper( hWallet, @@ -812,8 +805,7 @@ describe('getFullHistory', () => { // Injecting some funds on this wallet const fundDestinationAddress = await hWallet.getAddressAtIndex(0); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(fundDestinationAddress, 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, fundDestinationAddress, 10); // Validating the full history increased in one await expect(hWallet.storage.store.historyCount()).resolves.toEqual(1); @@ -892,11 +884,11 @@ describe('getFullHistory', () => { it('should return full history (custom token)', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds( + await GenesisWalletHelper.injectFunds( + hWallet, await hWallet.getAddressAtIndex(0), 10 ); - await waitForTxReceived(hWallet, fundsHash); const tokenName = 'Full History Token'; const tokenSymbol = 'FHT'; const { hash: tokenUid } = await createTokenHelper( @@ -965,10 +957,10 @@ describe('getTxBalance', () => { it('should get tx balance', async () => { const hWallet = await generateWalletHelper(); const { hash: tx1Hash } = await GenesisWalletHelper.injectFunds( + hWallet, await hWallet.getAddressAtIndex(0), 10 ); - await waitForTxReceived(hWallet, tx1Hash); // Validating tx balance for a transaction with a single token (htr) const tx1 = await hWallet.getTx(tx1Hash); @@ -1056,8 +1048,7 @@ describe('getFullTxById', () => { it('should download an existing transaction from the fullnode', async () => { const hWallet = await generateWalletHelper(); - const tx1 = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, tx1.hash); + const tx1 = await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); const fullTx = await hWallet.getFullTxById(tx1.hash); expect(fullTx.success).toStrictEqual(true); @@ -1093,8 +1084,7 @@ describe('getTxConfirmationData', () => { it('should download confirmation data for an existing transaction from the fullnode', async () => { const hWallet = await generateWalletHelper(); - const tx1 = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, tx1.hash); + const tx1 = await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); const confirmationData = await hWallet.getTxConfirmationData(tx1.hash); @@ -1130,8 +1120,7 @@ describe('graphvizNeighborsQuery', () => { it('should download graphviz neighbors data for a existing transaction from the fullnode', async () => { const hWallet = await generateWalletHelper(); - const tx1 = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, tx1.hash); + const tx1 = await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); const neighborsData = await hWallet.graphvizNeighborsQuery(tx1.hash, 'funds', 1); expect(neighborsData).toMatch(/digraph {/); @@ -1139,8 +1128,7 @@ describe('graphvizNeighborsQuery', () => { it('should capture errors when graphviz returns error', async () => { const hWallet = await generateWalletHelper(); - const tx1 = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, tx1.hash); + const tx1 = await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); await expect(hWallet.graphvizNeighborsQuery(tx1.hash)).rejects.toThrowError('Request failed with status code 500'); }); @@ -1162,8 +1150,7 @@ describe('sendTransaction', () => { it('should send HTR transactions', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); // Sending a transaction inside the same wallet const tx1 = await hWallet.sendTransaction(await hWallet.getAddressAtIndex(2), 6); @@ -1217,8 +1204,7 @@ describe('sendTransaction', () => { it('should send custom token transactions', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); const { hash: tokenUid } = await createTokenHelper( hWallet, 'Token to Send', @@ -1255,6 +1241,7 @@ describe('sendTransaction', () => { } ); await waitForTxReceived(hWallet, tx2Hash); + await waitForTxReceived(gWallet, tx2Hash); // Balance was reduced htrBalance = await hWallet.getBalance(tokenUid); @@ -1270,8 +1257,7 @@ describe('sendTransaction', () => { const mhWallet1 = await generateMultisigWalletHelper({ walletIndex: 0 }); const mhWallet2 = await generateMultisigWalletHelper({ walletIndex: 1 }); const mhWallet3 = await generateMultisigWalletHelper({ walletIndex: 2 }); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await mhWallet1.getAddressAtIndex(0), 10); - await waitForTxReceived(mhWallet1, fundsHash); + await GenesisWalletHelper.injectFunds(mhWallet1, await mhWallet1.getAddressAtIndex(0), 10); /* * Building tx proposal: @@ -1339,8 +1325,7 @@ describe('sendManyOutputsTransaction', () => { it('should send simple HTR transactions', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 100); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 100); // Single input and single output const rawSimpleTx = await hWallet.sendManyOutputsTransaction( @@ -1417,8 +1402,7 @@ describe('sendManyOutputsTransaction', () => { it('should send transactions with multiple tokens', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); const { hash: tokenUid } = await createTokenHelper( hWallet, 'Multiple Tokens Tk', @@ -1479,8 +1463,7 @@ describe('sendManyOutputsTransaction', () => { it('should respect timelocks', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); // Defining timelocks (milliseconds) and timestamps (seconds) const startTime = Date.now().valueOf(); @@ -1551,6 +1534,7 @@ describe('sendManyOutputsTransaction', () => { // Confirm that now the balance is available const sendTx = await hWallet.sendTransaction(await hWallet.getAddressAtIndex(4), 8); expect(sendTx).toHaveProperty('hash'); + await waitForTxReceived(hWallet, sendTx.hash); }); }); @@ -1564,8 +1548,7 @@ describe('createNewToken', () => { // Creating the wallet with the funds const hWallet = await generateWalletHelper(); const addr0 = await hWallet.getAddressAtIndex(0); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(addr0, 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, addr0, 10); // Creating the new token const newTokenResponse = await hWallet.createNewToken( @@ -1593,8 +1576,7 @@ describe('createNewToken', () => { // Creating the wallet with the funds const hWallet = await generateWalletHelper(); const addr0 = await hWallet.getAddressAtIndex(0); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(addr0, 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, addr0, 10); // Creating the new token const destinationAddress = await hWallet.getAddressAtIndex(4); @@ -1609,7 +1591,6 @@ describe('createNewToken', () => { } ); await waitForTxReceived(hWallet, tokenUid); - await delay(500); // Validating the tokens are on the correct addresses const { utxos: utxosTokens } = await hWallet.getUtxos({ token: tokenUid }); expect(utxosTokens).toContainEqual( @@ -1626,8 +1607,7 @@ describe('createNewToken', () => { // Creating the wallet with the funds const hWallet = await generateWalletHelper(); const addr0 = await hWallet.getAddressAtIndex(0); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(addr0, 1); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, addr0, 1); // Creating the new token const newTokenResponse = await hWallet.createNewToken( @@ -1643,6 +1623,7 @@ describe('createNewToken', () => { // Checking for authority outputs on the transaction const authorityOutputs = newTokenResponse.outputs.filter(o => transaction.isAuthorityOutput(o)); expect(authorityOutputs).toHaveLength(0); + await waitForTxReceived(hWallet, newTokenResponse.hash); }); it('Create token using mint/melt address', async () => { @@ -1651,8 +1632,7 @@ describe('createNewToken', () => { const addr0 = await hWallet.getAddressAtIndex(0); const addr10 = await hWallet.getAddressAtIndex(10); const addr11 = await hWallet.getAddressAtIndex(11); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(addr0, 1); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, addr0, 1); // Creating the new token const newTokenResponse = await hWallet.createNewToken( @@ -1703,8 +1683,7 @@ describe('createNewToken', () => { const addr0 = await hWallet.getAddressAtIndex(0); const addr2_0 = await hWallet2.getAddressAtIndex(0); const addr2_1 = await hWallet2.getAddressAtIndex(1); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(addr0, 1); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, addr0, 1); // Error creating token with external address await expect(hWallet.createNewToken( @@ -1745,6 +1724,7 @@ describe('createNewToken', () => { // Validating the creation tx expect(newTokenResponse).toHaveProperty('hash'); await waitForTxReceived(hWallet, newTokenResponse.hash); + await waitForTxReceived(hWallet2, newTokenResponse.hash); // Validating a new mint authority was created by default const authorityOutputs = newTokenResponse.outputs.filter( @@ -1781,8 +1761,7 @@ describe('mintTokens', () => { it('should mint new tokens', async () => { // Setting up the custom token const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); const { hash: tokenUid } = await createTokenHelper( hWallet, 'Token to Mint', @@ -1857,6 +1836,7 @@ describe('mintTokens', () => { }); expect(mintResponse4.hash).toBeDefined(); await waitForTxReceived(hWallet, mintResponse4.hash); + await waitForTxReceived(hWallet2, mintResponse4.hash); // Validating there is a correct reference to the custom token expect(mintResponse4).toHaveProperty('tokens.length', 1); @@ -1892,8 +1872,7 @@ describe('mintTokens', () => { // Setting up scenario const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); const { hash: tokenUid } = await createTokenHelper( hWallet, 'Token to Mint', @@ -1947,8 +1926,7 @@ describe('meltTokens', () => { it('should melt tokens', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); // Creating the token const { hash: tokenUid } = await createTokenHelper( @@ -2006,6 +1984,7 @@ describe('meltTokens', () => { allowExternalMeltAuthorityAddress: true }); await waitForTxReceived(hWallet, meltResponse3.hash); + await waitForTxReceived(hWallet2, meltResponse3.hash); // Validating a new melt authority was created by default const authorityOutputs3 = meltResponse3.outputs.filter( @@ -2037,8 +2016,7 @@ describe('meltTokens', () => { // Setting up scenario const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 20); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 20); const { hash: tokenUid } = await createTokenHelper( hWallet, 'Token to Melt', @@ -2058,7 +2036,6 @@ describe('meltTokens', () => { meltResponse = await hWallet.meltTokens(tokenUid, 100); expectedHtrFunds += 1; await waitForTxReceived(hWallet, meltResponse.hash); - await delay(100); expect(await getHtrBalance(hWallet)).toBe(expectedHtrFunds); // Melting between 1.00 and 2.00 tokens recovers 0.01 HTR @@ -2105,8 +2082,7 @@ describe('delegateAuthority', () => { it('should delegate authority between wallets', async () => { // Creating a Custom Token on wallet 1 - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet1.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet1, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet1, await hWallet1.getAddressAtIndex(0), 10); const { hash: tokenUid } = await createTokenHelper( hWallet1, 'Delegate Token', @@ -2127,14 +2103,6 @@ describe('delegateAuthority', () => { await waitForTxReceived(hWallet1, delegateMintTxId); await waitForTxReceived(hWallet2, delegateMintTxId); - /* - * XXX: Authority Token delegation usually takes longer than usual to be reflected on the local - * caches. This forced recalculation will be executed before each authority validation below - * to avoid a small possibility of the caches being obsolete at assertion time. - */ - await delay(100); - await hWallet1.storage.processHistory(); - // Expect wallet 1 to still have one mint authority let authorities1 = await hWallet1.getMintAuthority(tokenUid); expect(authorities1).toHaveLength(1); @@ -2143,7 +2111,6 @@ describe('delegateAuthority', () => { authorities: TOKEN_MINT_MASK }); // Expect wallet 2 to also have one mint authority - await hWallet2.storage.processHistory(); let authorities2 = await hWallet2.getMintAuthority(tokenUid); expect(authorities2).toHaveLength(1); expect(authorities2[0]).toMatchObject({ @@ -2162,7 +2129,6 @@ describe('delegateAuthority', () => { await waitForTxReceived(hWallet2, delegateMeltTxId); // Expect wallet 1 to still have one melt authority - await hWallet1.storage.processHistory(); authorities1 = await hWallet1.getMeltAuthority(tokenUid); expect(authorities1).toHaveLength(1); expect(authorities1[0]).toMatchObject({ @@ -2170,7 +2136,6 @@ describe('delegateAuthority', () => { authorities: TOKEN_MELT_MASK }); // Expect wallet 2 to also have one melt authority - await hWallet2.storage.processHistory(); authorities2 = await hWallet2.getMeltAuthority(tokenUid); expect(authorities2).toHaveLength(1); expect(authorities2[0]).toMatchObject({ @@ -2181,8 +2146,7 @@ describe('delegateAuthority', () => { it('should delegate authority to another wallet without keeping one', async () => { // Creating a Custom Token on wallet 1 - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet1.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet1, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet1, await hWallet1.getAddressAtIndex(0), 10); const { hash: tokenUid } = await createTokenHelper( hWallet1, 'Delegate Token', @@ -2206,8 +2170,7 @@ describe('delegateAuthority', () => { // TODO: The type of errors on mint and melt are different. They should have a standard. // Validating success on mint tokens from Wallet 2 - const { hash: fundsHash2 } = await GenesisWalletHelper.injectFunds(await hWallet2.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet2, fundsHash2); + await GenesisWalletHelper.injectFunds(hWallet2, await hWallet2.getAddressAtIndex(0), 10); const mintTxWallet2 = await hWallet2.mintTokens(tokenUid, 100); expect(mintTxWallet2).toHaveProperty('hash'); await waitForTxReceived(hWallet2, mintTxWallet2.hash); @@ -2232,8 +2195,7 @@ describe('delegateAuthority', () => { it('should delegate mint authority to another wallet while keeping one', async () => { // Creating a Custom Token on wallet 1 - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet1.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet1, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet1, await hWallet1.getAddressAtIndex(0), 10); const { hash: tokenUid } = await createTokenHelper( hWallet1, 'Delegate Token 2', @@ -2302,8 +2264,7 @@ describe('delegateAuthority', () => { it('should delegate melt authority to another wallet while keeping one', async () => { // Creating a Custom Token on wallet 1 - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet1.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet1, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet1, await hWallet1.getAddressAtIndex(0), 10); const { hash: tokenUid } = await createTokenHelper( hWallet1, 'Delegate Token 2', @@ -2379,8 +2340,7 @@ describe('destroyAuthority', () => { it('should destroy mint authorities', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); const { hash: tokenUid } = await createTokenHelper( hWallet, 'Token for MintDestroy', @@ -2425,8 +2385,7 @@ describe('destroyAuthority', () => { it('should destroy melt authorities', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); const { hash: tokenUid } = await createTokenHelper( hWallet, 'Token for MeltDestroy', @@ -2477,8 +2436,7 @@ describe('createNFT', () => { it('should create an NFT with mint/melt authorities', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); // Creating one NFT with default authorities const nftTx = await hWallet.createNFT( @@ -2549,8 +2507,7 @@ describe('createNFT', () => { it('should create an NFT without authorities', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); // Creating one NFT without authorities, and with a specific destination address const nftTx = await hWallet.createNFT( @@ -2584,8 +2541,7 @@ describe('createNFT', () => { const addr0 = await hWallet.getAddressAtIndex(0); const addr10 = await hWallet.getAddressAtIndex(10); const addr11 = await hWallet.getAddressAtIndex(11); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(addr0, 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, addr0, 10); // Creating the new token const newTokenResponse = await hWallet.createNFT( @@ -2637,8 +2593,7 @@ describe('createNFT', () => { const addr0 = await hWallet.getAddressAtIndex(0); const addr2_0 = await hWallet2.getAddressAtIndex(0); const addr2_1 = await hWallet2.getAddressAtIndex(1); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(addr0, 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, addr0, 10); // Error creating token with external address await expect(hWallet.createNFT( @@ -2682,6 +2637,7 @@ describe('createNFT', () => { // Validating the creation tx expect(newTokenResponse).toHaveProperty('hash'); await waitForTxReceived(hWallet, newTokenResponse.hash); + await waitForTxReceived(hWallet2, newTokenResponse.hash); // Validating a new mint authority was created by default const authorityOutputs = newTokenResponse.outputs.filter( @@ -2717,8 +2673,7 @@ describe('getToken methods', () => { it('should get the correct responses for a valid token', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); // Validating `getTokenDetails` for custom token not in this wallet await expect(hWallet.getTokenDetails(fakeTokenUid)).rejects.toThrow('Unknown token'); @@ -2798,8 +2753,7 @@ describe('signTx', () => { const hWallet = await generateWalletHelper(); const addr0 = await hWallet.getAddressAtIndex(0); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(addr0, 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, addr0, 10); const { hash: tokenUid } = await createTokenHelper( hWallet, @@ -2859,8 +2813,7 @@ describe('getTxHistory', () => { expect(txHistory).toHaveLength(0); // HTR transaction incoming - const tx1 = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, tx1.hash); + const tx1 = await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); txHistory = await hWallet.getTxHistory(); expect(txHistory).toStrictEqual([ expect.objectContaining({ @@ -2878,6 +2831,7 @@ describe('getTxHistory', () => { await waitUntilNextTimestamp(hWallet, tx2.hash); const tx3 = await hWallet.sendTransaction(await gWallet.getAddressAtIndex(0), 3); await waitForTxReceived(hWallet, tx3.hash); + await waitForTxReceived(gWallet, tx3.hash); txHistory = await hWallet.getTxHistory(); expect(txHistory).toHaveLength(3); @@ -2899,8 +2853,7 @@ describe('getTxHistory', () => { it('should show custom token transactions in correct order', async () => { const hWallet = await generateWalletHelper(); - const { hash: fundsHash } = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - await waitForTxReceived(hWallet, fundsHash); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); let txHistory = await hWallet.getTxHistory({ token_id: fakeTokenUid, @@ -2937,6 +2890,7 @@ describe('getTxHistory', () => { { token: tokenUid } ); await waitForTxReceived(hWallet, tx2Hash); + await waitForTxReceived(gWallet, tx2Hash); txHistory = await hWallet.getTxHistory({ token_id: tokenUid }); expect(txHistory).toHaveLength(3); diff --git a/__tests__/integration/hathorwallet_others.test.js b/__tests__/integration/hathorwallet_others.test.js index 7330a4a33..2e054e983 100644 --- a/__tests__/integration/hathorwallet_others.test.js +++ b/__tests__/integration/hathorwallet_others.test.js @@ -40,8 +40,7 @@ describe('getAddressInfo', () => { }); // Validating address after 1 transaction - await GenesisWalletHelper.injectFunds(addr0, 10); - await delay(500); + await GenesisWalletHelper.injectFunds(hWallet, addr0, 10); await expect(hWallet.getAddressInfo(addr0)).resolves.toMatchObject({ total_amount_received: 10, total_amount_sent: 0, @@ -152,7 +151,7 @@ describe('getAddressInfo', () => { const addr1Custom = await hWalletCustom.getAddressAtIndex(1); // Creating custom token - await GenesisWalletHelper.injectFunds(addr0Custom, 1); + await GenesisWalletHelper.injectFunds(hWalletCustom, addr0Custom, 1); const { hash: tokenUid } = await createTokenHelper( hWalletCustom, 'getAddressInfo Token', @@ -213,6 +212,7 @@ describe('getTxAddresses', () => { changeAddress: WALLET_CONSTANTS.genesis.addresses[0] }); await waitForTxReceived(hWallet, tx.hash); + await waitForTxReceived(gWallet, tx.hash); // Validating the method results const decodedTx = await hWallet.getTx(tx.hash); @@ -269,7 +269,7 @@ describe('getAvailableUtxos', () => { expect(utxoGenResult).toStrictEqual({ done: true, value: undefined }); // Inject a transaction and validate the results - const tx1 = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); + const tx1 = await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); // Get correct results for a single transaction utxoGenerator = await hWallet.getAvailableUtxos(); @@ -300,8 +300,8 @@ describe('getAvailableUtxos', () => { * @type HathorWallet */ const hWallet = await generateWalletHelper(); - const tx1 = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); - const tx2 = await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(1), 5); + const tx1 = await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); + const tx2 = await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(1), 5); // Validate that on the address that received tx1, the UTXO is listed let utxoGenerator = await hWallet.getAvailableUtxos({ filter_address: await hWallet.getAddressAtIndex(0) }); @@ -331,7 +331,7 @@ describe('getAvailableUtxos', () => { * @type HathorWallet */ const hWallet = await generateWalletHelper(); - await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 10); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 10); const { hash: tokenUid } = await createTokenHelper( hWallet, 'getAvailableUtxos Token', @@ -420,7 +420,7 @@ describe('getUtxosForAmount', () => { it('should work on a wallet containing a single tx', async () => { const addr0 = await hWallet.getAddressAtIndex(0); const addr1 = await hWallet.getAddressAtIndex(1); - const tx1 = await GenesisWalletHelper.injectFunds(addr0, 10); + const tx1 = await GenesisWalletHelper.injectFunds(hWallet, addr0, 10); fundTx1hash = tx1.hash; // No change amount @@ -463,7 +463,7 @@ describe('getUtxosForAmount', () => { it('should work on a wallet containing multiple txs', async () => { const addr0 = await hWallet.getAddressAtIndex(0); const addr1 = await hWallet.getAddressAtIndex(1); - const tx2 = await GenesisWalletHelper.injectFunds(addr1, 20); + const tx2 = await GenesisWalletHelper.injectFunds(hWallet, addr1, 20); /* * Since we don't know which order the transactions will be stored on the history, @@ -572,8 +572,7 @@ describe('getUtxosForAmount', () => { it('should not retrieve utxos marked as selected', async () => { // Retrieving the utxo's data and marking it as selected const addr = await hWallet.getAddressAtIndex(11); - await GenesisWalletHelper.injectFunds(addr, 100); - await delay(100); + await GenesisWalletHelper.injectFunds(hWallet, addr, 100); const utxosAddr1 = await hWallet.getUtxos({ filter_address: addr }); const singleUtxoAddr1 = utxosAddr1.utxos[0]; @@ -601,7 +600,7 @@ describe('consolidateUtxos', () => { beforeAll(async () => { hWallet1 = await generateWalletHelper(); hWallet2 = await generateWalletHelper(); - await GenesisWalletHelper.injectFunds(await hWallet2.getAddressAtIndex(0), 110); + await GenesisWalletHelper.injectFunds(hWallet2, await hWallet2.getAddressAtIndex(0), 110); const { hash: tokenUid } = await createTokenHelper( hWallet2, 'Consolidate Token', @@ -635,6 +634,7 @@ describe('consolidateUtxos', () => { { token } ); await waitForTxReceived(hWallet1, cleanTx.hash); + await waitForTxReceived(hWallet2, cleanTx.hash); await waitUntilNextTimestamp(hWallet1, cleanTx.hash); } @@ -649,6 +649,7 @@ describe('consolidateUtxos', () => { { address: await hWallet1.getAddressAtIndex(1), value: 5, token: HATHOR_TOKEN_CONFIG.uid }, ]); await waitForTxReceived(hWallet1, fundTx.hash); + await waitForTxReceived(hWallet2, fundTx.hash); await expect(hWallet1.consolidateUtxos(hWallet2.getAddressAtIndex(0))) .rejects.toThrow('not owned by this wallet'); @@ -663,6 +664,7 @@ describe('consolidateUtxos', () => { { address: await hWallet1.getAddressAtIndex(1), value: 5, token: HATHOR_TOKEN_CONFIG.uid }, ]); await waitForTxReceived(hWallet1, fundTx.hash); + await waitForTxReceived(hWallet2, fundTx.hash); // Sending transaction and validating the method response const consolidateTx = await hWallet1.consolidateUtxos( @@ -718,6 +720,7 @@ describe('consolidateUtxos', () => { { address: await hWallet1.getAddressAtIndex(4), value: 50, token: tokenHash }, ]); await waitForTxReceived(hWallet1, fundTx.hash); + await waitForTxReceived(hWallet2, fundTx.hash); // Sending transaction and validating the method response const consolidateTx = await hWallet1.consolidateUtxos( @@ -779,6 +782,7 @@ describe('consolidateUtxos', () => { { address: addr2, value: 3, token: HATHOR_TOKEN_CONFIG.uid }, ]); await waitForTxReceived(hWallet1, fundTx.hash); + await waitForTxReceived(hWallet2, fundTx.hash); // Sending transaction and validating the method response const consolidateTx = await hWallet1.consolidateUtxos( @@ -823,6 +827,7 @@ describe('consolidateUtxos', () => { { address: addr1, value: 5, token: HATHOR_TOKEN_CONFIG.uid }, ]); await waitForTxReceived(hWallet1, fundTx.hash); + await waitForTxReceived(hWallet2, fundTx.hash); // Sending transaction and validating the method response const consolidateTx = await hWallet1.consolidateUtxos( @@ -861,6 +866,7 @@ describe('consolidateUtxos', () => { { address: addr2, value: 5, token: HATHOR_TOKEN_CONFIG.uid }, ]); await waitForTxReceived(hWallet1, fundTx.hash); + await waitForTxReceived(hWallet2, fundTx.hash); // Sending transaction and validating the method response const consolidateTx = await hWallet1.consolidateUtxos( @@ -899,10 +905,11 @@ describe('consolidateUtxos', () => { { address: addr2, value: 40, token: HATHOR_TOKEN_CONFIG.uid }, ]); await waitForTxReceived(hWallet1, fundTx.hash); + await waitForTxReceived(hWallet2, fundTx.hash); // Sending transaction and validating the method response const consolidateTx = await hWallet1.consolidateUtxos( - await await hWallet1.getAddressAtIndex(4), + await hWallet1.getAddressAtIndex(4), { token: HATHOR_TOKEN_CONFIG.uid, amount_bigger_than: 2, @@ -940,6 +947,7 @@ describe('consolidateUtxos', () => { { address: await hWallet1.getAddressAtIndex(0), value: 1, token: tokenHash }, ]); await waitForTxReceived(hWallet1, fundTx.hash); + await waitForTxReceived(hWallet2, fundTx.hash); // We should now have 4 utxos on wallet1 for this custom token expect(await hWallet1.getUtxos({ token: tokenHash })).toHaveProperty('total_utxos_available', 4); @@ -990,7 +998,7 @@ describe('getAuthorityUtxos', () => { it('should find one authority utxo', async () => { // Creating the token - await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(0), 1); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 1); const { hash: tokenUid } = await createTokenHelper( hWallet, 'getAuthorityUtxos Token', diff --git a/__tests__/integration/helpers/genesis-wallet.helper.js b/__tests__/integration/helpers/genesis-wallet.helper.js index 1227b88ec..8d5cc2405 100644 --- a/__tests__/integration/helpers/genesis-wallet.helper.js +++ b/__tests__/integration/helpers/genesis-wallet.helper.js @@ -57,6 +57,7 @@ export class GenesisWalletHelper { /** * Internal method to send HTR to another wallet's address. + * @param {HathorWallet} destinationWallet Wallet object that we are sending the funds to * @param {string} address * @param {number} value * @param [options] @@ -65,7 +66,7 @@ export class GenesisWalletHelper { * @returns {Promise} * @private */ - async _injectFunds(address, value, options = {}) { + async _injectFunds(destinationWallet, address, value, options = {}) { try { const result = await this.hWallet.sendTransaction( address, @@ -80,6 +81,7 @@ export class GenesisWalletHelper { } await waitForTxReceived(this.hWallet, result.hash, options.waitTimeout); + await waitForTxReceived(destinationWallet, result.hash, options.waitTimeout); await waitUntilNextTimestamp(this.hWallet, result.hash); return result; } catch (e) { @@ -107,6 +109,7 @@ export class GenesisWalletHelper { /** * An easy way to send HTR to another wallet's address for testing. + * @param {HathorWallet} destinationWallet Wallet object that we are sending the funds to * @param {string} address * @param {number} value * @param [options] @@ -114,9 +117,9 @@ export class GenesisWalletHelper { * Passing 0 here skips this waiting. * @returns {Promise} */ - static async injectFunds(address, value, options) { + static async injectFunds(destinationWallet, address, value, options) { const instance = await GenesisWalletHelper.getSingleton(); - return instance._injectFunds(address, value, options); + return instance._injectFunds(destinationWallet, address, value, options); } /** diff --git a/__tests__/integration/helpers/wallet.helper.js b/__tests__/integration/helpers/wallet.helper.js index 1b4dd3ea0..28f36114a 100644 --- a/__tests__/integration/helpers/wallet.helper.js +++ b/__tests__/integration/helpers/wallet.helper.js @@ -244,7 +244,6 @@ export async function createTokenHelper(hWallet, name, symbol, amount, options) const tokenUid = newTokenResponse.hash; await waitForTxReceived(hWallet, tokenUid); await waitUntilNextTimestamp(hWallet, tokenUid); - await delay(1000); return newTokenResponse; } diff --git a/__tests__/integration/storage/leveldb.test.js b/__tests__/integration/storage/leveldb.test.js index 217c86af5..460e66391 100644 --- a/__tests__/integration/storage/leveldb.test.js +++ b/__tests__/integration/storage/leveldb.test.js @@ -65,9 +65,8 @@ describe('LevelDB persistent store', () => { expect(Object.keys(hWallet.getFullHistory())).toHaveLength(0); // Injecting some funds on this wallet - await GenesisWalletHelper.injectFunds(await hWallet.getAddressAtIndex(1), 10); + await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(1), 10); - await delay(100); // Validating the full history increased in one expect(Object.keys(await hWallet.getFullHistory())).toHaveLength(1); diff --git a/__tests__/integration/storage/storage.test.js b/__tests__/integration/storage/storage.test.js index a07558c0f..85c904b8a 100644 --- a/__tests__/integration/storage/storage.test.js +++ b/__tests__/integration/storage/storage.test.js @@ -65,9 +65,7 @@ describe('locked utxos', () => { async function testUnlockWhenSpent(storage, walletData) { const hwallet = await startWallet(storage, walletData); const address = await hwallet.getAddressAtIndex(0); - const tx = await GenesisWalletHelper.injectFunds(address, 1); - await waitForTxReceived(hwallet, tx.hash); - await delay(500); + const tx = await GenesisWalletHelper.injectFunds(hwallet, address, 1); const sendTx = new SendTransaction({ storage: hwallet.storage, @@ -88,7 +86,6 @@ describe('locked utxos', () => { // Send a transaction spending the only utxo on the wallet. const tx1 = await sendTx.runFromMining(); await waitForTxReceived(hwallet, tx1.hash); - await delay(500); await expect(hwallet.storage.isUtxoSelectedAsInput(utxoId)).resolves.toBe(false); }