From 541886750eb1bc32b9aa0323868dd24776baf0eb Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 6 Mar 2024 11:02:13 +0100 Subject: taler-harness: test aborting refreshes --- packages/taler-wallet-core/src/index.ts | 2 + .../taler-wallet-core/src/pay-peer-pull-debit.ts | 50 ++++++------- packages/taler-wallet-core/src/refresh.ts | 85 +++++++++++----------- 3 files changed, 68 insertions(+), 69 deletions(-) (limited to 'packages/taler-wallet-core') diff --git a/packages/taler-wallet-core/src/index.ts b/packages/taler-wallet-core/src/index.ts index fa984fc8f..fe2d3af15 100644 --- a/packages/taler-wallet-core/src/index.ts +++ b/packages/taler-wallet-core/src/index.ts @@ -32,6 +32,8 @@ export * from "./versions.js"; export * from "./wallet-api-types.js"; export * from "./wallet.js"; +export { parseTransactionIdentifier } from "./transactions.js"; + export { createPairTimeline } from "./denominations.js"; // FIXME: Should these really be exported?! diff --git a/packages/taler-wallet-core/src/pay-peer-pull-debit.ts b/packages/taler-wallet-core/src/pay-peer-pull-debit.ts index 7348a30ce..2418f08da 100644 --- a/packages/taler-wallet-core/src/pay-peer-pull-debit.ts +++ b/packages/taler-wallet-core/src/pay-peer-pull-debit.ts @@ -26,7 +26,6 @@ import { AcceptPeerPullPaymentResponse, Amounts, - CancellationToken, CoinRefreshRequest, ConfirmPeerPullDebitRequest, ContractTermsUtil, @@ -95,7 +94,7 @@ import { notifyTransition, parseTransactionIdentifier, } from "./transactions.js"; -import { InternalWalletState, WalletExecutionContext } from "./wallet.js"; +import { WalletExecutionContext } from "./wallet.js"; const logger = new Logger("pay-peer-pull-debit.ts"); @@ -552,15 +551,9 @@ export async function processPeerPullDebit( switch (peerPullInc.status) { case PeerPullDebitRecordStatus.PendingDeposit: - return await processPeerPullDebitPendingDeposit( - wex, - peerPullInc, - ); + return await processPeerPullDebitPendingDeposit(wex, peerPullInc); case PeerPullDebitRecordStatus.AbortingRefresh: - return await processPeerPullDebitAbortingRefresh( - wex, - peerPullInc, - ); + return await processPeerPullDebitAbortingRefresh(wex, peerPullInc); } return TaskRunResult.finished(); } @@ -791,23 +784,26 @@ export async function preparePeerPullDebit( coinSelRes.result.coins, ); - await wex.db.runReadWriteTx(["peerPullDebit", "contractTerms"], async (tx) => { - await tx.contractTerms.put({ - h: contractTermsHash, - contractTermsRaw: contractTerms, - }), - await tx.peerPullDebit.add({ - peerPullDebitId, - contractPriv: contractPriv, - exchangeBaseUrl: exchangeBaseUrl, - pursePub: pursePub, - timestampCreated: timestampPreciseToDb(TalerPreciseTimestamp.now()), - contractTermsHash, - amount: contractTerms.amount, - status: PeerPullDebitRecordStatus.DialogProposed, - totalCostEstimated: Amounts.stringify(totalAmount), - }); - }); + await wex.db.runReadWriteTx( + ["peerPullDebit", "contractTerms"], + async (tx) => { + await tx.contractTerms.put({ + h: contractTermsHash, + contractTermsRaw: contractTerms, + }), + await tx.peerPullDebit.add({ + peerPullDebitId, + contractPriv: contractPriv, + exchangeBaseUrl: exchangeBaseUrl, + pursePub: pursePub, + timestampCreated: timestampPreciseToDb(TalerPreciseTimestamp.now()), + contractTermsHash, + amount: contractTerms.amount, + status: PeerPullDebitRecordStatus.DialogProposed, + totalCostEstimated: Amounts.stringify(totalAmount), + }); + }, + ); return { amount: contractTerms.amount, diff --git a/packages/taler-wallet-core/src/refresh.ts b/packages/taler-wallet-core/src/refresh.ts index c6ece1536..9c272ad18 100644 --- a/packages/taler-wallet-core/src/refresh.ts +++ b/packages/taler-wallet-core/src/refresh.ts @@ -107,7 +107,7 @@ import { getCandidateWithdrawalDenomsTx } from "./withdraw.js"; const logger = new Logger("refresh.ts"); export class RefreshTransactionContext implements TransactionContext { - public transactionId: TransactionIdStr; + readonly transactionId: TransactionIdStr; readonly taskId: TaskIdStr; constructor( @@ -126,53 +126,54 @@ export class RefreshTransactionContext implements TransactionContext { async deleteTransaction(): Promise { const refreshGroupId = this.refreshGroupId; - const ws = this.wex; - await ws.db.runReadWriteTx(["refreshGroups", "tombstones"], async (tx) => { - const rg = await tx.refreshGroups.get(refreshGroupId); - if (rg) { - await tx.refreshGroups.delete(refreshGroupId); - await tx.tombstones.put({ - id: TombstoneTag.DeleteRefreshGroup + ":" + refreshGroupId, - }); - } - }); + await this.wex.db.runReadWriteTx( + ["refreshGroups", "tombstones"], + async (tx) => { + const rg = await tx.refreshGroups.get(refreshGroupId); + if (rg) { + await tx.refreshGroups.delete(refreshGroupId); + await tx.tombstones.put({ + id: TombstoneTag.DeleteRefreshGroup + ":" + refreshGroupId, + }); + } + }, + ); } async suspendTransaction(): Promise { const { wex, refreshGroupId, transactionId } = this; - let res = await wex.db.runReadWriteTx(["refreshGroups"], async (tx) => { - const dg = await tx.refreshGroups.get(refreshGroupId); - if (!dg) { - logger.warn( - `can't suspend refresh group, refreshGroupId=${refreshGroupId} not found`, - ); - return undefined; - } - const oldState = computeRefreshTransactionState(dg); - switch (dg.operationStatus) { - case RefreshOperationStatus.Finished: + let transitionInfo = await wex.db.runReadWriteTx( + ["refreshGroups"], + async (tx) => { + const dg = await tx.refreshGroups.get(refreshGroupId); + if (!dg) { + logger.warn( + `can't suspend refresh group, refreshGroupId=${refreshGroupId} not found`, + ); return undefined; - case RefreshOperationStatus.Pending: { - dg.operationStatus = RefreshOperationStatus.Suspended; - await tx.refreshGroups.put(dg); - return { - oldTxState: oldState, - newTxState: computeRefreshTransactionState(dg), - }; } - case RefreshOperationStatus.Suspended: - return undefined; - } - return undefined; - }); - if (res) { - wex.ws.notify({ - type: NotificationType.TransactionStateTransition, - transactionId, - oldTxState: res.oldTxState, - newTxState: res.newTxState, - }); - } + const oldState = computeRefreshTransactionState(dg); + switch (dg.operationStatus) { + case RefreshOperationStatus.Finished: + case RefreshOperationStatus.Suspended: + case RefreshOperationStatus.Failed: + return undefined; + case RefreshOperationStatus.Pending: { + dg.operationStatus = RefreshOperationStatus.Suspended; + await tx.refreshGroups.put(dg); + break; + } + default: + assertUnreachable(dg.operationStatus); + } + return { + oldTxState: oldState, + newTxState: computeRefreshTransactionState(dg), + }; + }, + ); + wex.taskScheduler.stopShepherdTask(this.taskId); + notifyTransition(wex, transactionId, transitionInfo); } async abortTransaction(): Promise { -- cgit v1.2.3