From 52a1f63e0a8cc2ca78910e8b56326376eb1d75d0 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 21 Feb 2024 14:23:01 +0100 Subject: wallet-core: use cancellation tokens when possible --- packages/taler-wallet-core/src/withdraw.ts | 78 ++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 21 deletions(-) (limited to 'packages/taler-wallet-core/src/withdraw.ts') diff --git a/packages/taler-wallet-core/src/withdraw.ts b/packages/taler-wallet-core/src/withdraw.ts index 2d9f5c35c..a54295613 100644 --- a/packages/taler-wallet-core/src/withdraw.ts +++ b/packages/taler-wallet-core/src/withdraw.ts @@ -100,7 +100,7 @@ import { } from "./coinSelection.js"; import { PendingTaskType, - TaskId, + TaskIdStr, TaskRunResult, TaskRunResultType, TombstoneTag, @@ -155,7 +155,7 @@ const logger = new Logger("operations/withdraw.ts"); export class WithdrawTransactionContext implements TransactionContext { readonly transactionId: TransactionIdStr; - readonly taskId: TaskId; + readonly taskId: TaskIdStr; constructor( public ws: InternalWalletState, @@ -799,6 +799,7 @@ async function handleKycRequired( resp: HttpResponse, startIdx: number, requestCoinIdxs: number[], + cancellationToken: CancellationToken, ): Promise { logger.info("withdrawal requires KYC"); const respJson = await resp.json(); @@ -822,6 +823,7 @@ async function handleKycRequired( logger.info(`kyc url ${url.href}`); const kycStatusRes = await ws.http.fetch(url.href, { method: "GET", + cancellationToken, }); let kycUrl: string; let amlStatus: ExchangeAmlStatus | undefined; @@ -1003,7 +1005,14 @@ async function processPlanchetExchangeBatchRequest( timeout: Duration.fromSpec({ seconds: 40 }), }); if (resp.status === HttpStatusCode.UnavailableForLegalReasons) { - await handleKycRequired(ws, withdrawalGroup, resp, 0, requestCoinIdxs); + await handleKycRequired( + ws, + withdrawalGroup, + resp, + 0, + requestCoinIdxs, + cancellationToken, + ); return { batchResp: { ev_sigs: [] }, coinIdxs: [], @@ -1359,6 +1368,7 @@ interface WithdrawalGroupContext { async function processWithdrawalGroupAbortingBank( ws: InternalWalletState, withdrawalGroup: WithdrawalGroupRecord, + cancellationToken: CancellationToken, ): Promise { const { withdrawalGroupId } = withdrawalGroup; const transactionId = constructTransactionIdentifier({ @@ -1375,6 +1385,7 @@ async function processWithdrawalGroupAbortingBank( const abortResp = await ws.http.fetch(abortUrl, { method: "POST", body: {}, + cancellationToken, }); logger.info(`abort response status: ${abortResp.status}`); @@ -1708,7 +1719,11 @@ export async function processWithdrawalGroup( cancellationToken, ); case WithdrawalGroupStatus.AbortingBank: - return await processWithdrawalGroupAbortingBank(ws, withdrawalGroup); + return await processWithdrawalGroupAbortingBank( + ws, + withdrawalGroup, + cancellationToken, + ); case WithdrawalGroupStatus.AbortedBank: case WithdrawalGroupStatus.AbortedExchange: case WithdrawalGroupStatus.FailedAbortingBank: @@ -1749,10 +1764,14 @@ export async function getExchangeWithdrawalInfo( ); } - const withdrawalAccountsList = await fetchWithdrawalAccountInfo(ws, { - exchange, - instructedAmount, - }); + const withdrawalAccountsList = await fetchWithdrawalAccountInfo( + ws, + { + exchange, + instructedAmount, + }, + CancellationToken.CONTINUE, + ); logger.trace("updating withdrawal denoms"); await updateWithdrawalDenoms(ws, exchangeBaseUrl); @@ -2069,6 +2088,7 @@ export function getBankAbortUrl(talerWithdrawUri: string): string { async function registerReserveWithBank( ws: InternalWalletState, withdrawalGroupId: string, + cancellationToken: CancellationToken, ): Promise { const withdrawalGroup = await ws.db.runReadOnlyTx( ["withdrawalGroups"], @@ -2106,6 +2126,7 @@ async function registerReserveWithBank( method: "POST", body: reqBody, timeout: getReserveRequestTimeout(withdrawalGroup), + cancellationToken, }); const status = await readSuccessResponseJsonOrThrow( httpResp, @@ -2231,7 +2252,7 @@ async function processBankRegisterReserve( // FIXME: Put confirm transfer URL in the DB! - await registerReserveWithBank(ws, withdrawalGroupId); + await registerReserveWithBank(ws, withdrawalGroupId, cancellationToken); return TaskRunResult.progress(); } @@ -2631,10 +2652,14 @@ export async function acceptWithdrawalFromUri( const exchange = await fetchFreshExchange(ws, selectedExchange); - const withdrawalAccountList = await fetchWithdrawalAccountInfo(ws, { - exchange, - instructedAmount: withdrawInfo.amount, - }); + const withdrawalAccountList = await fetchWithdrawalAccountInfo( + ws, + { + exchange, + instructedAmount: withdrawInfo.amount, + }, + CancellationToken.CONTINUE, + ); const withdrawalGroup = await internalCreateWithdrawalGroup(ws, { amount: withdrawInfo.amount, @@ -2762,7 +2787,8 @@ async function fetchAccount( ws: InternalWalletState, instructedAmount: AmountJson, acct: ExchangeWireAccount, - reservePub?: string, + reservePub: string | undefined, + cancellationToken: CancellationToken, ): Promise { let paytoUri: string; let transferAmount: AmountString | undefined = undefined; @@ -2773,7 +2799,9 @@ async function fetchAccount( "amount_credit", Amounts.stringify(instructedAmount), ); - const httpResp = await ws.http.fetch(reqUrl.href); + const httpResp = await ws.http.fetch(reqUrl.href, { + cancellationToken, + }); const respOrErr = await readSuccessResponseJsonOrErrorCode( httpResp, codecForCashinConversionResponse(), @@ -2789,7 +2817,9 @@ async function fetchAccount( paytoUri = acct.payto_uri; transferAmount = resp.amount_debit; const configUrl = new URL("config", acct.conversion_url); - const configResp = await ws.http.fetch(configUrl.href); + const configResp = await ws.http.fetch(configUrl.href, { + cancellationToken, + }); const configRespOrError = await readSuccessResponseJsonOrErrorCode( configResp, codecForConversionBankConfig(), @@ -2840,6 +2870,7 @@ async function fetchWithdrawalAccountInfo( instructedAmount: AmountJson; reservePub?: string; }, + cancellationToken: CancellationToken, ): Promise { const { exchange } = req; const withdrawalAccounts: WithdrawalExchangeAccountDetails[] = []; @@ -2849,6 +2880,7 @@ async function fetchWithdrawalAccountInfo( req.instructedAmount, acct, req.reservePub, + cancellationToken, ); withdrawalAccounts.push(acctInfo); } @@ -2885,11 +2917,15 @@ export async function createManualWithdrawal( {}, ); - const withdrawalAccountsList = await fetchWithdrawalAccountInfo(ws, { - exchange, - instructedAmount: amount, - reservePub: reserveKeyPair.pub, - }); + const withdrawalAccountsList = await fetchWithdrawalAccountInfo( + ws, + { + exchange, + instructedAmount: amount, + reservePub: reserveKeyPair.pub, + }, + CancellationToken.CONTINUE, + ); const withdrawalGroup = await internalCreateWithdrawalGroup(ws, { amount: Amounts.jsonifyAmount(req.amount), -- cgit v1.2.3