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: fund l2 accounts and deploy deterministic deployment proxy #316

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
ffa80f9
fund l2 accounts
minhd-vu Oct 16, 2024
4c83027
fix lint
minhd-vu Oct 16, 2024
b18f509
fix lint
minhd-vu Oct 16, 2024
4b1a810
remove comment
minhd-vu Oct 16, 2024
81e198b
remove additional services
minhd-vu Oct 16, 2024
48bab40
Merge branch 'main' into minhd-vu/fund-l2-accounts
minhd-vu Oct 16, 2024
112cbef
fix typo
minhd-vu Oct 16, 2024
b3e5ae1
Merge branch 'main' into minhd-vu/fund-l2-accounts
minhd-vu Oct 17, 2024
3d234d4
use toolbox for l2 contracts image
minhd-vu Oct 17, 2024
922b3e0
update docs
minhd-vu Oct 17, 2024
12621f7
funded bridge autoclaimer
minhd-vu Oct 17, 2024
7a22a03
fix typo
minhd-vu Oct 17, 2024
1f228c2
remove extra space
minhd-vu Oct 21, 2024
ccf88dd
Merge branch 'main' into minhd-vu/fund-l2-accounts
minhd-vu Oct 21, 2024
6f569b2
update dockerfiles
minhd-vu Oct 21, 2024
180045d
Merge branch 'main' into minhd-vu/fund-l2-accounts
minhd-vu Oct 21, 2024
6c65c52
update network helper
minhd-vu Oct 21, 2024
ae8f5ab
add ddp branch
minhd-vu Oct 22, 2024
a841ff9
deploy ddp
minhd-vu Oct 22, 2024
bc4ea58
add node to toolbox
minhd-vu Oct 22, 2024
0ae219b
Merge branch 'main' into minhd-vu/fund-l2-accounts
minhd-vu Oct 22, 2024
9e1b0ff
fix ddp deployment
minhd-vu Oct 23, 2024
2bd3e66
try using ci
minhd-vu Oct 23, 2024
240bf5a
sort
minhd-vu Oct 23, 2024
cde2d8f
fix external l1 ci
minhd-vu Oct 23, 2024
b8a9a02
fix: shell lint
minhd-vu Oct 23, 2024
e15c2f0
fix: shell lint
minhd-vu Oct 23, 2024
c7a30a6
maybe fix
minhd-vu Oct 23, 2024
2dd7905
Merge branch 'main' into minhd-vu/fund-l2-accounts
minhd-vu Oct 23, 2024
09d033a
Merge branch 'main' into minhd-vu/fund-l2-accounts
minhd-vu Oct 24, 2024
734e664
Merge branch 'main' into minhd-vu/fund-l2-accounts
minhd-vu Oct 24, 2024
cfc82c1
fix: l2_rpc_url
minhd-vu Oct 24, 2024
4a80813
Merge branch 'main' into minhd-vu/fund-l2-accounts
minhd-vu Oct 24, 2024
7b14837
chore: shell if style
minhd-vu Oct 25, 2024
b09f443
address comments
minhd-vu Oct 25, 2024
089e54a
update docs
minhd-vu Oct 25, 2024
938d2ca
fix doc
minhd-vu Oct 25, 2024
7b5bfe3
Merge branch 'main' into minhd-vu/fund-l2-accounts
minhd-vu Oct 25, 2024
d376d42
format readme
minhd-vu Oct 25, 2024
cb78ffc
reuse zkevm-contracts container
minhd-vu Oct 25, 2024
4d4a339
update docs
minhd-vu Oct 25, 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
69 changes: 69 additions & 0 deletions deploy_l2_contracts.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
service_package = import_module("./lib/service.star")

ARTIFACTS = [
{
"name": "run-l2-contract-setup.sh",
"file": "./templates/contract-deploy/run-l2-contract-setup.sh",
},
]


def run(plan, args):
artifact_paths = list(ARTIFACTS)

l2_rpc_service = plan.get_service(
name="cdk-erigon-sequencer" + args["deployment_suffix"]
)
l2_rpc_url = "http://{}:{}".format(
l2_rpc_service.ip_address, l2_rpc_service.ports["rpc"].number
)

artifacts = []
for artifact_cfg in artifact_paths:
template = read_file(src=artifact_cfg["file"])
artifact = plan.render_templates(
name=artifact_cfg["name"],
config={
artifact_cfg["name"]: struct(
template=template,
data=args
| {
"l2_rpc_url": l2_rpc_url,
"deterministic_deployment_proxy_branch": "master",
},
)
},
)
artifacts.append(artifact)

# Create helper service to deploy contracts
contracts_service_name = "contracts-l2" + args["deployment_suffix"]
plan.add_service(
name=contracts_service_name,
config=ServiceConfig(
image=args["l2_contracts_image"],
files={
"/opt/zkevm": Directory(persistent_key="zkevm-l2-artifacts"),
"/opt/contract-deploy/": Directory(artifact_names=artifacts),
},
# These two lines are only necessary to deploy to any Kubernetes environment (e.g. GKE).
entrypoint=["bash", "-c"],
cmd=["sleep infinity"],
user=User(uid=0, gid=0), # Run the container as root user.
),
)

# Deploy contracts.
plan.exec(
description="Deploying contracts on L2",
service_name=contracts_service_name,
recipe=ExecRecipe(
command=[
"/bin/sh",
"-c",
"chmod +x {0} && {0}".format(
"/opt/contract-deploy/run-l2-contract-setup.sh"
),
]
),
)
4 changes: 2 additions & 2 deletions docker/toolbox.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21 AS polycli-builder
FROM golang:1.22 AS polycli-builder
ARG POLYCLI_VERSION
WORKDIR /opt/polygon-cli
RUN git clone --branch ${POLYCLI_VERSION} https://github.com/maticnetwork/polygon-cli.git . \
Expand All @@ -17,7 +17,7 @@
# WARNING (SC1091): (Sourced) file not included in mock.
# hadolint ignore=DL3008,DL3013,DL4006,SC1091
RUN apt-get update \
&& apt-get install --yes --no-install-recommends curl git jq pipx \
&& apt-get install --yes --no-install-recommends curl git jq pipx nodejs npm \

Check notice

Code scanning / SonarCloud

Arguments in long RUN instructions should be sorted

<!--SONAR_ISSUE_KEY:AZK1eCyy117yESGJTC9d-->Sort these package names alphanumerically. <p>See more on <a href="https://sonarcloud.io/project/issues?id=0xPolygon_kurtosis-cdk&issues=AZK1eCyy117yESGJTC9d&open=AZK1eCyy117yESGJTC9d&pullRequest=316">SonarCloud</a></p>
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& pipx ensurepath \
Expand Down
4 changes: 2 additions & 2 deletions docker/zkevm-contracts.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21 AS polycli-builder
FROM golang:1.22 AS polycli-builder
ARG POLYCLI_VERSION
WORKDIR /opt/polygon-cli
RUN git clone --branch ${POLYCLI_VERSION} https://github.com/maticnetwork/polygon-cli.git . \
Expand All @@ -12,7 +12,7 @@ LABEL description="Helper image to deploy zkevm contracts"
# STEP 1: Download zkevm contracts dependencies and compile contracts.
ARG ZKEVM_CONTRACTS_BRANCH
WORKDIR /opt/zkevm-contracts
RUN git clone https://github.com/0xPolygonHermez/zkevm-contracts . \
RUN git clone https://github.com/0xPolygonHermez/zkevm-contracts . \
&& git checkout ${ZKEVM_CONTRACTS_BRANCH} \
&& npm install --global npm@10.9.0 \
&& npm install \
Expand Down
2 changes: 1 addition & 1 deletion docs/trigger-a-reorg/network-helper.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21 AS polycli-builder
FROM golang:1.22 AS polycli-builder
WORKDIR /opt/polygon-cli
RUN git clone https://github.com/maticnetwork/polygon-cli.git . \
&& make build
Expand Down
14 changes: 13 additions & 1 deletion input_parser.star
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ DEFAULT_DEPLOYMENT_STAGES = {
# Deploy cdk-erigon node.
# TODO: Remove this parameter to incorporate cdk-erigon inside the central environment.
"deploy_cdk_erigon_node": True,
# Deploy contracts on L2 (as well as fund accounts).
"deploy_l2_contracts": True,
}

DEFAULT_IMAGES = {
"agglayer_image": "ghcr.io/agglayer/agglayer-rs:pr-96", # https://github.com/agglayer/agglayer/pkgs/container/agglayer-rs
"cdk_erigon_node_image": "hermeznetwork/cdk-erigon:v2.1.0", # https://hub.docker.com/r/hermeznetwork/cdk-erigon/tags
"cdk_node_image": "ghcr.io/0xpolygon/cdk:0.3.0", # https://github.com/0xpolygon/cdk/pkgs/container/cdk
"cdk_validium_node_image": "0xpolygon/cdk-validium-node:0.7.0-cdk", # https://hub.docker.com/r/0xpolygon/cdk-validium-node/tags
"l2_contracts_image": "leovct/toolbox:0.0.5", # https://hub.docker.com/r/leovct/toolbox/tags
"zkevm_bridge_proxy_image": "haproxy:3.0-bookworm", # https://hub.docker.com/_/haproxy/tags
"zkevm_bridge_service_image": "hermeznetwork/zkevm-bridge-service:v0.6.0-RC1", # https://hub.docker.com/r/hermeznetwork/zkevm-bridge-service/tags
"zkevm_bridge_ui_image": "leovct/zkevm-bridge-ui:multi-network", # https://hub.docker.com/r/leovct/zkevm-bridge-ui/tags
Expand Down Expand Up @@ -123,7 +126,7 @@ DEFAULT_L1_ARGS = {
# - blutgang
# - forky
# - apache
# - tracoor
# - tracoor
# Check the ethereum-package for more details: https://github.com/ethpandaops/ethereum-package
"l1_additional_services": [],
# Preset for the network.
Expand All @@ -142,6 +145,14 @@ DEFAULT_L1_ARGS = {
"l1_funding_amount": "100ether",
}

DEFAULT_L2_ARGS = {
# The number of accounts to fund on L2. The accounts will be derived from:
# polycli wallet inspect --mnemonic '{{.l1_preallocated_mnemonic}}'
"l2_accounts_to_fund": 10,
# The amount of ETH sent to each of the prefunded l2 accounts.
"l2_funding_amount": "100ether",
}

DEFAULT_ROLLUP_ARGS = {
# The keystore password.
"zkevm_l2_keystore_password": "pSnv6Dh5s9ahuzGzH9RoCDrKAMddaX3m",
Expand Down Expand Up @@ -206,6 +217,7 @@ DEFAULT_ARGS = (
| DEFAULT_L1_ARGS
| DEFAULT_ROLLUP_ARGS
| DEFAULT_PLESS_ZKEVM_NODE_ARGS
| DEFAULT_L2_ARGS
)

# A list of fork identifiers currently supported by Kurtosis CDK.
Expand Down
10 changes: 9 additions & 1 deletion main.star
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ databases_package = "./databases.star"
deploy_zkevm_contracts_package = "./deploy_zkevm_contracts.star"
ethereum_package = "./ethereum.star"
zkevm_pool_manager_package = "./zkevm_pool_manager.star"
deploy_l2_contracts_package = "./deploy_l2_contracts.star"

# Additional service packages.
arpeggio_package = "./src/additional_services/arpeggio.star"
Expand Down Expand Up @@ -79,7 +80,7 @@ def run(plan, args={}):
# Deploy cdk central/trusted environment.
if deployment_stages.get("deploy_cdk_central_environment", False):
# Deploy cdk-erigon sequencer node.
# TODO this is a little weird if the erigon sequencer is deployed before the exector?
# TODO this is a little weird if the erigon sequencer is deployed before the executor?
if args["sequencer_type"] == "erigon":
plan.print("Deploying cdk-erigon sequencer")
import_module(cdk_erigon_package).run_sequencer(plan, args)
Expand Down Expand Up @@ -125,6 +126,13 @@ def run(plan, args={}):
else:
plan.print("Skipping the deployment of the agglayer")

# Deploy contracts on L2.
if deployment_stages.get("deploy_l2_contracts", False):
plan.print("Deploying contracts on L2")
import_module(deploy_l2_contracts_package).run(plan, args)
else:
plan.print("Skipping the deployment of contracts on L2")

# Launching additional services.
additional_services = args["additional_services"]

Expand Down
141 changes: 141 additions & 0 deletions templates/contract-deploy/run-l2-contract-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/bin/bash

global_log_level="{{.global_log_level}}"
if [[ $global_log_level == "debug" ]]; then
set -x
fi

echo_ts() {
local green="\e[32m"
local end_color="\e[0m"
local timestamp
timestamp=$(date +"[%Y-%m-%d %H:%M:%S]")

echo -e "$green$timestamp$end_color $1" >&2
}

wait_for_rpc_to_be_available() {
local counter=0
local max_retries=20
local retry_interval=5

until cast send --legacy \
--rpc-url "{{.l2_rpc_url}}" \
--private-key "{{.zkevm_l2_admin_private_key}}" \
--value 0 "{{.zkevm_l2_sequencer_address}}" &> /dev/null; do
((counter++))
echo_ts "Can't send L2 transfers yet... Retrying ($counter)..."
if [[ $counter -ge $max_retries ]]; then
echo_ts "Exceeded maximum retry attempts. Exiting."
exit 1
fi
sleep $retry_interval
done
}

fund_account_on_l2() {
local address="$1"
echo_ts "Funding $address"
cast send \
--legacy \
--rpc-url "{{.l2_rpc_url}}" \
--private-key "{{.zkevm_l2_admin_private_key}}" \
--value "{{.l2_funding_amount}}" \
"$address"
}

if [[ -e "/opt/zkevm/.init-complete{{.deployment_suffix}}.lock" ]]; then
echo_ts "This script has already been executed"
exit 1
fi

echo_ts "Waiting for the L2 RPC to be available"
wait_for_rpc_to_be_available
echo_ts "L2 RPC is now available"

echo_ts "Funding bridge autoclaimer account on l2"
fund_account_on_l2 "{{.zkevm_l2_claimtxmanager_address}}"

echo_ts "Funding accounts on l2"
accounts=$(
polycli wallet inspect \
--mnemonic "{{.l1_preallocated_mnemonic}}" \
--addresses "{{.l2_accounts_to_fund}}"
)
echo "$accounts" | jq -r ".Addresses[].ETHAddress" | while read -r address; do
fund_account_on_l2 "$address"
done

echo_ts "Building deterministic deployment proxy"
git clone https://github.com/Arachnid/deterministic-deployment-proxy.git \
minhd-vu marked this conversation as resolved.
Show resolved Hide resolved
/opt/deterministic-deployment-proxy \
--branch "{{.deterministic_deployment_proxy_branch}}"
cd /opt/deterministic-deployment-proxy
npm ci
npm run build
minhd-vu marked this conversation as resolved.
Show resolved Hide resolved

signer_address="0x$(cat output/deployment.json | jq --raw-output '.signerAddress')"
gas_price=$(cat output/deployment.json | jq --raw-output '.gasPrice')
gas_limit=$(cat output/deployment.json | jq --raw-output '.gasLimit')
gas_cost=$((gas_price * gas_limit))
transaction="0x$(cat output/deployment.json | jq --raw-output '.transaction')"
deployer_address="0x$(cat output/deployment.json | jq --raw-output '.address')"
l1_private_key=$(
polycli wallet inspect \
--mnemonic "{{.l1_preallocated_mnemonic}}" \
--addresses 1 \
| jq -r ".Addresses[].HexPrivateKey"
)

echo_ts "Deploying deterministic deployment proxy on l1"
cast send \
--rpc-url "{{.l1_rpc_url}}" \
--private-key "$l1_private_key" \
--value "$gas_cost" \
"$signer_address"
cast publish --rpc-url "{{.l1_rpc_url}}" "$transaction"

echo_ts "Deploying deterministic deployment proxy on l2"
cast send \
--legacy \
--rpc-url "{{.l2_rpc_url}}" \
--private-key "{{.zkevm_l2_admin_private_key}}" \
--value "$gas_cost" \
"$signer_address"
cast publish --rpc-url "{{.l2_rpc_url}}" "$transaction"

contract_method_signature="banana()(uint8)"
expected="42"
salt="0x0000000000000000000000000000000000000000000000000000000000000000"
# contract: pragma solidity 0.5.8; contract Apple {function banana() external pure returns (uint8) {return 42;}}
bytecode="6080604052348015600f57600080fd5b5060848061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063c3cafc6f14602d575b600080fd5b6033604f565b604051808260ff1660ff16815260200191505060405180910390f35b6000602a90509056fea165627a7a72305820ab7651cb86b8c1487590004c2444f26ae30077a6b96c6bc62dda37f1328539250029"
contract_address=$(cast create2 --salt $salt --init-code $bytecode)

echo_ts "Testing deterministic deployment proxy on l1"
cast send \
--legacy \
--rpc-url "{{.l1_rpc_url}}" \
--private-key "$l1_private_key" \
"$deployer_address" \
"$salt$bytecode"
l1_actual=$(cast call --rpc-url "{{.l1_rpc_url}}" "$contract_address" "$contract_method_signature")
if [ "$expected" != "$l1_actual" ]; then
minhd-vu marked this conversation as resolved.
Show resolved Hide resolved
echo_ts "Failed to deploy deterministic deployment proxy on l1 (expected: $expected, actual $l1_actual)"
exit 1
fi

echo_ts "Testing deterministic deployment proxy on l2"
cast send \
--legacy \
--rpc-url "{{.l2_rpc_url}}" \
--private-key "{{.zkevm_l2_admin_private_key}}" \
"$deployer_address" \
"$salt$bytecode"
l2_actual=$(cast call --rpc-url "{{.l2_rpc_url}}" "$contract_address" "$contract_method_signature")
if [ "$expected" != "$l2_actual" ]; then
echo_ts "Failed to deploy deterministic deployment proxy on l2 (expected: $expected, actual $l2_actual)"
exit 1
fi

# The contract setup is done!
touch "/opt/zkevm/.init-complete{{.deployment_suffix}}.lock"
Loading