diff options
Diffstat (limited to 'packages/taler-wallet-core/src/operations/pay-peer-pull-credit.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/pay-peer-pull-credit.ts | 230 |
1 files changed, 118 insertions, 112 deletions
diff --git a/packages/taler-wallet-core/src/operations/pay-peer-pull-credit.ts b/packages/taler-wallet-core/src/operations/pay-peer-pull-credit.ts index cc41abde9..e97466084 100644 --- a/packages/taler-wallet-core/src/operations/pay-peer-pull-credit.ts +++ b/packages/taler-wallet-core/src/operations/pay-peer-pull-credit.ts @@ -108,9 +108,9 @@ export class PeerPullCreditTransactionContext implements TransactionContext { async deleteTransaction(): Promise<void> { const { ws, pursePub } = this; - await ws.db - .mktx((x) => [x.withdrawalGroups, x.peerPullCredit, x.tombstones]) - .runReadWrite(async (tx) => { + await ws.db.runReadWriteTx( + ["withdrawalGroups", "peerPullCredit", "tombstones"], + async (tx) => { const pullIni = await tx.peerPullCredit.get(pursePub); if (!pullIni) { return; @@ -130,16 +130,17 @@ export class PeerPullCreditTransactionContext implements TransactionContext { await tx.tombstones.put({ id: TombstoneTag.DeletePeerPullCredit + ":" + pursePub, }); - }); + }, + ); return; } async suspendTransaction(): Promise<void> { const { ws, pursePub, retryTag, transactionId } = this; - const transitionInfo = await ws.db - .mktx((x) => [x.peerPullCredit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullCredit"], + async (tx) => { const pullCreditRec = await tx.peerPullCredit.get(pursePub); if (!pullCreditRec) { logger.warn(`peer pull credit ${pursePub} not found`); @@ -189,16 +190,17 @@ export class PeerPullCreditTransactionContext implements TransactionContext { }; } return undefined; - }); + }, + ); ws.taskScheduler.stopShepherdTask(retryTag); notifyTransition(ws, transactionId, transitionInfo); } async failTransaction(): Promise<void> { const { ws, pursePub, retryTag, transactionId } = this; - const transitionInfo = await ws.db - .mktx((x) => [x.peerPullCredit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullCredit"], + async (tx) => { const pullCreditRec = await tx.peerPullCredit.get(pursePub); if (!pullCreditRec) { logger.warn(`peer pull credit ${pursePub} not found`); @@ -239,16 +241,17 @@ export class PeerPullCreditTransactionContext implements TransactionContext { }; } return undefined; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); ws.taskScheduler.stopShepherdTask(retryTag); } async resumeTransaction(): Promise<void> { const { ws, pursePub, retryTag, transactionId } = this; - const transitionInfo = await ws.db - .mktx((x) => [x.peerPullCredit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullCredit"], + async (tx) => { const pullCreditRec = await tx.peerPullCredit.get(pursePub); if (!pullCreditRec) { logger.warn(`peer pull credit ${pursePub} not found`); @@ -297,16 +300,17 @@ export class PeerPullCreditTransactionContext implements TransactionContext { }; } return undefined; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); ws.taskScheduler.startShepherdTask(retryTag); } async abortTransaction(): Promise<void> { const { ws, pursePub, retryTag, transactionId } = this; - const transitionInfo = await ws.db - .mktx((x) => [x.peerPullCredit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullCredit"], + async (tx) => { const pullCreditRec = await tx.peerPullCredit.get(pursePub); if (!pullCreditRec) { logger.warn(`peer pull credit ${pursePub} not found`); @@ -350,7 +354,8 @@ export class PeerPullCreditTransactionContext implements TransactionContext { }; } return undefined; - }); + }, + ); ws.taskScheduler.stopShepherdTask(retryTag); notifyTransition(ws, transactionId, transitionInfo); ws.taskScheduler.startShepherdTask(retryTag); @@ -382,9 +387,9 @@ async function queryPurseForPeerPullCredit( switch (resp.status) { case HttpStatusCode.Gone: { // Exchange says that purse doesn't exist anymore => expired! - const transitionInfo = await ws.db - .mktx((x) => [x.peerPullCredit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullCredit"], + async (tx) => { const finPi = await tx.peerPullCredit.get(pullIni.pursePub); if (!finPi) { logger.warn("peerPullCredit not found anymore"); @@ -397,7 +402,8 @@ async function queryPurseForPeerPullCredit( await tx.peerPullCredit.put(finPi); const newTxState = computePeerPullCreditTransactionState(finPi); return { oldTxState, newTxState }; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); return TaskRunResult.backoff(); } @@ -419,11 +425,9 @@ async function queryPurseForPeerPullCredit( return TaskRunResult.backoff(); } - const reserve = await ws.db - .mktx((x) => [x.reserves]) - .runReadOnly(async (tx) => { - return await tx.reserves.get(pullIni.mergeReserveRowId); - }); + const reserve = await ws.db.runReadOnlyTx(["reserves"], async (tx) => { + return await tx.reserves.get(pullIni.mergeReserveRowId); + }); if (!reserve) { throw Error("reserve for peer pull credit not found in wallet DB"); @@ -443,9 +447,9 @@ async function queryPurseForPeerPullCredit( pub: reserve.reservePub, }, }); - const transitionInfo = await ws.db - .mktx((x) => [x.peerPullCredit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullCredit"], + async (tx) => { const finPi = await tx.peerPullCredit.get(pullIni.pursePub); if (!finPi) { logger.warn("peerPullCredit not found anymore"); @@ -458,7 +462,8 @@ async function queryPurseForPeerPullCredit( await tx.peerPullCredit.put(finPi); const newTxState = computePeerPullCreditTransactionState(finPi); return { oldTxState, newTxState }; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); return TaskRunResult.backoff(); } @@ -496,9 +501,9 @@ async function longpollKycStatus( // remove after the exchange is fixed or clarified kycStatusRes.status === HttpStatusCode.NoContent ) { - const transitionInfo = await ws.db - .mktx((x) => [x.peerPullCredit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullCredit"], + async (tx) => { const peerIni = await tx.peerPullCredit.get(pursePub); if (!peerIni) { return; @@ -513,7 +518,8 @@ async function longpollKycStatus( const newTxState = computePeerPullCreditTransactionState(peerIni); await tx.peerPullCredit.put(peerIni); return { oldTxState, newTxState }; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); } else if (kycStatusRes.status === HttpStatusCode.Accepted) { // FIXME: Do we have to update the URL here? @@ -545,15 +551,15 @@ async function processPeerPullCreditAbortingDeletePurse( }); logger.info(`deleted purse with response status ${resp.status}`); - const transitionInfo = await ws.db - .mktx((x) => [ - x.peerPullCredit, - x.refreshGroups, - x.denominations, - x.coinAvailability, - x.coins, - ]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + [ + "peerPullCredit", + "refreshGroups", + "denominations", + "coinAvailability", + "coins", + ], + async (tx) => { const ppiRec = await tx.peerPullCredit.get(pursePub); if (!ppiRec) { return undefined; @@ -569,7 +575,8 @@ async function processPeerPullCreditAbortingDeletePurse( oldTxState, newTxState, }; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); return TaskRunResult.backoff(); @@ -588,9 +595,9 @@ async function handlePeerPullCreditWithdrawing( }); const wgId = pullIni.withdrawalGroupId; let finished: boolean = false; - const transitionInfo = await ws.db - .mktx((x) => [x.peerPullCredit, x.withdrawalGroups]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullCredit", "withdrawalGroups"], + async (tx) => { const ppi = await tx.peerPullCredit.get(pullIni.pursePub); if (!ppi) { finished = true; @@ -619,7 +626,8 @@ async function handlePeerPullCreditWithdrawing( oldTxState, newTxState, }; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); if (finished) { return TaskRunResult.finished(); @@ -635,21 +643,20 @@ async function handlePeerPullCreditCreatePurse( ): Promise<TaskRunResult> { const purseFee = Amounts.stringify(Amounts.zeroOfAmount(pullIni.amount)); const pursePub = pullIni.pursePub; - const mergeReserve = await ws.db - .mktx((x) => [x.reserves]) - .runReadOnly(async (tx) => { - return tx.reserves.get(pullIni.mergeReserveRowId); - }); + const mergeReserve = await ws.db.runReadOnlyTx(["reserves"], async (tx) => { + return tx.reserves.get(pullIni.mergeReserveRowId); + }); if (!mergeReserve) { throw Error("merge reserve for peer pull payment not found in database"); } - const contractTermsRecord = await ws.db - .mktx((x) => [x.contractTerms]) - .runReadOnly(async (tx) => { + const contractTermsRecord = await ws.db.runReadOnlyTx( + ["contractTerms"], + async (tx) => { return tx.contractTerms.get(pullIni.contractTermsHash); - }); + }, + ); if (!contractTermsRecord) { throw Error("contract terms for peer pull payment not found in database"); @@ -731,9 +738,9 @@ async function handlePeerPullCreditCreatePurse( pursePub: pullIni.pursePub, }); - const transitionInfo = await ws.db - .mktx((x) => [x.peerPullCredit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullCredit"], + async (tx) => { const pi2 = await tx.peerPullCredit.get(pursePub); if (!pi2) { return; @@ -743,7 +750,8 @@ async function handlePeerPullCreditCreatePurse( await tx.peerPullCredit.put(pi2); const newTxState = computePeerPullCreditTransactionState(pi2); return { oldTxState, newTxState }; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); return TaskRunResult.backoff(); } @@ -753,11 +761,9 @@ export async function processPeerPullCredit( pursePub: string, cancellationToken: CancellationToken, ): Promise<TaskRunResult> { - const pullIni = await ws.db - .mktx((x) => [x.peerPullCredit]) - .runReadOnly(async (tx) => { - return tx.peerPullCredit.get(pursePub); - }); + const pullIni = await ws.db.runReadOnlyTx(["peerPullCredit"], async (tx) => { + return tx.peerPullCredit.get(pursePub); + }); if (!pullIni) { throw Error("peer pull payment initiation not found in database"); } @@ -843,9 +849,9 @@ async function processPeerPullCreditKycRequired( } else if (kycStatusRes.status === HttpStatusCode.Accepted) { const kycStatus = await kycStatusRes.json(); logger.info(`kyc status: ${j2s(kycStatus)}`); - const { transitionInfo, result } = await ws.db - .mktx((x) => [x.peerPullCredit]) - .runReadWrite(async (tx) => { + const { transitionInfo, result } = await ws.db.runReadWriteTx( + ["peerPullCredit"], + async (tx) => { const peerInc = await tx.peerPullCredit.get(pursePub); if (!peerInc) { return { @@ -877,7 +883,8 @@ async function processPeerPullCreditKycRequired( transitionInfo: { oldTxState, newTxState }, result: res, }; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); return TaskRunResult.backoff(); } else { @@ -943,44 +950,42 @@ async function getPreferredExchangeForCurrency( ): Promise<string | undefined> { // Find an exchange with the matching currency. // Prefer exchanges with the most recent withdrawal. - const url = await ws.db - .mktx((x) => [x.exchanges]) - .runReadOnly(async (tx) => { - const exchanges = await tx.exchanges.iter().toArray(); - let candidate = undefined; - for (const e of exchanges) { - if (e.detailsPointer?.currency !== currency) { - continue; - } - if (!candidate) { + const url = await ws.db.runReadOnlyTx(["exchanges"], async (tx) => { + const exchanges = await tx.exchanges.iter().toArray(); + let candidate = undefined; + for (const e of exchanges) { + if (e.detailsPointer?.currency !== currency) { + continue; + } + if (!candidate) { + candidate = e; + continue; + } + if (candidate.lastWithdrawal && !e.lastWithdrawal) { + continue; + } + const exchangeLastWithdrawal = timestampOptionalPreciseFromDb( + e.lastWithdrawal, + ); + const candidateLastWithdrawal = timestampOptionalPreciseFromDb( + candidate.lastWithdrawal, + ); + if (exchangeLastWithdrawal && candidateLastWithdrawal) { + if ( + AbsoluteTime.cmp( + AbsoluteTime.fromPreciseTimestamp(exchangeLastWithdrawal), + AbsoluteTime.fromPreciseTimestamp(candidateLastWithdrawal), + ) > 0 + ) { candidate = e; - continue; - } - if (candidate.lastWithdrawal && !e.lastWithdrawal) { - continue; - } - const exchangeLastWithdrawal = timestampOptionalPreciseFromDb( - e.lastWithdrawal, - ); - const candidateLastWithdrawal = timestampOptionalPreciseFromDb( - candidate.lastWithdrawal, - ); - if (exchangeLastWithdrawal && candidateLastWithdrawal) { - if ( - AbsoluteTime.cmp( - AbsoluteTime.fromPreciseTimestamp(exchangeLastWithdrawal), - AbsoluteTime.fromPreciseTimestamp(candidateLastWithdrawal), - ) > 0 - ) { - candidate = e; - } } } - if (candidate) { - return candidate.baseUrl; - } - return undefined; - }); + } + if (candidate) { + return candidate.baseUrl; + } + return undefined; + }); return url; } @@ -1036,9 +1041,9 @@ export async function initiatePeerPullPayment( const mergeTimestamp = TalerPreciseTimestamp.now(); - const transitionInfo = await ws.db - .mktx((x) => [x.peerPullCredit, x.contractTerms]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullCredit", "contractTerms"], + async (tx) => { const ppi: PeerPullCreditRecord = { amount: req.partialContractTerms.amount, contractTermsHash: hContractTerms, @@ -1066,7 +1071,8 @@ export async function initiatePeerPullPayment( h: hContractTerms, }); return { oldTxState, newTxState }; - }); + }, + ); const ctx = new PeerPullCreditTransactionContext(ws, pursePair.pub); |