diff --git a/.github/workflows/deploy-randomness.yml b/.github/workflows/deploy-randomness.yml index 6a3837794e..1790a9c36b 100644 --- a/.github/workflows/deploy-randomness.yml +++ b/.github/workflows/deploy-randomness.yml @@ -2,6 +2,9 @@ name: Deploy Randomness on: workflow_dispatch: + push: + branches: + - gabriel/debug-randomness env: REGISTRY: ghcr.io diff --git a/packages/txm/lib/BlockMonitor.ts b/packages/txm/lib/BlockMonitor.ts index 804436b8c9..944d2c2ad6 100644 --- a/packages/txm/lib/BlockMonitor.ts +++ b/packages/txm/lib/BlockMonitor.ts @@ -14,23 +14,46 @@ export type LatestBlock = Block */ export class BlockMonitor { private txmgr: TransactionManager - + private unwatch: (() => void) | undefined + private blockTimeout: ReturnType | 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() } -} + } \ No newline at end of file