Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/ninety-boxes-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/fault-detector': patch
---

Smarter starting height for fault-detector
13 changes: 13 additions & 0 deletions packages/fault-detector/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { HardhatUserConfig } from 'hardhat/types'

// Hardhat plugins
import '@nomiclabs/hardhat-ethers'
import '@nomiclabs/hardhat-waffle'

const config: HardhatUserConfig = {
mocha: {
timeout: 50000,
},
}

export default config
12 changes: 11 additions & 1 deletion packages/fault-detector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
],
"scripts": {
"start": "ts-node ./src/service.ts",
"test:coverage": "echo 'No tests defined.'",
"test": "hardhat test",
"test:coverage": "nyc hardhat test && nyc merge .nyc_output coverage.json",
"build": "tsc -p tsconfig.json",
"clean": "rimraf dist/ ./tsconfig.tsbuildinfo",
"lint": "yarn lint:fix && yarn lint:check",
Expand All @@ -32,13 +33,22 @@
"url": "https://github.com/ethereum-optimism/optimism.git"
},
"devDependencies": {
"@defi-wonderland/smock": "^2.0.7",
"@nomiclabs/hardhat-ethers": "^2.0.6",
"@nomiclabs/hardhat-waffle": "^2.0.3",
"@types/chai": "^4.3.1",
"@types/dateformat": "^5.0.0",
"chai-as-promised": "^7.1.1",
"dateformat": "^4.5.1",
"ethereum-waffle": "^3.4.4",
"ethers": "^5.6.8",
"hardhat": "^2.9.6",
"lodash": "^4.17.21",
"ts-node": "^10.7.0"
},
"dependencies": {
"@eth-optimism/common-ts": "^0.2.8",
"@eth-optimism/contracts": "^0.5.24",
"@eth-optimism/core-utils": "^0.8.5",
"@eth-optimism/sdk": "^1.1.6",
"@ethersproject/abstract-provider": "^5.6.1"
Expand Down
64 changes: 64 additions & 0 deletions packages/fault-detector/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Contract, ethers } from 'ethers'

/**
* Finds the Event that corresponds to a given state batch by index.
*
* @param scc StateCommitmentChain contract.
* @param index State batch index to search for.
* @returns Event corresponding to the batch.
*/
export const findEventForStateBatch = async (
scc: Contract,
index: number
): Promise<ethers.Event> => {
const events = await scc.queryFilter(scc.filters.StateBatchAppended(index))

// Only happens if the batch with the given index does not exist yet.
if (events.length === 0) {
throw new Error(`unable to find event for batch`)
}

// Should never happen.
if (events.length > 1) {
throw new Error(`found too many events for batch`)
}

return events[0]
}

/**
* Finds the first state batch index that has not yet passed the fault proof window.
*
* @param scc StateCommitmentChain contract.
* @returns Starting state root batch index.
*/
export const findFirstUnfinalizedStateBatchIndex = async (
scc: Contract
): Promise<number> => {
const fpw = (await scc.FRAUD_PROOF_WINDOW()).toNumber()
const latestBlock = await scc.provider.getBlock('latest')
const totalBatches = (await scc.getTotalBatches()).toNumber()

// Perform a binary search to find the next batch that will pass the challenge period.
let lo = 0
let hi = totalBatches
while (lo !== hi) {
const mid = Math.floor((lo + hi) / 2)
const event = await findEventForStateBatch(scc, mid)
const block = await event.getBlock()

if (block.timestamp + fpw < latestBlock.timestamp) {
lo = mid + 1
} else {
hi = mid
}
}

// Result will be zero if the chain is less than FPW seconds old. Only returns undefined in the
// case that no batches have been submitted for an entire challenge period.
if (lo === totalBatches) {
return undefined
} else {
return lo
}
}
1 change: 1 addition & 0 deletions packages/fault-detector/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './service'
export * from './helpers'
80 changes: 43 additions & 37 deletions packages/fault-detector/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { BaseServiceV2, Gauge, validators } from '@eth-optimism/common-ts'
import { sleep, toRpcHexString } from '@eth-optimism/core-utils'
import { getChainId, sleep, toRpcHexString } from '@eth-optimism/core-utils'
import { CrossChainMessenger } from '@eth-optimism/sdk'
import { Provider } from '@ethersproject/abstract-provider'
import { ethers } from 'ethers'
import { Contract, ethers } from 'ethers'
import dateformat from 'dateformat'

import {
findFirstUnfinalizedStateBatchIndex,
findEventForStateBatch,
} from './helpers'

type Options = {
l1RpcProvider: Provider
l2RpcProvider: Provider
Expand All @@ -19,6 +24,7 @@ type Metrics = {
}

type State = {
scc: Contract
messenger: CrossChainMessenger
highestCheckedBatchIndex: number
}
Expand All @@ -41,7 +47,7 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
},
startBatchIndex: {
validator: validators.num,
default: 0,
default: -1,
desc: 'Batch index to start checking from',
},
},
Expand All @@ -67,19 +73,31 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
}

async init(): Promise<void> {
const network = await this.options.l1RpcProvider.getNetwork()
this.state.messenger = new CrossChainMessenger({
l1SignerOrProvider: this.options.l1RpcProvider,
l2SignerOrProvider: this.options.l2RpcProvider,
l1ChainId: network.chainId,
l1ChainId: await getChainId(this.options.l1RpcProvider),
})

this.state.highestCheckedBatchIndex = this.options.startBatchIndex
// We use this a lot, a bit cleaner to pull out to the top level of the state object.
this.state.scc = this.state.messenger.contracts.l1.StateCommitmentChain

// Figure out where to start syncing from.
if (this.options.startBatchIndex === -1) {
this.logger.info(`finding appropriate starting height`)
this.state.highestCheckedBatchIndex =
await findFirstUnfinalizedStateBatchIndex(this.state.scc)
} else {
this.state.highestCheckedBatchIndex = this.options.startBatchIndex
}

this.logger.info(`starting height`, {
startBatchIndex: this.state.highestCheckedBatchIndex,
})
}

async main(): Promise<void> {
const latestBatchIndex =
await this.state.messenger.contracts.l1.StateCommitmentChain.getTotalBatches()
const latestBatchIndex = await this.state.scc.getTotalBatches()
if (this.state.highestCheckedBatchIndex >= latestBatchIndex.toNumber()) {
await sleep(15000)
return
Expand All @@ -89,41 +107,30 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {

this.logger.info(`checking batch`, {
batchIndex: this.state.highestCheckedBatchIndex,
latestIndex: latestBatchIndex.toNumber(),
})

const targetEvents =
await this.state.messenger.contracts.l1.StateCommitmentChain.queryFilter(
this.state.messenger.contracts.l1.StateCommitmentChain.filters.StateBatchAppended(
this.state.highestCheckedBatchIndex
)
let event: ethers.Event
try {
event = await findEventForStateBatch(
this.state.scc,
this.state.highestCheckedBatchIndex
)

if (targetEvents.length === 0) {
this.logger.error(`unable to find event for batch`, {
batchIndex: this.state.highestCheckedBatchIndex,
})
this.metrics.inUnexpectedErrorState.set(1)
return
}

if (targetEvents.length > 1) {
this.logger.error(`found too many events for batch`, {
} catch (err) {
this.logger.error(`got unexpected error while searching for batch`, {
batchIndex: this.state.highestCheckedBatchIndex,
error: err,
})
this.metrics.inUnexpectedErrorState.set(1)
return
}

const targetEvent = targetEvents[0]
const batchTransaction = await targetEvent.getTransaction()
const [stateRoots] =
this.state.messenger.contracts.l1.StateCommitmentChain.interface.decodeFunctionData(
'appendStateBatch',
batchTransaction.data
)
const batchTransaction = await event.getTransaction()
const [stateRoots] = this.state.scc.interface.decodeFunctionData(
'appendStateBatch',
batchTransaction.data
)

const batchStart = targetEvent.args._prevTotalElements.toNumber() + 1
const batchSize = targetEvent.args._batchSize.toNumber()
const batchStart = event.args._prevTotalElements.toNumber() + 1
const batchSize = event.args._batchSize.toNumber()

// `getBlockRange` has a limit of 1000 blocks, so we have to break this request out into
// multiple requests of maximum 1000 blocks in the case that batchSize > 1000.
Expand All @@ -143,8 +150,7 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
for (const [i, stateRoot] of stateRoots.entries()) {
if (blocks[i].stateRoot !== stateRoot) {
this.metrics.isCurrentlyMismatched.set(1)
const fpw =
await this.state.messenger.contracts.l1.StateCommitmentChain.FRAUD_PROOF_WINDOW()
const fpw = await this.state.scc.FRAUD_PROOF_WINDOW()
this.logger.error(`state root mismatch`, {
blockNumber: blocks[i].number,
expectedStateRoot: blocks[i].stateRoot,
Expand Down
Loading