commit 253e639558d811b3d6e62f737dc5046c88f7e128
parent 7d2fb66afe0dbde8edc6d2fe576f84ab1134a9c4
Author: Florian Dold <dold@taler.net>
Date: Tue, 17 Nov 2026 23:19:10 +0100
wallet-core: reuse pending denomination signature checks
A withdrawal can need a signature check that's already running in the
background. Let it wait for that signature and cancel independently.
Diffstat:
3 files changed, 96 insertions(+), 12 deletions(-)
diff --git a/packages/taler-wallet-core/src/denominations.test.ts b/packages/taler-wallet-core/src/denominations.test.ts
@@ -26,6 +26,8 @@ import {
Amounts,
DenominationInfo,
AmountString,
+ CancellationToken,
+ openPromise,
} from "@gnu-taler/taler-util";
// import { expect } from "chai";
import {
@@ -70,19 +72,25 @@ function makeUnverifiedDenom(): WalletDenomination {
} as WalletDenomination;
}
-test("denomination verification patches only a still-matching current row", async () => {
+test("cancelling one caller preserves a shared signature check and concurrent row updates", async () => {
const snapshot = makeUnverifiedDenom();
let current = structuredClone(snapshot);
+ const checked = openPromise<{ valid: boolean }>();
+ const source = CancellationToken.create();
+ let verifications = 0;
const wex = {
+ cancellationToken: source.token,
cryptoApi: {
async isValidDenom() {
+ verifications++;
current.isRevoked = true;
current.exchangeBaseUrl = "https://new.example/";
- return { valid: true };
+ return checked.promise;
},
},
ws: {
config: { testing: { insecureTrustExchange: false } },
+ pendingDenomVerifications: new Map(),
denomInfoCache: { clear() {} },
},
async runWalletDbTx<T>(
@@ -99,8 +107,16 @@ test("denomination verification patches only a still-matching current row", asyn
},
} as unknown as WalletExecutionContext;
- await validateDenoms(wex, [snapshot]);
-
+ const cancelled = validateDenoms(wex, [snapshot]);
+ const withdrawal = validateDenoms(
+ { ...wex, cancellationToken: CancellationToken.CONTINUE },
+ [structuredClone(snapshot)],
+ );
+ source.cancel();
+ await assert.rejects(cancelled, CancellationToken.CancellationError);
+ checked.resolve({ valid: true });
+ await withdrawal;
+ assert.equal(verifications, 1);
assert.strictEqual(current.isRevoked, true);
assert.strictEqual(current.exchangeBaseUrl, "https://new.example/");
assert.strictEqual(
@@ -109,6 +125,58 @@ test("denomination verification patches only a still-matching current row", asyn
);
});
+test(
+ "a withdrawal can use a verified denomination while maintenance checks the rest of its batch",
+ { timeout: 2000 },
+ async () => {
+ const first = makeUnverifiedDenom();
+ const second = { ...first, denomPubHash: "other-hash" };
+ const firstCheck = openPromise<{ valid: boolean }>();
+ const secondCheck = openPromise<{ valid: boolean }>();
+ const calls: string[] = [];
+ const rows = new Map([first, second].map((d) => [d.denomPubHash, d]));
+ const wex = {
+ cancellationToken: CancellationToken.CONTINUE,
+ cryptoApi: {
+ isValidDenom({ denomPubHash }: { denomPubHash: string }) {
+ calls.push(denomPubHash);
+ return denomPubHash === first.denomPubHash
+ ? firstCheck.promise
+ : secondCheck.promise;
+ },
+ },
+ ws: {
+ config: { testing: {} },
+ pendingDenomVerifications: new Map(),
+ denomInfoCache: new Map(),
+ },
+ runWalletDbTx<T>(run: (tx: WalletDbTransaction) => Promise<T>) {
+ return run({
+ getDenomination: async (ref: WalletDenomination) =>
+ rows.get(ref.denomPubHash),
+ upsertDenomination: async (rec: WalletDenomination) =>
+ rows.set(rec.denomPubHash, rec),
+ } as unknown as WalletDbTransaction);
+ },
+ } as unknown as WalletExecutionContext;
+ const maintenance = validateDenoms(wex, [first, second]);
+ const withdrawal = validateDenoms(wex, [first]);
+ firstCheck.resolve({ valid: true });
+ await withdrawal;
+ assert.equal(
+ rows.get(first.denomPubHash)?.verificationStatus,
+ DenominationVerificationStatus.VerifiedGood,
+ );
+ assert.equal(
+ rows.get(second.denomPubHash)?.verificationStatus,
+ DenominationVerificationStatus.Unverified,
+ );
+ assert.deepEqual(calls, [first.denomPubHash, second.denomPubHash]);
+ secondCheck.resolve({ valid: true });
+ await maintenance;
+ },
+);
+
/**
* Create some constants to be used as reference in the tests
*/
diff --git a/packages/taler-wallet-core/src/denominations.ts b/packages/taler-wallet-core/src/denominations.ts
@@ -453,17 +453,28 @@ function denominationVerificationInput(d: WalletDenomination): string {
});
}
+function verifyDenomination(
+ wex: WalletExecutionContext,
+ denom: WalletDenomination,
+ input: string,
+): Promise<boolean> {
+ const pending = wex.ws.pendingDenomVerifications;
+ let check = pending.get(input);
+ if (!check) {
+ check = isValidDenomRecord(wex, denom.exchangeMasterPub, denom).finally(
+ () => pending.delete(input),
+ );
+ pending.set(input, check);
+ }
+ return wex.cancellationToken.racePromise(check);
+}
+
export async function validateDenoms(
wex: WalletExecutionContext,
denoms: WalletDenomination[],
): Promise<void> {
- if (denoms.length == 0) {
- logger.info(`no denominations need to be validated`);
- return;
- }
-
+ if (!denoms.length) return;
logger.info(`need to validate ${denoms.length} denominations`);
-
let current = 0;
while (current < denoms.length) {
const verificationResults: {
@@ -486,12 +497,14 @@ export async function validateDenoms(
` signature of ${denom.denomPubHash}`,
);
+ wex.cancellationToken.throwIfCancelled();
+ const input = denominationVerificationInput(denom);
let valid = false;
if (wex.ws.config.testing.insecureTrustExchange) {
valid = true;
} else {
- valid = await isValidDenomRecord(wex, denom.exchangeMasterPub, denom);
+ valid = await verifyDenomination(wex, denom, input);
}
logger.trace(`Done validating ${denom.denomPubHash}`);
@@ -509,7 +522,7 @@ export async function validateDenoms(
verificationResults.push({
exchangeMasterPub: denom.exchangeMasterPub,
denomPubHash: denom.denomPubHash,
- input: denominationVerificationInput(denom),
+ input,
status,
});
}
diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts
@@ -1369,6 +1369,9 @@ export class InternalWalletState {
Duration.fromSpec({ minutes: 1 }),
);
+ /** In-flight signature checks keyed by the signed denomination fields. */
+ readonly pendingDenomVerifications = new Map<string, Promise<boolean>>();
+
denomInfoCache: Cache<DenominationInfo> = new Cache(
1000,
Duration.fromSpec({ minutes: 1 }),