Skip to content

Commit

Permalink
feat(k8s): Add a zk init job (#10)
Browse files Browse the repository at this point in the history
* Create init job

* Update job

* Rename zk-init with zk_init

* Add README

* Fix L1_NAME getter
  • Loading branch information
ManuelBilbao authored Aug 13, 2024
1 parent 91de135 commit cc95b83
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 0 deletions.
31 changes: 31 additions & 0 deletions infra/images/zk_init/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM matterlabs/zksync-build-base:latest

ARG ZKSYNC_TAG=core-v24.12.0

# Install dependencies
RUN apt-get update && apt-get install -y git wget jq

# Install kubectl
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
RUN mv ./kubectl /usr/local/bin/kubectl && chmod +x /usr/local/bin/kubectl

# Install node and yarn
ENV NODE_MAJOR=18
RUN mkdir -p /etc/apt/keyrings && \
wget -c -O - https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list && \
apt-get update && apt-get install nodejs -y && \
npm install -g yarn

# Download zksync
RUN git clone -b $ZKSYNC_TAG https://github.com/matter-labs/zksync-era /zksync

WORKDIR /zksync

RUN git submodule update --init

RUN cargo build --release -p zksync_server

COPY init.sh .

ENTRYPOINT ["bash", "init.sh"]
19 changes: 19 additions & 0 deletions infra/images/zk_init/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ZK init

This image clones the zksync-era repo and runs a `zk init` that launch a new chain. Once launched, it creates a ConfigMap with the env to the cluster. It's used among `infra/kubernetes/jobs/zk_init.yaml`.

To build and push the image, run:

```sh
docker build -t us-central1-docker.pkg.dev/zksync-413615/zksync/zk-init:latest --platform=linux/amd64 .
docker push us-central1-docker.pkg.dev/zksync-413615/zksync/zk-init
```

The `zk-init` job requires a ConfigMap with a `custom.toml` file, containing the chain config. Check `infra/kubernetes/configmaps/zk_init.yaml` for more details.

### Base Token

If you want to deploy a Base Token based network, you need to add the `BASE_TOKEN_SYMBOL` env var to the job, among with the `contracts.base_token_addr` config on the `custom.toml` file.

> [!WARNING]
> Currently, the WETH address is mocked with an example, you may need to change it. Take a look at `init.sh`.
65 changes: 65 additions & 0 deletions infra/images/zk_init/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash

CONFIGMAP_NAME=${CONFIGMAP_NAME:-test-server-envs}

# If ConfigMap already exists, quit
if [[ "$(kubectl get configmaps -oNAME | grep ^configmap/$CONFIGMAP_NAME$)" != "" ]]
then
echo "The ConfigMap $CONFIGMAP_NAME already exist. Delete it before running this job or change the CONFIGMAP_NAME env var"
exit 1
fi

export ZKSYNC_ENV=${ZKSYNC_ENV:-custom}
export ZKSYNC_HOME=/zksync
export PATH=${ZKSYNC_HOME}/bin:$PATH

cp /configs/${ZKSYNC_ENV}.toml ${ZKSYNC_HOME}/etc/env/configs/${ZKSYNC_ENV}.toml

export CUSTOM_BASE_TOKEN=$(grep '^BASE_TOKEN_ADDR[ ]*=' ${ZKSYNC_HOME}/etc/env/configs/${ZKSYNC_ENV}.toml | head -n1 | sed -E 's/BASE_TOKEN_ADDR[ ]*=[ "]*0x([a-fA-F0-9]*)"*/0x\1/')
if [[ "$CUSTOM_BASE_TOKEN" == "0x0000000000000000000000000000000000000001" ]]
then
export CUSTOM_BASE_TOKEN=""
else
if [[ "$BASE_TOKEN_SYMBOL" == "" ]]; then
echo "A base token address is provided but BASE_TOKEN_SYMBOL is not set"
exit 1
fi
BASE_TOKEN_DECIMALS=${BASE_TOKEN_DECIMALS:-18}
BASE_TOKEN_NAME=${BASE_TOKEN_NAME:-$BASE_TOKEN_SYMBOL}
L1_NAME=$(grep '^network[ ]*=' ${ZKSYNC_HOME}/etc/env/configs/${ZKSYNC_ENV}.toml | sed -E 's/^network[ ]*=[ "]*([^"]*)"*/\1/' || echo "localhost")

ls ${ZKSYNC_HOME}/etc/tokens/${L1_NAME}.json >/dev/null 2>&1 || echo "[]" > ${ZKSYNC_HOME}/etc/tokens/${L1_NAME}.json
jq '. |= . + [{
"name": "'$BASE_TOKEN_NAME'",
"symbol": "'$BASE_TOKEN_SYMBOL'",
"decimals": '$BASE_TOKEN_DECIMALS',
"address": "'$CUSTOM_BASE_TOKEN'"
},{
"name": "WETH",
"symbol": "WETH",
"decimals": 18,
"address": "0xeBcDEd21470fc7Bdf30D78e1263399cdfA743c9c"
}]' ${ZKSYNC_HOME}/etc/tokens/${L1_NAME}.json > tokens_new.json
mv tokens_new.json ${ZKSYNC_HOME}/etc/tokens/${L1_NAME}.json
fi

echo "CUSTOM BASE TOKEN: $CUSTOM_BASE_TOKEN"

zk
zk env ${ZKSYNC_ENV}

if [[ "$BASE_TOKEN_SYMBOL" == "" || "$CUSTOM_BASE_TOKEN" == "0x0000000000000000000000000000000000000001" ]]
then
zk init --skip-env-setup
else
zk init --base-token-name ${BASE_TOKEN_SYMBOL} --skip-env-setup --skip-submodules-checkout --skip-test-token-deployment
fi

zk env ${ZKSYNC_ENV}

sed -i -E 's/API_WEB3_JSON_RPC_ACCOUNT_PKS="([a-fA-F0-9x,]*)"/API_WEB3_JSON_RPC_ACCOUNT_PKS=\1/' ${ZKSYNC_HOME}/etc/env/target/$ZKSYNC_ENV.env
sed -i -E 's/ETH_SENDER_SENDER_AGGREGATED_PROOF_SIZES="(.*)"/ETH_SENDER_SENDER_AGGREGATED_PROOF_SIZES=\1/' ${ZKSYNC_HOME}/etc/env/target/$ZKSYNC_ENV.env

kubectl create configmap $CONFIGMAP_NAME --from-env-file=${ZKSYNC_HOME}/etc/env/target/$ZKSYNC_ENV.env

echo "Chain successfully initialized"
30 changes: 30 additions & 0 deletions infra/kubernetes/configmaps/zk_init.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: zk-init-configmap
namespace: default
data:
custom.toml: |
__imports__ = [ "base", "l1-inits/.init.env", "l2-inits/custom.init.env" ]
ETH_SENDER_SENDER_PUBDATA_SENDING_MODE = "Blobs"
[contracts]
BASE_TOKEN_ADDR = "0x0000000000000000000000000000000000000001"
[contract_verifier]
url = "http://server:3070"
[api.web3_json_rpc]
http_url = "http://server:3050"
ws_url = "ws://server:3051"
[api.prometheus]
pushgateway_url = "http://pushgateway:9091"
[fri_prover_gateway]
api_url="http://server:3320"
prometheus_pushgateway_url = "http://pushgateway:9091"
[fri_witness_vector_generator]
prometheus_pushgateway_url = "http://pushgateway:9091"
24 changes: 24 additions & 0 deletions infra/kubernetes/jobs/zk_init.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: batch/v1
kind: Job
metadata:
name: zk-init-job
namespace: default
spec:
completions: 1
template:
spec:
serviceAccountName: configmaps-w
containers:
- name: zksync
image: us-central1-docker.pkg.dev/zksync-413615/zksync/zk-init:latest
env:
- name: BASE_TOKEN_SYMBOL
value: IHC
volumeMounts:
- name: custom-config-volume
mountPath: /configs
restartPolicy: Never
volumes:
- name: custom-config-volume
configMap:
name: zk-init-configmap
28 changes: 28 additions & 0 deletions infra/kubernetes/roles/configmap_creator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: configmap-creator
namespace: default
rules:
- resources: ["configmaps"]
verbs: ["list", "create"]
apiGroups: [""]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: configmap-creator-binding
namespace: default
subjects:
- kind: ServiceAccount
name: configmaps-w
roleRef:
kind: Role
name: configmap-creator
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: configmaps-w
namespace: default

0 comments on commit cc95b83

Please sign in to comment.