commit 988f551fa38eb48c52b82af897eb1db93826953d
parent e70d28b93941fc22c3298fcce965b42b2311683e
Author: Florian Dold <florian@dold.me>
Date: Fri, 4 Oct 2024 15:18:34 +0200
wallet-core: always return at least 'limit' transactions if available
Diffstat:
1 file changed, 29 insertions(+), 27 deletions(-)
diff --git a/packages/taler-wallet-core/src/transactions.ts b/packages/taler-wallet-core/src/transactions.ts
@@ -26,7 +26,6 @@ import {
AbsoluteTime,
Amounts,
assertUnreachable,
- checkLogicInvariant,
GetTransactionsV2Request,
j2s,
Logger,
@@ -219,16 +218,31 @@ function checkFilterIncludes(
return included;
}
-function addFiltered(
+async function addFiltered(
+ wex: WalletExecutionContext,
+ tx: WalletDbAllStoresReadOnlyTransaction,
req: GetTransactionsV2Request | undefined,
- target: TransactionMetaRecord[],
+ target: Transaction[],
source: TransactionMetaRecord[],
-): void {
+): Promise<number> {
+ let numAdded: number = 0;
for (const mtx of source) {
+ if (req?.limit != null && target.length >= Math.abs(req.limit)) {
+ break;
+ }
if (checkFilterIncludes(req, mtx)) {
- target.push(mtx);
+ const ctx = await getContextForTransaction(wex, mtx.transactionId);
+ const txDetails = await ctx.lookupFullTransaction(tx);
+ // FIXME: This means that in some cases we can return fewer transactions
+ // than requested.
+ if (!txDetails) {
+ continue;
+ }
+ numAdded += 1;
+ target.push(txDetails);
}
}
+ return numAdded;
}
/**
@@ -328,7 +342,6 @@ export async function getTransactionsV2(
const resultTransactions: Transaction[] = [];
await wex.db.runAllStoresReadOnlyTx({}, async (tx) => {
- let mtxs: TransactionMetaRecord[] = [];
let forwards =
transactionsRequest?.limit == null || transactionsRequest.limit >= 0;
let limit =
@@ -341,7 +354,7 @@ export async function getTransactionsV2(
// Fast path for returning *everything* that matches the filter.
// FIXME: We could use the DB for filtering here
const res = await tx.transactionsMeta.indexes.byStatus.getAll();
- addFiltered(transactionsRequest, mtxs, res);
+ await addFiltered(wex, tx, transactionsRequest, resultTransactions, res);
} else if (!forwards) {
// Descending, backwards request.
// Slow implementation. Doing it properly would require using cursors,
@@ -358,15 +371,13 @@ export async function getTransactionsV2(
} else {
start = 0;
}
- let numAdded = 0;
for (let i = start; i < res.length; i++) {
- if (limit != null && numAdded >= limit) {
+ if (limit != null && resultTransactions.length >= limit) {
break;
}
- if (checkFilterIncludes(transactionsRequest, res[i])) {
- mtxs.push(res[i]);
- numAdded += 1;
- }
+ await addFiltered(wex, tx, transactionsRequest, resultTransactions, [
+ res[i],
+ ]);
}
} else {
// Forward request
@@ -379,8 +390,11 @@ export async function getTransactionsV2(
query,
limit,
);
- addFiltered(transactionsRequest, mtxs, res);
- if (res.length === 0 || (limit != null && mtxs.length >= limit)) {
+ if (res.length === 0) {
+ break;
+ }
+ addFiltered(wex, tx, transactionsRequest, resultTransactions, res);
+ if (limit != null && resultTransactions.length >= limit) {
break;
}
// Continue after last result
@@ -390,18 +404,6 @@ export async function getTransactionsV2(
);
}
}
-
- // Lookup full transactions
- for (const mtx of mtxs) {
- const ctx = await getContextForTransaction(wex, mtx.transactionId);
- const txDetails = await ctx.lookupFullTransaction(tx);
- // FIXME: This means that in some cases we can return fewer transactions
- // than requested.
- if (!txDetails) {
- continue;
- }
- resultTransactions.push(txDetails);
- }
});
sortTransactions(transactionsRequest, resultTransactions);