wallet-core: remove redundant deposit status field in DB

This commit is contained in:
Florian Dold 2023-09-08 12:54:31 +02:00
parent 50b0b324ae
commit 2ae952cdfa
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 41 additions and 48 deletions

View File

@ -921,8 +921,11 @@ export enum RefreshOperationStatus {
* Status of a single element of a deposit group. * Status of a single element of a deposit group.
*/ */
export enum DepositElementStatus { export enum DepositElementStatus {
Unknown = 0x0100_0000, DepositPending = 0x0100_0000,
Accepted = 0x0100_0001, /**
* Accepted, but tracking.
*/
Tracking = 0x0100_0001,
KycRequired = 0x0100_0002, KycRequired = 0x0100_0002,
Wired = 0x0500_0000, Wired = 0x0500_0000,
RefundSuccess = 0x0503_0000, RefundSuccess = 0x0503_0000,
@ -1723,10 +1726,8 @@ export interface DepositGroupRecord {
/** /**
* The counterparty effective deposit amount. * The counterparty effective deposit amount.
*
* FIXME: If possible, rename to counterpartyEffectiveDepositAmount.
*/ */
effectiveDepositAmount: AmountString; counterpartyEffectiveDepositAmount: AmountString;
timestampCreated: TalerPreciseTimestamp; timestampCreated: TalerPreciseTimestamp;
@ -1734,11 +1735,7 @@ export interface DepositGroupRecord {
operationStatus: DepositOperationStatus; operationStatus: DepositOperationStatus;
// FIXME: Duplication between this and transactionPerCoin! statusPerCoin: DepositElementStatus[];
depositedPerCoin: boolean[];
// FIXME: Improve name!
transactionPerCoin: DepositElementStatus[];
/** /**
* When the deposit transaction was aborted and * When the deposit transaction was aborted and

View File

@ -510,10 +510,10 @@ async function refundDepositGroup(
ws: InternalWalletState, ws: InternalWalletState,
depositGroup: DepositGroupRecord, depositGroup: DepositGroupRecord,
): Promise<TaskRunResult> { ): Promise<TaskRunResult> {
const newTxPerCoin = [...depositGroup.transactionPerCoin]; const newTxPerCoin = [...depositGroup.statusPerCoin];
logger.info(`status per coin: ${j2s(depositGroup.transactionPerCoin)}`); logger.info(`status per coin: ${j2s(depositGroup.statusPerCoin)}`);
for (let i = 0; i < depositGroup.transactionPerCoin.length; i++) { for (let i = 0; i < depositGroup.statusPerCoin.length; i++) {
const st = depositGroup.transactionPerCoin[i]; const st = depositGroup.statusPerCoin[i];
switch (st) { switch (st) {
case DepositElementStatus.RefundFailed: case DepositElementStatus.RefundFailed:
case DepositElementStatus.RefundSuccess: case DepositElementStatus.RefundSuccess:
@ -593,7 +593,7 @@ async function refundDepositGroup(
if (!newDg) { if (!newDg) {
return; return;
} }
newDg.transactionPerCoin = newTxPerCoin; newDg.statusPerCoin = newTxPerCoin;
const refreshCoins: CoinRefreshRequest[] = []; const refreshCoins: CoinRefreshRequest[] = [];
for (let i = 0; i < newTxPerCoin.length; i++) { for (let i = 0; i < newTxPerCoin.length; i++) {
refreshCoins.push({ refreshCoins.push({
@ -766,7 +766,7 @@ async function processDepositGroupPendingTrack(
cancellationToken?: CancellationToken, cancellationToken?: CancellationToken,
): Promise<TaskRunResult> { ): Promise<TaskRunResult> {
const { depositGroupId } = depositGroup; const { depositGroupId } = depositGroup;
for (let i = 0; i < depositGroup.depositedPerCoin.length; i++) { for (let i = 0; i < depositGroup.statusPerCoin.length; i++) {
const coinPub = depositGroup.payCoinSelection.coinPubs[i]; const coinPub = depositGroup.payCoinSelection.coinPubs[i];
// FIXME: Make the URL part of the coin selection? // FIXME: Make the URL part of the coin selection?
const exchangeBaseUrl = await ws.db const exchangeBaseUrl = await ws.db
@ -785,7 +785,7 @@ async function processDepositGroupPendingTrack(
} }
| undefined; | undefined;
if (depositGroup.transactionPerCoin[i] !== DepositElementStatus.Wired) { if (depositGroup.statusPerCoin[i] !== DepositElementStatus.Wired) {
const track = await trackDeposit( const track = await trackDeposit(
ws, ws,
depositGroup, depositGroup,
@ -810,7 +810,7 @@ async function processDepositGroupPendingTrack(
exchangeBaseUrl, exchangeBaseUrl,
); );
} else { } else {
updatedTxStatus = DepositElementStatus.Accepted; updatedTxStatus = DepositElementStatus.Tracking;
} }
} else if (track.type === "wired") { } else if (track.type === "wired") {
updatedTxStatus = DepositElementStatus.Wired; updatedTxStatus = DepositElementStatus.Wired;
@ -840,7 +840,7 @@ async function processDepositGroupPendingTrack(
id: track.exchange_sig, id: track.exchange_sig,
}; };
} else { } else {
updatedTxStatus = DepositElementStatus.Unknown; updatedTxStatus = DepositElementStatus.DepositPending;
} }
} }
@ -853,7 +853,7 @@ async function processDepositGroupPendingTrack(
return; return;
} }
if (updatedTxStatus !== undefined) { if (updatedTxStatus !== undefined) {
dg.transactionPerCoin[i] = updatedTxStatus; dg.statusPerCoin[i] = updatedTxStatus;
} }
if (newWiredCoin) { if (newWiredCoin) {
/** /**
@ -885,8 +885,8 @@ async function processDepositGroupPendingTrack(
return undefined; return undefined;
} }
const oldTxState = computeDepositTransactionStatus(dg); const oldTxState = computeDepositTransactionStatus(dg);
for (let i = 0; i < depositGroup.depositedPerCoin.length; i++) { for (let i = 0; i < depositGroup.statusPerCoin.length; i++) {
if (depositGroup.transactionPerCoin[i] !== DepositElementStatus.Wired) { if (depositGroup.statusPerCoin[i] !== DepositElementStatus.Wired) {
allWired = false; allWired = false;
break; break;
} }
@ -943,7 +943,7 @@ async function processDepositGroupPendingDeposit(
for (let i = 0; i < depositPermissions.length; i++) { for (let i = 0; i < depositPermissions.length; i++) {
const perm = depositPermissions[i]; const perm = depositPermissions[i];
if (depositGroup.depositedPerCoin[i]) { if (depositGroup.statusPerCoin[i] !== DepositElementStatus.DepositPending) {
continue; continue;
} }
@ -980,8 +980,12 @@ async function processDepositGroupPendingDeposit(
if (!dg) { if (!dg) {
return; return;
} }
dg.depositedPerCoin[i] = true; const coinStatus = dg.statusPerCoin[i];
await tx.depositGroups.put(dg); switch (coinStatus) {
case DepositElementStatus.DepositPending:
dg.statusPerCoin[i] = DepositElementStatus.Tracking;
await tx.depositGroups.put(dg);
}
}); });
} }
@ -1373,16 +1377,15 @@ export async function createDepositGroup(
noncePub: noncePair.pub, noncePub: noncePair.pub,
timestampCreated: AbsoluteTime.toPreciseTimestamp(now), timestampCreated: AbsoluteTime.toPreciseTimestamp(now),
timestampFinished: undefined, timestampFinished: undefined,
transactionPerCoin: payCoinSel.coinSel.coinPubs.map( statusPerCoin: payCoinSel.coinSel.coinPubs.map(
() => DepositElementStatus.Unknown, () => DepositElementStatus.DepositPending,
), ),
payCoinSelection: payCoinSel.coinSel, payCoinSelection: payCoinSel.coinSel,
payCoinSelectionUid: encodeCrock(getRandomBytes(32)), payCoinSelectionUid: encodeCrock(getRandomBytes(32)),
depositedPerCoin: payCoinSel.coinSel.coinPubs.map(() => false),
merchantPriv: merchantPair.priv, merchantPriv: merchantPair.priv,
merchantPub: merchantPair.pub, merchantPub: merchantPair.pub,
totalPayCost: Amounts.stringify(totalDepositCost), totalPayCost: Amounts.stringify(totalDepositCost),
effectiveDepositAmount: Amounts.stringify( counterpartyEffectiveDepositAmount: Amounts.stringify(
counterpartyEffectiveDepositAmount, counterpartyEffectiveDepositAmount,
), ),
wire: { wire: {

View File

@ -46,6 +46,7 @@ import {
RefundGroupStatus, RefundGroupStatus,
ExchangeEntryDbUpdateStatus, ExchangeEntryDbUpdateStatus,
RefreshOperationStatus, RefreshOperationStatus,
DepositElementStatus,
} from "../db.js"; } from "../db.js";
import { import {
PendingOperationsResponse, PendingOperationsResponse,
@ -277,8 +278,8 @@ async function gatherDepositPending(
): Promise<void> { ): Promise<void> {
await iterRecordsForDeposit(tx, { onlyState: "nonfinal" }, async (dg) => { await iterRecordsForDeposit(tx, { onlyState: "nonfinal" }, async (dg) => {
let deposited = true; let deposited = true;
for (const d of dg.depositedPerCoin) { for (const d of dg.statusPerCoin) {
if (!d) { if (d === DepositElementStatus.DepositPending) {
deposited = false; deposited = false;
} }
} }
@ -480,9 +481,7 @@ export async function iterRecordsForPeerPullInitiation(
PeerPullPaymentCreditStatus.PendingCreatePurse, PeerPullPaymentCreditStatus.PendingCreatePurse,
PeerPullPaymentCreditStatus.AbortingDeletePurse, PeerPullPaymentCreditStatus.AbortingDeletePurse,
); );
await tx.peerPullCredit.indexes.byStatus await tx.peerPullCredit.indexes.byStatus.iter(keyRange).forEachAsync(f);
.iter(keyRange)
.forEachAsync(f);
} else { } else {
await tx.peerPullCredit.indexes.byStatus.iter().forEachAsync(f); await tx.peerPullCredit.indexes.byStatus.iter().forEachAsync(f);
} }
@ -528,9 +527,7 @@ export async function iterRecordsForPeerPullDebit(
PeerPullDebitRecordStatus.PendingDeposit, PeerPullDebitRecordStatus.PendingDeposit,
PeerPullDebitRecordStatus.AbortingRefresh, PeerPullDebitRecordStatus.AbortingRefresh,
); );
await tx.peerPullDebit.indexes.byStatus await tx.peerPullDebit.indexes.byStatus.iter(keyRange).forEachAsync(f);
.iter(keyRange)
.forEachAsync(f);
} else { } else {
await tx.peerPullDebit.indexes.byStatus.iter().forEachAsync(f); await tx.peerPullDebit.indexes.byStatus.iter().forEachAsync(f);
} }
@ -576,9 +573,7 @@ export async function iterRecordsForPeerPushInitiation(
PeerPushDebitStatus.PendingCreatePurse, PeerPushDebitStatus.PendingCreatePurse,
PeerPushDebitStatus.AbortingRefresh, PeerPushDebitStatus.AbortingRefresh,
); );
await tx.peerPushDebit.indexes.byStatus await tx.peerPushDebit.indexes.byStatus.iter(keyRange).forEachAsync(f);
.iter(keyRange)
.forEachAsync(f);
} else { } else {
await tx.peerPushDebit.indexes.byStatus.iter().forEachAsync(f); await tx.peerPushDebit.indexes.byStatus.iter().forEachAsync(f);
} }
@ -624,9 +619,7 @@ export async function iterRecordsForPeerPushCredit(
PeerPushCreditStatus.PendingMerge, PeerPushCreditStatus.PendingMerge,
PeerPushCreditStatus.PendingWithdrawing, PeerPushCreditStatus.PendingWithdrawing,
); );
await tx.peerPushCredit.indexes.byStatus await tx.peerPushCredit.indexes.byStatus.iter(keyRange).forEachAsync(f);
.iter(keyRange)
.forEachAsync(f);
} else { } else {
await tx.peerPushCredit.indexes.byStatus.iter().forEachAsync(f); await tx.peerPushCredit.indexes.byStatus.iter().forEachAsync(f);
} }

View File

@ -791,8 +791,8 @@ function buildTransactionForDeposit(
ort?: OperationRetryRecord, ort?: OperationRetryRecord,
): Transaction { ): Transaction {
let deposited = true; let deposited = true;
for (const d of dg.depositedPerCoin) { for (const d of dg.statusPerCoin) {
if (!d) { if (d == DepositElementStatus.DepositPending) {
deposited = false; deposited = false;
} }
} }
@ -801,7 +801,7 @@ function buildTransactionForDeposit(
type: TransactionType.Deposit, type: TransactionType.Deposit,
txState: computeDepositTransactionStatus(dg), txState: computeDepositTransactionStatus(dg),
txActions: computeDepositTransactionActions(dg), txActions: computeDepositTransactionActions(dg),
amountRaw: Amounts.stringify(dg.effectiveDepositAmount), amountRaw: Amounts.stringify(dg.counterpartyEffectiveDepositAmount),
amountEffective: Amounts.stringify(dg.totalPayCost), amountEffective: Amounts.stringify(dg.totalPayCost),
timestamp: dg.timestampCreated, timestamp: dg.timestampCreated,
targetPaytoUri: dg.wire.payto_uri, targetPaytoUri: dg.wire.payto_uri,
@ -812,11 +812,11 @@ function buildTransactionForDeposit(
}), }),
wireTransferProgress: wireTransferProgress:
(100 * (100 *
dg.transactionPerCoin.reduce( dg.statusPerCoin.reduce(
(prev, cur) => prev + (cur === DepositElementStatus.Wired ? 1 : 0), (prev, cur) => prev + (cur === DepositElementStatus.Wired ? 1 : 0),
0, 0,
)) / )) /
dg.transactionPerCoin.length, dg.statusPerCoin.length,
depositGroupId: dg.depositGroupId, depositGroupId: dg.depositGroupId,
trackingState: Object.values(dg.trackingState ?? {}), trackingState: Object.values(dg.trackingState ?? {}),
deposited, deposited,