Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(contract-verifier): Partial matching & automatic verification #3527

Merged
merged 20 commits into from
Jan 30, 2025

Conversation

popzxc
Copy link
Member

@popzxc popzxc commented Jan 24, 2025

Fixes #3190
Fixes #3309

I still have to test the migration logic (want to make sure that Era mainnet data can be migrated correctly & quickly), but the PR is reviewable otherwise.

This one is much bigger than I indended it to be, sorry 🥲

  • Changes zksolc used in contract verifier tests to 1.5.10 (old one didn't have ipfs metadata hash support).
  • Fixes problems that occured with newer compilers (e.g. Yul bytecode extraction).
  • Makes it easier to run contract verifier tests locally (pins compiler versions, so that if you have a lot of compilers locally, tests don't crash).
  • Introduces partial matching for contracts
  • Reworks the schema so that contracts with matching bytecode can be "automatically" verified.
  • Adds a migration to the new table.

@popzxc popzxc force-pushed the popzxc-contract-verifier branch 4 times, most recently from 2a0083d to 44aac98 Compare January 24, 2025 12:39
@popzxc popzxc force-pushed the popzxc-contract-verifier branch from 44aac98 to 514900f Compare January 24, 2025 13:22
@popzxc popzxc marked this pull request as ready for review January 24, 2025 14:01
@popzxc popzxc requested a review from a team as a code owner January 24, 2025 14:01
@popzxc popzxc requested a review from slowli January 28, 2025 11:03
Copy link
Contributor

@perekopskiy perekopskiy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach with indexing hashes is nit!

core/lib/dal/src/contract_verification_dal.rs Outdated Show resolved Hide resolved
core/lib/dal/src/contract_verification_dal.rs Outdated Show resolved Hide resolved
@popzxc popzxc requested a review from perekopskiy January 28, 2025 13:24
perekopskiy
perekopskiy previously approved these changes Jan 28, 2025
core/lib/contract_verifier/src/compilers/solc.rs Outdated Show resolved Hide resolved
core/bin/contract-verifier/src/main.rs Show resolved Hide resolved
core/bin/contract-verifier/src/main.rs Outdated Show resolved Hide resolved
core/lib/dal/src/contract_verification_dal.rs Outdated Show resolved Hide resolved
core/lib/dal/src/contract_verification_dal.rs Outdated Show resolved Hide resolved
core/lib/dal/src/contract_verification_dal.rs Show resolved Hide resolved
core/lib/dal/src/contract_verification_dal.rs Show resolved Hide resolved
core/lib/dal/src/contract_verification_dal.rs Show resolved Hide resolved
core/lib/dal/src/contract_verification_dal.rs Outdated Show resolved Hide resolved
@popzxc popzxc requested a review from slowli January 30, 2025 07:40
@popzxc popzxc enabled auto-merge January 30, 2025 12:31
@popzxc popzxc added this pull request to the merge queue Jan 30, 2025
Merged via the queue into main with commit bf9fe85 Jan 30, 2025
42 checks passed
@popzxc popzxc deleted the popzxc-contract-verifier branch January 30, 2025 14:33
popzxc added a commit to matter-labs/block-explorer that referenced this pull request Feb 7, 2025
Adds visuals for automatic verification and partial matching introduced
in matter-labs/zksync-era#3527

Demo:

<details>
<summary>Unverified contract (no changes from previous
behavior)</summary>


![image](https://github.com/user-attachments/assets/d0872c9d-c9ec-4e00-afef-25e1e9e6a1d4)
</details>
<details>
<summary>Fully verified contract (no changes from previous
behavior)</summary>


![image](https://github.com/user-attachments/assets/68f90165-46af-44ac-8ce8-b595fb2a6450)
</details>
<details>
  <summary>Automatically verified contract</summary>


![image](https://github.com/user-attachments/assets/3536d7be-5da7-4364-b5e7-a32ba184afb1)
</details>
<details>
<summary>Partially verified contract (metadata hash mismatch)</summary>


![image](https://github.com/user-attachments/assets/7cdb4daa-9b59-433b-9a15-f5d833c15e3f)
</details>
<details>
<summary>Partially verified contract + automatic verification (e.g.
there is a verified contract with the same bytecode but different
metadata hash)</summary>


![image](https://github.com/user-attachments/assets/e1883ede-9fbd-474a-8bc9-401b2e050f06)
</details>

Unfortunately, since this feature depends on contract verifier version
that isn't deployed anywhere yet, it's not easy to test it.
If you want to ~~suffer~~ reproduce it locally, you can do it as
follows:

<details>
  <summary>How to test locally</summary>

1. Use `zksync-era` repo on the most recent `main` & install `zkstack`
CLI.
2. `zkstack e init --dev`
3. `zkstack contract-verifier init --zksolc-version=v1.5.7
--zkvyper-version=v1.5.4 --solc-version=0.8.26 --vyper-version=v0.3.10
--era-vm-solc-version=0.8.26-1.0.1 --only`
4. Run `zkstack explorer init`
5. Run `zkstack server --components
api,tree,eth,state_keeper,housekeeper,commitment_generator,da_dispatcher,vm_runner_protective_reads,contract_verification_api`
in one terminal
6. Run `zkstack contract-verifier run` in another terminal
7. Run `zkstack explorer run-backend` in third terminal
8. Configure block explorer FE to interact with dockerized explorer BE
and contract verifier: change `apiUrl` port to `3002` and add
`"verificationApiUrl": "http://127.0.0.1:3070"`
9. Run block explorer FE (only)
10. Use `npx zksync-cli bridge deposit`, choose dockerized setup, PK for
rich wallet is
`0xbe79721778b48bcc679b78edac0ce48306a8578186ffcb9f2ee455ae6efeace1`
(address `0xe706e60ab5Dc512C36A4646D719b889F398cbBcB`)
11. Do `export COMPILERS=/path/to/zksync-era/etc`
12. Go to `zksync-era/core/tests/ts-integration/contracts/counter`
13. Do `$COMPILERS/zksolc-bin/v1.5.7/zksolc --solc
$COMPILERS/solc-bin/zkVM-0.8.28-1.0.1/solc counter.sol --bin`
14. Deploy the bytecode with the script shown below
15. Open tx in the explorer, find the log with three topics, one of
which is deployer address; the third one will contain the address of the
deployed contract.
16. Verify the contract through UI

Now you can do the following:
- To check automatic verification, just deploy the same contract once
again. Verification will not be needed
- To check partial matching, change the last byte (you will corrupt the
metadata hash, and it will trigger partial verification)

Script
```
import { ethers, hexlify, solidityPacked } from "ethers";
import { Wallet, Provider } from "zksync-ethers";
import { CONTRACT_DEPLOYER_ADDRESS, hashBytecode } from "zksync-ethers/build/utils";

const PK = "0xbe79721778b48bcc679b78edac0ce48306a8578186ffcb9f2ee455ae6efeace1";
const BYTECODE = "0x<insert bytecode here>";

async function main() {
    // Create deploy transaction using zksync-ethers using bytecode
    // and send it to the network
    const provider = new Provider("http://localhost:3050");
    const wallet = new Wallet(PK, provider);

    const bytecodeHash = hashBytecode(BYTECODE);
    // const contract = new ethers.Contract(CONTRACT_DEPLOYER_ADDRESS, abi, wallet);
    const selector = "create(bytes32,bytes32,bytes)";
    const selectorHash = ethers.keccak256(ethers.toUtf8Bytes(selector)).substring(0, 10);
    console.log(`Selector hash: ${selectorHash}`);
    const calldata = selectorHash + solidityPacked(["bytes32", "bytes32", "bytes"], [new Uint8Array(32), bytecodeHash, new Uint8Array(64)]).substring(2);
    console.log(`Calldata: ${calldata}`);

    const bytecode_u8_array = ethers.getBytes(BYTECODE);
    const tx = await wallet.sendTransaction({
        to: CONTRACT_DEPLOYER_ADDRESS,
        data: calldata,
        value: 0,
        customData: {
            factoryDeps: [bytecode_u8_array],
        }
    });
    console.log(`Transaction hash: ${tx.hash}`);
    await tx.wait();
}

main()
    .then(() => process.exit(0))
    .catch((err) => {
        console.error('Error:', err.message || err);
        process.exit(1);
    });
```

</details>

Disclaimer: I don't know front-end, I hate front-end, I question myself
why I thought it's a good idea to work on this.

![i_have_no_idea_what_im_doing_meme_640_07](https://github.com/user-attachments/assets/27f7053d-42b3-4ece-a69b-631c3e28e84c)

P.S. I wanted to add Ukrainian translation as well, but looks like
contract verification has no translations, so I thought that it deserves
a dedicated effort to add all the missing translations when/if it would
be required.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(contract-verifier): Auto-verify the same bytecode Support partial matches in contract verifier
4 participants