From 3cf2d4cba919203065f210f80f3f081948ad257a Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 9 Feb 2023 22:44:36 +0100 Subject: wallet-core: expose withdrawal progress, towards huge withdrawal test --- .../taler-wallet-core/src/operations/withdraw.ts | 59 ++++++++++++++++++---- 1 file changed, 48 insertions(+), 11 deletions(-) (limited to 'packages/taler-wallet-core/src/operations') diff --git a/packages/taler-wallet-core/src/operations/withdraw.ts b/packages/taler-wallet-core/src/operations/withdraw.ts index 667b97361..caa280fe5 100644 --- a/packages/taler-wallet-core/src/operations/withdraw.ts +++ b/packages/taler-wallet-core/src/operations/withdraw.ts @@ -462,9 +462,10 @@ async function processPlanchetGenerate( */ async function processPlanchetExchangeRequest( ws: InternalWalletState, - withdrawalGroup: WithdrawalGroupRecord, + wgContext: WithdrawalGroupContext, coinIdx: number, ): Promise { + const withdrawalGroup: WithdrawalGroupRecord = wgContext.wgRecord; logger.info( `processing planchet exchange request ${withdrawalGroup.withdrawalGroupId}/${coinIdx}`, ); @@ -593,8 +594,9 @@ async function processPlanchetExchangeRequest( */ async function processPlanchetExchangeBatchRequest( ws: InternalWalletState, - withdrawalGroup: WithdrawalGroupRecord, + wgContext: WithdrawalGroupContext, ): Promise { + const withdrawalGroup: WithdrawalGroupRecord = wgContext.wgRecord; logger.info( `processing planchet exchange batch request ${withdrawalGroup.withdrawalGroupId}`, ); @@ -671,10 +673,11 @@ async function processPlanchetExchangeBatchRequest( async function processPlanchetVerifyAndStoreCoin( ws: InternalWalletState, - withdrawalGroup: WithdrawalGroupRecord, + wgContext: WithdrawalGroupContext, coinIdx: number, resp: WithdrawResponse, ): Promise { + const withdrawalGroup = wgContext.wgRecord; const d = await ws.db .mktx((x) => [x.withdrawalGroups, x.planchets, x.denominations]) .runReadOnly(async (tx) => { @@ -786,6 +789,8 @@ async function processPlanchetVerifyAndStoreCoin( const planchetCoinPub = planchet.coinPub; + wgContext.planchetsFinished.add(planchet.coinPub); + // Check if this is the first time that the whole // withdrawal succeeded. If so, mark the withdrawal // group as finished. @@ -811,6 +816,8 @@ async function processPlanchetVerifyAndStoreCoin( if (firstSuccess) { ws.notify({ type: NotificationType.CoinWithdrawn, + numTotal: wgContext.numPlanchets, + numWithdrawn: wgContext.planchetsFinished.size, }); } } @@ -983,6 +990,21 @@ enum BankStatusResultCode { Aborted = "aborted", } +/** + * Withdrawal context that is kept in-memory. + * + * Used to store some cached info during a withdrawal operation. + */ +export interface WithdrawalGroupContext { + numPlanchets: number; + planchetsFinished: Set; + + /** + * Cached withdrawal group record from the database. + */ + wgRecord: WithdrawalGroupRecord; +} + export async function processWithdrawalGroup( ws: InternalWalletState, withdrawalGroupId: string, @@ -1122,8 +1144,27 @@ export async function processWithdrawalGroup( .map((x) => x.count) .reduce((a, b) => a + b); + const wgContext: WithdrawalGroupContext = { + numPlanchets: numTotalCoins, + planchetsFinished: new Set(), + wgRecord: withdrawalGroup, + }; + let work: Promise[] = []; + await ws.db + .mktx((x) => [x.planchets]) + .runReadOnly(async (tx) => { + const planchets = await tx.planchets.indexes.byGroup.getAll( + withdrawalGroupId, + ); + for (const p of planchets) { + if (p.planchetStatus === PlanchetStatus.WithdrawalDone) { + wgContext.planchetsFinished.add(p.coinPub); + } + } + }); + for (let i = 0; i < numTotalCoins; i++) { work.push(processPlanchetGenerate(ws, withdrawalGroup, i)); } @@ -1134,7 +1175,7 @@ export async function processWithdrawalGroup( work = []; if (ws.batchWithdrawal) { - const resp = await processPlanchetExchangeBatchRequest(ws, withdrawalGroup); + const resp = await processPlanchetExchangeBatchRequest(ws, wgContext); if (!resp) { throw Error("unable to do batch withdrawal"); } @@ -1142,7 +1183,7 @@ export async function processWithdrawalGroup( work.push( processPlanchetVerifyAndStoreCoin( ws, - withdrawalGroup, + wgContext, coinIdx, resp.ev_sigs[coinIdx], ), @@ -1150,16 +1191,12 @@ export async function processWithdrawalGroup( } } else { for (let coinIdx = 0; coinIdx < numTotalCoins; coinIdx++) { - const resp = await processPlanchetExchangeRequest( - ws, - withdrawalGroup, - coinIdx, - ); + const resp = await processPlanchetExchangeRequest(ws, wgContext, coinIdx); if (!resp) { continue; } work.push( - processPlanchetVerifyAndStoreCoin(ws, withdrawalGroup, coinIdx, resp), + processPlanchetVerifyAndStoreCoin(ws, wgContext, coinIdx, resp), ); } } -- cgit v1.2.3