Commit 17999a54 authored by smartcontracts's avatar smartcontracts Committed by GitHub

feat(fd): add fault status API (#3476)

Adds a fault status API endpoint so people can easily query if the node
is currently diverged.
Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent e164e22e
---
'@eth-optimism/fault-detector': patch
---
Adds a fault status API to the Fault Detector.
import { BaseServiceV2, Gauge, validators } from '@eth-optimism/common-ts' import {
BaseServiceV2,
ExpressRouter,
Gauge,
validators,
} from '@eth-optimism/common-ts'
import { getChainId, sleep, toRpcHexString } from '@eth-optimism/core-utils' import { getChainId, sleep, toRpcHexString } from '@eth-optimism/core-utils'
import { CrossChainMessenger } from '@eth-optimism/sdk' import { CrossChainMessenger } from '@eth-optimism/sdk'
import { Provider } from '@ethersproject/abstract-provider' import { Provider } from '@ethersproject/abstract-provider'
...@@ -29,6 +34,7 @@ type State = { ...@@ -29,6 +34,7 @@ type State = {
scc: Contract scc: Contract
messenger: CrossChainMessenger messenger: CrossChainMessenger
highestCheckedBatchIndex: number highestCheckedBatchIndex: number
diverged: boolean
} }
export class FaultDetector extends BaseServiceV2<Options, Metrics, State> { export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
...@@ -90,6 +96,9 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> { ...@@ -90,6 +96,9 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
l2ChainId: await getChainId(this.options.l2RpcProvider), l2ChainId: await getChainId(this.options.l2RpcProvider),
}) })
// Not diverged by default.
this.state.diverged = false
// We use this a lot, a bit cleaner to pull out to the top level of the state object. // 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 this.state.scc = this.state.messenger.contracts.l1.StateCommitmentChain
this.state.fpw = (await this.state.scc.FRAUD_PROOF_WINDOW()).toNumber() this.state.fpw = (await this.state.scc.FRAUD_PROOF_WINDOW()).toNumber()
...@@ -108,6 +117,14 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> { ...@@ -108,6 +117,14 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
}) })
} }
async routes(router: ExpressRouter): Promise<void> {
router.get('/status', async (req, res) => {
return res.status(200).json({
ok: !this.state.diverged,
})
})
}
async main(): Promise<void> { async main(): Promise<void> {
let latestBatchIndex: number let latestBatchIndex: number
try { try {
...@@ -226,6 +243,7 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> { ...@@ -226,6 +243,7 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
for (const [i, stateRoot] of stateRoots.entries()) { for (const [i, stateRoot] of stateRoots.entries()) {
if (blocks[i].stateRoot !== stateRoot) { if (blocks[i].stateRoot !== stateRoot) {
this.state.diverged = true
this.metrics.isCurrentlyMismatched.set(1) this.metrics.isCurrentlyMismatched.set(1)
this.logger.error(`state root mismatch`, { this.logger.error(`state root mismatch`, {
blockNumber: blocks[i].number, blockNumber: blocks[i].number,
...@@ -250,6 +268,7 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> { ...@@ -250,6 +268,7 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
) )
// If we got through the above without throwing an error, we should be fine to reset. // If we got through the above without throwing an error, we should be fine to reset.
this.state.diverged = false
this.metrics.isCurrentlyMismatched.set(0) this.metrics.isCurrentlyMismatched.set(0)
} }
} }
......
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