|
| 1 | +const { AssertionError } = require('chai') |
| 2 | +const chalk = require('chalk') |
| 3 | +const { web3 } = require('hardhat') |
| 4 | +const { readJSON } = require('../scripts/helpers/fs') |
| 5 | +const { APPS_TO_NAMES, CONTRACTS_TO_NAMES, IGNORE_METADATA_CONTRACTS } = require('./deployed-bytecode-consts') |
| 6 | + |
| 7 | +const empty32bytes = '0000000000000000000000000000000000000000000000000000000000000000' |
| 8 | + |
| 9 | +function isInsideEmpty32bytes(byteCode, index) { |
| 10 | + const start = index - 63 >= 0 ? index - 63 : 0 |
| 11 | + const end = index + 64 <= byteCode.length ? index + 64 : byteCode.length |
| 12 | + return byteCode.slice(start, end).indexOf(empty32bytes) >= 0 |
| 13 | +} |
| 14 | + |
| 15 | +function stripMetadata(byteCode) { |
| 16 | + let metaDataLength = parseInt(byteCode.slice(-4), 16) * 2 |
| 17 | + let metaDataIndex = byteCode.length - metaDataLength - 4 |
| 18 | + if (metaDataIndex > 0) { |
| 19 | + return byteCode.slice(0, metaDataIndex) |
| 20 | + } |
| 21 | + return byteCode |
| 22 | +} |
| 23 | + |
| 24 | +function isBytecodeSimilar(first, second, ignoreMetadata) { |
| 25 | + if (ignoreMetadata) { |
| 26 | + first = stripMetadata(first) |
| 27 | + second = stripMetadata(second) |
| 28 | + } |
| 29 | + if (first.length != second.length) { |
| 30 | + return false |
| 31 | + } |
| 32 | + for (var i = 0; i < first.length; i++) { |
| 33 | + if (first[i] != second[i] && !isInsideEmpty32bytes(first, i) && !isInsideEmpty32bytes(second, i)) { |
| 34 | + return false |
| 35 | + } |
| 36 | + } |
| 37 | + return true |
| 38 | +} |
| 39 | + |
| 40 | +async function assertByteCode(address, artifactName, deployTx) { |
| 41 | + const artifact = await artifacts.readArtifact(artifactName) |
| 42 | + let bytecodeFromArtifact = artifact.deployedBytecode.toLowerCase() |
| 43 | + const bytecodeFromRpc = (await web3.eth.getCode(address)).toLowerCase() |
| 44 | + const ignoreMetadata = IGNORE_METADATA_CONTRACTS.includes(artifactName) |
| 45 | + if (bytecodeFromRpc === bytecodeFromArtifact) { |
| 46 | + console.log(chalk.green(`Compiled bytecode for ${chalk.yellow(address)}(${artifactName}) MATCHES deployed bytecode!`)) |
| 47 | + } else if (isBytecodeSimilar(bytecodeFromArtifact, bytecodeFromRpc, ignoreMetadata)) { |
| 48 | + console.log(chalk.hex('#FFA500')(`Compiled bytecode for ${chalk.yellow(address)}(${artifactName}) is SIMILAR to deployed bytecode!`)) |
| 49 | + if (deployTx) { |
| 50 | + await assertByteCodeByDeployTx(address, deployTx, artifact, ignoreMetadata) |
| 51 | + } else { |
| 52 | + throw new AssertionError( |
| 53 | + `No deployTx found for ${chalk.yellow(address)}(${artifactName}).\n` + |
| 54 | + `Double check is impossible, but required due to differences in the deployed bytecode` |
| 55 | + ) |
| 56 | + } |
| 57 | + } else { |
| 58 | + throw new AssertionError(`Compiled bytecode for ${chalk.yellow(address)}(${artifactName}) DOESN'T MATCH deployed bytecode!`) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +async function assertByteCodeByDeployTx(address, deployTx, artifact, ignoreMetadata) { |
| 63 | + const tx = await web3.eth.getTransaction(deployTx) |
| 64 | + const txData = tx.input.toLowerCase() |
| 65 | + const byteCode = ignoreMetadata ? stripMetadata(artifact.bytecode) : artifact.bytecode |
| 66 | + if (!txData.startsWith(byteCode)) { |
| 67 | + throw new AssertionError( |
| 68 | + `Bytecode from deploy TX DOESN'T MATCH compiled bytecode for ${chalk.yellow(address)}(${artifact.contractName})` |
| 69 | + ) |
| 70 | + } |
| 71 | + console.log( |
| 72 | + chalk.green( |
| 73 | + `Bytecode from deploy TX ${ignoreMetadata ? 'SIMILAR to' : 'MATCHES'} compiled bytecode for ${chalk.yellow(address)}(${ |
| 74 | + artifact.contractName |
| 75 | + })` |
| 76 | + ) |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +async function assertDeployedByteCodeMain() { |
| 81 | + const deployInfo = await readJSON(`deployed-mainnet.json`) |
| 82 | + |
| 83 | + // handle APPs |
| 84 | + const resultsApps = await Promise.allSettled( |
| 85 | + Object.entries(deployInfo).map(async ([key, value]) => { |
| 86 | + if (key.startsWith('app:') && !key.startsWith('app:aragon')) { |
| 87 | + const name = APPS_TO_NAMES.get(key.split(':')[1]) |
| 88 | + if (!name) { |
| 89 | + throw `Unknown APP ${key}` |
| 90 | + } |
| 91 | + const address = value.baseAddress |
| 92 | + if (!address) { |
| 93 | + throw `APP ${key} has no baseAddress` |
| 94 | + } |
| 95 | + await assertByteCode(address, name) |
| 96 | + } |
| 97 | + }) |
| 98 | + ) |
| 99 | + // handle standalone contracts |
| 100 | + const resultsContracts = await Promise.allSettled( |
| 101 | + Object.entries(deployInfo).map(async ([key, value]) => { |
| 102 | + if (!key.startsWith('app:') && key.endsWith('Address')) { |
| 103 | + const name = CONTRACTS_TO_NAMES.get(key.replace('Address', '')) |
| 104 | + if (!name) { |
| 105 | + return |
| 106 | + } |
| 107 | + const address = value |
| 108 | + const deployTx = deployInfo[key.replace('Address', 'DeployTx')] |
| 109 | + await assertByteCode(address, name, deployTx) |
| 110 | + } |
| 111 | + }) |
| 112 | + ) |
| 113 | + let errors = [] |
| 114 | + resultsApps.concat(resultsContracts).forEach((result) => { |
| 115 | + if (result.status == 'rejected') { |
| 116 | + errors.push(result.reason) |
| 117 | + } |
| 118 | + }) |
| 119 | + if (errors.length > 0) { |
| 120 | + throw new Error(`Following errors occurred during execution:\n${chalk.red(errors.join('\n'))}`) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +var myfunc = assertDeployedByteCodeMain() |
| 125 | +myfunc.catch((err) => { |
| 126 | + console.log(err) |
| 127 | + process.exit([1]) |
| 128 | +}) |
0 commit comments