Skip to content

Commit

Permalink
fix: broken ws reconnection after web3 upgrade (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvtaveras authored Jun 11, 2024
1 parent f41c861 commit 9a65ff4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
26 changes: 22 additions & 4 deletions src/reporters/block_based_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockHeader> | undefined

private _highestObservedBlockNumber: number
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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, {
Expand All @@ -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()
Expand Down

0 comments on commit 9a65ff4

Please sign in to comment.