taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 2454eedb44a07a07c4b3a2a3d9147a807e675602
parent d6745d25839966898544a9ad28392abd18e85b38
Author: Florian Dold <dold@taler.net>
Date:   Sun, 22 Nov 2026 21:34:17 +0100

wallet-core: notify clients when recoup finishes

Diffstat:
Apackages/taler-harness/src/integrationtests/test-recoup-spent-coin.ts | 153+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpackages/taler-harness/src/integrationtests/test-revocation.ts | 57++++++++++++++++++++++++++++++++++-----------------------
Mpackages/taler-harness/src/integrationtests/testrunner.ts | 2++
Mpackages/taler-wallet-core/src/recoup.ts | 10++++++++++
4 files changed, 199 insertions(+), 23 deletions(-)

diff --git a/packages/taler-harness/src/integrationtests/test-recoup-spent-coin.ts b/packages/taler-harness/src/integrationtests/test-recoup-spent-coin.ts @@ -0,0 +1,153 @@ +/* + This file is part of GNU Taler + (C) 2026 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +import { + NotificationType, + openPromise, + TransactionMajorState, + TransactionType, +} from "@gnu-taler/taler-util"; +import { + parseTransactionIdentifier, + WalletApiOperation as Op, +} from "@gnu-taler/taler-wallet-core"; +import { makeLargeCoinTestConfig } from "../harness/denomStructures.js"; +import { + createSimpleTestkudosEnvironmentV3, + createWalletDaemonWithClient, + withdrawViaBankV4, +} from "../harness/environments.js"; +import { GlobalTestState } from "../harness/harness.js"; + +export async function runRecoupSpentCoinTest(t: GlobalTestState) { + t.allowLog({ + file: "wallet-restored-stderr.log", + level: "WARNING", + message: /^denomination [A-Z0-9]+ is a loss$/, + maxOccurrences: 1, + justification: + "the test revokes the only denomination held in the restored wallet", + }); + // Keep /keys available when the one-unit denomination is revoked. + const [one] = makeLargeCoinTestConfig(); + const { + walletClient: original, + bank, + exchange, + faultyExchange, + } = await createSimpleTestkudosEnvironmentV3( + t, + [one, { ...one, name: "two", value: "TESTKUDOS:2" }], + { + faultInjection: true, + }, + ); + const w = await withdrawViaBankV4(t, { + walletClient: original, + bank, + exchange: faultyExchange, + amount: "TESTKUDOS:1", + }); + await w.withdrawalFinishedCond; + const [coin] = (await original.call(Op.DumpCoins, {})).coins; + const recoupDone = openPromise<void>(); + const startRestored = () => + createWalletDaemonWithClient(t, { + name: "restored", + persistent: true, + handleNotification(n) { + if ( + n.type === NotificationType.TransactionStateTransition && + parseTransactionIdentifier(n.transactionId)?.tag === + TransactionType.Recoup && + n.newTxState.major === TransactionMajorState.Done + ) { + recoupDone.resolve(); + } + }, + }); + let restored = await startRestored(); + await restored.walletClient.call(Op.ImportDb, { + dump: await original.call(Op.ExportDb, {}), + }); + await original.call(Op.ForceRefresh, { + refreshCoinSpecs: [{ coinPub: coin.coinPub }], + }); + await original.call(Op.TestingWaitRefreshesFinal, {}); + const historyRead = openPromise<void>(); + const releaseHistory = openPromise<void>(); + let historyReads = 0; + let recoupPosts = 0; + faultyExchange.faultProxy.addFault({ + async modifyResponse(resp) { + const path = new URL(resp.request.requestUrl).pathname; + if ( + path.endsWith(`/coins/${coin.coinPub}/history`) && + resp.statusCode === 200 + ) { + const history = JSON.parse(resp.responseBody!.toString()); + t.assertAmountEquals(history.balance, "TESTKUDOS:0"); + if (++historyReads === 1) { + historyRead.resolve(); + await releaseHistory.promise; + } + } + if ( + resp.request.method === "POST" && + /\/recoup(?:-withdraw|-refresh)?$/.test(path) + ) + recoupPosts++; + }, + }); + await exchange.revokeDenomination(coin.denomPubHash); + await exchange.keyup(); + await restored.walletClient.call(Op.UpdateExchangeEntry, { + exchangeBaseUrl: faultyExchange.baseUrl, + force: true, + }); + await historyRead.promise; + try { + restored.walletClient.remoteWallet?.close(); + await restored.walletService.stop(); + } finally { + releaseHistory.resolve(); + } + restored = await startRestored(); + await recoupDone.promise; + for (let i = 0; i < 2; i++) { + await restored.walletClient.call(Op.UpdateExchangeEntry, { + exchangeBaseUrl: faultyExchange.baseUrl, + force: true, + }); + await restored.walletClient.call(Op.TestingWaitExchangeReady, { + exchangeBaseUrl: faultyExchange.baseUrl, + }); + await restored.walletClient.call(Op.TestingWaitTransactionsFinal, {}); + const recoups = ( + await restored.walletClient.call(Op.GetTransactions, {}) + ).transactions.filter((tx) => tx.type === TransactionType.Recoup); + t.assertDeepEqual(recoups.length, 1); + t.assertDeepEqual(recoups[0].txState.major, TransactionMajorState.Done); + t.assertAmountEquals( + (await restored.walletClient.call(Op.GetBalances, {})).balances[0] + .available, + "TESTKUDOS:0", + ); + t.assertDeepEqual(historyReads, 2); + t.assertDeepEqual(recoupPosts, 0); + } +} +runRecoupSpentCoinTest.suites = ["wallet"]; diff --git a/packages/taler-harness/src/integrationtests/test-revocation.ts b/packages/taler-harness/src/integrationtests/test-revocation.ts @@ -17,7 +17,11 @@ /** * Imports. */ -import { TalerMerchantApi } from "@gnu-taler/taler-util"; +import { + TalerMerchantApi, + TransactionType, + TransactionMajorState, +} from "@gnu-taler/taler-util"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { CoinConfig } from "../harness/denomStructures.js"; import { @@ -231,6 +235,12 @@ export async function runRevocationTest(t: GlobalTestState) { exchangeBaseUrl: exchange.baseUrl, }); await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + // Recoup completes when it creates the recovery withdrawal. Wait for the + // withdrawn coins to arrive before checking what the wallet can spend. + await walletClient.call(WalletApiOperation.TestingWaitBalance, { + type: "material", + amount: "TESTKUDOS:15", + }); const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); console.log("wallet balance", bal); t.assertAmountEquals(bal.balances[0].available, "TESTKUDOS:15"); @@ -281,6 +291,10 @@ export async function runRevocationTest(t: GlobalTestState) { await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); await walletClient.call(WalletApiOperation.TestingWaitRefreshesFinal, {}); { + await walletClient.call(WalletApiOperation.TestingWaitBalance, { + type: "material", + amount: "TESTKUDOS:15", + }); const bal = await walletClient.call(WalletApiOperation.GetBalances, {}); console.log("wallet balance", bal); t.assertAmountEquals(bal.balances[0].available, "TESTKUDOS:15"); @@ -322,36 +336,33 @@ export async function runRevocationTest(t: GlobalTestState) { await walletClient.call(WalletApiOperation.TestingWaitExchangeReady, { exchangeBaseUrl: exchange.baseUrl, }); - let recovered = false; - for (let i = 0; i < 200; i++) { - const balance = await walletClient.call(WalletApiOperation.GetBalances, {}); - if (balance.balances[0]?.available === "TESTKUDOS:15") { - recovered = true; - break; - } - await delayMs(50); - } - t.assertTrue(recovered); + // Releasing the blocked melt before recoup takes ownership of its inputs + // would let the wallet send it to the exchange after revocation. + const transactions = await walletClient.call( + WalletApiOperation.GetTransactions, + {}, + ); + const recoup = transactions.transactions.find( + (tx) => tx.type === TransactionType.Recoup, + ); + t.assertTrue(!!recoup); + await walletClient.call(WalletApiOperation.TestingWaitTransactionState, { + transactionId: recoup.transactionId, + txState: { major: TransactionMajorState.Done }, + }); await walletClient.call(WalletApiOperation.ApplyDevExperiment, { devExperimentUri: "taler://dev-experiment/stop-block-refresh", }); await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); await walletClient.call(WalletApiOperation.TestingWaitRefreshesFinal, {}); - let finalBalance = await walletClient.call( + await walletClient.call(WalletApiOperation.TestingWaitBalance, { + type: "material", + amount: "TESTKUDOS:15", + }); + const finalBalance = await walletClient.call( WalletApiOperation.GetBalances, {}, ); - // Recoup settles the superseded refresh when the source is credited. Its - // hidden replacement withdrawal can still be running after both waits. - for (let i = 0; i < 200; i++) { - if ( - finalBalance.balances[0]?.available === "TESTKUDOS:15" && - finalBalance.balances[0]?.pendingIncoming === "TESTKUDOS:0" - ) - break; - await delayMs(50); - finalBalance = await walletClient.call(WalletApiOperation.GetBalances, {}); - } t.assertAmountEquals(finalBalance.balances[0].available, "TESTKUDOS:15"); t.assertAmountEquals(finalBalance.balances[0].pendingIncoming, "TESTKUDOS:0"); } diff --git a/packages/taler-harness/src/integrationtests/testrunner.ts b/packages/taler-harness/src/integrationtests/testrunner.ts @@ -185,6 +185,7 @@ import { runRefundGoneTest } from "./test-refund-gone.js"; import { runRefundIncrementalTest } from "./test-refund-incremental.js"; import { runRefundTest } from "./test-refund.js"; import { runRepurchaseTest } from "./test-repurchase.js"; +import { runRecoupSpentCoinTest } from "./test-recoup-spent-coin.js"; import { runRevocationTest } from "./test-revocation.js"; import { runSimplePaymentTest } from "./test-simple-payment.js"; import { runTimetravelAutorefreshTest } from "./test-timetravel-autorefresh.js"; @@ -406,6 +407,7 @@ const allTests: TestMainFunction[] = [ runRefundIncrementalTest, withWalletDbBackend(runRefundTest, "default"), withWalletDbBackend(runRefundTest, "indexeddb"), + runRecoupSpentCoinTest, runRevocationTest, runWithdrawalManualTest, runTimetravelAutorefreshTest, diff --git a/packages/taler-wallet-core/src/recoup.ts b/packages/taler-wallet-core/src/recoup.ts @@ -53,6 +53,7 @@ import { TransactionState, TransactionType, WalletNotification, + NotificationType, checkDbInvariant, encodeCrock, getRandomBytes, @@ -666,6 +667,7 @@ export async function completeRecoupGroup( withdrawal.withdrawalGroup.withdrawalGroupId, ).updateTransactionMeta(tx); } + const oldTxState = computeRecoupTransactionState(rg2); rg2.timestampFinished = timestampPreciseToDb(TalerPreciseTimestamp.now()); rg2.operationStatus = rg2.failedCoinPubs?.length ? RecoupOperationStatus.Failed @@ -686,6 +688,14 @@ export async function completeRecoupGroup( } await tx.upsertRecoupGroup(rg2); await ctx.updateTransactionMeta(tx); + tx.notify({ + type: NotificationType.TransactionStateTransition, + causeHint: "recoup-completed", + transactionId: ctx.transactionId, + oldTxState, + newTxState: computeRecoupTransactionState(rg2), + newStId: rg2.operationStatus, + }); } export function deterministicRecoupWithdrawalGroupId(