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/transactions.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/transactions.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/transactions.ts | 234 |
1 files changed, 108 insertions, 126 deletions
diff --git a/packages/taler-wallet-core/src/operations/transactions.ts b/packages/taler-wallet-core/src/operations/transactions.ts index 671ccd556..1d3ea3d5a 100644 --- a/packages/taler-wallet-core/src/operations/transactions.ts +++ b/packages/taler-wallet-core/src/operations/transactions.ts @@ -69,13 +69,12 @@ import { WithdrawalRecordType, } from "../db.js"; import { - GetReadOnlyAccess, OPERATION_STATUS_ACTIVE_FIRST, OPERATION_STATUS_ACTIVE_LAST, PeerPushDebitStatus, timestampPreciseFromDb, timestampProtocolFromDb, - WalletStoresV1, + WalletDbReadOnlyTransaction, } from "../index.js"; import { InternalWalletState } from "../internal-wallet-state.js"; import { PendingTaskType, TaskId } from "../pending-types.js"; @@ -229,14 +228,14 @@ export async function getTransactionById( case TransactionType.InternalWithdrawal: case TransactionType.Withdrawal: { const withdrawalGroupId = parsedTx.withdrawalGroupId; - return await ws.db - .mktx((x) => [ - x.withdrawalGroups, - x.exchangeDetails, - x.exchanges, - x.operationRetries, - ]) - .runReadWrite(async (tx) => { + return await ws.db.runReadWriteTx( + [ + "withdrawalGroups", + "exchangeDetails", + "exchanges", + "operationRetries", + ], + async (tx) => { const withdrawalGroupRecord = await tx.withdrawalGroups.get(withdrawalGroupId); @@ -265,7 +264,8 @@ export async function getTransactionById( exchangeDetails, ort, ); - }); + }, + ); } case TransactionType.Recoup: @@ -273,15 +273,15 @@ export async function getTransactionById( case TransactionType.Payment: { const proposalId = parsedTx.proposalId; - return await ws.db - .mktx((x) => [ - x.purchases, - x.tombstones, - x.operationRetries, - x.refundGroups, - x.contractTerms, - ]) - .runReadWrite(async (tx) => { + return await ws.db.runReadWriteTx( + [ + "purchases", + "tombstones", + "operationRetries", + "contractTerms", + "refundGroups", + ], + async (tx) => { const purchase = await tx.purchases.get(proposalId); if (!purchase) throw Error("not found"); const download = await expectProposalDownload(ws, purchase, tx); @@ -299,7 +299,8 @@ export async function getTransactionById( refunds, payRetryRecord, ); - }); + }, + ); } case TransactionType.Refresh: { @@ -322,9 +323,9 @@ export async function getTransactionById( case TransactionType.Reward: { const tipId = parsedTx.walletRewardId; - return await ws.db - .mktx((x) => [x.rewards, x.operationRetries]) - .runReadWrite(async (tx) => { + return await ws.db.runReadWriteTx( + ["rewards", "operationRetries"], + async (tx) => { const tipRecord = await tx.rewards.get(tipId); if (!tipRecord) throw Error("not found"); @@ -332,14 +333,15 @@ export async function getTransactionById( TaskIdentifiers.forTipPickup(tipRecord), ); return buildTransactionForTip(tipRecord, retries); - }); + }, + ); } case TransactionType.Deposit: { const depositGroupId = parsedTx.depositGroupId; - return await ws.db - .mktx((x) => [x.depositGroups, x.operationRetries]) - .runReadWrite(async (tx) => { + return await ws.db.runReadWriteTx( + ["depositGroups", "operationRetries"], + async (tx) => { const depositRecord = await tx.depositGroups.get(depositGroupId); if (!depositRecord) throw Error("not found"); @@ -347,13 +349,14 @@ export async function getTransactionById( TaskIdentifiers.forDeposit(depositRecord), ); return buildTransactionForDeposit(depositRecord, retries); - }); + }, + ); } case TransactionType.Refund: { - return await ws.db - .mktx((x) => [x.refundGroups, x.contractTerms, x.purchases]) - .runReadOnly(async (tx) => { + return await ws.db.runReadOnlyTx( + ["refundGroups", "purchases", "operationRetries", "contractTerms"], + async (tx) => { const refundRecord = await tx.refundGroups.get( parsedTx.refundGroupId, ); @@ -365,12 +368,13 @@ export async function getTransactionById( refundRecord?.proposalId, ); return buildTransactionForRefund(refundRecord, contractData); - }); + }, + ); } case TransactionType.PeerPullDebit: { - return await ws.db - .mktx((x) => [x.peerPullDebit, x.contractTerms]) - .runReadWrite(async (tx) => { + return await ws.db.runReadWriteTx( + ["peerPullDebit", "contractTerms"], + async (tx) => { const debit = await tx.peerPullDebit.get(parsedTx.peerPullDebitId); if (!debit) throw Error("not found"); const contractTermsRec = await tx.contractTerms.get( @@ -382,13 +386,14 @@ export async function getTransactionById( debit, contractTermsRec.contractTermsRaw, ); - }); + }, + ); } case TransactionType.PeerPushDebit: { - return await ws.db - .mktx((x) => [x.peerPushDebit, x.contractTerms]) - .runReadWrite(async (tx) => { + return await ws.db.runReadWriteTx( + ["peerPushDebit", "contractTerms"], + async (tx) => { const debit = await tx.peerPushDebit.get(parsedTx.pursePub); if (!debit) throw Error("not found"); const ct = await tx.contractTerms.get(debit.contractTermsHash); @@ -397,19 +402,20 @@ export async function getTransactionById( debit, ct.contractTermsRaw, ); - }); + }, + ); } case TransactionType.PeerPushCredit: { const peerPushCreditId = parsedTx.peerPushCreditId; - return await ws.db - .mktx((x) => [ - x.peerPushCredit, - x.contractTerms, - x.withdrawalGroups, - x.operationRetries, - ]) - .runReadWrite(async (tx) => { + return await ws.db.runReadWriteTx( + [ + "peerPushCredit", + "contractTerms", + "withdrawalGroups", + "operationRetries", + ], + async (tx) => { const pushInc = await tx.peerPushCredit.get(peerPushCreditId); if (!pushInc) throw Error("not found"); const ct = await tx.contractTerms.get(pushInc.contractTermsHash); @@ -434,19 +440,20 @@ export async function getTransactionById( wg, wgOrt, ); - }); + }, + ); } case TransactionType.PeerPullCredit: { const pursePub = parsedTx.pursePub; - return await ws.db - .mktx((x) => [ - x.peerPullCredit, - x.contractTerms, - x.withdrawalGroups, - x.operationRetries, - ]) - .runReadWrite(async (tx) => { + return await ws.db.runReadWriteTx( + [ + "peerPullCredit", + "contractTerms", + "withdrawalGroups", + "operationRetries", + ], + async (tx) => { const pushInc = await tx.peerPullCredit.get(pursePub); if (!pushInc) throw Error("not found"); const ct = await tx.contractTerms.get(pushInc.contractTermsHash); @@ -472,7 +479,8 @@ export async function getTransactionById( wg, wgOrt, ); - }); + }, + ); } } } @@ -941,10 +949,7 @@ function buildTransactionForTip( } async function lookupMaybeContractData( - tx: GetReadOnlyAccess<{ - purchases: typeof WalletStoresV1.purchases; - contractTerms: typeof WalletStoresV1.contractTerms; - }>, + tx: WalletDbReadOnlyTransaction<["purchases", "contractTerms"]>, proposalId: string, ): Promise<WalletContractData | undefined> { let contractData: WalletContractData | undefined = undefined; @@ -1037,14 +1042,9 @@ export async function getWithdrawalTransactionByUri( ws: InternalWalletState, request: WithdrawalTransactionByURIRequest, ): Promise<TransactionWithdrawal | undefined> { - return await ws.db - .mktx((x) => [ - x.withdrawalGroups, - x.exchangeDetails, - x.exchanges, - x.operationRetries, - ]) - .runReadWrite(async (tx) => { + return await ws.db.runReadWriteTx( + ["withdrawalGroups", "exchangeDetails", "exchanges", "operationRetries"], + async (tx) => { const withdrawalGroupRecord = await tx.withdrawalGroups.indexes.byTalerWithdrawUri.get( request.talerWithdrawUri, @@ -1077,7 +1077,8 @@ export async function getWithdrawalTransactionByUri( exchangeDetails, ort, ); - }); + }, + ); } /** @@ -1094,29 +1095,29 @@ export async function getTransactions( filter.onlyState = transactionsRequest.filterByState; } - await ws.db - .mktx((x) => [ - x.coins, - x.denominations, - x.depositGroups, - x.exchangeDetails, - x.exchanges, - x.operationRetries, - x.peerPullDebit, - x.peerPushDebit, - x.peerPushCredit, - x.peerPullCredit, - x.planchets, - x.purchases, - x.contractTerms, - x.recoupGroups, - x.rewards, - x.tombstones, - x.withdrawalGroups, - x.refreshGroups, - x.refundGroups, - ]) - .runReadOnly(async (tx) => { + await ws.db.runReadOnlyTx( + [ + "coins", + "denominations", + "depositGroups", + "exchangeDetails", + "exchanges", + "operationRetries", + "peerPullDebit", + "peerPushDebit", + "peerPushCredit", + "peerPullCredit", + "planchets", + "purchases", + "contractTerms", + "recoupGroups", + "rewards", + "tombstones", + "withdrawalGroups", + "refreshGroups", + "refundGroups", + ], + async (tx) => { await iterRecordsForPeerPushDebit(tx, filter, async (pi) => { const amount = Amounts.parseOrThrow(pi.amount); const exchangesInTx = [pi.exchangeBaseUrl]; @@ -1463,7 +1464,8 @@ export async function getTransactions( transactions.push(buildTransactionForTip(tipRecord, retryRecord)); }); //ends REMOVE REWARDS - }); + }, + ); // One-off checks, because of a bug where the wallet previously // did not migrate the DB correctly and caused these amounts @@ -1829,9 +1831,7 @@ export function notifyTransition( * Iterate refresh records based on a filter. */ async function iterRecordsForRefresh( - tx: GetReadOnlyAccess<{ - refreshGroups: typeof WalletStoresV1.refreshGroups; - }>, + tx: WalletDbReadOnlyTransaction<["refreshGroups"]>, filter: TransactionRecordFilter, f: (r: RefreshGroupRecord) => Promise<void>, ): Promise<void> { @@ -1852,9 +1852,7 @@ async function iterRecordsForRefresh( } async function iterRecordsForWithdrawal( - tx: GetReadOnlyAccess<{ - withdrawalGroups: typeof WalletStoresV1.withdrawalGroups; - }>, + tx: WalletDbReadOnlyTransaction<["withdrawalGroups"]>, filter: TransactionRecordFilter, f: (r: WithdrawalGroupRecord) => Promise<void>, ): Promise<void> { @@ -1876,9 +1874,7 @@ async function iterRecordsForWithdrawal( } async function iterRecordsForDeposit( - tx: GetReadOnlyAccess<{ - depositGroups: typeof WalletStoresV1.depositGroups; - }>, + tx: WalletDbReadOnlyTransaction<["depositGroups"]>, filter: TransactionRecordFilter, f: (r: DepositGroupRecord) => Promise<void>, ): Promise<void> { @@ -1899,9 +1895,7 @@ async function iterRecordsForDeposit( } async function iterRecordsForReward( - tx: GetReadOnlyAccess<{ - rewards: typeof WalletStoresV1.rewards; - }>, + tx: WalletDbReadOnlyTransaction<["rewards"]>, filter: TransactionRecordFilter, f: (r: RewardRecord) => Promise<void>, ): Promise<void> { @@ -1917,9 +1911,7 @@ async function iterRecordsForReward( } async function iterRecordsForRefund( - tx: GetReadOnlyAccess<{ - refundGroups: typeof WalletStoresV1.refundGroups; - }>, + tx: WalletDbReadOnlyTransaction<["refundGroups"]>, filter: TransactionRecordFilter, f: (r: RefundGroupRecord) => Promise<void>, ): Promise<void> { @@ -1935,9 +1927,7 @@ async function iterRecordsForRefund( } async function iterRecordsForPurchase( - tx: GetReadOnlyAccess<{ - purchases: typeof WalletStoresV1.purchases; - }>, + tx: WalletDbReadOnlyTransaction<["purchases"]>, filter: TransactionRecordFilter, f: (r: PurchaseRecord) => Promise<void>, ): Promise<void> { @@ -1953,9 +1943,7 @@ async function iterRecordsForPurchase( } async function iterRecordsForPeerPullCredit( - tx: GetReadOnlyAccess<{ - peerPullCredit: typeof WalletStoresV1.peerPullCredit; - }>, + tx: WalletDbReadOnlyTransaction<["peerPullCredit"]>, filter: TransactionRecordFilter, f: (r: PeerPullCreditRecord) => Promise<void>, ): Promise<void> { @@ -1971,9 +1959,7 @@ async function iterRecordsForPeerPullCredit( } async function iterRecordsForPeerPullDebit( - tx: GetReadOnlyAccess<{ - peerPullDebit: typeof WalletStoresV1.peerPullDebit; - }>, + tx: WalletDbReadOnlyTransaction<["peerPullDebit"]>, filter: TransactionRecordFilter, f: (r: PeerPullPaymentIncomingRecord) => Promise<void>, ): Promise<void> { @@ -1989,9 +1975,7 @@ async function iterRecordsForPeerPullDebit( } async function iterRecordsForPeerPushDebit( - tx: GetReadOnlyAccess<{ - peerPushDebit: typeof WalletStoresV1.peerPushDebit; - }>, + tx: WalletDbReadOnlyTransaction<["peerPushDebit"]>, filter: TransactionRecordFilter, f: (r: PeerPushDebitRecord) => Promise<void>, ): Promise<void> { @@ -2007,9 +1991,7 @@ async function iterRecordsForPeerPushDebit( } async function iterRecordsForPeerPushCredit( - tx: GetReadOnlyAccess<{ - peerPushCredit: typeof WalletStoresV1.peerPushCredit; - }>, + tx: WalletDbReadOnlyTransaction<["peerPushCredit"]>, filter: TransactionRecordFilter, f: (r: PeerPushPaymentIncomingRecord) => Promise<void>, ): Promise<void> { |