Skip to content

Commit 00c1df8

Browse files
fix(txm): not process the same block multiple times
1 parent 8372d4b commit 00c1df8

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

packages/txm/lib/BlockMonitor.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class BlockMonitor {
1818
private txmgr: TransactionManager
1919
private unwatch: (() => void) | undefined
2020
private blockTimeout: ReturnType<typeof setTimeout> | undefined
21+
private latestProcessedBlockNumber: bigint | undefined
2122
constructor(_transactionManager: TransactionManager) {
2223
this.txmgr = _transactionManager
2324
}
@@ -27,10 +28,6 @@ export class BlockMonitor {
2728
this.unwatch = this.txmgr.viemClient.watchBlocks({
2829
onBlock: this.onNewBlock.bind(this),
2930
...(this.txmgr.transportProtocol === "http" ? { pollingInterval: this.txmgr.pollingInterval } : {}),
30-
onError: (error) => {
31-
Logger.instance.error(LogTag.TXM, "Error watching blocks", error)
32-
this.resetBlockSubscription()
33-
},
3431
})
3532
}
3633

@@ -40,6 +37,16 @@ export class BlockMonitor {
4037
return
4138
}
4239

40+
if (this.latestProcessedBlockNumber && block.number <= this.latestProcessedBlockNumber) {
41+
Logger.instance.warn(
42+
LogTag.TXM,
43+
"Received block number less than or equal to latest processed block number. Skipping.",
44+
)
45+
return
46+
}
47+
48+
this.latestProcessedBlockNumber = block.number
49+
4350
if (this.blockTimeout) clearTimeout(this.blockTimeout)
4451
eventBus.emit(Topics.NewBlock, block)
4552

0 commit comments

Comments
 (0)