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

Feature/update deployment logic using factory #52

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0177f5e
Add ZKSyncCreate2Factory, EmailAccountRecovery test casees split by f…
wshino Aug 28, 2024
68c5399
Failed: update integration testing with foundry-zksync.
wshino Aug 28, 2024
e50c303
Update integration testing command with foundry-zksync.
wshino Aug 28, 2024
6ea336e
Failed: update integration testing with foundry.
wshino Aug 28, 2024
39b43a0
Add DeployEmailAuthWithCreate2.s.sol.
wshino Aug 30, 2024
7427a96
Merge commit '39b43a055ace204bfd3450496a87ca5ba363e61e' into feature/…
wshino Aug 30, 2024
7b0235f
Disable DeployEmailAuthWIthCreate2.s.sol.
wshino Aug 30, 2024
2769102
Merge branch 'feat/audit-fix-2024-08' into feature/update-deployment-…
wshino Sep 9, 2024
2fb6e3e
Fix integration test for foundry
wshino Sep 9, 2024
68119bc
Fail: integration testing for foundry zksync.
wshino Sep 9, 2024
9f6b16d
Fix integration testing for foundry zksync
wshino Sep 9, 2024
e7255a3
Tweak README.md
wshino Sep 9, 2024
fd6b30f
Add EmailAccountRecoveryZkSync
wshino Sep 10, 2024
cc8e115
Uncomment some contracts which are used for foundry zksync
wshino Sep 10, 2024
e2640ec
make deployEmailAuthProxy as internal function
wshino Sep 10, 2024
564bb55
Update yarn build, yarn test
wshino Sep 10, 2024
a36cf98
Remove specific foundry version
wshino Sep 10, 2024
6857cfa
Update foundry version
wshino Sep 10, 2024
5a0c283
chore: git rm --cached
wshino Sep 10, 2024
e0e1b53
chore: Update after git rm --cached
wshino Sep 10, 2024
39905c2
chore: git rm --cached
wshino Sep 10, 2024
3a61642
chore: Update after git rm --cached
wshino Sep 10, 2024
618ed4f
Fix build-test-fmt on github action
wshino Sep 10, 2024
14f0626
Rename DeployEmailAuthWIthCreate2
wshino Sep 10, 2024
0069604
Update deployment files in docs
wshino Sep 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@ book

# For zksync
zkout
.cache
114 changes: 63 additions & 51 deletions packages/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,62 +251,52 @@ Next, you should uncomment the following lines in `foundry.toml`.
# via-ir = true
```

And then you should uncomment the following lines in `src/EmailAccountRecovery.sol`.
Uncomment the following commented-out files.
These are commented out in the files for ZkSync to avoid problems when testing in foundry.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to avoid commenting out those zksync files by adding an option to the command of yarn build that excludes zksync files?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@SoraSuegami Maybe we can use --skip flag, let me try it.


```
// import {SystemContractsCaller} from "@matterlabs/zksync-contracts/l2/system-contracts/libraries/SystemContractsCaller.sol";
// import {DEPLOYER_SYSTEM_CONTRACT} from "@matterlabs/zksync-contracts/l2/system-contracts/Constants.sol";
```
The following are entire files that are commented out

- script/DeployRecoveryControllerZkSync.s.sol
- src/EmailAccountRecoveryZkSync.sol
- test/IntegrationZkSync.t.sol
- test/EmailAccountRecoveryZkSync/EmailAccountRecoveryZkSync_*.t.sol
- test/helpers/RecoveryControllerZkSync.sol
- test/ComputeCreate2Address.t.sol

Partial comment-out files can be found the following. Search `FOR_ZKSYNC:START` and `FOR_ZKSYNC:END`.

And lines 229 - 263 in the `handleAcceptance` function too.
- src/utils/ZKSyncCreate2Factory.sol
- test/helpers/DeploymentHelper.sol

At the first forge build, you got the following warning like the following.
At the first forge build, you need to detect the missing libraries.

```
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Warning: Your code or one of its dependencies uses the 'extcodesize' instruction, which is │
│ usually needed in the following cases: │
│ 1. To detect whether an address belongs to a smart contract. │
│ 2. To detect whether the deploy code execution has finished. │
│ zkSync Era comes with native account abstraction support (so accounts are smart contracts, │
│ including private-key controlled EOAs), and you should avoid differentiating between contracts │
│ and non-contract addresses. │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
--> ../../node_modules/forge-std/src/StdCheats.sol

Failed to compile with zksolc: Missing libraries detected [ZkMissingLibrary { contract_name: "SubjectUtils", contract_path: "src/libraries/SubjectUtils.sol", missing_libraries: ["src/libraries/DecimalUtils.sol:DecimalUtils"] }, ZkMissingLibrary { contract_name: "DecimalUtils", contract_path: "src/libraries/DecimalUtils.sol", missing_libraries: [] }]
```
forge build --zksync --zk-detect-missing-libraries
```

Please run the following command in order to deploy the missing libraries:
As you saw before, you need to deploy missing libraries.
You can deploy them by the following command for example.

```
forge create --deploy-missing-libraries --private-key <PRIVATE_KEY> --rpc-url <RPC_URL> --chain <CHAIN_ID> --zksync
forge create --deploy-missing-libraries --private-key {YOUR_PRIVATE_KEY} --rpc-url https://sepolia.era.zksync.dev --chain 300 --zksync
$ forge build --zksync --zk-detect-missing-libraries
Missing libraries detected: src/libraries/SubjectUtils.sol:SubjectUtils, src/libraries/DecimalUtils.sol:DecimalUtils
```

The above command output the following(for example):
Run the following command in order to deploy each missing library:

```
[⠊] Compiling...
No files changed, compilation skipped
Deployer: 0xfB1CcCBDa2C41a77cDAC448641006Fc7fcf1f3b9
Deployed to: 0x91cc0f0A227b8dD56794f9391E8Af48B40420A0b
Transaction hash: 0x4f94ab71443d01988105540c3abb09ed66f8af5d0bb6a88691e2dafa88b3583d
[⠢] Compiling...
[⠃] Compiling 68 files with 0.8.26
[⠆] Solc 0.8.26 finished in 12.20s
Compiler run successful!
Deployer: 0xfB1CcCBDa2C41a77cDAC448641006Fc7fcf1f3b9
Deployed to: 0x981E3Df952358A57753C7B85dE7949Da4aBCf54A
Transaction hash: 0xfdca7b9eb3ae933ca123111489572427ee95eb6be74978b24c73fe74cb4988d7
forge create src/libraries/DecimalUtils.sol:DecimalUtils --private-key {YOUR_PRIVATE_KEY} --rpc-url https://sepolia.era.zksync.dev --chain 300 --zksync
forge create src/libraries/SubjectUtils.sol:SubjectUtils --private-key {YOUR_PRIVATE_KEY} --rpc-url https://sepolia.era.zksync.dev --chain 300 --zksync --libraries src/libraries/DecimalUtils.sol:DecimalUtils:{DECIMAL_UTILS_DEPLOYED_ADDRESS}
```

After that, you can see the following line in foundry.toml.
Also, this line is needed only for foundry-zksync, if you use foundry, please remove this line. Otherwise, the test will fail.

```
libraries = ["{PROJECT_DIR}/packages/contracts/src/libraries/DecimalUtils.sol:DecimalUtils:{DEPLOYED_ADDRESS}", "{PROJECT_DIR}/packages/contracts/src/libraries/SubjectUtils.sol:SubjectUtils:{DEPLOYED_ADDRESS}"]

libraries = [
"{PROJECT_DIR}/packages/contracts/src/libraries/DecimalUtils.sol:DecimalUtils:{DEPLOYED_ADDRESS}",
"{PROJECT_DIR}/packages/contracts/src/libraries/SubjectUtils.sol:SubjectUtils:{DEPLOYED_ADDRESS}"]
```

Incidentally, the above line already exists in `foundy.toml` with it commented out, if you uncomment it by replacing `{PROJECT_DIR}` with the appropriate path, it will also work.
Expand Down Expand Up @@ -336,7 +326,7 @@ https://github.com/matter-labs/foundry-zksync/issues/382

Failing test cases are here.

EmailAuthWithUserOverrideableDkim.t.sol
DKIMRegistryUpgrade.t.sol

- testAuthEmail()

Expand All @@ -348,25 +338,47 @@ EmailAuth.t.sol
- testExpectRevertAuthEmailInvalidSubject()
- testExpectRevertAuthEmailInvalidTimestamp()

DeployCommons.t.sol
EmailAuthWithUserOverrideableDkim.t.sol

- test_run()
- testAuthEmail()

DeployRecoveryController.t.sol
# For integration testing

- test_run()
To pass the instegration testing, you should use era-test-node.
See the following URL and install it.
https://github.com/matter-labs/era-test-node

DeploySimpleWallet.t.sol
Run the era-test-node

- test_run()
- test_run_no_dkim()
- test_run_no_email_auth()
- test_run_no_simple_wallet()
- test_run_no_verifier()
```
era_test_node fork https://sepolia.era.zksync.dev
```

# For integration testing
You remove .zksolc-libraries-cache directory, and run the following command.

forge test --match-test 'testIntegration_Account_Recovery' --zksync --chain 300 -vvv --ffi
```
forge build --zksync --zk-detect-missing-libraries
```

As you saw before, you need to deploy missing libraries.
You can deploy them by the following command for example.

```
Missing libraries detected: src/libraries/SubjectUtils.sol:SubjectUtils, src/libraries/DecimalUtils.sol:DecimalUtils

Run the following command in order to deploy each missing library:

forge create src/libraries/DecimalUtils.sol:DecimalUtils --private-key {YOUR_PRIVATE_KEY} --rpc-url http://127.0.0.1:8011 --chain 260 --zksync
forge create src/libraries/SubjectUtils.sol:SubjectUtils --private-key {YOUR_PRIVATE_KEY} --rpc-url http://127.0.0.1:8011 --chain 260 --zksync --libraries src/libraries/DecimalUtils.sol:DecimalUtils:{DECIMAL_UTILS_DEPLOYED_ADDRESS}
```

Set the libraries in foundry.toml using the above deployed address.

And then, run the integration testing.

```
forge test --match-test 'testIntegration_Account_Recovery_ZkSync' --system-mode=true --zksync --gas-limit 1000000000 --chain 300 -vvv --ffi
```

# For zkSync deployment (For test net)

Expand All @@ -375,6 +387,6 @@ Second just run the following commands with `--zksync`

```
source .env
forge script script/DeployCommons.s.sol:Deploy --zksync --rpc-url $SEPOLIA_RPC_URL --broadcast -vvvv
forge script script/DeployCommons.s.sol:Deploy --zksync --rpc-url $RPC_URL --broadcast --slow --via-ir --system-mode true -vvvv
```

5 changes: 4 additions & 1 deletion packages/contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ build_info = true
extra_output = ["storageLayout"]

# For missing libraries, please comment out this if you use foundry-zksync
#libraries = ["{PROJECT_DIR}/packages/contracts/src/libraries/DecimalUtils.sol:DecimalUtils:0x91cc0f0a227b8dd56794f9391e8af48b40420a0b", "{PROJECT_DIR}/packages/contracts/src/libraries/SubjectUtils.sol:SubjectUtils:0x981e3df952358a57753c7b85de7949da4abcf54a"]
#libraries = [
# "{PROJECT_DIR}/packages/contracts/src/libraries/DecimalUtils.sol:DecimalUtils:0x91cc0f0a227b8dd56794f9391e8af48b40420a0b",
# "{PROJECT_DIR}/packages/contracts/src/libraries/SubjectUtils.sol:SubjectUtils:0x981e3df952358a57753c7b85de7949da4abcf54a"
#]

[rpc_endpoints]
localhost = "${LOCALHOST_RPC_URL}"
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"build": "forge build",
"zkbuild": "forge build --zksync",
"test": "forge test --no-match-test \"testIntegration\"",
"zktest": "forge test --no-match-test \"testIntegration\" --zksync --chain 300",
"zktest": "forge test --no-match-test \"testIntegration\" --system-mode=true --zksync --gas-limit 1000000000 --chain 300",
"lint": "solhint 'src/**/*.sol'"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/script/ChangeOwners.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Own

contract ChangeOwners is Script {
function run() external {

uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
if (deployerPrivateKey == 0) {
console.log("PRIVATE_KEY env var not set");
Expand Down
56 changes: 56 additions & 0 deletions packages/contracts/script/DeployEmailAuthWithCreate2.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// // SPDX-License-Identifier: UNLICENSED
// pragma solidity ^0.8.13;

// import "forge-std/Script.sol";

// import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
// import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
// import "../src/EmailAuth.sol";
// import {ZKSyncCreate2Factory} from "../src/utils/ZKSyncCreate2Factory.sol";

// contract Deploy is Script {
// using ECDSA for *;

// function run() external {
// uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
// if (deployerPrivateKey == 0) {
// console.log("PRIVATE_KEY env var not set");
// return;
// }

// vm.startBroadcast(deployerPrivateKey);

// EmailAuth emailAuth = new EmailAuth();

// address recoveredAccount = address(0x1);
// bytes32 accountSalt = 0x0;

// ERC1967Proxy proxy = new ERC1967Proxy(
// address(emailAuth),
// abi.encodeCall(emailAuth.initialize, (recoveredAccount, accountSalt, address(this)))
// );
// console.log("normal deployed proxyAddress %s", address(proxy));

// ZKSyncCreate2Factory factory = new ZKSyncCreate2Factory();
// string memory artifact = vm.readFile("zkout/ERC1967Proxy.sol/ERC1967Proxy.json");
// bytes32 bytecodeHash = vm.parseJsonBytes32(artifact, ".hash");
// console.log("bytecodeHash");
// console.logBytes32(bytes32(bytecodeHash));

// bytes memory emailAuthInit = abi.encode(
// address(emailAuth), abi.encodeCall(EmailAuth.initialize, (recoveredAccount, accountSalt, address(this)))
// );

// address computedAddress = factory.computeAddress(accountSalt, bytecodeHash, emailAuthInit);
// console.log("computedAddress", computedAddress);

// (bool success, bytes memory returnData) = factory.deploy(accountSalt, bytecodeHash, emailAuthInit);

// address payable proxyAddress = abi.decode(returnData, (address));
// console.log("proxyAddress %s", proxyAddress);

// EmailAuth emailAuthProxy = EmailAuth(proxyAddress);
// console.log("emailAuthProxy.controller() %s", emailAuthProxy.controller());
// vm.stopBroadcast();
// }
// }
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is fully commented out, please remove this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason why I commented out is it needs for via-ir option when we compile contracts directory.
This is the script that checks deployed address and computeAddress are the same address. And it checks deployedAddress has been initialized correctly.
I think we should keep this script for future use.

Loading
Loading