diff options
author | Florian Dold <florian@dold.me> | 2024-02-19 12:49:17 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-02-19 17:47:35 +0100 |
commit | 1ec521b9d214b286e747b3ccb3113730ac3a2509 (patch) | |
tree | 2f3d2b2906810dca45859b8cbfb8d18d53b27e80 /packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts | |
parent | 1034ecb5f20bd8c75e37e0b4b454ea6c1f4c1da6 (diff) | |
download | wallet-core-1ec521b9d214b286e747b3ccb3113730ac3a2509.tar.xz |
wallet-core: simplify/unify DB access
Diffstat (limited to 'packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts | 124 |
1 files changed, 63 insertions, 61 deletions
diff --git a/packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts b/packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts index e5ae6b73b..1504f3d83 100644 --- a/packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts +++ b/packages/taler-wallet-core/src/operations/pay-peer-pull-debit.ts @@ -63,7 +63,7 @@ import { readTalerErrorResponse, } from "@gnu-taler/taler-util/http"; import { - DbReadWriteTransactionArr, + DbReadWriteTransaction, InternalWalletState, PeerPullDebitRecordStatus, PeerPullPaymentIncomingRecord, @@ -125,15 +125,13 @@ export class PeerPullDebitTransactionContext implements TransactionContext { const transactionId = this.transactionId; const ws = this.ws; const peerPullDebitId = this.peerPullDebitId; - await ws.db - .mktx((x) => [x.peerPullDebit, x.tombstones]) - .runReadWrite(async (tx) => { - const debit = await tx.peerPullDebit.get(peerPullDebitId); - if (debit) { - await tx.peerPullDebit.delete(peerPullDebitId); - await tx.tombstones.put({ id: transactionId }); - } - }); + await ws.db.runReadWriteTx(["peerPullDebit", "tombstones"], async (tx) => { + const debit = await tx.peerPullDebit.get(peerPullDebitId); + if (debit) { + await tx.peerPullDebit.delete(peerPullDebitId); + await tx.tombstones.put({ id: transactionId }); + } + }); } async suspendTransaction(): Promise<void> { @@ -141,9 +139,9 @@ export class PeerPullDebitTransactionContext implements TransactionContext { const transactionId = this.transactionId; const ws = this.ws; const peerPullDebitId = this.peerPullDebitId; - const transitionInfo = await ws.db - .mktx((x) => [x.peerPullDebit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullDebit"], + async (tx) => { const pullDebitRec = await tx.peerPullDebit.get(peerPullDebitId); if (!pullDebitRec) { logger.warn(`peer pull debit ${peerPullDebitId} not found`); @@ -183,7 +181,8 @@ export class PeerPullDebitTransactionContext implements TransactionContext { }; } return undefined; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); ws.taskScheduler.stopShepherdTask(taskId); } @@ -295,7 +294,7 @@ export class PeerPullDebitTransactionContext implements TransactionContext { opts: { extraStores: StoreNameArray }, f: ( rec: PeerPullPaymentIncomingRecord, - tx: DbReadWriteTransactionArr< + tx: DbReadWriteTransaction< typeof WalletStoresV1, ["peerPullDebit", ...StoreNameArray] >, @@ -498,9 +497,9 @@ async function processPeerPullDebitAbortingRefresh( tag: TransactionType.PeerPullDebit, peerPullDebitId, }); - const transitionInfo = await ws.db - .mktx((x) => [x.refreshGroups, x.peerPullDebit]) - .runReadWrite(async (tx) => { + const transitionInfo = await ws.db.runReadWriteTx( + ["peerPullDebit", "refreshGroups"], + async (tx) => { const refreshGroup = await tx.refreshGroups.get(abortRefreshGroupId); let newOpState: PeerPullDebitRecordStatus | undefined; if (!refreshGroup) { @@ -529,7 +528,8 @@ async function processPeerPullDebitAbortingRefresh( return { oldTxState, newTxState }; } return undefined; - }); + }, + ); notifyTransition(ws, transactionId, transitionInfo); // FIXME: Shouldn't this be finished in some cases?! return TaskRunResult.backoff(); @@ -539,11 +539,12 @@ export async function processPeerPullDebit( ws: InternalWalletState, peerPullDebitId: string, ): Promise<TaskRunResult> { - const peerPullInc = await ws.db - .mktx((x) => [x.peerPullDebit]) - .runReadOnly(async (tx) => { + const peerPullInc = await ws.db.runReadOnlyTx( + ["peerPullDebit"], + async (tx) => { return tx.peerPullDebit.get(peerPullDebitId); - }); + }, + ); if (!peerPullInc) { throw Error("peer pull debit not found"); } @@ -575,11 +576,12 @@ export async function confirmPeerPullDebit( throw Error("invalid request, transactionId or peerPullDebitId required"); } - const peerPullInc = await ws.db - .mktx((x) => [x.peerPullDebit]) - .runReadOnly(async (tx) => { + const peerPullInc = await ws.db.runReadOnlyTx( + ["peerPullDebit"], + async (tx) => { return tx.peerPullDebit.get(peerPullDebitId); - }); + }, + ); if (!peerPullInc) { throw Error( @@ -610,16 +612,16 @@ export async function confirmPeerPullDebit( coinSelRes.result.coins, ); - await ws.db - .mktx((x) => [ - x.exchanges, - x.coins, - x.denominations, - x.refreshGroups, - x.peerPullDebit, - x.coinAvailability, - ]) - .runReadWrite(async (tx) => { + await ws.db.runReadWriteTx( + [ + "exchanges", + "coins", + "denominations", + "refreshGroups", + "peerPullDebit", + "coinAvailability", + ], + async (tx) => { await spendCoins(ws, tx, { // allocationId: `txn:peer-pull-debit:${req.peerPullDebitId}`, allocationId: constructTransactionIdentifier({ @@ -646,7 +648,8 @@ export async function confirmPeerPullDebit( }; } await tx.peerPullDebit.put(pi); - }); + }, + ); const ctx = new PeerPullDebitTransactionContext(ws, peerPullDebitId); @@ -678,9 +681,9 @@ export async function preparePeerPullDebit( throw Error("got invalid taler://pay-pull URI"); } - const existing = await ws.db - .mktx((x) => [x.peerPullDebit, x.contractTerms]) - .runReadOnly(async (tx) => { + const existing = await ws.db.runReadOnlyTx( + ["peerPullDebit", "contractTerms"], + async (tx) => { const peerPullDebitRecord = await tx.peerPullDebit.indexes.byExchangeAndContractPriv.get([ uri.exchangeBaseUrl, @@ -696,7 +699,8 @@ export async function preparePeerPullDebit( return; } return { peerPullDebitRecord, contractTerms }; - }); + }, + ); if (existing) { return { @@ -780,25 +784,23 @@ export async function preparePeerPullDebit( coinSelRes.result.coins, ); - await ws.db - .mktx((x) => [x.peerPullDebit, x.contractTerms]) - .runReadWrite(async (tx) => { - await tx.contractTerms.put({ - h: contractTermsHash, - contractTermsRaw: contractTerms, - }), - await tx.peerPullDebit.add({ - peerPullDebitId, - contractPriv: contractPriv, - exchangeBaseUrl: exchangeBaseUrl, - pursePub: pursePub, - timestampCreated: timestampPreciseToDb(TalerPreciseTimestamp.now()), - contractTermsHash, - amount: contractTerms.amount, - status: PeerPullDebitRecordStatus.DialogProposed, - totalCostEstimated: Amounts.stringify(totalAmount), - }); - }); + await ws.db.runReadWriteTx(["peerPullDebit", "contractTerms"], async (tx) => { + await tx.contractTerms.put({ + h: contractTermsHash, + contractTermsRaw: contractTerms, + }), + await tx.peerPullDebit.add({ + peerPullDebitId, + contractPriv: contractPriv, + exchangeBaseUrl: exchangeBaseUrl, + pursePub: pursePub, + timestampCreated: timestampPreciseToDb(TalerPreciseTimestamp.now()), + contractTermsHash, + amount: contractTerms.amount, + status: PeerPullDebitRecordStatus.DialogProposed, + totalCostEstimated: Amounts.stringify(totalAmount), + }); + }); return { amount: contractTerms.amount, |