Skip to content

Commit

Permalink
Add verification step (l2beat#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
sz-piotr committed Aug 8, 2022
1 parent 7445abc commit 358204f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/update-monitor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
},
"dependencies": {
"@l2beat/common": "*",
"@types/lodash": "^4.14.182",
"@types/mkdirp": "^1.0.2",
"chalk": "^4.1.2",
"dotenv": "^16.0.1",
"ethers": "^5.6.8",
"lodash": "^4.17.21",
"mkdirp": "^1.0.4"
},
"devDependencies": {
Expand Down
16 changes: 15 additions & 1 deletion packages/update-monitor/src/projects/zkSync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getSimpleEip1967Proxy } from '../../common/getSimpleEip1967Proxy'
import { getGnosisSafe } from '../../common/gnosisSafe'
import { DiscoveryEngine } from '../../discovery/DiscoveryEngine'
import { ProjectParameters } from '../../types'
import { verify } from '../../verify/verify'
import { addresses } from './constants'
import { getGovernance } from './contracts/governance'
import { getTokenGovernance } from './contracts/tokenGovernance'
Expand All @@ -15,7 +16,7 @@ export const ZK_SYNC_NAME = 'zkSync'
export async function getZkSyncParameters(
provider: providers.JsonRpcProvider,
): Promise<ProjectParameters> {
return {
const parameters: ProjectParameters = {
name: ZK_SYNC_NAME,
contracts: await Promise.all([
getUpgradeGatekeeper(provider),
Expand All @@ -26,6 +27,19 @@ export async function getZkSyncParameters(
getGnosisSafe(provider, addresses.multisig, 'Multisig'),
]),
}
verify(parameters, [
['UpgradeGatekeeper.managedContracts[0]', 'Governance'],
['UpgradeGatekeeper.managedContracts[1]', 'Verifier'],
['UpgradeGatekeeper.managedContracts[2]', 'zkSync'],
['UpgradeGatekeeper.mainContract', 'zkSync'],
['UpgradeGatekeeper.master', 'Multisig'],
['Governance.admin', 'UpgradeGatekeeper'],
['Verifier.admin', 'UpgradeGatekeeper'],
['zkSync.admin', 'UpgradeGatekeeper'],
['Governance.networkGovernor', 'Multisig'],
['Governance.tokenGovernance', 'TokenGovernance'],
])
return parameters
}

export async function discoverZkSync(discoveryEngine: DiscoveryEngine) {
Expand Down
70 changes: 70 additions & 0 deletions packages/update-monitor/src/verify/verify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import chalk from 'chalk'
import _ from 'lodash'

import { ProjectParameters } from '../types'

export function verify(
parameters: ProjectParameters,
rules: [string, string][],
) {
for (const [left, right] of rules) {
const leftValue = getValue(parameters, left)
const rightValue = getValue(parameters, right)
if (leftValue !== rightValue) {
console.warn(
chalk.bgRed(' ERROR '),
chalk.red(`Verification mismatch in ${parameters.name}!`),
)
const [leftSelector, rightSelector] = alignLeft(left, right)
const [leftResult, rightResult] = alignRight(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`${leftValue}`,
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`${rightValue}`,
)
console.warn(` ${leftSelector} ${chalk.red(leftResult)}`)
console.warn(` ${rightSelector} ${chalk.red(rightResult)}`)
}
}
}

function alignLeft(a: string, b: string): [string, string] {
return [
a.length < b.length ? a.padEnd(b.length, ' ') : a,
b.length < a.length ? b.padEnd(a.length, ' ') : b,
]
}

function alignRight(a: string, b: string): [string, string] {
return [
a.length < b.length ? a.padStart(b.length, ' ') : a,
b.length < a.length ? b.padStart(a.length, ' ') : b,
]
}

function getValue(parameters: ProjectParameters, selector: string) {
const [contractName, ...path] = selector
.split(/\.|\[|\]/)
.filter((x) => x !== '')
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (contractName === undefined) {
throw new Error(`Invalid selector ${JSON.stringify(selector)}`)
}
const contract = parameters.contracts.find((x) => x.name === contractName)
if (contract === undefined) {
throw new Error(`Cannot find ${contractName} in ${parameters.name}`)
}
if (path.length === 0) {
return contract.address
}
if (path[0] !== 'upgradeability') {
path.unshift('values')
}
const value: unknown = _.get(contract, path)
if (value === undefined) {
throw new Error(
`Cannot find ${JSON.stringify(selector)} in ${parameters.name}`,
)
}
return value
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,11 @@
resolved "https://registry.yarnpkg.com/@types/linkify-it/-/linkify-it-3.0.2.tgz#fd2cd2edbaa7eaac7e7f3c1748b52a19143846c9"
integrity sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==

"@types/lodash@^4.14.182":
version "4.14.182"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.182.tgz#05301a4d5e62963227eaafe0ce04dd77c54ea5c2"
integrity sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==

"@types/markdown-it@^12.2.3":
version "12.2.3"
resolved "https://registry.yarnpkg.com/@types/markdown-it/-/markdown-it-12.2.3.tgz#0d6f6e5e413f8daaa26522904597be3d6cd93b51"
Expand Down

0 comments on commit 358204f

Please sign in to comment.