wallet-core: emit balance-change notifications, do less implicit background work

This commit is contained in:
Florian Dold 2023-06-28 10:49:31 +02:00
parent 97a9e92d8b
commit 35ae9f2c22
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
10 changed files with 42 additions and 36 deletions

View File

@ -2499,6 +2499,8 @@ export interface InitiatePeerPullCreditResponse {
/** /**
* Taler URI for the other party to make the payment * Taler URI for the other party to make the payment
* that was requested. * that was requested.
*
* @deprecated since it's not necessarily valid yet until the tx is in the right state
*/ */
talerUri: string; talerUri: string;

View File

@ -26,7 +26,6 @@ import {
codecForDepositSuccess, codecForDepositSuccess,
codecForTackTransactionAccepted, codecForTackTransactionAccepted,
codecForTackTransactionWired, codecForTackTransactionWired,
CoinDepositPermission,
CoinRefreshRequest, CoinRefreshRequest,
CreateDepositGroupRequest, CreateDepositGroupRequest,
CreateDepositGroupResponse, CreateDepositGroupResponse,

View File

@ -907,6 +907,8 @@ async function handleInsufficientFunds(
refreshReason: RefreshReason.PayMerchant, refreshReason: RefreshReason.PayMerchant,
}); });
}); });
ws.notify({ type: NotificationType.BalanceChange });
} }
async function unblockBackup( async function unblockBackup(
@ -1446,6 +1448,7 @@ export async function confirmPay(
}); });
notifyTransition(ws, transactionId, transitionInfo); notifyTransition(ws, transactionId, transitionInfo);
ws.notify({ type: NotificationType.BalanceChange });
return runPayForConfirmPay(ws, proposalId); return runPayForConfirmPay(ws, proposalId);
} }

View File

@ -26,6 +26,7 @@ import {
InitiatePeerPullCreditRequest, InitiatePeerPullCreditRequest,
InitiatePeerPullCreditResponse, InitiatePeerPullCreditResponse,
Logger, Logger,
NotificationType,
TalerErrorCode, TalerErrorCode,
TalerPreciseTimestamp, TalerPreciseTimestamp,
TalerUriAction, TalerUriAction,
@ -785,24 +786,14 @@ export async function initiatePeerPullPayment(
}); });
}); });
// FIXME: Should we somehow signal to the client
// whether purse creation has failed, or does the client/
// check this asynchronously from the transaction status?
const taskId = constructTaskIdentifier({
tag: PendingTaskType.PeerPullCredit,
pursePub: pursePair.pub,
});
await runTaskWithErrorReporting(ws, taskId, async () => {
return processPeerPullCredit(ws, pursePair.pub);
});
const transactionId = constructTransactionIdentifier({ const transactionId = constructTransactionIdentifier({
tag: TransactionType.PeerPullCredit, tag: TransactionType.PeerPullCredit,
pursePub: pursePair.pub, pursePub: pursePair.pub,
}); });
// The pending-incoming balance has changed.
ws.notify({ type: NotificationType.BalanceChange });
return { return {
talerUri: stringifyTalerUri({ talerUri: stringifyTalerUri({
type: TalerUriAction.PayPull, type: TalerUriAction.PayPull,

View File

@ -22,6 +22,7 @@ import {
ExchangePurseDeposits, ExchangePurseDeposits,
HttpStatusCode, HttpStatusCode,
Logger, Logger,
NotificationType,
PeerContractTerms, PeerContractTerms,
PreparePeerPullDebitRequest, PreparePeerPullDebitRequest,
PreparePeerPullDebitResponse, PreparePeerPullDebitResponse,
@ -388,7 +389,6 @@ export async function confirmPeerPullDebit(
ws: InternalWalletState, ws: InternalWalletState,
req: ConfirmPeerPullDebitRequest, req: ConfirmPeerPullDebitRequest,
): Promise<AcceptPeerPullPaymentResponse> { ): Promise<AcceptPeerPullPaymentResponse> {
let peerPullPaymentIncomingId: string; let peerPullPaymentIncomingId: string;
if (req.transactionId) { if (req.transactionId) {
@ -400,7 +400,9 @@ export async function confirmPeerPullDebit(
} else if (req.peerPullPaymentIncomingId) { } else if (req.peerPullPaymentIncomingId) {
peerPullPaymentIncomingId = req.peerPullPaymentIncomingId; peerPullPaymentIncomingId = req.peerPullPaymentIncomingId;
} else { } else {
throw Error("invalid request, transactionId or peerPullPaymentIncomingId required"); throw Error(
"invalid request, transactionId or peerPullPaymentIncomingId required",
);
} }
const peerPullInc = await ws.db const peerPullInc = await ws.db
@ -479,13 +481,7 @@ export async function confirmPeerPullDebit(
return pi; return pi;
}); });
await runTaskWithErrorReporting( ws.notify({ type: NotificationType.BalanceChange });
ws,
TaskIdentifiers.forPeerPullPaymentDebit(ppi),
async () => {
return processPeerPullDebit(ws, ppi.peerPullPaymentIncomingId);
},
);
const transactionId = constructTransactionIdentifier({ const transactionId = constructTransactionIdentifier({
tag: TransactionType.PeerPullDebit, tag: TransactionType.PeerPullDebit,

View File

@ -22,6 +22,7 @@ import {
ExchangePurseMergeRequest, ExchangePurseMergeRequest,
HttpStatusCode, HttpStatusCode,
Logger, Logger,
NotificationType,
PeerContractTerms, PeerContractTerms,
PreparePeerPushCreditRequest, PreparePeerPushCreditRequest,
PreparePeerPushCreditResponse, PreparePeerPushCreditResponse,
@ -60,7 +61,12 @@ import {
} from "../index.js"; } from "../index.js";
import { assertUnreachable } from "../util/assertUnreachable.js"; import { assertUnreachable } from "../util/assertUnreachable.js";
import { checkDbInvariant } from "../util/invariants.js"; import { checkDbInvariant } from "../util/invariants.js";
import { OperationAttemptResult, OperationAttemptResultType, constructTaskIdentifier, runLongpollAsync } from "./common.js"; import {
OperationAttemptResult,
OperationAttemptResultType,
constructTaskIdentifier,
runLongpollAsync,
} from "./common.js";
import { updateExchangeFromUrl } from "./exchanges.js"; import { updateExchangeFromUrl } from "./exchanges.js";
import { import {
codecForExchangePurseStatus, codecForExchangePurseStatus,
@ -206,6 +212,8 @@ export async function preparePeerPushCredit(
}); });
}); });
ws.notify({ type: NotificationType.BalanceChange });
return { return {
amount: purseStatus.balance, amount: purseStatus.balance,
amountEffective: wi.withdrawalAmountEffective, amountEffective: wi.withdrawalAmountEffective,
@ -631,15 +639,15 @@ export async function confirmPeerPushCredit(
} }
peerPushPaymentIncomingId = parsedTx.peerPushPaymentIncomingId; peerPushPaymentIncomingId = parsedTx.peerPushPaymentIncomingId;
} else { } else {
throw Error("no transaction ID (or deprecated peerPushPaymentIncomingId) provided"); throw Error(
"no transaction ID (or deprecated peerPushPaymentIncomingId) provided",
);
} }
await ws.db await ws.db
.mktx((x) => [x.contractTerms, x.peerPushPaymentIncoming]) .mktx((x) => [x.contractTerms, x.peerPushPaymentIncoming])
.runReadWrite(async (tx) => { .runReadWrite(async (tx) => {
peerInc = await tx.peerPushPaymentIncoming.get( peerInc = await tx.peerPushPaymentIncoming.get(peerPushPaymentIncomingId);
peerPushPaymentIncomingId,
);
if (!peerInc) { if (!peerInc) {
return; return;
} }

View File

@ -24,6 +24,7 @@ import {
InitiatePeerPushDebitRequest, InitiatePeerPushDebitRequest,
InitiatePeerPushDebitResponse, InitiatePeerPushDebitResponse,
Logger, Logger,
NotificationType,
RefreshReason, RefreshReason,
TalerError, TalerError,
TalerErrorCode, TalerErrorCode,
@ -678,6 +679,7 @@ export async function initiatePeerPushDebit(
}; };
}); });
notifyTransition(ws, transactionId, transitionInfo); notifyTransition(ws, transactionId, transitionInfo);
ws.notify({ type: NotificationType.BalanceChange });
return { return {
contractPriv: contractKeyPair.priv, contractPriv: contractKeyPair.priv,

View File

@ -278,6 +278,7 @@ async function refreshCreateSession(
} }
await tx.refreshGroups.put(rg); await tx.refreshGroups.put(rg);
}); });
ws.notify({ type: NotificationType.BalanceChange });
return; return;
} }
@ -458,6 +459,7 @@ async function refreshMelt(
} }
await tx.refreshGroups.put(rg); await tx.refreshGroups.put(rg);
}); });
ws.notify({ type: NotificationType.BalanceChange });
return; return;
} }

View File

@ -30,6 +30,7 @@ import {
getRandomBytes, getRandomBytes,
j2s, j2s,
Logger, Logger,
NotificationType,
parseTipUri, parseTipUri,
PrepareTipResult, PrepareTipResult,
TalerErrorCode, TalerErrorCode,
@ -57,7 +58,13 @@ import {
readSuccessResponseJsonOrThrow, readSuccessResponseJsonOrThrow,
} from "@gnu-taler/taler-util/http"; } from "@gnu-taler/taler-util/http";
import { checkDbInvariant, checkLogicInvariant } from "../util/invariants.js"; import { checkDbInvariant, checkLogicInvariant } from "../util/invariants.js";
import { constructTaskIdentifier, makeCoinAvailable, makeCoinsVisible, OperationAttemptResult, OperationAttemptResultType } from "./common.js"; import {
constructTaskIdentifier,
makeCoinAvailable,
makeCoinsVisible,
OperationAttemptResult,
OperationAttemptResultType,
} from "./common.js";
import { updateExchangeFromUrl } from "./exchanges.js"; import { updateExchangeFromUrl } from "./exchanges.js";
import { import {
getCandidateWithdrawalDenoms, getCandidateWithdrawalDenoms,
@ -421,6 +428,7 @@ export async function processTip(
return { oldTxState, newTxState }; return { oldTxState, newTxState };
}); });
notifyTransition(ws, transactionId, transitionInfo); notifyTransition(ws, transactionId, transitionInfo);
ws.notify({ type: NotificationType.BalanceChange });
return { return {
type: OperationAttemptResultType.Finished, type: OperationAttemptResultType.Finished,

View File

@ -62,7 +62,6 @@ import {
ExchangeWithdrawResponse, ExchangeWithdrawResponse,
WithdrawUriInfoResponse, WithdrawUriInfoResponse,
ExchangeBatchWithdrawRequest, ExchangeBatchWithdrawRequest,
WalletNotification,
TransactionState, TransactionState,
TransactionMajorState, TransactionMajorState,
TransactionMinorState, TransactionMinorState,
@ -76,7 +75,6 @@ import {
DenominationRecord, DenominationRecord,
DenominationVerificationStatus, DenominationVerificationStatus,
KycPendingInfo, KycPendingInfo,
KycUserType,
PlanchetRecord, PlanchetRecord,
PlanchetStatus, PlanchetStatus,
WalletStoresV1, WalletStoresV1,
@ -108,11 +106,7 @@ import {
readSuccessResponseJsonOrThrow, readSuccessResponseJsonOrThrow,
throwUnexpectedRequestError, throwUnexpectedRequestError,
} from "@gnu-taler/taler-util/http"; } from "@gnu-taler/taler-util/http";
import { import { checkDbInvariant, checkLogicInvariant } from "../util/invariants.js";
checkDbInvariant,
checkLogicInvariant,
InvariantViolatedError,
} from "../util/invariants.js";
import { import {
DbAccess, DbAccess,
GetReadOnlyAccess, GetReadOnlyAccess,
@ -1614,6 +1608,7 @@ async function processWithdrawalGroupPendingReady(
} }
notifyTransition(ws, transactionId, res.transitionInfo); notifyTransition(ws, transactionId, res.transitionInfo);
ws.notify({ type: NotificationType.BalanceChange });
if (numPlanchetErrors > 0) { if (numPlanchetErrors > 0) {
return { return {