From 1ac526c61d86010fca05449703afa5af59d9c111 Mon Sep 17 00:00:00 2001 From: Noah Saso Date: Mon, 2 Oct 2023 11:36:26 -0700 Subject: [PATCH] Only wait if all block heights in cache are before the current one. --- src/scripts/export/trace.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/scripts/export/trace.ts b/src/scripts/export/trace.ts index be09ec7f..4086d5ee 100644 --- a/src/scripts/export/trace.ts +++ b/src/scripts/export/trace.ts @@ -108,15 +108,19 @@ const trace = async () => { // Helper function that gets block time for height, cached in memory, which is // filled in by the NewBlock WebSocket listener. const blockHeightToTimeCache = new LRUCache({ - max: 10000, + max: 5000, }) const getBlockTimeUnixMs = async (trace: TracedEvent): Promise => { const { blockHeight } = trace.metadata - // If not in cache but WebSocket is connected, wait for up to 5 seconds for - // it to be added to the cache. We might be just a moment ahead of the new - // block event. - if (!blockHeightToTimeCache.has(blockHeight) && webSocketConnected) { + // If not in cache but WebSocket is connected and every block is less than + // the current one, wait for up to 5 seconds for it to be added to the + // cache. We might be just a moment ahead of the new block event. + if ( + !blockHeightToTimeCache.has(blockHeight) && + webSocketConnected && + blockHeightToTimeCache.dump().every(([key]) => key < blockHeight) + ) { const time = await new Promise((resolve) => { const interval = setInterval(() => { if (blockHeightToTimeCache.has(blockHeight)) {