From 2a2aeee1aa2eb7f5804bc8b7c4296dbfd8d928d4 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Sat, 17 Feb 2024 17:19:36 +0100 Subject: [PATCH] fix: eth1 provider error logging --- .../src/eth1/provider/eth1Provider.ts | 2 +- packages/utils/src/waitFor.ts | 8 ++++++-- packages/utils/test/unit/waitFor.test.ts | 16 ++++++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/beacon-node/src/eth1/provider/eth1Provider.ts b/packages/beacon-node/src/eth1/provider/eth1Provider.ts index d594c74a3ab..08d658af52c 100644 --- a/packages/beacon-node/src/eth1/provider/eth1Provider.ts +++ b/packages/beacon-node/src/eth1/provider/eth1Provider.ts @@ -109,7 +109,7 @@ export class Eth1Provider implements IEth1Provider { if (this.state !== Eth1ProviderState.ONLINE) { if (isOneMinutePassed()) { this.logger?.error( - "Eth1Provider faced error", + "Eth1Provider error", { state: this.state, lastErrorAt: new Date(Date.now() - isOneMinutePassed.msSinceLastCall).toLocaleTimeString(), diff --git a/packages/utils/src/waitFor.ts b/packages/utils/src/waitFor.ts index ea4fdf91b9e..5aafa8c2b85 100644 --- a/packages/utils/src/waitFor.ts +++ b/packages/utils/src/waitFor.ts @@ -70,9 +70,13 @@ export function createElapsedTimeTracker({minElapsedTime}: {minElapsedTime: numb function elapsedTimeTracker(): boolean { const now = Date.now(); const msSinceLastCall = now - (lastTimeCalled ?? 0); - lastTimeCalled = now; - return msSinceLastCall > minElapsedTime; + if (msSinceLastCall > minElapsedTime) { + // Do not reset timer if called before timer elapsed + lastTimeCalled = now; + return true; + } + return false; } return Object.assign(elapsedTimeTracker, { diff --git a/packages/utils/test/unit/waitFor.test.ts b/packages/utils/test/unit/waitFor.test.ts index 293e5aba936..412c06f888e 100644 --- a/packages/utils/test/unit/waitFor.test.ts +++ b/packages/utils/test/unit/waitFor.test.ts @@ -36,8 +36,8 @@ describe("waitFor", () => { }); }); -describe("waitForElapsedTime", () => { - it("should true for the first time", () => { +describe("createElapsedTimeTracker", () => { + it("should return true for the first time", () => { const callIfTimePassed = createElapsedTimeTracker({minElapsedTime: 1000}); expect(callIfTimePassed()).toBe(true); @@ -52,6 +52,18 @@ describe("waitForElapsedTime", () => { expect(callIfTimePassed()).toBe(true); }); + it("should return true after the minElapsedTime has passed with intermediate calls", async () => { + const callIfTimePassed = createElapsedTimeTracker({minElapsedTime: 100}); + callIfTimePassed(); + + await sleep(75); + // Time has not elapsed yet but it should not reset timer + expect(callIfTimePassed()).toBe(false); + await sleep(75); + + expect(callIfTimePassed()).toBe(true); + }); + it("should return false before the minElapsedTime has passed", async () => { const callIfTimePassed = createElapsedTimeTracker({minElapsedTime: 100}); callIfTimePassed();