fetch-batches.ts 2.92 KB
Newer Older
1 2 3
import { ethers } from 'ethers'
import { task } from 'hardhat/config'
import * as types from 'hardhat/internal/core/params/argumentTypes'
4 5 6 7 8
import {
  BatchType,
  SequencerBatch,
  calldataCost,
} from '@eth-optimism/core-utils'
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

import { names } from '../src/address-names'
import { getContractFromArtifact } from '../src/deploy-utils'

// Need to export env vars
// CONTRACTS_TARGET_NETWORK
// CONTRACTS_DEPLOYER_KEY
// CONTRACTS_RPC_URL
task('fetch-batches')
  .addOptionalParam(
    'contractsRpcUrl',
    'Ethereum HTTP Endpoint',
    process.env.CONTRACTS_RPC_URL || 'http://127.0.0.1:8545',
    types.string
  )
  .addOptionalParam('start', 'Start block height', 0, types.int)
  .addOptionalParam('end', 'End block height', undefined, types.int)
  .setAction(async (args, hre) => {
    const provider = new ethers.providers.StaticJsonRpcProvider(
      args.contractsRpcUrl
    )

    let CanonicalTransactionChain = await getContractFromArtifact(
      hre,
      names.managed.contracts.CanonicalTransactionChain
    )
    CanonicalTransactionChain = CanonicalTransactionChain.connect(provider)

    const start = args.start
    let end = args.end
    if (!end) {
      end = await provider.getBlockNumber()
    }

    const batches = []

45
    for (let i = start; i <= end; i += 2001) {
46 47 48 49 50 51 52 53 54 55 56 57
      const tip = Math.min(i + 2000, end)
      console.error(`Querying events ${i}-${tip}`)

      const events = await CanonicalTransactionChain.queryFilter(
        CanonicalTransactionChain.filters.SequencerBatchAppended(),
        i,
        tip
      )

      for (const event of events) {
        const tx = await provider.getTransaction(event.transactionHash)
        const batch = (SequencerBatch as any).fromHex(tx.data)
58

59 60
        // Add extra fields to the resulting json
        // so that the serialization sizes and gas usage can be observed
61 62 63 64 65
        const json = batch.toJSON()
        json.sizes = {
          legacy: 0,
          zlib: 0,
        }
66 67 68 69
        json.gasUsage = {
          legacy: 0,
          zlib: 0,
        }
70 71 72 73

        // Create a copy of the batch to serialize in
        // the alternative format
        const copy = (SequencerBatch as any).fromHex(tx.data)
74 75
        let legacy: Buffer
        let zlib: Buffer
76 77
        if (batch.type === BatchType.ZLIB) {
          copy.type = BatchType.LEGACY
78 79
          legacy = copy.encode()
          zlib = batch.encode()
80 81
        } else {
          copy.type = BatchType.ZLIB
82 83
          zlib = copy.encode()
          legacy = batch.encode()
84 85
        }

86 87 88 89 90 91 92 93 94
        json.sizes.legacy = legacy.length
        json.sizes.zlib = zlib.length

        json.sizes.compressionRatio = json.sizes.zlib / json.sizes.legacy

        json.gasUsage.legacy = calldataCost(legacy).toNumber()
        json.gasUsage.zlib = calldataCost(zlib).toNumber()
        json.gasUsage.compressionRatio =
          json.gasUsage.zlib / json.gasUsage.legacy
95 96

        batches.push(json)
97 98 99 100 101
      }
    }

    console.log(JSON.stringify(batches, null, 2))
  })