Skip to content

Commit 0aea323

Browse files
authored
Merge pull request #1404 from ethereum-optimism/matt/watcher-l1-l2-blocksToFetch
Watcher: l1 vs l2 blocksToFetch
2 parents 08d1c86 + 8da0450 commit 0aea323

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@eth-optimism/core-utils': minor
3+
---
4+
5+
Allow a configurable L1 and L2 blocks to fetch in the watcher

packages/core-utils/src/watcher.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,43 @@
22
import { ethers } from 'ethers'
33
import { Provider, TransactionReceipt } from '@ethersproject/abstract-provider'
44

5+
const SENT_MESSAGE = ethers.utils.id('SentMessage(bytes)')
6+
const RELAYED_MESSAGE = ethers.utils.id(`RelayedMessage(bytes32)`)
7+
const FAILED_RELAYED_MESSAGE = ethers.utils.id(`FailedRelayedMessage(bytes32)`)
8+
59
export interface Layer {
610
provider: Provider
711
messengerAddress: string
12+
blocksToFetch?: number
813
}
914

1015
export interface WatcherOptions {
1116
l1: Layer
1217
l2: Layer
1318
pollInterval?: number
1419
blocksToFetch?: number
20+
pollForPending?: boolean
1521
}
1622

1723
export class Watcher {
1824
public l1: Layer
1925
public l2: Layer
2026
public pollInterval = 3000
2127
public blocksToFetch = 1500
28+
public pollForPending = true
2229

2330
constructor(opts: WatcherOptions) {
2431
this.l1 = opts.l1
2532
this.l2 = opts.l2
26-
if (opts.pollInterval) {
33+
if (typeof opts.pollInterval === 'number') {
2734
this.pollInterval = opts.pollInterval
2835
}
29-
if (opts.blocksToFetch) {
36+
if (typeof opts.blocksToFetch === 'number') {
3037
this.blocksToFetch = opts.blocksToFetch
3138
}
39+
if (typeof opts.pollForPending === 'boolean') {
40+
this.pollForPending = opts.pollForPending
41+
}
3242
}
3343

3444
public async getMessageHashesFromL1Tx(l1TxHash: string): Promise<string[]> {
@@ -40,14 +50,14 @@ export class Watcher {
4050

4151
public async getL1TransactionReceipt(
4252
l2ToL1MsgHash: string,
43-
pollForPending = true
53+
pollForPending?
4454
): Promise<TransactionReceipt> {
4555
return this.getTransactionReceipt(this.l1, l2ToL1MsgHash, pollForPending)
4656
}
4757

4858
public async getL2TransactionReceipt(
4959
l1ToL2MsgHash: string,
50-
pollForPending = true
60+
pollForPending?
5161
): Promise<TransactionReceipt> {
5262
return this.getTransactionReceipt(this.l2, l1ToL2MsgHash, pollForPending)
5363
}
@@ -65,7 +75,7 @@ export class Watcher {
6575
for (const log of receipt.logs) {
6676
if (
6777
log.address === layer.messengerAddress &&
68-
log.topics[0] === ethers.utils.id('SentMessage(bytes)')
78+
log.topics[0] === SENT_MESSAGE
6979
) {
7080
const [message] = ethers.utils.defaultAbiCoder.decode(
7181
['bytes'],
@@ -80,22 +90,31 @@ export class Watcher {
8090
public async getTransactionReceipt(
8191
layer: Layer,
8292
msgHash: string,
83-
pollForPending = true
93+
pollForPending?
8494
): Promise<TransactionReceipt> {
95+
if (typeof pollForPending !== 'boolean') {
96+
pollForPending = this.pollForPending
97+
}
98+
8599
let matches: ethers.providers.Log[] = []
86100

101+
let blocksToFetch = layer.blocksToFetch
102+
if (typeof blocksToFetch !== 'number') {
103+
blocksToFetch = this.blocksToFetch
104+
}
105+
87106
// scan for transaction with specified message
88107
while (matches.length === 0) {
89108
const blockNumber = await layer.provider.getBlockNumber()
90-
const startingBlock = Math.max(blockNumber - this.blocksToFetch, 0)
109+
const startingBlock = Math.max(blockNumber - blocksToFetch, 0)
91110
const successFilter: ethers.providers.Filter = {
92111
address: layer.messengerAddress,
93-
topics: [ethers.utils.id(`RelayedMessage(bytes32)`)],
112+
topics: [RELAYED_MESSAGE],
94113
fromBlock: startingBlock,
95114
}
96115
const failureFilter: ethers.providers.Filter = {
97116
address: layer.messengerAddress,
98-
topics: [ethers.utils.id(`FailedRelayedMessage(bytes32)`)],
117+
topics: [FAILED_RELAYED_MESSAGE],
99118
fromBlock: startingBlock,
100119
}
101120
const successLogs = await layer.provider.getLogs(successFilter)

0 commit comments

Comments
 (0)