Skip to content

Commit

Permalink
fix: 🩹 Solve issues for successfully running a local hyperchain (matt…
Browse files Browse the repository at this point in the history
…er-labs#893)

## 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](ffarall@a936768).
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](ffarall@a912b2b).
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](ffarall@c6150ce).
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](ffarall@13ddeee),
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 ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [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 <[email protected]>
  • Loading branch information
ffarall and montekki authored Jan 29, 2024
1 parent c95f3ee commit 1c2cb13
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bin/zk
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"
Expand Down
1 change: 1 addition & 0 deletions infrastructure/zk/src/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions infrastructure/zk/src/run/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/zk/src/up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dockerFile>', 'path to a custom docker file')
.action(async (cmd) => {
await up(cmd.dockerFile);
});

0 comments on commit 1c2cb13

Please sign in to comment.