From 1c2cb136eb2ab1f4fec66468714808115ecffb85 Mon Sep 17 00:00:00 2001 From: Facundo Farall <37149322+ffarall@users.noreply.github.com> Date: Mon, 29 Jan 2024 06:02:02 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=A9=B9=20Solve=20issues=20for=20su?= =?UTF-8?q?ccessfully=20running=20a=20local=20hyperchain=20(#893)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What ❔ This PR solves the following issues encountered when trying to run a hyperchain locally, according to [this quickstart tutorial](https://zkstack.io/quickstart#deploying-and-running-locally): 1. TypeScript not compiling in Remote Server. This was due to the `yarn.lock` file not being locked when installing the dependencies. The `zk` and `zk stack init` scripts both run `yarn` at some point, but without the `--frozen-lockfile`, meaning each machine would modify the `yarn.lock` file and the dependencies would be slightly different. In the Mac machine, this didn't cause any issues, but in the Ubuntu it made the TypeScript compiler fail. The solution was to run `yarn install --frozen-lockfile` in the `zk` and `zk` stack init scripts, which is added in [this commit](https://github.com/ffarall/zksync-era/commit/a936768e81fc1c87784f4f2dff14ecf5a5405e4c). Below, the compile error: ``` scripts/utils.ts:201:93 - error TS2339: Property ‘zksolc’ does not exist on type ‘HardhatConfig’. 201 const url = getZksolcUrl(“https://github.com/matter-labs/zksolc-prerelease”, hre. config.zksolc.version); ``` 2. After setting up a new hyperchain for the first time with `zk stack init` (let’s call it `my-hyperchain`) if you execute `zk clean —-all` it deletes `my-hyperchain.env` (among others). However, as soon as you run another `zk` command, the file is regenerated with the old content. For example, running `zk down`, regenerates the `my-hyperchain.env` that was just deleted. This is not really an issue, but it is a bit confusing/annoying. It is solved by also having `zk clean` delete the `.current` file, as done in [this commit](https://github.com/ffarall/zksync-era/commit/a912b2bc9580d428e63ead6d112a0d2f4b834353). 3. When executing the server, which is Rust code running in the local machine, the script tries to use `sqlx` to access the PostgreSQL database (which was running in a docker container named `postgres`), using the `URL DATABASE_URL=postgres://postgres:notsecurepassword@postgres:5432/zksync_local`. That is the environment variable that is autogenerated in `hyperchain_wizards.ts`, in this line: ```typescript wrapEnvModify('DATABASE_URL', 'postgres://postgres:notsecurepassword@postgres:5432/zksync_local'); ``` The problem is that the local machine cannot resolve the path `postgres:5432` by name, as it does not have the IP of the `postgres` container. That is why it could not find the database. Luckily, the container is forwarding the ports, so changing `hyperchain_wizards.ts` to generate the env variable as `DATABASE_URL=postgres://postgres:notsecurepassword@postgres:5432/zksync_local` solves the problem. There is a similar issue when the server tries to find `geth:8545`, the docker container running the Ethereum node. Both issues are solved in [this commit](https://github.com/ffarall/zksync-era/commit/c6150cee9356a8dea90f48e6f7b3dda676f68f4a). Interestingly enough, manually modifying the `DATABASE_URL` env variable after executing `zk stack init` would not work, as running `zk server` again would still use the autogenerated value. This is because some env variables are duplicated in the `my-hyperchain.env` and `.init.env`, and this is the case of `DATABASE_URL`. When loading the environment, `my-hyperchain.env` is loaded first, and then `.init.env` overwrites such variables. Manually modifying the variables on `.init.env` however, works. 4. Running `zk stack init` after doing `zk down` for stopping the docker containers doesn't boot the same containers again, but other ones. Running `zk up --docker-file` has a bug that interprets `--docker-file` as a boolean flag. This is fixed in [this commit](https://github.com/ffarall/zksync-era/commit/13ddeee4f846538e67b9c085d323994d9b6e8aed), which makes it possible to restart the containers that were just deleted with `zk down`, by running `zk up --docker-file ./docker-compose-zkstack-common.yml`. It is not a definitive fix for the issue, but improves the situation. ## Why ❔ ## Checklist - [x] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. _(Doesn't apply, it has been tested to work manually, which it didn't before)_ - [ ] Documentation comments have been added / updated. _(Doesn't apply, no comments needed updating)_ - [x] Code has been formatted via `zk fmt` and `zk lint`. - [x] Spellcheck has been run via `zk spellcheck`. --------- Co-authored-by: Fedor Sakharov --- bin/zk | 2 +- infrastructure/zk/src/clean.ts | 1 + infrastructure/zk/src/run/run.ts | 4 ++-- infrastructure/zk/src/up.ts | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/bin/zk b/bin/zk index ca899fd5f465..f22ce90440fc 100755 --- a/bin/zk +++ b/bin/zk @@ -41,7 +41,7 @@ check_subdirectory check_yarn_version if [ -z "$1" ]; then cd $ZKSYNC_HOME - yarn && yarn zk build + yarn install --frozen-lockfile && yarn zk build else # can't start this with yarn since it has quirks with `--` as an argument node -- $ZKSYNC_HOME/infrastructure/zk/build/index.js "$@" diff --git a/infrastructure/zk/src/clean.ts b/infrastructure/zk/src/clean.ts index 117635111840..96c68d5908be 100644 --- a/infrastructure/zk/src/clean.ts +++ b/infrastructure/zk/src/clean.ts @@ -27,6 +27,7 @@ export const command = new Command('clean') const env = process.env.ZKSYNC_ENV; clean(`etc/env/${env}.env`); clean('etc/env/.init.env'); + clean('etc/env/.current'); } if (cmd.all || cmd.artifacts) { diff --git a/infrastructure/zk/src/run/run.ts b/infrastructure/zk/src/run/run.ts index da5b66d8d687..a8044e75b1ec 100644 --- a/infrastructure/zk/src/run/run.ts +++ b/infrastructure/zk/src/run/run.ts @@ -56,7 +56,7 @@ export async function tokenInfo(address: string) { // installs all dependencies export async function yarn() { - await utils.spawn('yarn'); + await utils.spawn('yarn install --frozen-lockfile'); } export async function deployTestkit(genesisRoot: string) { @@ -121,7 +121,7 @@ export async function snapshots_creator() { export const command = new Command('run').description('run miscellaneous applications').addCommand(dataRestore.command); command.command('test-accounts').description('print ethereum test accounts').action(testAccounts); -command.command('yarn').description('install all JS dependencies').action(yarn); +command.command('yarn install --frozen-lockfile').description('install all JS dependencies').action(yarn); command.command('cat-logs [exit_code]').description('print server and prover logs').action(catLogs); command diff --git a/infrastructure/zk/src/up.ts b/infrastructure/zk/src/up.ts index de0ef41cf8c7..5cfed342669c 100644 --- a/infrastructure/zk/src/up.ts +++ b/infrastructure/zk/src/up.ts @@ -44,7 +44,7 @@ export async function up(composeFile?: string) { export const command = new Command('up') .description('start development containers') - .option('--docker-file', 'path to a custom docker file') + .option('--docker-file ', 'path to a custom docker file') .action(async (cmd) => { await up(cmd.dockerFile); });