diff options
Diffstat (limited to 'packages/taler-wallet-core/src/operations/pay-peer-push-debit.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/pay-peer-push-debit.ts | 204 |
1 files changed, 106 insertions, 98 deletions
diff --git a/packages/taler-wallet-core/src/operations/pay-peer-push-debit.ts b/packages/taler-wallet-core/src/operations/pay-peer-push-debit.ts index 165c8deee..91c5430be 100644 --- a/packages/taler-wallet-core/src/operations/pay-peer-push-debit.ts +++ b/packages/taler-wallet-core/src/operations/pay-peer-push-debit.ts @@ -101,22 +101,20 @@ export class PeerPushDebitTransactionContext implements TransactionContext { async deleteTransaction(): Promise<void> { const { ws, pursePub, transactionId } = this; - await ws.db - .mktx((x) => [x.peerPushDebit, x.tombstones]) - .runReadWrite(async (tx) => { - const debit = await tx.peerPushDebit.get(pursePub); - if (debit) { - await tx.peerPushDebit.delete(pursePub); - await tx.tombstones.put({ id: transactionId }); - } - }); + await ws.db.runReadWriteTx(["peerPushDebit", "tombstones"], async (tx) => { + const debit = await tx.peerPushDebit.get(pursePub); + if (debit) { + await tx.peerPushDebit.delete(pursePub); + await tx.tombstones.put({ id: transactionId }); + } + }); } async suspendTransaction(): Promise<void> { const { ws, pursePub, transactionId, retryTag } = this; - const transitionInfo = await ws.db - .mktx((x) => [x.peerPushDebit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPushDebit"], + async (tx) => { const pushDebitRec = await tx.peerPushDebit.get(pursePub); if (!pushDebitRec) { logger.warn(`peer push debit ${pursePub} not found`); @@ -164,16 +162,17 @@ export class PeerPushDebitTransactionContext implements TransactionContext { }; } return undefined; - }); + }, + ); ws.taskScheduler.stopShepherdTask(retryTag); notifyTransition(ws, transactionId, transitionInfo); } async abortTransaction(): Promise<void> { const { ws, pursePub, transactionId, retryTag } = this; - const transitionInfo = await ws.db - .mktx((x) => [x.peerPushDebit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPushDebit"], + async (tx) => { const pushDebitRec = await tx.peerPushDebit.get(pursePub); if (!pushDebitRec) { logger.warn(`peer push debit ${pursePub} not found`); @@ -216,7 +215,8 @@ export class PeerPushDebitTransactionContext implements TransactionContext { }; } return undefined; - }); + }, + ); ws.taskScheduler.stopShepherdTask(retryTag); notifyTransition(ws, transactionId, transitionInfo); ws.taskScheduler.startShepherdTask(retryTag); @@ -224,9 +224,9 @@ export class PeerPushDebitTransactionContext implements TransactionContext { async resumeTransaction(): Promise<void> { const { ws, pursePub, transactionId, retryTag } = this; - const transitionInfo = await ws.db - .mktx((x) => [x.peerPushDebit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPushDebit"], + async (tx) => { const pushDebitRec = await tx.peerPushDebit.get(pursePub); if (!pushDebitRec) { logger.warn(`peer push debit ${pursePub} not found`); @@ -274,16 +274,17 @@ export class PeerPushDebitTransactionContext implements TransactionContext { }; } return undefined; - }); + }, + ); ws.taskScheduler.startShepherdTask(retryTag); notifyTransition(ws, transactionId, transitionInfo); } async failTransaction(): Promise<void> { const { ws, pursePub, transactionId, retryTag } = this; - const transitionInfo = await ws.db - .mktx((x) => [x.peerPushDebit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPushDebit"], + async (tx) => { const pushDebitRec = await tx.peerPushDebit.get(pursePub); if (!pushDebitRec) { logger.warn(`peer push debit ${pursePub} not found`); @@ -326,7 +327,8 @@ export class PeerPushDebitTransactionContext implements TransactionContext { }; } return undefined; - }); + }, + ); ws.taskScheduler.stopShepherdTask(retryTag); notifyTransition(ws, transactionId, transitionInfo); ws.taskScheduler.startShepherdTask(retryTag); @@ -411,28 +413,26 @@ async function handlePurseCreationConflict( ); } - await ws.db - .mktx((x) => [x.peerPushDebit]) - .runReadWrite(async (tx) => { - const myPpi = await tx.peerPushDebit.get(peerPushInitiation.pursePub); - if (!myPpi) { - return; - } - switch (myPpi.status) { - case PeerPushDebitStatus.PendingCreatePurse: - case PeerPushDebitStatus.SuspendedCreatePurse: { - const sel = coinSelRes.result; - myPpi.coinSel = { - coinPubs: sel.coins.map((x) => x.coinPub), - contributions: sel.coins.map((x) => x.contribution), - }; - break; - } - default: - return; + await ws.db.runReadWriteTx(["peerPushDebit"], async (tx) => { + const myPpi = await tx.peerPushDebit.get(peerPushInitiation.pursePub); + if (!myPpi) { + return; + } + switch (myPpi.status) { + case PeerPushDebitStatus.PendingCreatePurse: + case PeerPushDebitStatus.SuspendedCreatePurse: { + const sel = coinSelRes.result; + myPpi.coinSel = { + coinPubs: sel.coins.map((x) => x.coinPub), + contributions: sel.coins.map((x) => x.contribution), + }; + break; } - await tx.peerPushDebit.put(myPpi); - }); + default: + return; + } + await tx.peerPushDebit.put(myPpi); + }); return TaskRunResult.progress(); } @@ -448,11 +448,12 @@ async function processPeerPushDebitCreateReserve( logger.trace(`processing ${transactionId} pending(create-reserve)`); - 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(hContractTerms); - }); + }, + ); if (!contractTermsRecord) { throw Error( @@ -583,15 +584,15 @@ async function processPeerPushDebitAbortingDeletePurse( }); logger.info(`deleted purse with response status ${resp.status}`); - const transitionInfo = await ws.db - .mktx((x) => [ - x.peerPushDebit, - x.refreshGroups, - x.denominations, - x.coinAvailability, - x.coins, - ]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + [ + "peerPushDebit", + "refreshGroups", + "denominations", + "coinAvailability", + "coins", + ], + async (tx) => { const ppiRec = await tx.peerPushDebit.get(pursePub); if (!ppiRec) { return undefined; @@ -626,7 +627,8 @@ async function processPeerPushDebitAbortingDeletePurse( oldTxState, newTxState, }; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); return TaskRunResult.backoff(); @@ -646,9 +648,9 @@ async function transitionPeerPushDebitTransaction( tag: TransactionType.PeerPushDebit, pursePub, }); - const transitionInfo = await ws.db - .mktx((x) => [x.peerPushDebit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPushDebit"], + async (tx) => { const ppiRec = await tx.peerPushDebit.get(pursePub); if (!ppiRec) { return undefined; @@ -664,7 +666,8 @@ async function transitionPeerPushDebitTransaction( oldTxState, newTxState, }; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); } @@ -679,9 +682,9 @@ async function processPeerPushDebitAbortingRefreshDeleted( tag: TransactionType.PeerPushDebit, pursePub: peerPushInitiation.pursePub, }); - const transitionInfo = await ws.db - .mktx((x) => [x.refreshGroups, x.peerPushDebit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["refreshGroups", "peerPushDebit"], + async (tx) => { const refreshGroup = await tx.refreshGroups.get(abortRefreshGroupId); let newOpState: PeerPushDebitStatus | undefined; if (!refreshGroup) { @@ -710,7 +713,8 @@ async function processPeerPushDebitAbortingRefreshDeleted( return { oldTxState, newTxState }; } return undefined; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); // FIXME: Shouldn't this be finished in some cases?! return TaskRunResult.backoff(); @@ -727,9 +731,9 @@ async function processPeerPushDebitAbortingRefreshExpired( tag: TransactionType.PeerPushDebit, pursePub: peerPushInitiation.pursePub, }); - const transitionInfo = await ws.db - .mktx((x) => [x.refreshGroups, x.peerPushDebit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPushDebit", "refreshGroups"], + async (tx) => { const refreshGroup = await tx.refreshGroups.get(abortRefreshGroupId); let newOpState: PeerPushDebitStatus | undefined; if (!refreshGroup) { @@ -758,7 +762,8 @@ async function processPeerPushDebitAbortingRefreshExpired( return { oldTxState, newTxState }; } return undefined; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); // FIXME: Shouldn't this be finished in some cases?! return TaskRunResult.backoff(); @@ -810,15 +815,15 @@ async function processPeerPushDebitReady( } } else if (resp.status === HttpStatusCode.Gone) { logger.info(`purse ${pursePub} is gone, aborting peer-push-debit`); - const transitionInfo = await ws.db - .mktx((x) => [ - x.peerPushDebit, - x.refreshGroups, - x.denominations, - x.coinAvailability, - x.coins, - ]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + [ + "peerPushDebit", + "refreshGroups", + "denominations", + "coinAvailability", + "coins", + ], + async (tx) => { const ppiRec = await tx.peerPushDebit.get(pursePub); if (!ppiRec) { return undefined; @@ -853,7 +858,8 @@ async function processPeerPushDebitReady( oldTxState, newTxState, }; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); return TaskRunResult.backoff(); } else { @@ -867,11 +873,12 @@ export async function processPeerPushDebit( pursePub: string, cancellationToken: CancellationToken, ): Promise<TaskRunResult> { - const peerPushInitiation = await ws.db - .mktx((x) => [x.peerPushDebit]) - .runReadOnly(async (tx) => { + const peerPushInitiation = await ws.db.runReadOnlyTx( + ["peerPushDebit"], + async (tx) => { return tx.peerPushDebit.get(pursePub); - }); + }, + ); if (!peerPushInitiation) { throw Error("peer push payment not found"); } @@ -953,17 +960,17 @@ export async function initiatePeerPushDebit( const contractEncNonce = encodeCrock(getRandomBytes(24)); - const transitionInfo = await ws.db - .mktx((x) => [ - x.exchanges, - x.contractTerms, - x.coins, - x.coinAvailability, - x.denominations, - x.refreshGroups, - x.peerPushDebit, - ]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + [ + "exchanges", + "contractTerms", + "coins", + "coinAvailability", + "denominations", + "refreshGroups", + "peerPushDebit", + ], + async (tx) => { // FIXME: Instead of directly doing a spendCoin here, // we might want to mark the coins as used and spend them // after we've been able to create the purse. @@ -1012,7 +1019,8 @@ export async function initiatePeerPushDebit( oldTxState: { major: TransactionMajorState.None }, newTxState, }; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); ws.notify({ type: NotificationType.BalanceChange, |