differential-testing.ts 6.53 KB
Newer Older
1 2 3 4 5 6 7 8 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
import { BigNumber, utils, constants } from 'ethers'
import {
  decodeVersionedNonce,
  hashCrossDomainMessage,
  DepositTx,
  SourceHashDomain,
  encodeCrossDomainMessage,
  hashWithdrawal,
  hashOutputRootProof,
} from '@eth-optimism/core-utils'
import { SecureTrie } from '@ethereumjs/trie'
import { Account, Address, toBuffer, bufferToHex } from '@ethereumjs/util'

import { predeploys } from '../src'

const { hexZeroPad, RLP, keccak256 } = utils

const args = process.argv.slice(2)
const command = args[0]

;(async () => {
  switch (command) {
    case 'decodeVersionedNonce': {
      const input = BigNumber.from(args[1])
      const [nonce, version] = decodeVersionedNonce(input)

      const output = utils.defaultAbiCoder.encode(
        ['uint256', 'uint256'],
        [nonce.toHexString(), version.toHexString()]
      )
      process.stdout.write(output)
      break
    }
    case 'encodeCrossDomainMessage': {
      const nonce = BigNumber.from(args[1])
      const sender = args[2]
      const target = args[3]
      const value = BigNumber.from(args[4])
      const gasLimit = BigNumber.from(args[5])
      const data = args[6]

      const encoding = encodeCrossDomainMessage(
        nonce,
        sender,
        target,
        value,
        gasLimit,
        data
      )

      const output = utils.defaultAbiCoder.encode(['bytes'], [encoding])
      process.stdout.write(output)
      break
    }
    case 'hashCrossDomainMessage': {
      const nonce = BigNumber.from(args[1])
      const sender = args[2]
      const target = args[3]
      const value = BigNumber.from(args[4])
      const gasLimit = BigNumber.from(args[5])
      const data = args[6]

      const hash = hashCrossDomainMessage(
        nonce,
        sender,
        target,
        value,
        gasLimit,
        data
      )
      const output = utils.defaultAbiCoder.encode(['bytes32'], [hash])
      process.stdout.write(output)
      break
    }
    case 'hashDepositTransaction': {
      // The solidity transaction hash computation currently only works with
      // user deposits. System deposit transaction hashing is not supported.
      const l1BlockHash = args[1]
      const logIndex = BigNumber.from(args[2])
      const from = args[3]
      const to = args[4]
      const mint = BigNumber.from(args[5])
      const value = BigNumber.from(args[6])
      const gas = BigNumber.from(args[7])
      const data = args[8]

      const tx = new DepositTx({
        l1BlockHash,
        logIndex,
        from,
        to,
        mint,
        value,
        gas,
        data,
96
        isSystemTransaction: false,
97 98 99 100 101 102 103
        domain: SourceHashDomain.UserDeposit,
      })

      const digest = tx.hash()
      const output = utils.defaultAbiCoder.encode(['bytes32'], [digest])
      process.stdout.write(output)
      break
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    }
    case 'encodeDepositTransaction': {
      const from = args[1]
      const to = args[2]
      const value = BigNumber.from(args[3])
      const mint = BigNumber.from(args[4])
      const gasLimit = BigNumber.from(args[5])
      const isCreate = args[6] === 'true' ? true : false
      const data = args[7]
      const l1BlockHash = args[8]
      const logIndex = BigNumber.from(args[9])

      const tx = new DepositTx({
        from,
        to: isCreate ? null : to,
        value,
        mint,
        gas: gasLimit,
        data,
        l1BlockHash,
        logIndex,
        domain: SourceHashDomain.UserDeposit,
      })

      const raw = tx.encode()
      const output = utils.defaultAbiCoder.encode(['bytes'], [raw])
      process.stdout.write(output)
      break
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    }
    case 'hashWithdrawal': {
      const nonce = BigNumber.from(args[1])
      const sender = args[2]
      const target = args[3]
      const value = BigNumber.from(args[4])
      const gas = BigNumber.from(args[5])
      const data = args[6]

      const hash = hashWithdrawal(nonce, sender, target, value, gas, data)
      const output = utils.defaultAbiCoder.encode(['bytes32'], [hash])
      process.stdout.write(output)
      break
    }
    case 'hashOutputRootProof': {
      const version = hexZeroPad(BigNumber.from(args[1]).toHexString(), 32)
      const stateRoot = hexZeroPad(BigNumber.from(args[2]).toHexString(), 32)
149
      const messagePasserStorageRoot = hexZeroPad(
150 151 152 153 154 155 156 157 158 159 160
        BigNumber.from(args[3]).toHexString(),
        32
      )
      const latestBlockhash = hexZeroPad(
        BigNumber.from(args[4]).toHexString(),
        32
      )

      const hash = hashOutputRootProof({
        version,
        stateRoot,
161
        messagePasserStorageRoot,
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
        latestBlockhash,
      })
      const output = utils.defaultAbiCoder.encode(['bytes32'], [hash])
      process.stdout.write(output)
      break
    }
    case 'getFinalizeWithdrawalTransactionInputs': {
      const nonce = BigNumber.from(args[1])
      const sender = args[2]
      const target = args[3]
      const value = BigNumber.from(args[4])
      const gas = BigNumber.from(args[5])
      const data = args[6]

      // Compute the withdrawalHash
      const withdrawalHash = hashWithdrawal(
        nonce,
        sender,
        target,
        value,
        gas,
        data
      )

      // Compute the storage slot the withdrawalHash will be stored in
      const slot = utils.defaultAbiCoder.encode(
        ['bytes32', 'bytes32'],
        [withdrawalHash, utils.hexZeroPad('0x', 32)]
      )
      const key = keccak256(slot)

      // Create the account storage trie
      const storage = new SecureTrie()
      // Put a bool "true" into storage
      await storage.put(toBuffer(key), toBuffer('0x01'))

      // Put the storage root into the L2ToL1MessagePasser storage
      const address = Address.fromString(predeploys.L2ToL1MessagePasser)
      const account = Account.fromAccountData({
        nonce: 0,
        balance: 0,
        stateRoot: storage.root,
      })

      const world = new SecureTrie()
      await world.put(address.toBuffer(), account.serialize())

      const proof = await SecureTrie.createProof(storage, toBuffer(key))

      const outputRoot = hashOutputRootProof({
        version: constants.HashZero,
        stateRoot: bufferToHex(world.root),
214
        messagePasserStorageRoot: bufferToHex(storage.root),
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        latestBlockhash: constants.HashZero,
      })

      const encodedProof = RLP.encode(proof)

      const output = utils.defaultAbiCoder.encode(
        ['bytes32', 'bytes32', 'bytes32', 'bytes32', 'bytes'],
        [world.root, storage.root, outputRoot, withdrawalHash, encodedProof]
      )
      process.stdout.write(output)
      break
    }
  }
})().catch((err: Error) => {
  console.error(err)
  process.stdout.write('')
})