From 1edc144b3595ae1ab6b8af7a43d26b811b2c2623 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 23 Apr 2024 03:09:40 +0200 Subject: wallet-core: pass options object to all transactions --- packages/taler-wallet-core/src/deposits.ts | 258 ++++++++++++++++------------- 1 file changed, 139 insertions(+), 119 deletions(-) (limited to 'packages/taler-wallet-core/src/deposits.ts') diff --git a/packages/taler-wallet-core/src/deposits.ts b/packages/taler-wallet-core/src/deposits.ts index 50f26ea9c..dbba55247 100644 --- a/packages/taler-wallet-core/src/deposits.ts +++ b/packages/taler-wallet-core/src/deposits.ts @@ -139,22 +139,25 @@ export class DepositTransactionContext implements TransactionContext { const ws = this.wex; // FIXME: We should check first if we are in a final state // where deletion is allowed. - await ws.db.runReadWriteTx(["depositGroups", "tombstones"], async (tx) => { - const tipRecord = await tx.depositGroups.get(depositGroupId); - if (tipRecord) { - await tx.depositGroups.delete(depositGroupId); - await tx.tombstones.put({ - id: TombstoneTag.DeleteDepositGroup + ":" + depositGroupId, - }); - } - }); + await ws.db.runReadWriteTx( + { storeNames: ["depositGroups", "tombstones"] }, + async (tx) => { + const tipRecord = await tx.depositGroups.get(depositGroupId); + if (tipRecord) { + await tx.depositGroups.delete(depositGroupId); + await tx.tombstones.put({ + id: TombstoneTag.DeleteDepositGroup + ":" + depositGroupId, + }); + } + }, + ); return; } async suspendTransaction(): Promise { const { wex, depositGroupId, transactionId, taskId: retryTag } = this; const transitionInfo = await wex.db.runReadWriteTx( - ["depositGroups"], + { storeNames: ["depositGroups"] }, async (tx) => { const dg = await tx.depositGroups.get(depositGroupId); if (!dg) { @@ -197,7 +200,7 @@ export class DepositTransactionContext implements TransactionContext { async abortTransaction(): Promise { const { wex, depositGroupId, transactionId, taskId: retryTag } = this; const transitionInfo = await wex.db.runReadWriteTx( - ["depositGroups"], + { storeNames: ["depositGroups"] }, async (tx) => { const dg = await tx.depositGroups.get(depositGroupId); if (!dg) { @@ -235,7 +238,7 @@ export class DepositTransactionContext implements TransactionContext { async resumeTransaction(): Promise { const { wex, depositGroupId, transactionId, taskId: retryTag } = this; const transitionInfo = await wex.db.runReadWriteTx( - ["depositGroups"], + { storeNames: ["depositGroups"] }, async (tx) => { const dg = await tx.depositGroups.get(depositGroupId); if (!dg) { @@ -278,7 +281,7 @@ export class DepositTransactionContext implements TransactionContext { async failTransaction(): Promise { const { wex, depositGroupId, transactionId, taskId } = this; const transitionInfo = await wex.db.runReadWriteTx( - ["depositGroups"], + { storeNames: ["depositGroups"] }, async (tx) => { const dg = await tx.depositGroups.get(depositGroupId); if (!dg) { @@ -435,7 +438,7 @@ async function refundDepositGroup( default: { const coinPub = payCoinSelection.coinPubs[i]; const coinExchange = await wex.db.runReadOnlyTx( - ["coins"], + { storeNames: ["coins"] }, async (tx) => { const coinRecord = await tx.coins.get(coinPub); checkDbInvariant(!!coinRecord); @@ -497,14 +500,16 @@ async function refundDepositGroup( const currency = Amounts.currencyOf(depositGroup.totalPayCost); const res = await wex.db.runReadWriteTx( - [ - "depositGroups", - "refreshGroups", - "refreshSessions", - "coins", - "denominations", - "coinAvailability", - ], + { + storeNames: [ + "depositGroups", + "refreshGroups", + "refreshSessions", + "coins", + "denominations", + "coinAvailability", + ], + }, async (tx) => { const newDg = await tx.depositGroups.get(depositGroup.depositGroupId); if (!newDg) { @@ -571,7 +576,7 @@ async function waitForRefreshOnDepositGroup( depositGroupId: depositGroup.depositGroupId, }); const transitionInfo = await wex.db.runReadWriteTx( - ["depositGroups", "refreshGroups"], + { storeNames: ["depositGroups", "refreshGroups"] }, async (tx) => { const refreshGroup = await tx.refreshGroups.get(abortRefreshGroupId); let newOpState: DepositOperationStatus | undefined; @@ -660,7 +665,7 @@ async function processDepositGroupPendingKyc( kycStatusRes.status === HttpStatusCode.NoContent ) { const transitionInfo = await wex.db.runReadWriteTx( - ["depositGroups"], + { storeNames: ["depositGroups"] }, async (tx) => { const newDg = await tx.depositGroups.get(depositGroupId); if (!newDg) { @@ -719,7 +724,7 @@ async function transitionToKycRequired( const kycStatus = await kycStatusReq.json(); logger.info(`kyc status: ${j2s(kycStatus)}`); const transitionInfo = await wex.db.runReadWriteTx( - ["depositGroups"], + { storeNames: ["depositGroups"] }, async (tx) => { const dg = await tx.depositGroups.get(depositGroupId); if (!dg) { @@ -768,7 +773,7 @@ async function processDepositGroupPendingTrack( const coinPub = payCoinSelection.coinPubs[i]; // FIXME: Make the URL part of the coin selection? const exchangeBaseUrl = await wex.db.runReadWriteTx( - ["coins"], + { storeNames: ["coins"] }, async (tx) => { const coinRecord = await tx.coins.get(coinPub); checkDbInvariant(!!coinRecord); @@ -844,41 +849,44 @@ async function processDepositGroupPendingTrack( } if (updatedTxStatus !== undefined) { - await wex.db.runReadWriteTx(["depositGroups"], async (tx) => { - const dg = await tx.depositGroups.get(depositGroupId); - if (!dg) { - return; - } - if (!dg.statusPerCoin) { - return; - } - if (updatedTxStatus !== undefined) { - dg.statusPerCoin[i] = updatedTxStatus; - } - if (newWiredCoin) { - /** - * FIXME: if there is a new wire information from the exchange - * it should add up to the previous tracking states. - * - * This may loose information by overriding prev state. - * - * And: add checks to integration tests - */ - if (!dg.trackingState) { - dg.trackingState = {}; + await wex.db.runReadWriteTx( + { storeNames: ["depositGroups"] }, + async (tx) => { + const dg = await tx.depositGroups.get(depositGroupId); + if (!dg) { + return; } - - dg.trackingState[newWiredCoin.id] = newWiredCoin.value; - } - await tx.depositGroups.put(dg); - }); + if (!dg.statusPerCoin) { + return; + } + if (updatedTxStatus !== undefined) { + dg.statusPerCoin[i] = updatedTxStatus; + } + if (newWiredCoin) { + /** + * FIXME: if there is a new wire information from the exchange + * it should add up to the previous tracking states. + * + * This may loose information by overriding prev state. + * + * And: add checks to integration tests + */ + if (!dg.trackingState) { + dg.trackingState = {}; + } + + dg.trackingState[newWiredCoin.id] = newWiredCoin.value; + } + await tx.depositGroups.put(dg); + }, + ); } } let allWired = true; const transitionInfo = await wex.db.runReadWriteTx( - ["depositGroups"], + { storeNames: ["depositGroups"] }, async (tx) => { const dg = await tx.depositGroups.get(depositGroupId); if (!dg) { @@ -929,7 +937,7 @@ async function processDepositGroupPendingDeposit( logger.info("processing deposit group in pending(deposit)"); const depositGroupId = depositGroup.depositGroupId; const contractTermsRec = await wex.db.runReadOnlyTx( - ["contractTerms"], + { storeNames: ["contractTerms"] }, async (tx) => { return tx.contractTerms.get(depositGroup.contractTermsHash); }, @@ -987,14 +995,16 @@ async function processDepositGroupPendingDeposit( } const transitionDone = await wex.db.runReadWriteTx( - [ - "depositGroups", - "coins", - "coinAvailability", - "refreshGroups", - "refreshSessions", - "denominations", - ], + { + storeNames: [ + "depositGroups", + "coins", + "coinAvailability", + "refreshGroups", + "refreshSessions", + "denominations", + ], + }, async (tx) => { const dg = await tx.depositGroups.get(depositGroupId); if (!dg) { @@ -1094,27 +1104,30 @@ async function processDepositGroupPendingDeposit( codecForBatchDepositSuccess(), ); - await wex.db.runReadWriteTx(["depositGroups"], async (tx) => { - const dg = await tx.depositGroups.get(depositGroupId); - if (!dg) { - return; - } - if (!dg.statusPerCoin) { - return; - } - for (const batchIndex of batchIndexes) { - const coinStatus = dg.statusPerCoin[batchIndex]; - switch (coinStatus) { - case DepositElementStatus.DepositPending: - dg.statusPerCoin[batchIndex] = DepositElementStatus.Tracking; - await tx.depositGroups.put(dg); + await wex.db.runReadWriteTx( + { storeNames: ["depositGroups"] }, + async (tx) => { + const dg = await tx.depositGroups.get(depositGroupId); + if (!dg) { + return; } - } - }); + if (!dg.statusPerCoin) { + return; + } + for (const batchIndex of batchIndexes) { + const coinStatus = dg.statusPerCoin[batchIndex]; + switch (coinStatus) { + case DepositElementStatus.DepositPending: + dg.statusPerCoin[batchIndex] = DepositElementStatus.Tracking; + await tx.depositGroups.put(dg); + } + } + }, + ); } const transitionInfo = await wex.db.runReadWriteTx( - ["depositGroups"], + { storeNames: ["depositGroups"] }, async (tx) => { const dg = await tx.depositGroups.get(depositGroupId); if (!dg) { @@ -1140,7 +1153,7 @@ export async function processDepositGroup( depositGroupId: string, ): Promise { const depositGroup = await wex.db.runReadOnlyTx( - ["depositGroups"], + { storeNames: ["depositGroups"] }, async (tx) => { return tx.depositGroups.get(depositGroupId); }, @@ -1174,7 +1187,7 @@ async function getExchangeWireFee( time: TalerProtocolTimestamp, ): Promise { const exchangeDetails = await wex.db.runReadOnlyTx( - ["exchangeDetails", "exchanges"], + { storeNames: ["exchangeDetails", "exchanges"] }, async (tx) => { const ex = await tx.exchanges.get(baseUrl); if (!ex || !ex.detailsPointer) return undefined; @@ -1281,19 +1294,22 @@ export async function checkDepositGroup( const exchangeInfos: ExchangeHandle[] = []; - await wex.db.runReadOnlyTx(["exchangeDetails", "exchanges"], async (tx) => { - const allExchanges = await tx.exchanges.iter().toArray(); - for (const e of allExchanges) { - const details = await getExchangeWireDetailsInTx(tx, e.baseUrl); - if (!details || amount.currency !== details.currency) { - continue; + await wex.db.runReadOnlyTx( + { storeNames: ["exchangeDetails", "exchanges"] }, + async (tx) => { + const allExchanges = await tx.exchanges.iter().toArray(); + for (const e of allExchanges) { + const details = await getExchangeWireDetailsInTx(tx, e.baseUrl); + if (!details || amount.currency !== details.currency) { + continue; + } + exchangeInfos.push({ + master_pub: details.masterPublicKey, + url: e.baseUrl, + }); } - exchangeInfos.push({ - master_pub: details.masterPublicKey, - url: e.baseUrl, - }); - } - }); + }, + ); const now = AbsoluteTime.now(); const nowRounded = AbsoluteTime.toProtocolTimestamp(now); @@ -1404,19 +1420,22 @@ export async function createDepositGroup( const exchangeInfos: { url: string; master_pub: string }[] = []; - await wex.db.runReadOnlyTx(["exchanges", "exchangeDetails"], async (tx) => { - const allExchanges = await tx.exchanges.iter().toArray(); - for (const e of allExchanges) { - const details = await getExchangeWireDetailsInTx(tx, e.baseUrl); - if (!details || amount.currency !== details.currency) { - continue; + await wex.db.runReadOnlyTx( + { storeNames: ["exchanges", "exchangeDetails"] }, + async (tx) => { + const allExchanges = await tx.exchanges.iter().toArray(); + for (const e of allExchanges) { + const details = await getExchangeWireDetailsInTx(tx, e.baseUrl); + if (!details || amount.currency !== details.currency) { + continue; + } + exchangeInfos.push({ + master_pub: details.masterPublicKey, + url: e.baseUrl, + }); } - exchangeInfos.push({ - master_pub: details.masterPublicKey, - url: e.baseUrl, - }); - } - }); + }, + ); const now = AbsoluteTime.now(); const wireDeadline = AbsoluteTime.toProtocolTimestamp( @@ -1569,16 +1588,18 @@ export async function createDepositGroup( const transactionId = ctx.transactionId; const newTxState = await wex.db.runReadWriteTx( - [ - "depositGroups", - "coins", - "recoupGroups", - "denominations", - "refreshGroups", - "refreshSessions", - "coinAvailability", - "contractTerms", - ], + { + storeNames: [ + "depositGroups", + "coins", + "recoupGroups", + "denominations", + "refreshGroups", + "refreshSessions", + "coinAvailability", + "contractTerms", + ], + }, async (tx) => { if (depositGroup.payCoinSelection) { await spendCoins(wex, tx, { @@ -1635,7 +1656,7 @@ export async function getCounterpartyEffectiveDepositAmount( const exchangeSet: Set = new Set(); await wex.db.runReadOnlyTx( - ["coins", "denominations", "exchangeDetails", "exchanges"], + { storeNames: ["coins", "denominations", "exchangeDetails", "exchanges"] }, async (tx) => { for (let i = 0; i < pcs.length; i++) { const denom = await getDenomInfo( @@ -1694,10 +1715,9 @@ async function getTotalFeesForDepositAmount( const coinFee: AmountJson[] = []; const refreshFee: AmountJson[] = []; const exchangeSet: Set = new Set(); - const currency = Amounts.currencyOf(total); await wex.db.runReadOnlyTx( - ["coins", "denominations", "exchanges", "exchangeDetails"], + { storeNames: ["coins", "denominations", "exchanges", "exchangeDetails"] }, async (tx) => { for (let i = 0; i < pcs.length; i++) { const denom = await getDenomInfo( -- cgit v1.2.3