Commit 0c2dee8a authored by Maurelian's avatar Maurelian

feat(chain-mon): Add backwards walk to find last finalized block

parent e7946239
import { Provider } from '@ethersproject/abstract-provider'
import { Logger } from '@eth-optimism/common-ts'
/**
* Finds
*
* @param
* @param
* @param
* @returns
*/
export const getLastFinalizedBlock = async (
l1RpcProvider: Provider,
faultProofWindow: number,
logger: Logger
): Promise<number> => {
let guessWindowStartBlock
try {
const l1Block = await l1RpcProvider.getBlock('latest')
// The time corresponding to the start of the FPW, based on the current block.
const windowStartTime = l1Block.timestamp - faultProofWindow
// Use the FPW to find the block number that is the start of the FPW.
guessWindowStartBlock = l1Block.number - faultProofWindow / 12
let block = await l1RpcProvider.getBlock(guessWindowStartBlock)
while (block.timestamp > windowStartTime) {
guessWindowStartBlock--
block = await l1RpcProvider.getBlock(guessWindowStartBlock)
}
return block.number
} catch (err) {
logger.fatal('error when calling querying for block', {
errors: err,
})
throw new Error(
`unable to find block number ${guessWindowStartBlock || 'latest'}`
)
}
}
...@@ -13,6 +13,7 @@ import { Event } from 'ethers' ...@@ -13,6 +13,7 @@ import { Event } from 'ethers'
import dateformat from 'dateformat' import dateformat from 'dateformat'
import { version } from '../../package.json' import { version } from '../../package.json'
import { getLastFinalizedBlock as getLastFinalizedBlock } from './helpers'
type Options = { type Options = {
l1RpcProvider: Provider l1RpcProvider: Provider
...@@ -117,10 +118,12 @@ export class WithdrawalMonitor extends BaseServiceV2<Options, Metrics, State> { ...@@ -117,10 +118,12 @@ export class WithdrawalMonitor extends BaseServiceV2<Options, Metrics, State> {
// Set the start block number. // Set the start block number.
if (this.options.startBlockNumber === -1) { if (this.options.startBlockNumber === -1) {
// We default to starting from the earliest block still in the fault proof window. // We default to starting from the last finalized block.
const l1BlockNumber = await this.options.l1RpcProvider.getBlockNumber() this.state.highestUncheckedBlockNumber = await getLastFinalizedBlock(
this.state.highestUncheckedBlockNumber = this.options.l1RpcProvider,
l1BlockNumber - this.state.faultProofWindow / 12 this.state.faultProofWindow,
this.logger
)
} else { } else {
this.state.highestUncheckedBlockNumber = this.options.startBlockNumber this.state.highestUncheckedBlockNumber = this.options.startBlockNumber
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment