Skip to content

Commit

Permalink
tests: always wait for tx received when injecting funds
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroferreira1 committed Nov 2, 2023
1 parent 7df3d06 commit 933c829
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 132 deletions.
6 changes: 3 additions & 3 deletions __tests__/integration/atomic_swap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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);
Expand Down
164 changes: 59 additions & 105 deletions __tests__/integration/hathorwallet_facade.test.js

Large diffs are not rendered by default.

36 changes: 22 additions & 14 deletions __tests__/integration/hathorwallet_others.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) });
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -635,6 +634,7 @@ describe('consolidateUtxos', () => {
{ token }
);
await waitForTxReceived(hWallet1, cleanTx.hash);
await waitForTxReceived(hWallet2, cleanTx.hash);
await waitUntilNextTimestamp(hWallet1, cleanTx.hash);
}

Expand All @@ -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');
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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',
Expand Down
9 changes: 6 additions & 3 deletions __tests__/integration/helpers/genesis-wallet.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -65,7 +66,7 @@ export class GenesisWalletHelper {
* @returns {Promise<BaseTransactionResponse>}
* @private
*/
async _injectFunds(address, value, options = {}) {
async _injectFunds(destinationWallet, address, value, options = {}) {
try {
const result = await this.hWallet.sendTransaction(
address,
Expand All @@ -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) {
Expand Down Expand Up @@ -107,16 +109,17 @@ 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]
* @param {number} [options.waitTimeout] Optional timeout for the websocket confirmation.
* Passing 0 here skips this waiting.
* @returns {Promise<BaseTransactionResponse>}
*/
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);
}

/**
Expand Down
1 change: 0 additions & 1 deletion __tests__/integration/helpers/wallet.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 1 addition & 2 deletions __tests__/integration/storage/leveldb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 1 addition & 4 deletions __tests__/integration/storage/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}

Expand Down

0 comments on commit 933c829

Please sign in to comment.