From 2e8a3596ea280f066da28bacccd077343192abea Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 22 Feb 2024 13:49:06 +0100 Subject: wallet-core: cache control when force-reloading exchange entries --- packages/taler-wallet-core/src/db.ts | 6 ++++++ packages/taler-wallet-core/src/exchanges.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/packages/taler-wallet-core/src/db.ts b/packages/taler-wallet-core/src/db.ts index ab7a1562a..3a593a523 100644 --- a/packages/taler-wallet-core/src/db.ts +++ b/packages/taler-wallet-core/src/db.ts @@ -646,6 +646,12 @@ export interface ExchangeEntryRecord { updateStatus: ExchangeEntryDbUpdateStatus; + /** + * If set to true, the next update to the exchange + * status will request /keys with no-cache headers set. + */ + cachebreakNextUpdate?: boolean; + /** * Etag of the current ToS of the exchange. */ diff --git a/packages/taler-wallet-core/src/exchanges.ts b/packages/taler-wallet-core/src/exchanges.ts index d17005705..3e38925c1 100644 --- a/packages/taler-wallet-core/src/exchanges.ts +++ b/packages/taler-wallet-core/src/exchanges.ts @@ -637,12 +637,18 @@ async function downloadExchangeKeysInfo( http: HttpRequestLibrary, timeout: Duration, cancellationToken: CancellationToken, + noCache: boolean, ): Promise { const keysUrl = new URL("keys", baseUrl); + const headers: Record = {}; + if (noCache) { + headers["cache-control"] = "no-cache"; + } const resp = await http.fetch(keysUrl.href, { timeout, cancellationToken, + headers, }); logger.info("got response to /keys request"); @@ -871,10 +877,13 @@ async function startUpdateExchangeEntry( const oldExchangeState = getExchangeState(r); switch (r.updateStatus) { case ExchangeEntryDbUpdateStatus.UnavailableUpdate: + r.cachebreakNextUpdate = options.forceUpdate; break; case ExchangeEntryDbUpdateStatus.Suspended: + r.cachebreakNextUpdate = options.forceUpdate; break; case ExchangeEntryDbUpdateStatus.ReadyUpdate: + r.cachebreakNextUpdate = options.forceUpdate; break; case ExchangeEntryDbUpdateStatus.Ready: { const nextUpdateTimestamp = AbsoluteTime.fromPreciseTimestamp( @@ -886,12 +895,17 @@ async function startUpdateExchangeEntry( AbsoluteTime.isExpired(nextUpdateTimestamp) ) { r.updateStatus = ExchangeEntryDbUpdateStatus.ReadyUpdate; + r.cachebreakNextUpdate = options.forceUpdate; } break; } case ExchangeEntryDbUpdateStatus.Initial: + r.cachebreakNextUpdate = options.forceUpdate; r.updateStatus = ExchangeEntryDbUpdateStatus.InitialUpdate; break; + case ExchangeEntryDbUpdateStatus.InitialUpdate: + r.cachebreakNextUpdate = options.forceUpdate; + break; } await tx.exchanges.put(r); const newExchangeState = getExchangeState(r); @@ -1216,6 +1230,7 @@ export async function updateExchangeFromUrlHandler( ws.http, timeout, cancellationToken, + oldExchangeRec.cachebreakNextUpdate ?? false, ); logger.trace("validating exchange wire info"); @@ -1346,7 +1361,9 @@ export async function updateExchangeFromUrlHandler( }; } r.updateStatus = ExchangeEntryDbUpdateStatus.Ready; + r.cachebreakNextUpdate = false; await tx.exchanges.put(r); + logger.info(`putting new exchange details in DB: ${j2s(newDetails)}`); const drRowId = await tx.exchangeDetails.put(newDetails); checkDbInvariant(typeof drRowId.key === "number"); @@ -1685,6 +1702,7 @@ export async function downloadExchangeInfo( http, Duration.getForever(), CancellationToken.CONTINUE, + false, ); return { keys: keysInfo, -- cgit v1.2.3