Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration test for getHistoricalTxs() #477

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions tests/integration/wallet/sync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { refreshChainData } from '../../../scripts/global.js';
import { COIN } from '../../../scripts/chain_params.js';
import { flushPromises } from '@vue/test-utils';
import { HistoricalTxType } from '../../../scripts/historical_tx.js';

vi.mock('../../../scripts/network/network_manager.js');

Expand Down Expand Up @@ -222,7 +223,134 @@ describe('Wallet sync tests', () => {
await mineBlocks(1);
expect(walletHD.getCurrentAddress()).toBe(newAddress);
});
it('correctly updates historical txs', async () => {
// Keep track of previous historical txs
let prevIds = walletHD.getHistoricalTxs().map((hTx) => {
return hTx.id;
});
/**
* @type {HistoricalTx[]}
*/
let diffCache = [].concat(walletHD.getHistoricalTxs());

// 1) Receive a transaction to the current address
let addr = walletHD.getCurrentAddress();
let blockHeight = getNetwork().getBlockCount() + 1;
await createAndSendTransaction(walletLegacy, addr, 0.01 * 10 ** 8);

// Sanity check: Before mining the block the history is unchanged (receiving from external wallet)
getHistDiff(walletHD.getHistoricalTxs(), prevIds, 0);
checkHistPersistence(walletHD.getHistoricalTxs(), diffCache);

// Mine and assert that a new tx has been added
await mineBlocks(1);
let diff = getHistDiff(walletHD.getHistoricalTxs(), prevIds, 1);
checkHistPersistence(walletHD.getHistoricalTxs(), diffCache);
prevIds.push(diff[0].id);
diffCache.push(diff[0]);
checkHistDiff(diff[0], {
blockHeight,
isToSelf: false,
type: HistoricalTxType.RECEIVED,
receivers: [addr, walletLegacy.getCurrentAddress()],
});

// 2) This time send a transaction (To external wallet)
blockHeight = getNetwork().getBlockCount() + 1;
addr = walletLegacy.getCurrentAddress();
await createAndSendTransaction(walletHD, addr, 0.5 * 10 ** 8);

// Since walletHD created the tx, it must already be in its history... even without mining a block
diff = getHistDiff(walletHD.getHistoricalTxs(), prevIds, 1);
checkHistPersistence(walletHD.getHistoricalTxs(), diffCache);
checkHistDiff(diff[0], {
blockHeight: -1, // pending block height
isToSelf: false,
type: HistoricalTxType.SENT,
receivers: [addr],
});

await mineBlocks(1);
diff = getHistDiff(walletHD.getHistoricalTxs(), prevIds, 1);
checkHistPersistence(walletHD.getHistoricalTxs(), diffCache);
checkHistDiff(diff[0], {
blockHeight,
isToSelf: false,
type: HistoricalTxType.SENT,
receivers: [walletLegacy.getCurrentAddress()],
});
diffCache.push(diff[0]);
prevIds.push(diff[0].id);

// 3) Create a self transaction
blockHeight = getNetwork().getBlockCount() + 1;
addr = walletHD.getCurrentAddress();
await createAndSendTransaction(walletHD, addr, 0.2 * 10 ** 8);

diff = getHistDiff(walletHD.getHistoricalTxs(), prevIds, 1);
checkHistPersistence(walletHD.getHistoricalTxs(), diffCache);
checkHistDiff(diff[0], {
blockHeight: -1, // pending block height
isToSelf: true,
type: HistoricalTxType.SENT,
receivers: [addr],
});

await mineBlocks(1);
diff = getHistDiff(walletHD.getHistoricalTxs(), prevIds, 1);
checkHistPersistence(walletHD.getHistoricalTxs(), diffCache);
checkHistDiff(diff[0], {
blockHeight,
isToSelf: true,
type: HistoricalTxType.SENT,
receivers: [addr],
});
});
afterAll(() => {
vi.clearAllMocks();
});

/**
* @param {HistoricalTx[]} historicalTxs
* @param {string[]} txIds
* @param {number} expectedLength - expected size of the diff
* @returns {HistoricalTx[]} the history difference
*/
function getHistDiff(historicalTxs, txIds, expectedLength) {
const diff = historicalTxs.filter((hTx) => {
return !txIds.includes(hTx.id);
});
expect(diff.length).toBe(expectedLength);
return diff;
}

/**
* @typedef {Object} SimplifiedHistoricalTx
* @property {number} blockHeight
* @property {HistoricalTxType} type
* @property {boolean} isToSelf
* @property {string[]} receivers - a SUBSET of the receivers of a historical tx
*/

/**
* @param {HistoricalTx} historicalTx
* @param {SimplifiedHistoricalTx} exp
*/
function checkHistDiff(historicalTx, exp) {
expect(historicalTx.blockHeight).toStrictEqual(exp.blockHeight);
expect(historicalTx.type).toStrictEqual(exp.type);
expect(historicalTx.isToSelf).toStrictEqual(exp.isToSelf);
for (let r of exp.receivers) {
expect(historicalTx.receivers.includes(r)).toBeTruthy();
}
}
/**
* @param {HistoricalTx[]} historicalTxs
* @param {HistoricalTx[]} diffCache
*/
function checkHistPersistence(historicalTxs, diffCache) {
for (let hTx of diffCache) {
expect(historicalTxs.includes(hTx)).toBeTruthy();
}
}
});