diff options
author | Florian Dold <florian@dold.me> | 2022-09-14 20:34:37 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2022-09-14 20:40:38 +0200 |
commit | c021876b41bff11ad28c3a43808795fa0d02ce99 (patch) | |
tree | c92f4e83def462ddb0d446c9c476fd32f648d744 /packages/taler-wallet-core/src/operations/peer-to-peer.ts | |
parent | 9d044058e267e9e94f3ee63809a1e22426151ee5 (diff) | |
download | wallet-core-c021876b41bff11ad28c3a43808795fa0d02ce99.tar.xz |
wallet-core: cache fresh coin count in DB
Diffstat (limited to 'packages/taler-wallet-core/src/operations/peer-to-peer.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/peer-to-peer.ts | 46 |
1 files changed, 18 insertions, 28 deletions
diff --git a/packages/taler-wallet-core/src/operations/peer-to-peer.ts b/packages/taler-wallet-core/src/operations/peer-to-peer.ts index 59dad3d55..449a91c68 100644 --- a/packages/taler-wallet-core/src/operations/peer-to-peer.ts +++ b/packages/taler-wallet-core/src/operations/peer-to-peer.ts @@ -75,6 +75,8 @@ import { internalCreateWithdrawalGroup } from "./withdraw.js"; import { GetReadOnlyAccess } from "../util/query.js"; import { createRefreshGroup } from "./refresh.js"; import { updateExchangeFromUrl } from "./exchanges.js"; +import { spendCoins } from "../wallet.js"; +import { RetryTags } from "../util/retries.js"; const logger = new Logger("operations/peer-to-peer.ts"); @@ -256,18 +258,14 @@ export async function initiatePeerToPeerPush( return undefined; } - const pubs: CoinPublicKey[] = []; - for (const c of sel.coins) { - const coin = await tx.coins.get(c.coinPub); - checkDbInvariant(!!coin); - coin.currentAmount = Amounts.sub( - coin.currentAmount, - Amounts.parseOrThrow(c.contribution), - ).amount; - coin.status = CoinStatus.Dormant; - pubs.push({ coinPub: coin.coinPub }); - await tx.coins.put(coin); - } + await spendCoins(ws, tx, { + allocationId: `peer-push:${pursePair.pub}`, + coinPubs: sel.coins.map((x) => x.coinPub), + contributions: sel.coins.map((x) => + Amounts.parseOrThrow(x.contribution), + ), + refreshReason: RefreshReason.PayPeerPush, + }); await tx.peerPushPaymentInitiations.add({ amount: Amounts.stringify(instructedAmount), @@ -284,8 +282,6 @@ export async function initiatePeerToPeerPush( timestampCreated: TalerProtocolTimestamp.now(), }); - await createRefreshGroup(ws, tx, pubs, RefreshReason.PayPeerPush); - return sel; }); logger.info(`selected p2p coins (push): ${j2s(coinSelRes)}`); @@ -588,20 +584,14 @@ export async function acceptPeerPullPayment( return undefined; } - const pubs: CoinPublicKey[] = []; - for (const c of sel.coins) { - const coin = await tx.coins.get(c.coinPub); - checkDbInvariant(!!coin); - coin.currentAmount = Amounts.sub( - coin.currentAmount, - Amounts.parseOrThrow(c.contribution), - ).amount; - coin.status = CoinStatus.Dormant; - pubs.push({ coinPub: coin.coinPub }); - await tx.coins.put(coin); - } - - await createRefreshGroup(ws, tx, pubs, RefreshReason.PayPeerPull); + await spendCoins(ws, tx, { + allocationId: `peer-pull:${req.peerPullPaymentIncomingId}`, + coinPubs: sel.coins.map((x) => x.coinPub), + contributions: sel.coins.map((x) => + Amounts.parseOrThrow(x.contribution), + ), + refreshReason: RefreshReason.PayPeerPull, + }); const pi = await tx.peerPullPaymentIncoming.get( req.peerPullPaymentIncomingId, |