Commit b6e4d067 authored by Kevin Chen's avatar Kevin Chen

Remove the OutputOracle interface.

parent c4910baa
import { Contract, BigNumber } from 'ethers'
import { Contract } from 'ethers'
import { Logger } from '@eth-optimism/common-ts'
export interface OutputOracle {
contract: Contract
filter: any
getTotalElements: () => Promise<BigNumber>
getEventIndex: (args: any) => BigNumber
}
/**
* Partial event interface, meant to reduce the size of the event cache to avoid
* running out of memory.
......@@ -55,11 +48,11 @@ const getCache = (
* @param filter Event filter to use.
*/
export const updateOracleCache = async (
oracle: OutputOracle,
oracle: Contract,
logger?: Logger
): Promise<void> => {
const cache = getCache(oracle.contract.address)
const endBlock = await oracle.contract.provider.getBlockNumber()
const cache = getCache(oracle.address)
const endBlock = await oracle.provider.getBlockNumber()
logger?.info('visiting uncached oracle events for range', {
node: 'l1',
cachedUntilBlock: cache.highestBlock,
......@@ -77,15 +70,15 @@ export const updateOracleCache = async (
blockRangeSize: step,
})
const events = await oracle.contract.queryFilter(
oracle.filter,
const events = await oracle.queryFilter(
oracle.filters.OutputProposed(),
currentBlock,
currentBlock + step
)
// Throw the events into the cache.
for (const event of events) {
cache.eventCache[oracle.getEventIndex(event.args).toNumber()] = {
cache.eventCache[event.args.l2OutputIndex.toNumber()] = {
blockNumber: event.blockNumber,
transactionHash: event.transactionHash,
args: event.args,
......@@ -134,11 +127,11 @@ export const updateOracleCache = async (
* @returns Event corresponding to the batch.
*/
export const findEventForStateBatch = async (
oracle: OutputOracle,
oracle: Contract,
index: number,
logger?: Logger
): Promise<PartialEvent> => {
const cache = getCache(oracle.contract.address)
const cache = getCache(oracle.address)
// Try to find the event in cache first.
if (cache.eventCache[index]) {
......@@ -165,12 +158,12 @@ export const findEventForStateBatch = async (
* @returns Starting state root batch index.
*/
export const findFirstUnfinalizedStateBatchIndex = async (
oracle: OutputOracle,
oracle: Contract,
fpw: number,
logger?: Logger
): Promise<number> => {
const latestBlock = await oracle.contract.provider.getBlock('latest')
const totalBatches = (await oracle.getTotalElements()).toNumber()
const latestBlock = await oracle.provider.getBlock('latest')
const totalBatches = (await oracle.nextOutputIndex()).toNumber()
// Perform a binary search to find the next batch that will pass the challenge period.
let lo = 0
......@@ -178,7 +171,7 @@ export const findFirstUnfinalizedStateBatchIndex = async (
while (lo !== hi) {
const mid = Math.floor((lo + hi) / 2)
const event = await findEventForStateBatch(oracle, mid, logger)
const block = await oracle.contract.provider.getBlock(event.blockNumber)
const block = await oracle.provider.getBlock(event.blockNumber)
if (block.timestamp + fpw < latestBlock.timestamp) {
lo = mid + 1
......
......@@ -16,7 +16,7 @@ import {
OEL1ContractsLike,
} from '@eth-optimism/sdk'
import { Provider } from '@ethersproject/abstract-provider'
import { ethers } from 'ethers'
import { Contract, ethers } from 'ethers'
import dateformat from 'dateformat'
import { version } from '../package.json'
......@@ -24,7 +24,6 @@ import {
findFirstUnfinalizedStateBatchIndex,
findEventForStateBatch,
PartialEvent,
OutputOracle,
updateOracleCache,
} from './helpers'
......@@ -33,8 +32,6 @@ type Options = {
l2RpcProvider: Provider
startBatchIndex: number
optimismPortalAddress?: string
bedrock: boolean
stateCommitmentChainAddress?: string
}
type Metrics = {
......@@ -45,7 +42,7 @@ type Metrics = {
type State = {
faultProofWindow: number
outputOracle: OutputOracle
outputOracle: Contract
messenger: CrossChainMessenger
currentBatchIndex: number
diverged: boolean
......@@ -82,19 +79,6 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
desc: '[Custom OP Chains] Deployed OptimismPortal contract address. Used to retrieve necessary info for ouput verification ',
public: true,
},
// Deprecated options.
bedrock: {
validator: validators.bool,
default: true,
desc: '[Deprecated, must be set to true] Whether or not the service is running against a Bedrock chain',
public: false,
},
stateCommitmentChainAddress: {
validator: validators.str,
default: ethers.constants.AddressZero,
desc: '[Deprecated, must be set to 0x0] Deployed StateCommitmentChain contract address. Used to fetch necessary info for output verification.',
public: false,
},
},
metricsSpec: {
highestBatchIndex: {
......@@ -210,13 +194,7 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
`fault proof window is ${this.state.faultProofWindow} seconds`
)
const outputOracle = this.state.messenger.contracts.l1.L2OutputOracle
this.state.outputOracle = {
contract: outputOracle,
filter: outputOracle.filters.OutputProposed(),
getTotalElements: async () => outputOracle.nextOutputIndex(),
getEventIndex: (args) => args.l2OutputIndex,
}
this.state.outputOracle = this.state.messenger.contracts.l1.L2OutputOracle
// Populate the event cache.
this.logger.info('warming event cache, this might take a while...')
......@@ -236,7 +214,7 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
// but it happens often on testnets because the FAULTPROOFWINDOW is very short.
if (firstUnfinalized === undefined) {
this.logger.info('no unfinalized batches found. skipping all batches.')
const totalBatches = await this.state.outputOracle.getTotalElements()
const totalBatches = await this.state.outputOracle.nextOutputIndex()
this.state.currentBatchIndex = totalBatches.toNumber() - 1
} else {
this.state.currentBatchIndex = firstUnfinalized
......@@ -266,17 +244,17 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
let latestBatchIndex: number
try {
const totalBatches = await this.state.outputOracle.getTotalElements()
const totalBatches = await this.state.outputOracle.nextOutputIndex()
latestBatchIndex = totalBatches.toNumber() - 1
} catch (err) {
this.logger.error('failed to query total # of batches', {
error: err,
node: 'l1',
section: 'getTotalElements',
section: 'nextOutputIndex',
})
this.metrics.nodeConnectionFailures.inc({
layer: 'l1',
section: 'getTotalElements',
section: 'nextOutputIndex',
})
await sleep(15000)
return
......@@ -410,7 +388,7 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
new Date(
(ethers.BigNumber.from(outputBlock.timestamp).toNumber() +
this.state.faultProofWindow) *
1000
1000
),
'mmmm dS, yyyy, h:MM:ss TT'
),
......
......@@ -8,7 +8,6 @@ import { expect } from './setup'
import {
findEventForStateBatch,
findFirstUnfinalizedStateBatchIndex,
OutputOracle,
} from '../src'
describe('helpers', () => {
......@@ -29,7 +28,6 @@ describe('helpers', () => {
})
let L2OutputOracle: Contract
let oracle: OutputOracle
beforeEach(async () => {
L2OutputOracle = await getContractFactory('L2OutputOracle', signer).deploy(
deployConfig.l2OutputOracleSubmissionInterval,
......@@ -40,13 +38,6 @@ describe('helpers', () => {
deployConfig.l2OutputOracleChallenger,
deployConfig.finalizationPeriodSeconds
)
oracle = {
contract: L2OutputOracle,
filter: L2OutputOracle.filters.OutputProposed(),
getTotalElements: async () => L2OutputOracle.nextOutputIndex(),
getEventIndex: (args: any) => args.l2OutputIndex,
}
})
describe('findEventForStateBatch', () => {
......@@ -70,7 +61,7 @@ describe('helpers', () => {
})
it('should return the event', async () => {
const event = await findEventForStateBatch(oracle, 0)
const event = await findEventForStateBatch(L2OutputOracle, 0)
expect(event.args.l2OutputIndex).to.equal(0)
})
......@@ -79,7 +70,7 @@ describe('helpers', () => {
describe('when the event does not exist', () => {
it('should throw an error', async () => {
await expect(
findEventForStateBatch(oracle, 0)
findEventForStateBatch(L2OutputOracle, 0)
).to.eventually.be.rejectedWith('unable to find event for batch')
})
})
......@@ -126,7 +117,7 @@ describe('helpers', () => {
it('should find the first batch older than the FPW', async () => {
const first = await findFirstUnfinalizedStateBatchIndex(
oracle,
L2OutputOracle,
deployConfig.finalizationPeriodSeconds
)
......@@ -168,7 +159,7 @@ describe('helpers', () => {
it('should return zero', async () => {
const first = await findFirstUnfinalizedStateBatchIndex(
oracle,
L2OutputOracle,
deployConfig.finalizationPeriodSeconds
)
......@@ -218,7 +209,7 @@ describe('helpers', () => {
it('should return undefined', async () => {
const first = await findFirstUnfinalizedStateBatchIndex(
oracle,
L2OutputOracle,
deployConfig.finalizationPeriodSeconds
)
......
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