taler-typescript-core

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

commit c196325e7d86692dd514e21b969c9987b44bd088
parent 0a92e4bbb88c2cb3d0d5d4b34be46bc271c5ef1b
Author: Florian Dold <dold@taler.net>
Date:   Tue, 24 Nov 2026 12:58:03 +0100

wallet-core: skip change refresh for fully spent coins

An exact payment can still use deposit-valid coins after the exchange's
denominations have left their withdrawal window. A coin with no change
has zero refresh cost and needs no refresh group.

Diffstat:
Mpackages/taler-wallet-core/src/common.ts | 33++++++++++++++++++---------------
Mpackages/taler-wallet-core/src/refresh.test.ts | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpackages/taler-wallet-core/src/refresh.ts | 4++++
3 files changed, 94 insertions(+), 15 deletions(-)

diff --git a/packages/taler-wallet-core/src/common.ts b/packages/taler-wallet-core/src/common.ts @@ -174,8 +174,7 @@ export async function makeCoinAvailable( } /** - * Spend coins. Marks the coins as used, adds a coin history items - * and creates refresh group. + * Record each spend and refresh any change left on the coins. */ export async function spendCoins( wex: WalletExecutionContext, @@ -188,7 +187,7 @@ export async function spendCoins( if (csi.coinPubs.length === 0) { return; } - let refreshCoinPubs: CoinRefreshRequest[] = []; + const refreshCoinPubs: CoinRefreshRequest[] = []; const loadedCoins = await tx.getCoinsByPubs(csi.coinPubs); const coinsByPub = new Map(loadedCoins.map((coin) => [coin.coinPub, coin])); const denomByRef = await getDenomInfos(wex, tx, loadedCoins); @@ -274,10 +273,12 @@ export async function spendCoins( if (remaining.saturated) { throw Error("not enough remaining balance on coin for payment"); } - refreshCoinPubs.push({ - amount: Amounts.stringify(remaining.amount), - coinPub: coin.coinPub, - }); + if (!Amounts.isZero(remaining.amount)) { + refreshCoinPubs.push({ + amount: Amounts.stringify(remaining.amount), + coinPub: coin.coinPub, + }); + } if (coinAvailability.freshCoinCount === 0) { throw Error( `invalid coin count ${coinAvailability.freshCoinCount} in DB`, @@ -313,14 +314,16 @@ export async function spendCoins( await tx.upsertCoinAvailability(coinAvailability); } - await createRefreshGroup( - wex, - tx, - Amounts.currencyOf(csi.contributions[0]), - refreshCoinPubs, - csi.refreshReason, - csi.transactionId, - ); + if (refreshCoinPubs.length > 0) { + await createRefreshGroup( + wex, + tx, + Amounts.currencyOf(csi.contributions[0]), + refreshCoinPubs, + csi.refreshReason, + csi.transactionId, + ); + } } export async function spendTokens( diff --git a/packages/taler-wallet-core/src/refresh.test.ts b/packages/taler-wallet-core/src/refresh.test.ts @@ -14,6 +14,7 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ import { validateAndRecomputeCoinHistoryBalance } from "./coin-history.js"; +import { spendCoins } from "./common.js"; import { AbsoluteTime, Amounts, @@ -21,9 +22,11 @@ import { DenominationInfo, DenomKeyType, encodeCrock, + RefreshReason, TalerError, TalerErrorCode, TransactionAction, + TransactionIdStr, TransactionType, } from "@gnu-taler/taler-util"; import assert from "node:assert"; @@ -32,6 +35,7 @@ import { classifyRecoveryRefresh, computeRefreshTransactionActions, getTotalRefreshCostInternal, + getTotalRefreshCosts, RefreshTransactionContext, requireValidNorevealIndex, } from "./refresh.js"; @@ -41,6 +45,7 @@ import { RefreshOperationStatus, WalletCoin, WalletCoinAvailability, + WalletCoinHistory, WalletDenomination, WalletRefreshGroup, WalletRefreshSession, @@ -90,6 +95,73 @@ function refreshOutputDenom(ageMask: number): WalletDenomination { }; } +test("an exact spend needs no refresh denominations and records only the spend", async (t) => { + const denom = refreshOutputDenom(0); + denom.stampExpireWithdraw = timestampProtocolToDb( + AbsoluteTime.toProtocolTimestamp(AbsoluteTime.now()), + ); + const info = WalletDenomination.toDenomInfo(denom); + const coin = { + coinPub: "exact", + exchangeBaseUrl: denom.exchangeBaseUrl, + exchangeMasterPub: denom.exchangeMasterPub, + denomPubHash: denom.denomPubHash, + maxAge: 0, + status: CoinStatus.Fresh, + visible: 1, + } as WalletCoin; + const availability = { ...coin, freshCoinCount: 1, visibleCoinCount: 1 }; + const saveCoin = t.mock.fn(async (_coin: WalletCoin) => {}); + const saveAvailability = t.mock.fn( + async (_row: WalletCoinAvailability) => {}, + ); + const saveHistory = t.mock.fn(async (_history: WalletCoinHistory) => {}); + const tx = { + getCoinsByPubs: async () => [coin], + getCoinAvailabilitiesByRefs: async () => [availability], + getCoinHistoriesByPubs: async () => [], + upsertCoin: saveCoin, + upsertCoinAvailability: saveAvailability, + upsertCoinHistory: saveHistory, + getExchange: () => assert.fail("an exact spend requested refresh keys"), + upsertRefreshGroup: () => + assert.fail("an exact spend created a refresh group"), + } as unknown as WalletDbTransaction; + const wex = { + ws: { + denomInfoCache: { get: () => info }, + refreshCostCache: { get: () => undefined }, + }, + } as unknown as WalletExecutionContext; + const zero = Amounts.zeroOfAmount(denom.value); + assert.deepEqual( + await getTotalRefreshCosts(wex, tx, [ + { refreshedDenom: info, amountLeft: zero }, + ]), + [zero], + ); + const transactionId = "txn:payment:exact" as TransactionIdStr; + await spendCoins(wex, tx, { + transactionId, + coinPubs: [coin.coinPub], + contributions: [Amounts.parseOrThrow(denom.value)], + refreshReason: RefreshReason.PayMerchant, + }); + assert.equal(saveCoin.mock.calls[0].arguments[0].status, CoinStatus.Dormant); + const savedAvailability = saveAvailability.mock.calls[0].arguments[0]; + assert.equal(savedAvailability.freshCoinCount, 0); + assert.equal(savedAvailability.visibleCoinCount, 0); + assert.deepEqual( + saveHistory.mock.calls.map((call) => call.arguments[0]), + [ + { + coinPub: coin.coinPub, + history: [{ type: "spend", transactionId, amount: denom.value }], + }, + ], + ); +}); + test("terminal refresh details exclude estimates for failed coins in older records", async () => { const group = { currency: "TESTKUDOS", diff --git a/packages/taler-wallet-core/src/refresh.ts b/packages/taler-wallet-core/src/refresh.ts @@ -442,6 +442,10 @@ export async function getTotalRefreshCosts( const groups = new Map<string, number[]>(); for (let i = 0; i < requests.length; i++) { const request = requests[i]; + if (Amounts.isZero(request.amountLeft)) { + results[i] = request.amountLeft; + continue; + } const cached = wex.ws.refreshCostCache.get( refreshCostCacheKey(request.refreshedDenom, request.amountLeft), );