validate-address-dictator.ts 9.95 KB
Newer Older
1 2 3 4 5 6
'use strict'

import { ethers } from 'ethers'
import { task } from 'hardhat/config'
import * as types from 'hardhat/internal/core/params/argumentTypes'
import { hexStringEquals } from '@eth-optimism/core-utils'
7

8 9
import { getContractFactory, getContractDefinition } from '../src/contract-defs'
import { names } from '../src/address-names'
10 11 12
import {
  getInput,
  color as c,
13
  getArtifactFromManagedName,
14
  getEtherscanUrl,
15
  printSectionHead,
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 45 46 47 48 49 50 51 52 53
  printComparison,
} from '../src/validation-utils'

task('validate:address-dictator')
  .addParam(
    'dictator',
    'Address of the AddressDictator to validate.',
    undefined,
    types.string
  )
  .addParam(
    'manager',
    'Address of the Address Manager contract which would be updated by the Dictator.',
    undefined,
    types.string
  )
  .addParam(
    'multisig',
    'Address of the multisig contract which should be the final owner',
    undefined,
    types.string
  )
  .addOptionalParam(
    'contractsRpcUrl',
    'RPC Endpoint to query for data',
    process.env.CONTRACTS_RPC_URL,
    types.string
  )
  .setAction(async (args) => {
    if (!args.contractsRpcUrl) {
      throw new Error(
        c.red('RPC URL must be set in your env, or passed as an argument.')
      )
    }
    const provider = new ethers.providers.JsonRpcProvider(args.contractsRpcUrl)

    const network = await provider.getNetwork()
    console.log()
54
    printSectionHead("First make sure you're on the right chain:")
55 56 57 58 59 60 61
    console.log(
      `Reading from the ${c.red(network.name)} network (Chain ID: ${c.red(
        '' + network.chainId
      )})`
    )
    await getInput(c.yellow('OK? Hit enter to continue.'))

62
    const dictatorArtifact = getContractDefinition('AddressDictator')
63
    const dictatorCode = await provider.getCode(args.dictator)
64 65 66 67 68 69
    printSectionHead(`
Validate the Address Dictator deployment at\n${getEtherscanUrl(
      network,
      args.dictator
    )}`)

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    printComparison(
      'Comparing deployed AddressDictator bytecode against local build artifacts',
      'Deployed AddressDictator code',
      { name: 'Compiled bytecode', value: dictatorArtifact.deployedBytecode },
      { name: 'Deployed bytecode', value: dictatorCode }
    )

    // Connect to the deployed AddressDictator.
    const dictatorContract = getContractFactory('AddressDictator')
      .attach(args.dictator)
      .connect(provider)

    const finalOwner = await dictatorContract.finalOwner()
    printComparison(
      'Comparing the finalOwner address in the AddressDictator to the multisig address',
      'finalOwner',
      { name: 'multisig address', value: args.multisig },
      { name: 'finalOwner      ', value: finalOwner }
    )

90
    const deployedManager = await dictatorContract.manager()
91 92 93
    printComparison(
      'Validating the AddressManager address in the AddressDictator',
      'addressManager',
94 95
      { name: 'manager        ', value: args.manager },
      { name: 'Address Manager', value: deployedManager }
96 97 98 99 100 101 102 103 104 105 106 107 108 109
    )
    await getInput(c.yellow('OK? Hit enter to continue.'))

    // Get names and addresses from the Dictator.
    const namedAddresses = await dictatorContract.getNamedAddresses()

    // In order to reduce noise for the user, we query the AddressManager identify addresses that
    // will not be changed, and skip over them in this block.
    const managerContract = getContractFactory('Lib_AddressManager')
      .attach(args.manager)
      .connect(provider)

    // Now we loop over those and compare the addresses/deployedBytecode to deployment artifacts.
    for (const pair of namedAddresses) {
110 111 112 113 114 115
      if (pair.name === 'L2CrossDomainMessenger') {
        console.log('L2CrossDomainMessenger is set to:', pair.addr)
        await getInput(c.yellow('OK? Hit enter to continue.'))
        // This is an L2 predeploy, so we skip bytecode and config validation.
        continue
      }
116
      const currentAddress = await managerContract.getAddress(pair.name)
117
      const artifact = getArtifactFromManagedName(pair.name)
118 119
      const addressChanged = !hexStringEquals(currentAddress, pair.addr)
      if (addressChanged) {
120 121
        printSectionHead(
          `Validate the ${pair.name} deployment.
122
Current address: ${getEtherscanUrl(network, currentAddress)}
123
Upgraded address ${getEtherscanUrl(network, pair.addr)}`
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        )

        const code = await provider.getCode(pair.addr)
        printComparison(
          `Verifying ${pair.name} source code against local deployment artifacts`,
          `Deployed ${pair.name} code`,
          {
            name: 'artifact.deployedBytecode',
            value: artifact.deployedBytecode,
          },
          { name: 'Deployed bytecode        ', value: code }
        )

        // Identify contracts which inherit from Lib_AddressResolver, and check that they
        // have the right manager address.
        if (Object.keys(artifact)) {
          if (artifact.abi.some((el) => el.name === 'libAddressManager')) {
            const libAddressManager = await getContractFactory(
              'Lib_AddressResolver'
            )
              .attach(pair.addr)
              .connect(provider)
              .libAddressManager()

            printComparison(
              `Verifying ${pair.name} has the correct AddressManager address`,
              `The AddressManager address in ${pair.name}`,
              { name: 'Deployed value', value: libAddressManager },
152
              { name: 'Expected value', value: deployedManager }
153
            )
154
            await getInput(c.yellow('OK? Hit enter to continue.'))
155 156 157
          }
        }
      }
158
      await validateDeployedConfig(provider, network, args.manager, pair)
159
    }
160
    console.log(c.green('\nAddressManager Validation complete!'))
161
  })
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

/**
 * Validates that the deployed contracts have the expected storage variables.
 *
 * @param {*} provider
 * @param {{ name: string; addr: string }} pair The contract name and address
 */
const validateDeployedConfig = async (
  provider,
  network,
  manager,
  pair: { name: string; addr: string }
) => {
  printSectionHead(`
Ensure that the ${pair.name} at\n${getEtherscanUrl(
    network,
    pair.addr
  )} is configured correctly`)
  if (pair.name === names.managed.contracts.StateCommitmentChain) {
    const scc = getContractFactory(pair.name)
      .attach(pair.addr)
      .connect(provider)
    //  --scc-fraud-proof-window 604800 \
    const fraudProofWindow = await scc.FRAUD_PROOF_WINDOW()
    printComparison(
      'Checking the fraudProofWindow of the StateCommitmentChain',
      'StateCommitmentChain.fraudProofWindow',
      {
        name: 'Configured fraudProofWindow',
        value: ethers.BigNumber.from(604_800).toHexString(),
      },
      {
        name: 'Deployed fraudProofWindow  ',
        value: ethers.BigNumber.from(fraudProofWindow).toHexString(),
      }
    )
    await getInput(c.yellow('OK? Hit enter to continue.'))

    //  --scc-sequencer-publish-window 12592000 \
    const sequencerPublishWindow = await scc.SEQUENCER_PUBLISH_WINDOW()
    printComparison(
      'Checking the sequencerPublishWindow of the StateCommitmentChain',
      'StateCommitmentChain.sequencerPublishWindow',
      {
        name: 'Configured sequencerPublishWindow  ',
        value: ethers.BigNumber.from(12592000).toHexString(),
      },
      {
        name: 'Deployed sequencerPublishWindow',
        value: ethers.BigNumber.from(sequencerPublishWindow).toHexString(),
      }
    )
    await getInput(c.yellow('OK? Hit enter to continue.'))
  } else if (pair.name === names.managed.contracts.CanonicalTransactionChain) {
    const ctc = getContractFactory(pair.name)
      .attach(pair.addr)
      .connect(provider)

    //  --ctc-max-transaction-gas-limit 15000000 \
    const maxTransactionGasLimit = await ctc.maxTransactionGasLimit()
    printComparison(
      'Checking the maxTransactionGasLimit of the CanonicalTransactionChain',
      'CanonicalTransactionChain.maxTransactionGasLimit',
      {
        name: 'Configured maxTransactionGasLimit',
        value: ethers.BigNumber.from(15_000_000).toHexString(),
      },
      {
        name: 'Deployed maxTransactionGasLimit  ',
        value: ethers.BigNumber.from(maxTransactionGasLimit).toHexString(),
      }
    )
    await getInput(c.yellow('OK? Hit enter to continue.'))
    //  --ctc-l2-gas-discount-divisor 32 \
    const l2GasDiscountDivisor = await ctc.l2GasDiscountDivisor()
    printComparison(
      'Checking the l2GasDiscountDivisor of the CanonicalTransactionChain',
      'CanonicalTransactionChain.l2GasDiscountDivisor',
      {
        name: 'Configured l2GasDiscountDivisor',
        value: ethers.BigNumber.from(32).toHexString(),
      },
      {
        name: 'Deployed l2GasDiscountDivisor  ',
        value: ethers.BigNumber.from(l2GasDiscountDivisor).toHexString(),
      }
    )
    await getInput(c.yellow('OK? Hit enter to continue.'))
    //  --ctc-enqueue-gas-cost 60000 \
    const enqueueGasCost = await ctc.enqueueGasCost()
    printComparison(
      'Checking the enqueueGasCost of the CanonicalTransactionChain',
      'CanonicalTransactionChain.enqueueGasCost',
      {
        name: 'Configured enqueueGasCost',
        value: ethers.BigNumber.from(60000).toHexString(),
      },
      {
        name: 'Deployed enqueueGasCost  ',
        value: ethers.BigNumber.from(enqueueGasCost).toHexString(),
      }
    )
    await getInput(c.yellow('OK? Hit enter to continue.'))
  } else if (pair.name === names.managed.contracts.OVM_L1CrossDomainMessenger) {
    const messengerManager = await getContractFactory('L1CrossDomainMessenger')
      .attach(pair.addr)
      .connect(provider)
      .libAddressManager()
    printComparison(
      'Ensure that the L1CrossDomainMessenger (implementation) is initialized with a non-zero Address Manager variable',
      "L1CrossDomainMessenger's Lib_AddressManager",
      {
        name: 'Configured Lib_AddressManager',
        value: messengerManager,
      },
      {
        name: 'Deployed Lib_AddressManager  ',
        value: manager,
      }
    )
  } else {
    console.log(c.green(`${pair.name} has no config to check`))
    await getInput(c.yellow('OK? Hit enter to continue.'))
  }
}