diff options
Diffstat (limited to 'packages/taler-wallet-core/src/operations/withdraw.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/withdraw.ts | 59 |
1 files changed, 48 insertions, 11 deletions
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<WithdrawResponse | undefined> { + 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<WithdrawBatchResponse | undefined> { + 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<void> { + 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<string>; + + /** + * 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<string>(), + wgRecord: withdrawalGroup, + }; + let work: Promise<void>[] = []; + 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), ); } } |