Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle invalid block updates #1701

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions main/chains/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class BlockMonitor extends EventEmitter {
}

start() {
log.verbose(`%cStarting block updates for chain ${parseInt(this.connection.chainId)}`, 'color: green')
log.verbose(`%cStarting block updates for chain ${this.chainId}`, 'color: green')

this.connection.on('message', this.handleMessage)

Expand All @@ -81,7 +81,7 @@ class BlockMonitor extends EventEmitter {
}

stop() {
log.verbose(`%cStopping block updates for chain ${parseInt(this.connection.chainId)}`, 'color: red')
log.verbose(`%cStopping block updates for chain ${this.chainId}`, 'color: red')

if (this.subscriptionId) {
this.clearSubscription()
Expand All @@ -92,6 +92,10 @@ class BlockMonitor extends EventEmitter {
}
}

get chainId() {
return parseInt(this.connection.chainId, 16)
}

private clearSubscription() {
this.connection.off('message', this.handleMessage)
this.subscriptionId = ''
Expand All @@ -106,7 +110,7 @@ class BlockMonitor extends EventEmitter {
this.connection
.send({ id: 1, jsonrpc: '2.0', method: 'eth_getBlockByNumber', params: ['latest', false] })
.then((block) => this.handleBlock(block))
.catch((err) => this.handleError(`Could not load block for chain ${this.connection.chainId}`, err))
.catch((err) => this.handleError(`Could not load block for chain ${this.chainId}`, err))
}

private handleMessage(message: SubscriptionMessage) {
Expand All @@ -115,16 +119,16 @@ class BlockMonitor extends EventEmitter {
}
}

private handleBlock(block: Block) {
log.debug(
`%cReceived block ${parseInt(block.number)} for chain ${parseInt(this.connection.chainId)}`,
'color: yellow',
{
latestBlock: parseInt(this.latestBlock)
}
)
private handleBlock(blockUpdate: unknown) {
if (!blockUpdate || typeof blockUpdate !== 'object') {
return this.handleError(`Received invalid block on chain ${this.chainId}`)
}

const block = blockUpdate as Block

if (!block) return this.handleError('handleBlock received undefined block')
log.debug(`%cReceived block ${parseInt(block.number)} for chain ${this.chainId}`, 'color: yellow', {
latestBlock: parseInt(this.latestBlock)
})

if (block.number !== this.latestBlock) {
this.latestBlock = block.number
Expand Down