check-block-hashes.ts 1.65 KB
Newer Older
1 2
import { task } from 'hardhat/config'
import { providers } from 'ethers'
3
import { getChainId } from '@eth-optimism/core-utils'
4 5 6 7 8 9 10 11 12 13 14 15 16

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

task(
  'check-block-hashes',
  'Compares the block hashes of two different replicas.'
)
  .addPositionalParam('replicaA', 'The first replica')
  .addPositionalParam('replicaB', 'The second replica')
  .setAction(async ({ replicaA, replicaB }) => {
    const providerA = new providers.JsonRpcProvider(replicaA)
    const providerB = new providers.JsonRpcProvider(replicaB)

17 18
    let chainIdA
    let chainIdB
19
    try {
20
      chainIdA = await getChainId(providerA)
21
    } catch (e) {
22
      console.error(`Error getting network chainId from ${replicaA}:`)
23 24 25
      die(e)
    }
    try {
26
      chainIdB = await getChainId(providerB)
27
    } catch (e) {
28
      console.error(`Error getting network chainId from ${replicaB}:`)
29 30 31
      die(e)
    }

32
    if (chainIdA !== chainIdB) {
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
      die('Chain IDs do not match')
      return
    }

    logStderr('Getting block height.')
    const heightA = await providerA.getBlockNumber()
    const heightB = await providerB.getBlockNumber()
    const endHeight = Math.min(heightA, heightB)
    logStderr(`Chose block height: ${endHeight}`)

    for (let n = endHeight; n >= 1; n--) {
      const blocks = await Promise.all([
        providerA.getBlock(n),
        providerB.getBlock(n),
      ])

      const hashA = blocks[0].hash
      const hashB = blocks[1].hash
      if (hashA !== hashB) {
        console.log(`HASH MISMATCH! block=${n} a=${hashA} b=${hashB}`)
        continue
      }

      console.log(`HASHES OK! block=${n} hash=${hashA}`)
      return
    }
  })