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

View File

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

View File

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

View File

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