Skip to content

Commit

Permalink
Replace getNetwork() calls with getChainId() utility (ethereum-optimi…
Browse files Browse the repository at this point in the history
…sm#2535)

* refactor: replace calls to getNetwork() with getChainId util

* requested changes
  • Loading branch information
FlattestWhite authored May 4, 2022
1 parent f414d95 commit 412688d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .changeset/thirty-garlics-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@eth-optimism/integration-tests': patch
'@eth-optimism/contracts': patch
'@eth-optimism/data-transport-layer': patch
'@eth-optimism/message-relayer': patch
---

Replace calls to getNetwork() with getChainId util
15 changes: 8 additions & 7 deletions integration-tests/tasks/check-block-hashes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { task } from 'hardhat/config'
import { providers } from 'ethers'
import { getChainId } from '@eth-optimism/core-utils'

import { die, logStderr } from '../test/shared/utils'

Expand All @@ -13,22 +14,22 @@ task(
const providerA = new providers.JsonRpcProvider(replicaA)
const providerB = new providers.JsonRpcProvider(replicaB)

let netA
let netB
let chainIdA
let chainIdB
try {
netA = await providerA.getNetwork()
chainIdA = await getChainId(providerA)
} catch (e) {
console.error(`Error getting network from ${replicaA}:`)
console.error(`Error getting network chainId from ${replicaA}:`)
die(e)
}
try {
netB = await providerA.getNetwork()
chainIdB = await getChainId(providerB)
} catch (e) {
console.error(`Error getting network from ${replicaB}:`)
console.error(`Error getting network chainId from ${replicaB}:`)
die(e)
}

if (netA.chainId !== netB.chainId) {
if (chainIdA !== chainIdB) {
die('Chain IDs do not match')
return
}
Expand Down
5 changes: 2 additions & 3 deletions integration-tests/test/rpc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Imports: External */
import { expectApprox, sleep } from '@eth-optimism/core-utils'
import { expectApprox, getChainId, sleep } from '@eth-optimism/core-utils'
import { Wallet, BigNumber, Contract, ContractFactory, constants } from 'ethers'
import { serialize } from '@ethersproject/transactions'
import { ethers } from 'hardhat'
Expand Down Expand Up @@ -471,8 +471,7 @@ describe('Basic RPC tests', () => {

describe('eth_chainId', () => {
it('should get the correct chainid', async () => {
const { chainId } = await env.l2Provider.getNetwork()
expect(chainId).to.be.eq(L2_CHAINID)
expect(await getChainId(env.l2Provider)).to.be.eq(L2_CHAINID)
})
})

Expand Down
5 changes: 2 additions & 3 deletions packages/contracts/src/deploy-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers, Contract } from 'ethers'
import { Provider } from '@ethersproject/abstract-provider'
import { Signer } from '@ethersproject/abstract-signer'
import { sleep, awaitCondition } from '@eth-optimism/core-utils'
import { sleep, awaitCondition, getChainId } from '@eth-optimism/core-utils'
import { HttpNetworkConfig } from 'hardhat/types'

import { getDeployConfig } from './deploy-config'
Expand Down Expand Up @@ -270,8 +270,7 @@ export const getContractFromArtifact = async (
}

export const isHardhatNode = async (hre) => {
const { chainId } = await hre.ethers.provider.getNetwork()
return chainId === 31337
return (await getChainId(hre.ethers.provider)) === 31337
}

// Large balance to fund accounts with.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Imports: External */
import { BaseService, Metrics } from '@eth-optimism/common-ts'
import { StaticJsonRpcProvider } from '@ethersproject/providers'
import { sleep, toRpcHexString } from '@eth-optimism/core-utils'
import { getChainId, sleep, toRpcHexString } from '@eth-optimism/core-utils'
import { BigNumber } from 'ethers'
import { LevelUp } from 'levelup'
import axios from 'axios'
Expand Down Expand Up @@ -127,9 +127,9 @@ export class L2IngestionService extends BaseService<L2IngestionServiceOptions> {
}

protected async checkConsistency(): Promise<void> {
const network = await this.state.l2RpcProvider.getNetwork()
const chainId = await getChainId(this.state.l2RpcProvider)
const shouldDoCheck = !(await this.state.db.getConsistencyCheckFlag())
if (shouldDoCheck && network.chainId === 69) {
if (shouldDoCheck && chainId === 69) {
this.logger.info('performing consistency check')
const highestBlock =
await this.state.db.getHighestSyncedUnconfirmedBlock()
Expand Down
6 changes: 2 additions & 4 deletions packages/message-relayer/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Imports: External */
import { Signer } from 'ethers'
import { sleep } from '@eth-optimism/core-utils'
import { getChainId, sleep } from '@eth-optimism/core-utils'
import {
BaseServiceV2,
validators,
Expand Down Expand Up @@ -80,12 +80,10 @@ export class MessageRelayerService extends BaseServiceV2<
this.options.l1RpcProvider
)

const l1Network = await this.state.wallet.provider.getNetwork()
const l1ChainId = l1Network.chainId
this.state.messenger = new CrossChainMessenger({
l1SignerOrProvider: this.state.wallet,
l2SignerOrProvider: this.options.l2RpcProvider,
l1ChainId,
l1ChainId: await getChainId(this.state.wallet.provider),
})

this.state.highestCheckedL2Tx = this.options.fromL2TransactionIndex || 1
Expand Down

0 comments on commit 412688d

Please sign in to comment.