Skip to content

Commit

Permalink
chore: playing with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-sanderson authored and komret committed Dec 18, 2024
1 parent 00011db commit c1c198b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 36 deletions.
57 changes: 44 additions & 13 deletions packages/suite/src/utils/wallet/__tests__/utxoSortingUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { sortUtxos } from '../utxoSortingUtils';

const UTXOS = [
testMocks.getUtxo({ amount: '1', blockHeight: undefined, txid: 'txid1', vout: 0 }),
testMocks.getUtxo({ amount: '1', blockHeight: undefined, txid: 'txid2', vout: 1 }),
testMocks.getUtxo({ amount: '2', blockHeight: undefined, txid: 'txid2', vout: 1 }),
testMocks.getUtxo({ amount: '2', blockHeight: 1, txid: 'txid2', vout: 0 }),
testMocks.getUtxo({ amount: '2', blockHeight: 2, txid: 'txid3', vout: 0 }),
];
Expand All @@ -15,28 +15,59 @@ const ACCOUNT_TRANSACTIONS = [
testMocks.getWalletTransaction({ txid: 'txid3', blockTime: 2 }),
];

describe('sortUtxos', () => {
it('should sort UTXOs by newest first', () => {
const findTx = (txid: string) => ACCOUNT_TRANSACTIONS.find(tx => tx.txid === txid);

describe(sortUtxos.name, () => {
it('sorts UTXOs by newest first', () => {
const sortedUtxos = sortUtxos(UTXOS, 'newestFirst', ACCOUNT_TRANSACTIONS);
expect(sortedUtxos).toEqual([UTXOS[3], UTXOS[1], UTXOS[2], UTXOS[0]]);
expect(
sortedUtxos.map(it => [
it.blockHeight ?? findTx(it.txid)?.blockTime,
`${it.txid}:${it.vout}`, // for stable sorting
]),
).toEqual([
[2, 'txid3:0'],
[1, 'txid2:1'],
[1, 'txid2:0'],
[undefined, 'txid1:0'],
]);
});

it('should sort UTXOs by oldest first', () => {
it('sorts UTXOs by oldest first', () => {
const sortedUtxos = sortUtxos(UTXOS, 'oldestFirst', ACCOUNT_TRANSACTIONS);
expect(sortedUtxos).toEqual([UTXOS[0], UTXOS[2], UTXOS[1], UTXOS[3]]);
expect(
sortedUtxos.map(it => [
it.blockHeight ?? findTx(it.txid)?.blockTime,
`${it.txid}:${it.vout}`, // for stable sorting
]),
).toEqual([
[undefined, 'txid1:0'],
[1, 'txid2:0'],
[1, 'txid2:1'],
[2, 'txid3:0'],
]);
});

it('sorts by size, largest first', () => {
const sortedUtxos = sortUtxos(UTXOS.slice(0, 2), 'largestFirst', ACCOUNT_TRANSACTIONS);
expect(sortedUtxos.map(it => it.amount)).toEqual(['2', '1']);
});

it('should sort UTXOs by largest first', () => {
const sortedUtxos = sortUtxos(UTXOS, 'largestFirst', ACCOUNT_TRANSACTIONS);
expect(sortedUtxos).toEqual([UTXOS[3], UTXOS[2], UTXOS[1], UTXOS[0]]);
it('sorts by size, smallest first', () => {
const sortedUtxos = sortUtxos(UTXOS.slice(0, 2), 'smallestFirst', ACCOUNT_TRANSACTIONS);
expect(sortedUtxos.map(it => it.amount)).toEqual(['1', '2']);
});

it('should sort UTXOs by smallest first', () => {
const sortedUtxos = sortUtxos(UTXOS, 'smallestFirst', ACCOUNT_TRANSACTIONS);
expect(sortedUtxos).toEqual([UTXOS[0], UTXOS[1], UTXOS[2], UTXOS[3]]);
it('sorts by secondary sorting by `txid` and `vout` in case of same values', () => {
const sortedUtxos = sortUtxos(UTXOS.slice(1, 4), 'smallestFirst', ACCOUNT_TRANSACTIONS);
expect(sortedUtxos.map(it => `${it.txid}:${it.vout}`)).toEqual([
'txid2:0',
'txid2:1',
'txid3:0',
]);
});

it('should return the original array if utxoSorting is undefined', () => {
it('returns the original array if utxoSorting is undefined', () => {
const sortedUtxos = sortUtxos(UTXOS, undefined, ACCOUNT_TRANSACTIONS);
expect(sortedUtxos).toEqual(UTXOS);
});
Expand Down
41 changes: 18 additions & 23 deletions packages/suite/src/utils/wallet/utxoSortingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,27 @@ const performSecondarySorting: UtxoSortingFunction = (a, b) => {
return secondaryComparison;
};

const sortFromLargestToSmallest: UtxoSortingFunctionWithContext = () => (a, b) => {
const comparisonResult = new BigNumber(b.amount).comparedTo(new BigNumber(a.amount));
const wrapSecondarySorting =
(sortFunction: UtxoSortingFunctionWithContext): UtxoSortingFunctionWithContext =>
context =>
(a, b) => {
const result = sortFunction(context)(a, b);

if (result !== 0) {
return result;
}

if (comparisonResult === 0) {
return performSecondarySorting(a, b);
}
};

return comparisonResult;
};
const sortFromLargestToSmallest: UtxoSortingFunctionWithContext = () => (a, b) =>
new BigNumber(b.amount).comparedTo(new BigNumber(a.amount));

const sortFromNewestToOldest: UtxoSortingFunctionWithContext =
({ accountTransactions }) =>
(a, b) => {
let valueA;
let valueB;
if (a.blockHeight > 0 && b.blockHeight > 0) {
valueA = a.blockHeight;
valueB = b.blockHeight;
return b.blockHeight - a.blockHeight;
} else {
// Pending transactions do not have blockHeight, so we must use blockTime of the transaction instead.
const getBlockTime = (txid: string) => {
Expand All @@ -44,31 +47,23 @@ const sortFromNewestToOldest: UtxoSortingFunctionWithContext =

return transaction?.blockTime ?? 0;
};
valueA = getBlockTime(a.txid);
valueB = getBlockTime(b.txid);
}

const comparisonResult = valueB - valueA;

if (comparisonResult === 0) {
return performSecondarySorting(a, b);
return getBlockTime(b.txid) - getBlockTime(a.txid);
}

return comparisonResult;
};

const utxoSortMap: Record<UtxoSorting, UtxoSortingFunctionWithContext> = {
largestFirst: sortFromLargestToSmallest,
largestFirst: wrapSecondarySorting(sortFromLargestToSmallest),
smallestFirst:
context =>
(...params) =>
sortFromLargestToSmallest(context)(...params) * -1,
wrapSecondarySorting(sortFromLargestToSmallest)(context)(...params) * -1,

newestFirst: sortFromNewestToOldest,
newestFirst: wrapSecondarySorting(sortFromNewestToOldest),
oldestFirst:
context =>
(...params) =>
sortFromNewestToOldest(context)(...params) * -1,
wrapSecondarySorting(sortFromNewestToOldest)(context)(...params) * -1,
};

export const sortUtxos = (
Expand Down

0 comments on commit c1c198b

Please sign in to comment.