Skip to content

Commit 981d62c

Browse files
authored
[price_service/server] Improve vaa counter metric (#573)
* [price_service/server] Improve vaa counter metric * Bump the version * Update comment * Update comment again
1 parent 6fe551c commit 981d62c

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

price_service/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/price-service-server",
3-
"version": "2.3.4",
3+
"version": "2.3.5",
44
"description": "Pyth price pervice server",
55
"main": "index.js",
66
"scripts": {

price_service/server/src/listen.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ export class Listener implements PriceStore {
157157
this.promClient = promClient;
158158
this.spyServiceHost = config.spyServiceHost;
159159
this.loadFilters(config.filtersRaw);
160-
// Don't store any prices received from wormhole that are over 1 hour old.
161-
this.ignorePricesOlderThanSecs = 60 * 60;
160+
// Don't store any prices received from wormhole that are over 5 minutes old.
161+
this.ignorePricesOlderThanSecs = 5 * 60;
162162
this.readinessConfig = config.readiness;
163163
this.updateCallbacks = [];
164164
this.observedVaas = new LRUCache({
165-
max: 10000, // At most 10000 items
166-
ttl: 60 * 1000, // 60 seconds
165+
max: 100000, // At most 100000 items
166+
ttl: 6 * 60 * 1000, // 6 minutes which is longer than ignorePricesOlderThanSecs
167167
});
168168
this.vaasCache = new VaaCache(
169169
config.cacheTtl,
@@ -255,16 +255,6 @@ export class Listener implements PriceStore {
255255
cachedInfo: PriceInfo | undefined,
256256
observedInfo: PriceInfo
257257
): boolean {
258-
// Sometimes we get old VAAs from wormhole (for unknown reasons). These VAAs can include price feeds that
259-
// were deleted and haven't been updated in a long time. This check filters out such feeds so they don't trigger
260-
// the stale feeds check.
261-
if (
262-
observedInfo.attestationTime <
263-
this.currentTimeInSeconds() - this.ignorePricesOlderThanSecs
264-
) {
265-
return false;
266-
}
267-
268258
if (cachedInfo === undefined) {
269259
return true;
270260
}
@@ -289,19 +279,17 @@ export class Listener implements PriceStore {
289279
const vaaEmitterAddressHex = Buffer.from(parsedVaa.emitterAddress).toString(
290280
"hex"
291281
);
282+
292283
const observedVaasKey: VaaKey = `${parsedVaa.emitterChain}#${vaaEmitterAddressHex}#${parsedVaa.sequence}`;
293284

294285
if (this.observedVaas.has(observedVaasKey)) {
295286
return;
296287
}
297288

298-
this.observedVaas.set(observedVaasKey, true);
299-
this.promClient?.incReceivedVaa();
300-
301289
let batchAttestation;
302290

303291
try {
304-
batchAttestation = await parseBatchPriceAttestation(
292+
batchAttestation = parseBatchPriceAttestation(
305293
Buffer.from(parsedVaa.payload)
306294
);
307295
} catch (e: any) {
@@ -310,6 +298,29 @@ export class Listener implements PriceStore {
310298
return;
311299
}
312300

301+
if (batchAttestation.priceAttestations.length === 0) {
302+
return;
303+
}
304+
305+
// Attestation time is the same in all feeds in the batch.
306+
// Return early if an attestation is old to exclude it from
307+
// the counter metric.
308+
if (
309+
batchAttestation.priceAttestations[0].attestationTime <
310+
this.currentTimeInSeconds() - this.ignorePricesOlderThanSecs
311+
) {
312+
return;
313+
}
314+
315+
// There is no `await` clause to release the current thread since the previous check
316+
// but this is here to ensure this is correct as the code evolves.
317+
if (this.observedVaas.has(observedVaasKey)) {
318+
return;
319+
} else {
320+
this.observedVaas.set(observedVaasKey, true);
321+
this.promClient?.incReceivedVaa();
322+
}
323+
313324
for (const priceAttestation of batchAttestation.priceAttestations) {
314325
const key = priceAttestation.priceId;
315326

0 commit comments

Comments
 (0)