taler-typescript-core

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

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

wallet-core: report zero refresh output for exhausted coins

Diffstat:
Mpackages/taler-harness/src/integrationtests/test-wallet-refresh-errors.ts | 47+++++++++++++++++++++++++++++++++++++++++++++--
Mpackages/taler-wallet-core/src/refresh.test.ts | 23+++++++++++++++++++++++
Mpackages/taler-wallet-core/src/refresh.ts | 25+++++++++++++++++--------
3 files changed, 85 insertions(+), 10 deletions(-)

diff --git a/packages/taler-harness/src/integrationtests/test-wallet-refresh-errors.ts b/packages/taler-harness/src/integrationtests/test-wallet-refresh-errors.ts @@ -17,7 +17,11 @@ /** * Imports. */ -import { AmountString } from "@gnu-taler/taler-util"; +import { + AmountString, + TransactionType, + TransactionMajorState, +} from "@gnu-taler/taler-util"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { CoinConfig } from "../harness/denomStructures.js"; import { @@ -67,7 +71,7 @@ export async function runWalletRefreshErrorsTest(t: GlobalTestState) { ], ], justification: - "the test verifies that the wallet reaches a final failed state for stale and duplicate refresh conflicts", + "the exchange rejects the stale melt amount and the duplicate melt of an exhausted coin", }); const coinConfigList: CoinConfig[] = [ @@ -128,6 +132,27 @@ export async function runWalletRefreshErrorsTest(t: GlobalTestState) { await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {}); + async function getRefreshes() { + const result = await walletClient.call(WalletApiOperation.GetTransactions, { + includeRefreshes: true, + }); + return result.transactions.filter( + (tx) => tx.type === TransactionType.Refresh, + ); + } + const restoredRefreshes = await getRefreshes(); + t.assertDeepEqual(restoredRefreshes.length, 1); + const corrected = restoredRefreshes[0]; + t.assertDeepEqual(corrected.txState.major, TransactionMajorState.Done); + t.assertAmountEquals(corrected.refreshInputAmount, "TESTKUDOS:2"); + t.assertAmountEquals(corrected.refreshOutputAmount, "TESTKUDOS:2"); + + t.assertAmountEquals( + (await walletClient.call(WalletApiOperation.GetBalances, {})).balances[0] + .available, + "TESTKUDOS:2", + ); + // Now reset everything and do more tests await walletClient.call(WalletApiOperation.ClearDb, {}); @@ -166,6 +191,24 @@ export async function runWalletRefreshErrorsTest(t: GlobalTestState) { WalletApiOperation.TestingWaitTransactionsFinal, {}, ); + const results = await getRefreshes(); + t.assertDeepEqual(results.map((x) => x.txState.major).sort(), [ + TransactionMajorState.Done, + TransactionMajorState.Failed, + ]); + for (const tx of results) { + t.assertAmountEquals( + tx.refreshOutputAmount, + tx.txState.major === TransactionMajorState.Done + ? "TESTKUDOS:5" + : "TESTKUDOS:0", + ); + } + t.assertAmountEquals( + (await walletClient.call(WalletApiOperation.GetBalances, {})).balances[0] + .available, + "TESTKUDOS:5", + ); } } diff --git a/packages/taler-wallet-core/src/refresh.test.ts b/packages/taler-wallet-core/src/refresh.test.ts @@ -23,6 +23,7 @@ import { TalerError, TalerErrorCode, TransactionAction, + TransactionType, } from "@gnu-taler/taler-util"; import assert from "node:assert"; import { test } from "node:test"; @@ -89,6 +90,28 @@ function refreshOutputDenom(ageMask: number): WalletDenomination { }; } +test("terminal refresh details exclude estimates for failed coins in older records", async () => { + const group = { + currency: "TESTKUDOS", + operationStatus: RefreshOperationStatus.Failed, + inputPerCoin: ["TESTKUDOS:5", "TESTKUDOS:2"], + expectedOutputPerCoin: ["TESTKUDOS:5", "TESTKUDOS:2"], + statusPerCoin: [RefreshCoinStatus.Failed, RefreshCoinStatus.Finished], + timestampCreated: 1, + } as WalletRefreshGroup; + const tx = { + getRefreshGroup: async () => group, + getOperationRetry: async () => undefined, + } as unknown as WalletDbTransaction; + const ctx = new RefreshTransactionContext( + {} as WalletExecutionContext, + "legacy", + ); + const detail = await ctx.lookupFullTransaction(tx); + assert.ok(detail?.type === TransactionType.Refresh); + assert.equal(detail.refreshOutputAmount, "TESTKUDOS:2"); +}); + test("refresh costs exclude outputs with a different age mask", () => { const outputs = [0, 257, 513].map(refreshOutputDenom); for (const output of outputs) { diff --git a/packages/taler-wallet-core/src/refresh.ts b/packages/taler-wallet-core/src/refresh.ts @@ -24,6 +24,7 @@ import { ensureRecoupProvenance } from "./recoup.js"; /** * Imports. */ +import { settledRefreshOutput } from "./transaction-amounts.js"; import { addPermanentCoinFailure } from "./transaction-reasons.js"; import { AutoRefreshOutputPlan, evaluateAutoRefresh } from "./autoRefresh.js"; import { @@ -207,10 +208,14 @@ export class RefreshTransactionContext implements TransactionContext { refreshGroupRecord.currency, refreshGroupRecord.inputPerCoin, ).amount; - const outputAmount = Amounts.sumOrZero( - refreshGroupRecord.currency, - refreshGroupRecord.expectedOutputPerCoin, - ).amount; + const outputAmount = + settledRefreshOutput(refreshGroupRecord) ?? + Amounts.stringify( + Amounts.sumOrZero( + refreshGroupRecord.currency, + refreshGroupRecord.expectedOutputPerCoin, + ).amount, + ); const txState = computeRefreshTransactionState(refreshGroupRecord); return { type: TransactionType.Refresh, @@ -231,7 +236,7 @@ export class RefreshTransactionContext implements TransactionContext { Amounts.zeroOfCurrency(refreshGroupRecord.currency), ), refreshInputAmount: Amounts.stringify(inputAmount), - refreshOutputAmount: Amounts.stringify(outputAmount), + refreshOutputAmount: outputAmount, originatingTransactionId: refreshGroupRecord.originatingTransactionId, timestamp: timestampPreciseFromDb(refreshGroupRecord.timestampCreated), transactionId: constructTransactionIdentifier({ @@ -1698,6 +1703,13 @@ export async function handleRefreshMeltConflict( if (!refreshSession) { throw Error("db invariant failed: missing refresh session in database"); } + const info = rg.infoPerExchange?.[oldCoin.exchangeBaseUrl]; + if (info) + info.outputEffective = Amounts.stringify( + Amounts.sub(info.outputEffective, rg.expectedOutputPerCoin[coinIndex]) + .amount, + ); + rg.expectedOutputPerCoin[coinIndex] = correctedBalance; if (rg.reason === RefreshReason.AbortPay) { // A coin selected for a partially completed payment can legitimately // have no balance left: the merchant deposited it before the wallet @@ -1706,9 +1718,6 @@ export async function handleRefreshMeltConflict( // coin has no output and is complete rather than failed. await destroyRefreshSession(tx, rg, refreshSession); await tx.deleteRefreshSession(ctx.refreshGroupId, coinIndex); - rg.expectedOutputPerCoin[coinIndex] = Amounts.stringify( - Amounts.zeroOfCurrency(rg.currency), - ); rg.statusPerCoin[coinIndex] = RefreshCoinStatus.Finished; await h.update(rg, "melt-conflict-abort-pay-spent"); } else {