Skip to content
Closed

debug #435

Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .github/workflows/deploy-randomness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Deploy Randomness

on:
workflow_dispatch:
push:
branches:
- gabriel/debug-randomness

env:
REGISTRY: ghcr.io
Expand Down
51 changes: 37 additions & 14 deletions packages/txm/lib/BlockMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,46 @@ export type LatestBlock = Block<bigint, false, "latest">
*/
export class BlockMonitor {
private txmgr: TransactionManager

private unwatch: (() => void) | undefined
private blockTimeout: ReturnType<typeof setTimeout> | undefined

constructor(_transactionManager: TransactionManager) {
this.txmgr = _transactionManager
this.txmgr = _transactionManager
}

async start() {
this.txmgr.viemClient.watchBlocks({
onBlock: this.onNewBlock.bind(this),
...(this.txmgr.transportProtocol === "http"
? {
pollingInterval: this.txmgr.pollingInterval,
}
: {}),
})
this.scheduleTimeout()
this.unwatch = this.txmgr.viemClient.watchBlocks({
onBlock: this.onNewBlock.bind(this),
...(this.txmgr.transportProtocol === "http"
? { pollingInterval: this.txmgr.pollingInterval }
: {}),
onError: (error) => {
console.error("Error watching blocks", error)
this.resetWatch()
},
})
}

private onNewBlock(block: LatestBlock) {
eventBus.emit(Topics.NewBlock, block)
if (this.blockTimeout) clearTimeout(this.blockTimeout)
eventBus.emit(Topics.NewBlock, block)
this.scheduleTimeout()
}

private scheduleTimeout() {
console.log("Scheduling timeout")
this.blockTimeout = setTimeout(() => {
console.log("Timeout reached. Resetting watch.")
this.resetWatch()
}, 20_000)
}

private resetWatch() {
console.log("No blocks received in 20s. Resetting watch.")
if (this.unwatch) {
this.unwatch()
}
this.start()
}
}
}
Loading