From bd9eb53223352c9c96c356ef251bc2b93b43506c Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Fri, 17 Jun 2022 00:09:44 +0100 Subject: [PATCH 1/2] Check validator status on startup --- src/validator-polling-service.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/validator-polling-service.js b/src/validator-polling-service.js index 62fced7..ac0bcd4 100644 --- a/src/validator-polling-service.js +++ b/src/validator-polling-service.js @@ -59,6 +59,7 @@ class ValidatorPollingService { } start() { + this.#pollValidators(); setInterval(this.#pollValidators.bind(this), this.#pollingIntervalSeconds * 1000); logger.info(`Started polling validators every ${this.#pollingIntervalSeconds} seconds.`) } From 376dac3957177e97b942bfca923dc46157df9d74 Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Fri, 17 Jun 2022 01:01:41 +0100 Subject: [PATCH 2/2] perform initial check on startup without waiting for an epoch --- package.json | 5 ++++- src/main.js | 6 +++++- src/validator-polling-service.js | 4 ++-- test/validator-polling-service.test.js | 12 +++++++++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index ee3db20..0f02c45 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,9 @@ }, "jest": { "transform": {}, - "testEnvironment": "jest-environment-node" + "testEnvironment": "jest-environment-node", + "restoreMocks": true, + "clearMocks": true, + "resetMocks": true } } diff --git a/src/main.js b/src/main.js index 3de4946..7b43af3 100644 --- a/src/main.js +++ b/src/main.js @@ -3,6 +3,7 @@ import { SMSNotifier } from './notifiers/index.js'; import { config } from './config.js'; import { BeaconAPIClient } from './beacon-api-client.js'; import { validatorBalanceReducedAlert, validatorStatusChangedAlert } from './alerts/index.js'; +import { logger } from './logger.js'; const smsNotifer = new SMSNotifier(config.sms) const beaconApiClient = new BeaconAPIClient(config.beaconAPIs); @@ -11,4 +12,7 @@ const validatorPollingService = new ValidatorPollingService(beaconApiClient); validatorPollingService.addValidators(config.pubkeys); validatorPollingService.addListener(validatorBalanceReducedAlert(smsNotifer, config.alerts.validatorBalanceReduced)); validatorPollingService.addListener(validatorStatusChangedAlert(smsNotifer)); -validatorPollingService.start(); +validatorPollingService.start() + .catch((err) => { + logger.error(`Error polling validators from Beacon API ${config.beaconAPIs}: `, err); + }); diff --git a/src/validator-polling-service.js b/src/validator-polling-service.js index ac0bcd4..896917f 100644 --- a/src/validator-polling-service.js +++ b/src/validator-polling-service.js @@ -58,8 +58,8 @@ class ValidatorPollingService { } } - start() { - this.#pollValidators(); + async start() { + await this.#pollValidators(); setInterval(this.#pollValidators.bind(this), this.#pollingIntervalSeconds * 1000); logger.info(`Started polling validators every ${this.#pollingIntervalSeconds} seconds.`) } diff --git a/test/validator-polling-service.test.js b/test/validator-polling-service.test.js index fa2f603..406edab 100644 --- a/test/validator-polling-service.test.js +++ b/test/validator-polling-service.test.js @@ -14,17 +14,23 @@ describe('ValidatorPollingService', () => { beaconApiClient = new BeaconAPIClient('http://localhost:5052'); svc = new ValidatorPollingService(beaconApiClient); jest.spyOn(BeaconAPIClient.prototype, 'getGenesisTime').mockImplementation(() => Promise.resolve()); + jest.clearAllTimers(); }); it('should allow adding listener functions', () => { svc.addListener(() => 'I hear you!') }); - it('should poll beaconApiClient every epoch', async () => { + it('should poll beaconApiClient on startup', async () => { const getValidatorsMock = jest.spyOn(BeaconAPIClient.prototype, 'getValidators').mockImplementation(() => Promise.resolve({ data: [] })); + await svc.start(); + expect(getValidatorsMock).toHaveBeenCalledTimes(2); + }); - svc.start(); - expect(getValidatorsMock).not.toBeCalled(); + it('should poll beaconApiClient every epoch', async () => { + const getValidatorsMock = jest.spyOn(BeaconAPIClient.prototype, 'getValidators').mockImplementation(() => Promise.resolve({ data: [] })); + await svc.start(); + getValidatorsMock.mockClear(); jest.advanceTimersByTime(SECONDS_PER_EPOCH * 1000); await flushPromises();