From 412688d5129485f8b86c23c6c23c0fea219d5621 Mon Sep 17 00:00:00 2001 From: Richard Guan Date: Thu, 5 May 2022 01:11:27 +1000 Subject: [PATCH] Replace getNetwork() calls with getChainId() utility (#2535) * refactor: replace calls to getNetwork() with getChainId util * requested changes --- .changeset/thirty-garlics-know.md | 8 ++++++++ integration-tests/tasks/check-block-hashes.ts | 15 ++++++++------- integration-tests/test/rpc.spec.ts | 5 ++--- packages/contracts/src/deploy-utils.ts | 5 ++--- .../src/services/l2-ingestion/service.ts | 6 +++--- packages/message-relayer/src/service.ts | 6 ++---- 6 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 .changeset/thirty-garlics-know.md diff --git a/.changeset/thirty-garlics-know.md b/.changeset/thirty-garlics-know.md new file mode 100644 index 000000000000..5cafe4ddb941 --- /dev/null +++ b/.changeset/thirty-garlics-know.md @@ -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 diff --git a/integration-tests/tasks/check-block-hashes.ts b/integration-tests/tasks/check-block-hashes.ts index c30d67cfdcc2..9c50d227afcd 100644 --- a/integration-tests/tasks/check-block-hashes.ts +++ b/integration-tests/tasks/check-block-hashes.ts @@ -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' @@ -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 } diff --git a/integration-tests/test/rpc.spec.ts b/integration-tests/test/rpc.spec.ts index 479d4a09f676..9102af2d4736 100644 --- a/integration-tests/test/rpc.spec.ts +++ b/integration-tests/test/rpc.spec.ts @@ -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' @@ -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) }) }) diff --git a/packages/contracts/src/deploy-utils.ts b/packages/contracts/src/deploy-utils.ts index eda852685117..971ab024d102 100644 --- a/packages/contracts/src/deploy-utils.ts +++ b/packages/contracts/src/deploy-utils.ts @@ -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' @@ -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. diff --git a/packages/data-transport-layer/src/services/l2-ingestion/service.ts b/packages/data-transport-layer/src/services/l2-ingestion/service.ts index ab9969248241..87e4fb1753b4 100644 --- a/packages/data-transport-layer/src/services/l2-ingestion/service.ts +++ b/packages/data-transport-layer/src/services/l2-ingestion/service.ts @@ -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' @@ -127,9 +127,9 @@ export class L2IngestionService extends BaseService { } protected async checkConsistency(): Promise { - 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() diff --git a/packages/message-relayer/src/service.ts b/packages/message-relayer/src/service.ts index 95b6dd26e4de..2a1e692eca9c 100644 --- a/packages/message-relayer/src/service.ts +++ b/packages/message-relayer/src/service.ts @@ -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, @@ -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