wallet-core: use status consistently for querying for pending tasks

This commit is contained in:
Florian Dold 2023-06-06 16:47:32 +02:00
parent 2d243b67c8
commit f9c33136b4
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 100 additions and 84 deletions

View File

@ -119,7 +119,7 @@ export const CURRENT_DB_CONFIG_KEY = "currentMainDbName";
* backwards-compatible way or object stores and indices * backwards-compatible way or object stores and indices
* are added. * are added.
*/ */
export const WALLET_DB_MINOR_VERSION = 7; export const WALLET_DB_MINOR_VERSION = 8;
/** /**
* Ranges for operation status fields. * Ranges for operation status fields.
@ -909,10 +909,11 @@ export enum RefreshOperationStatus {
} }
export enum DepositGroupOperationStatus { export enum DepositGroupOperationStatus {
Finished = 50 /* DORMANT_START */, Pending = 10,
Failed = 51 /* DORMANT_START + 1 */, AbortingWithRefresh = 11,
Pending = 10 /* ACTIVE_START */,
AbortingWithRefresh = 11 /* ACTIVE_START + 1 */, Finished = 50,
Failed = 51,
} }
/** /**
@ -2437,6 +2438,9 @@ export const WalletStoresV1 = {
"merchantTipId", "merchantTipId",
"merchantBaseUrl", "merchantBaseUrl",
]), ]),
byStatus: describeIndex("byStatus", "status", {
versionAdded: 8,
}),
}, },
), ),
withdrawalGroups: describeStore( withdrawalGroups: describeStore(

View File

@ -26,12 +26,14 @@ import {
WalletStoresV1, WalletStoresV1,
BackupProviderStateTag, BackupProviderStateTag,
RefreshCoinStatus, RefreshCoinStatus,
OperationStatus,
OperationStatusRange, OperationStatusRange,
PeerPushPaymentInitiationStatus, PeerPushPaymentInitiationStatus,
PeerPullDebitRecordStatus, PeerPullDebitRecordStatus,
PeerPushPaymentIncomingStatus, PeerPushPaymentIncomingStatus,
PeerPullPaymentInitiationStatus, PeerPullPaymentInitiationStatus,
WithdrawalGroupStatus,
DepositGroupOperationStatus,
TipRecordStatus,
} from "../db.js"; } from "../db.js";
import { import {
PendingOperationsResponse, PendingOperationsResponse,
@ -79,7 +81,8 @@ async function gatherExchangePending(
const opTag = TaskIdentifiers.forExchangeUpdate(exch); const opTag = TaskIdentifiers.forExchangeUpdate(exch);
let opr = await tx.operationRetries.get(opTag); let opr = await tx.operationRetries.get(opTag);
const timestampDue = const timestampDue =
opr?.retryInfo.nextRetry ?? AbsoluteTime.fromPreciseTimestamp(exch.nextUpdate); opr?.retryInfo.nextRetry ??
AbsoluteTime.fromPreciseTimestamp(exch.nextUpdate);
resp.pendingOperations.push({ resp.pendingOperations.push({
type: PendingTaskType.ExchangeUpdate, type: PendingTaskType.ExchangeUpdate,
...getPendingCommon(ws, opTag, timestampDue), ...getPendingCommon(ws, opTag, timestampDue),
@ -150,16 +153,12 @@ async function gatherWithdrawalPending(
now: AbsoluteTime, now: AbsoluteTime,
resp: PendingOperationsResponse, resp: PendingOperationsResponse,
): Promise<void> { ): Promise<void> {
const wsrs = await tx.withdrawalGroups.indexes.byStatus.getAll( const range = GlobalIDB.KeyRange.bound(
GlobalIDB.KeyRange.bound( WithdrawalGroupStatus.PendingRegisteringBank,
OperationStatusRange.ACTIVE_START, WithdrawalGroupStatus.PendingAml,
OperationStatusRange.ACTIVE_END,
),
); );
const wsrs = await tx.withdrawalGroups.indexes.byStatus.getAll(range);
for (const wsr of wsrs) { for (const wsr of wsrs) {
if (wsr.timestampFinish) {
return;
}
const opTag = TaskIdentifiers.forWithdrawal(wsr); const opTag = TaskIdentifiers.forWithdrawal(wsr);
let opr = await tx.operationRetries.get(opTag); let opr = await tx.operationRetries.get(opTag);
const now = AbsoluteTime.now(); const now = AbsoluteTime.now();
@ -199,8 +198,8 @@ async function gatherDepositPending(
): Promise<void> { ): Promise<void> {
const dgs = await tx.depositGroups.indexes.byStatus.getAll( const dgs = await tx.depositGroups.indexes.byStatus.getAll(
GlobalIDB.KeyRange.bound( GlobalIDB.KeyRange.bound(
OperationStatusRange.ACTIVE_START, DepositGroupOperationStatus.Pending,
OperationStatusRange.ACTIVE_END, DepositGroupOperationStatus.AbortingWithRefresh,
), ),
); );
for (const dg of dgs) { for (const dg of dgs) {
@ -239,7 +238,11 @@ async function gatherTipPending(
now: AbsoluteTime, now: AbsoluteTime,
resp: PendingOperationsResponse, resp: PendingOperationsResponse,
): Promise<void> { ): Promise<void> {
await tx.tips.iter().forEachAsync(async (tip) => { const range = GlobalIDB.KeyRange.bound(
TipRecordStatus.PendingPickup,
TipRecordStatus.PendingPickup,
);
await tx.tips.indexes.byStatus.iter(range).forEachAsync(async (tip) => {
// FIXME: The tip record needs a proper status field! // FIXME: The tip record needs a proper status field!
if (tip.pickedUpTimestamp) { if (tip.pickedUpTimestamp) {
return; return;
@ -271,8 +274,8 @@ async function gatherPurchasePending(
resp: PendingOperationsResponse, resp: PendingOperationsResponse,
): Promise<void> { ): Promise<void> {
const keyRange = GlobalIDB.KeyRange.bound( const keyRange = GlobalIDB.KeyRange.bound(
OperationStatusRange.ACTIVE_START, PurchaseStatus.PendingDownloadingProposal,
OperationStatusRange.ACTIVE_END, PurchaseStatus.PendingAcceptRefund,
); );
await tx.purchases.indexes.byStatus await tx.purchases.indexes.byStatus
.iter(keyRange) .iter(keyRange)
@ -302,6 +305,7 @@ async function gatherRecoupPending(
now: AbsoluteTime, now: AbsoluteTime,
resp: PendingOperationsResponse, resp: PendingOperationsResponse,
): Promise<void> { ): Promise<void> {
// FIXME: Have a status field!
await tx.recoupGroups.iter().forEachAsync(async (rg) => { await tx.recoupGroups.iter().forEachAsync(async (rg) => {
if (rg.timestampFinished) { if (rg.timestampFinished) {
return; return;
@ -367,13 +371,17 @@ async function gatherPeerPullInitiationPending(
now: AbsoluteTime, now: AbsoluteTime,
resp: PendingOperationsResponse, resp: PendingOperationsResponse,
): Promise<void> { ): Promise<void> {
await tx.peerPullPaymentInitiations.iter().forEachAsync(async (pi) => { const keyRange = GlobalIDB.KeyRange.bound(
if (pi.status === PeerPullPaymentInitiationStatus.DonePurseDeposited) { PeerPullPaymentInitiationStatus.PendingReady,
return; PeerPullPaymentInitiationStatus.AbortingDeletePurse,
} );
await tx.peerPullPaymentInitiations.indexes.byStatus
.iter(keyRange)
.forEachAsync(async (pi) => {
const opId = TaskIdentifiers.forPeerPullPaymentInitiation(pi); const opId = TaskIdentifiers.forPeerPullPaymentInitiation(pi);
const retryRecord = await tx.operationRetries.get(opId); const retryRecord = await tx.operationRetries.get(opId);
const timestampDue = retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now(); const timestampDue =
retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now();
resp.pendingOperations.push({ resp.pendingOperations.push({
type: PendingTaskType.PeerPullCredit, type: PendingTaskType.PeerPullCredit,
...getPendingCommon(ws, opId, timestampDue), ...getPendingCommon(ws, opId, timestampDue),
@ -393,18 +401,17 @@ async function gatherPeerPullDebitPending(
now: AbsoluteTime, now: AbsoluteTime,
resp: PendingOperationsResponse, resp: PendingOperationsResponse,
): Promise<void> { ): Promise<void> {
await tx.peerPullPaymentIncoming.iter().forEachAsync(async (pi) => { const keyRange = GlobalIDB.KeyRange.bound(
switch (pi.status) { PeerPullDebitRecordStatus.PendingDeposit,
case PeerPullDebitRecordStatus.DonePaid: PeerPullDebitRecordStatus.AbortingRefresh,
return; );
case PeerPullDebitRecordStatus.DialogProposed: await tx.peerPullPaymentIncoming.indexes.byStatus
return; .iter(keyRange)
case PeerPullDebitRecordStatus.PendingDeposit: .forEachAsync(async (pi) => {
break;
}
const opId = TaskIdentifiers.forPeerPullPaymentDebit(pi); const opId = TaskIdentifiers.forPeerPullPaymentDebit(pi);
const retryRecord = await tx.operationRetries.get(opId); const retryRecord = await tx.operationRetries.get(opId);
const timestampDue = retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now(); const timestampDue =
retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now();
resp.pendingOperations.push({ resp.pendingOperations.push({
type: PendingTaskType.PeerPullDebit, type: PendingTaskType.PeerPullDebit,
...getPendingCommon(ws, opId, timestampDue), ...getPendingCommon(ws, opId, timestampDue),
@ -424,13 +431,17 @@ async function gatherPeerPushInitiationPending(
now: AbsoluteTime, now: AbsoluteTime,
resp: PendingOperationsResponse, resp: PendingOperationsResponse,
): Promise<void> { ): Promise<void> {
await tx.peerPushPaymentInitiations.iter().forEachAsync(async (pi) => { const keyRange = GlobalIDB.KeyRange.bound(
if (pi.status === PeerPushPaymentInitiationStatus.Done) { PeerPushPaymentInitiationStatus.PendingCreatePurse,
return; PeerPushPaymentInitiationStatus.AbortingRefresh,
} );
await tx.peerPushPaymentInitiations.indexes.byStatus
.iter(keyRange)
.forEachAsync(async (pi) => {
const opId = TaskIdentifiers.forPeerPushPaymentInitiation(pi); const opId = TaskIdentifiers.forPeerPushPaymentInitiation(pi);
const retryRecord = await tx.operationRetries.get(opId); const retryRecord = await tx.operationRetries.get(opId);
const timestampDue = retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now(); const timestampDue =
retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now();
resp.pendingOperations.push({ resp.pendingOperations.push({
type: PendingTaskType.PeerPushDebit, type: PendingTaskType.PeerPushDebit,
...getPendingCommon(ws, opId, timestampDue), ...getPendingCommon(ws, opId, timestampDue),
@ -450,16 +461,17 @@ async function gatherPeerPushCreditPending(
now: AbsoluteTime, now: AbsoluteTime,
resp: PendingOperationsResponse, resp: PendingOperationsResponse,
): Promise<void> { ): Promise<void> {
await tx.peerPushPaymentIncoming.iter().forEachAsync(async (pi) => { const keyRange = GlobalIDB.KeyRange.bound(
switch (pi.status) { PeerPushPaymentIncomingStatus.PendingMerge,
case PeerPushPaymentIncomingStatus.DialogProposed: PeerPushPaymentIncomingStatus.PendingWithdrawing,
return; );
case PeerPushPaymentIncomingStatus.Done: await tx.peerPushPaymentIncoming.indexes.byStatus
return; .iter(keyRange)
} .forEachAsync(async (pi) => {
const opId = TaskIdentifiers.forPeerPushCredit(pi); const opId = TaskIdentifiers.forPeerPushCredit(pi);
const retryRecord = await tx.operationRetries.get(opId); const retryRecord = await tx.operationRetries.get(opId);
const timestampDue = retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now(); const timestampDue =
retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now();
resp.pendingOperations.push({ resp.pendingOperations.push({
type: PendingTaskType.PeerPushCredit, type: PendingTaskType.PeerPushCredit,
...getPendingCommon(ws, opId, timestampDue), ...getPendingCommon(ws, opId, timestampDue),