Skip to content

Commit 8c0e9b6

Browse files
authored
add new freshness check to price-service /ready endpoint (#275)
1 parent 05c631b commit 8c0e9b6

File tree

5 files changed

+18
-2
lines changed

5 files changed

+18
-2
lines changed

devnet/pyth-price-service.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,7 @@ spec:
7070
value: '5'
7171
- name: READINESS_NUM_LOADED_SYMBOLS
7272
value: '6'
73+
- name: READINESS_FRESHNESS_TIME_SECONDS
74+
value: '10'
7375
- name: LOG_LEVEL
7476
value: debug

third_party/pyth/price-service/.env.sample

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ SPY_SERVICE_FILTERS=[{"chain_id":1,"emitter_address":"71f8dcb863d176e2c420ad6610
1515
# Number of seconds to sync with spy to be sure to have latest messages
1616
READINESS_SPY_SYNC_TIME_SECONDS=60
1717
READINESS_NUM_LOADED_SYMBOLS=5
18+
READINESS_FRESHNESS_TIME_SECONDS=10
1819

1920
WS_PORT=6200
2021
REST_PORT=4200

third_party/pyth/price-service/docker-compose.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ services:
2424
- PROM_PORT=8081
2525
- READINESS_SPY_SYNC_TIME_SECONDS=60
2626
- READINESS_NUM_LOADED_SYMBOLS=8
27+
- READINESS_FRESHNESS_TIME_SECONDS=10
2728
- LOG_LEVEL=debug
2829
healthcheck:
2930
test:

third_party/pyth/price-service/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ async function run() {
3535
envOrErr("READINESS_SPY_SYNC_TIME_SECONDS")
3636
),
3737
numLoadedSymbols: parseInt(envOrErr("READINESS_NUM_LOADED_SYMBOLS")),
38+
freshnessTimeSeconds: parseInt(envOrErr("READINESS_FRESHNESS_TIME_SECONDS")),
3839
},
3940
},
4041
promClient

third_party/pyth/price-service/src/listen.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface PriceStore {
4343
type ListenerReadinessConfig = {
4444
spySyncTimeSeconds: number;
4545
numLoadedSymbols: number;
46+
freshnessTimeSeconds: number;
4647
};
4748

4849
type ListenerConfig = {
@@ -198,7 +199,7 @@ export class Listener implements PriceStore {
198199
attestationTime: priceAttestation.attestationTime,
199200
priceFeed,
200201
emitterChainId: parsedVAA.emitter_chain,
201-
}
202+
};
202203
this.priceFeedVaaMap.set(key, priceInfo);
203204

204205
for (let callback of this.updateCallbacks) {
@@ -234,7 +235,7 @@ export class Listener implements PriceStore {
234235
}
235236

236237
isReady(): boolean {
237-
let currentTime: TimestampInSec = new Date().getTime() / 1000;
238+
let currentTime: TimestampInSec = Math.floor(Date.now() / 1000);
238239
if (
239240
this.spyConnectionTime === undefined ||
240241
currentTime <
@@ -245,6 +246,16 @@ export class Listener implements PriceStore {
245246
if (this.priceFeedVaaMap.size < this.readinessConfig.numLoadedSymbols) {
246247
return false;
247248
}
249+
let priceIds = [...this.getPriceIds()];
250+
let arePricesFresh = priceIds.every(
251+
(priceId) =>
252+
currentTime -
253+
this.priceFeedVaaMap.get(priceId)!.priceFeed.publishTime <=
254+
this.readinessConfig.freshnessTimeSeconds
255+
);
256+
if (!arePricesFresh) {
257+
return false;
258+
}
248259
return true;
249260
}
250261
}

0 commit comments

Comments
 (0)