From 9a65ff4efbbaa72cdb85327f1a4c117f8945ad7e Mon Sep 17 00:00:00 2001 From: Nelson Taveras <4562733+nvtaveras@users.noreply.github.com> Date: Tue, 11 Jun 2024 18:26:21 +0200 Subject: [PATCH] fix: broken ws reconnection after web3 upgrade (#252) --- package.json | 2 +- src/reporters/block_based_reporter.ts | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 09e19be1..4969ee1d 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "test": "jest", "lint": "tslint -c tslint.json --project .", "lint:tests": "tslint -c tslint.json 'test/**/*.test.ts'", - "prettify": "pmpm prettier --config .prettierrc.js --write '**/*.+(ts|tsx|js|jsx|sol|java)'", + "prettify": "pnpm prettier --config .prettierrc.js --write '**/*.+(ts|tsx|js|jsx|sol|java)'", "prettify:diff": "pnpm prettier --config .prettierrc.js --list-different '**/*.+(ts|tsx|js|jsx|sol|java)'", "get-cert": "./scripts/get_cert_fingerprint.sh" }, diff --git a/src/reporters/block_based_reporter.ts b/src/reporters/block_based_reporter.ts index 36f1e34b..ae87ebdc 100644 --- a/src/reporters/block_based_reporter.ts +++ b/src/reporters/block_based_reporter.ts @@ -88,6 +88,14 @@ export class BlockBasedReporter extends BaseReporter { readonly config: BlockBasedReporterConfig + readonly wsConnectionOptions = { + // to enable auto reconnection + reconnect: { + auto: true, + delay: 5000, // ms, roughly a block + }, + } + private _blockHeaderSubscription: Subscription | undefined private _highestObservedBlockNumber: number @@ -112,7 +120,10 @@ export class BlockBasedReporter extends BaseReporter { metricCollector: this.config.metricCollector, swallowError: true, } - this.provider = new Web3.providers.WebsocketProvider(this.config.wsRpcProviderUrl) + this.provider = new Web3.providers.WebsocketProvider( + this.config.wsRpcProviderUrl, + this.wsConnectionOptions + ) this.web3 = new Web3(this.provider) this.initialized = false } @@ -296,7 +307,10 @@ export class BlockBasedReporter extends BaseReporter { private setupProviderAndSubscriptions(): void { this.logger.info('Setting up wsProvider and subscriptions') - this.provider = new Web3.providers.WebsocketProvider(this.config.wsRpcProviderUrl) + this.provider = new Web3.providers.WebsocketProvider( + this.config.wsRpcProviderUrl, + this.wsConnectionOptions + ) this.web3.setProvider(this.provider) this.config.metricCollector?.websocketProviderSetup() let setupNewProvider = false @@ -310,6 +324,10 @@ export class BlockBasedReporter extends BaseReporter { setupNewProvider = true } + this.provider.on('reconnect', () => { + this.logger.info('Attempting to reconnect to WebsocketProvider...') + }) + // @ts-ignore - the type definition does not include the error this.provider.on('error', async (error: Error) => { onError(error, { @@ -318,9 +336,9 @@ export class BlockBasedReporter extends BaseReporter { }) await setupNewProviderAndSubs() }) - this.provider.on('end', async () => { + this.provider.on('close', async () => { onError( - new Error('WebsocketProvider has ended, will restart'), + new Error('WebsocketProvider connection closed, will re-open'), this.blockHeaderSubscriptionErrorWrapper ) await setupNewProviderAndSubs()