From c0fe15570849f0c4c0e466d9a74c600b703ac42f Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Thu, 18 Jul 2024 10:22:37 +0530 Subject: [PATCH] Add stack for running individual nitro node (#2) * Build go-nitro container image * Run nitro node using stack * Add config setup in readme for running nitro node * Add script for deploying contracts --------- Co-authored-by: Shreerang Kale Co-authored-by: Neeraj --- .../compose/docker-compose-go-nitro.yml | 28 ++---- .../docker-compose-nitro-contracts.yml | 21 ++--- .../config/go-nitro/run-nitro-node.sh | 46 +--------- .../config/nitro-contracts/deploy.sh | 67 ++++---------- .../container-build/cerc-go-nitro/Dockerfile | 16 +++- .../container-build/cerc-go-nitro/build.sh | 0 .../cerc-nitro-rpc-client/Dockerfile | 1 - stack-orchestrator/stack/go-nitro/README.md | 88 ++++++++++++++++--- stack-orchestrator/stack/go-nitro/stack.yml | 9 +- 9 files changed, 128 insertions(+), 148 deletions(-) mode change 100644 => 100755 stack-orchestrator/config/go-nitro/run-nitro-node.sh mode change 100644 => 100755 stack-orchestrator/config/nitro-contracts/deploy.sh mode change 100644 => 100755 stack-orchestrator/container-build/cerc-go-nitro/build.sh diff --git a/stack-orchestrator/compose/docker-compose-go-nitro.yml b/stack-orchestrator/compose/docker-compose-go-nitro.yml index 6f79653..fd9a46a 100644 --- a/stack-orchestrator/compose/docker-compose-go-nitro.yml +++ b/stack-orchestrator/compose/docker-compose-go-nitro.yml @@ -5,38 +5,22 @@ services: image: cerc/go-nitro:local hostname: go-nitro restart: on-failure - depends_on: - # Wait for Nitro contracts to be deployed - nitro-contracts: - condition: service_completed_successfully environment: CERC_NITRO_CHAIN_URL: ${CERC_NITRO_CHAIN_URL} CERC_NITRO_PK: ${CERC_NITRO_PK} CERC_NITRO_CHAIN_PK: ${CERC_NITRO_CHAIN_PK} - CERC_NITRO_USE_DURABLE_STORE: ${CERC_NITRO_USE_DURABLE_STORE} - CERC_NITRO_DURABLE_STORE_FOLDER: ${CERC_NITRO_DURABLE_STORE_FOLDER} CERC_NITRO_MSG_PORT: ${CERC_NITRO_MSG_PORT} - CERC_NITRO_WS_MSG_PORT: ${CERC_NITRO_WS_MSG_PORT} CERC_NITRO_RPC_PORT: ${CERC_NITRO_RPC_PORT} CERC_NA_ADDRESS: ${CERC_NA_ADDRESS} CERC_VPA_ADDRESS: ${CERC_VPA_ADDRESS} CERC_CA_ADDRESS: ${CERC_CA_ADDRESS} + CERC_BOOT_PEERS: ${CERC_BOOT_PEERS} entrypoint: ["bash", "-c", "/app/run-nitro-node.sh"] volumes: - - go_nitro_data:/app/data - - nitro_deployment:/app/deployment - ../config/go-nitro/run-nitro-node.sh:/app/run-nitro-node.sh - healthcheck: - test: ["CMD", "nc", "-vz", "localhost", "4006"] - interval: 30s - timeout: 5s - retries: 10 - start_period: 10s ports: - - "3006:3006" - - "4006:4006" - - "5006:5006" - -volumes: - go_nitro_data: - nitro_deployment: + - 3006:3006 + - 4006:4006 + - 5006:5006 + extra_hosts: + - "host.docker.internal:host-gateway" diff --git a/stack-orchestrator/compose/docker-compose-nitro-contracts.yml b/stack-orchestrator/compose/docker-compose-nitro-contracts.yml index af1a66b..09d6d7d 100644 --- a/stack-orchestrator/compose/docker-compose-nitro-contracts.yml +++ b/stack-orchestrator/compose/docker-compose-nitro-contracts.yml @@ -2,24 +2,21 @@ name: nitro-contracts services: # Optionally deploys the Nitro contracts - # TODO: Rename to nitro-protocol nitro-contracts: image: cerc/nitro-contracts:local - restart: on-failure + # restart: on-failure environment: CERC_SCRIPT_DEBUG: ${CERC_SCRIPT_DEBUG} - CERC_ETH_RPC_ENDPOINT: ${CERC_ETH_RPC_ENDPOINT} - CERC_PRIVATE_KEY_DEPLOYER: ${CERC_PRIVATE_KEY_DEPLOYER} - CERC_NA_ADDRESS: ${CERC_NA_ADDRESS} - CERC_VPA_ADDRESS: ${CERC_VPA_ADDRESS} - CERC_CA_ADDRESS: ${CERC_CA_ADDRESS} - # TODO: Add bridge contract address + CERC_GETH_URL: ${CERC_GETH_URL} + CERC_OPTIMISM_URL: ${CERC_OPTIMISM_URL} + CERC_GETH_CHAIN_ID: ${CERC_GETH_CHAIN_ID} + CERC_OPTIMISM_CHAIN_ID: ${CERC_OPTIMISM_CHAIN_ID} + CERC_GETH_DEPLOYER_PK: ${CERC_GETH_DEPLOYER_PK} + CERC_OPTIMISM_DEPLOYER_PK: ${CERC_GETH_DEPLOYER_PK} + CERC_DISABLE_DETERMINISTIC_DEPLOYMENT: true volumes: - - ../config/nitro-contracts/deploy.sh:/app/deploy.sh - - nitro_deployment:/app/deployment + - ../config/nitro-contracts/deploy.sh:/app/deploy.sh command: ["bash", "-c", "/app/deploy.sh"] extra_hosts: - "host.docker.internal:host-gateway" -volumes: - nitro_deployment: diff --git a/stack-orchestrator/config/go-nitro/run-nitro-node.sh b/stack-orchestrator/config/go-nitro/run-nitro-node.sh old mode 100644 new mode 100755 index e43e139..819c293 --- a/stack-orchestrator/config/go-nitro/run-nitro-node.sh +++ b/stack-orchestrator/config/go-nitro/run-nitro-node.sh @@ -5,48 +5,4 @@ if [ -n "$CERC_SCRIPT_DEBUG" ]; then set -x fi -nitro_addresses_file="/app/deployment/nitro-addresses.json" - -# Check if CERC_NA_ADDRESS environment variable is set -if [ -n "$CERC_NA_ADDRESS" ]; then - echo "CERC_NA_ADDRESS is set to '$CERC_NA_ADDRESS'" - echo "CERC_VPA_ADDRESS is set to '$CERC_VPA_ADDRESS'" - echo "CERC_CA_ADDRESS is set to '$CERC_CA_ADDRESS'" - echo "Using the above Nitro addresses" - - NA_ADDRESS=${CERC_NA_ADDRESS} - VPA_ADDRESS=${CERC_VPA_ADDRESS} - CA_ADDRESS=${CERC_CA_ADDRESS} -elif [ -f ${nitro_addresses_file} ]; then - echo "Reading Nitro addresses from ${nitro_addresses_file}" - -# TODO: Use Bridge address instead of NA address while running node on L2 - NA_ADDRESS=$(jq -r '.nitroAdjudicatorAddress' ${nitro_addresses_file}) - VPA_ADDRESS=$(jq -r '.virtualPaymentAppAddress' ${nitro_addresses_file}) - CA_ADDRESS=$(jq -r '.consensusAppAddress' ${nitro_addresses_file}) -else - echo "File ${nitro_addresses_file} not found" - exit 1 -fi - -echo "Running Nitro node" - -# Assuming CERC_NITRO_CHAIN_URL is of format ://host:port -ws_host=$(echo "$CERC_NITRO_CHAIN_URL" | awk -F '://' '{print $2}' | cut -d ':' -f 1) -ws_port=$(echo "$CERC_NITRO_CHAIN_URL" | awk -F '://' '{print $2}' | cut -d ':' -f 2) - -# Wait till chain endpoint is available -retry_interval=5 -while true; do - nc -z -w 1 "$ws_host" "$ws_port" - - if [ $? -eq 0 ]; then - echo "Chain endpoint is available" - break - fi - - echo "Chain endpoint not yet available, retrying in $retry_interval seconds..." - sleep $retry_interval -done - -./nitro -chainurl ${CERC_NITRO_CHAIN_URL} -msgport ${CERC_NITRO_MSG_PORT} -rpcport ${CERC_NITRO_RPC_PORT} -wsmsgport ${CERC_NITRO_WS_MSG_PORT} -publicip "0.0.0.0" -pk ${CERC_NITRO_PK} -chainpk ${CERC_NITRO_CHAIN_PK} -naaddress ${NA_ADDRESS} -vpaaddress ${VPA_ADDRESS} -caaddress ${CA_ADDRESS} -usedurablestore ${CERC_NITRO_USE_DURABLE_STORE} -durablestorefolder ${CERC_NITRO_DURABLE_STORE_FOLDER} -tlscertfilepath "" -tlsKeyFilepath "" +./nitro -chainurl ${CERC_NITRO_CHAIN_URL} -msgport ${CERC_NITRO_MSG_PORT} -rpcport ${CERC_NITRO_RPC_PORT} -pk ${CERC_NITRO_PK} -chainpk ${CERC_NITRO_CHAIN_PK} -naaddress ${CERC_NA_ADDRESS} -vpaaddress ${CERC_VPA_ADDRESS} -caaddress ${CERC_CA_ADDRESS} diff --git a/stack-orchestrator/config/nitro-contracts/deploy.sh b/stack-orchestrator/config/nitro-contracts/deploy.sh old mode 100644 new mode 100755 index 7c6e0c1..42bb9ea --- a/stack-orchestrator/config/nitro-contracts/deploy.sh +++ b/stack-orchestrator/config/nitro-contracts/deploy.sh @@ -6,54 +6,23 @@ if [ -n "$CERC_SCRIPT_DEBUG" ]; then set -x fi -nitro_addresses_file="/app/deployment/nitro-addresses.json" +echo "Environment variables..." +echo "CERC_SCRIPT_DEBUG: ${CERC_SCRIPT_DEBUG}" +echo "CERC_GETH_URL: ${CERC_GETH_URL}" +echo "CERC_OPTIMISM_URL: ${CERC_OPTIMISM_URL}" +echo "CERC_GETH_CHAIN_ID: ${CERC_GETH_CHAIN_ID}" +echo "CERC_OPTIMISM_CHAIN_ID: ${CERC_OPTIMISM_CHAIN_ID}" +echo "CERC_GETH_DEPLOYER_PK: ${CERC_GETH_DEPLOYER_PK}" +echo "CERC_OPTIMISM_DEPLOYER_PK: ${CERC_OPTIMISM_DEPLOYER_PK}" +echo "CERC_DISABLE_DETERMINISTIC_DEPLOYMENT: true" -# Check if CERC_NA_ADDRESS environment variable set to skip contract deployment -if [ -n "$CERC_NA_ADDRESS" ]; then - echo "CERC_NA_ADDRESS is set to '$CERC_NA_ADDRESS'" - echo "CERC_VPA_ADDRESS is set to '$CERC_VPA_ADDRESS'" - echo "CERC_CA_ADDRESS is set to '$CERC_CA_ADDRESS'" - echo "Skipping Nitro contracts deployment" - exit -fi +export GETH_URL=$CERC_GETH_URL +export GETH_CHAIN_ID=$CERC_GETH_CHAIN_ID +export OPTIMISM_CHAIN_ID=$CERC_OPTIMISM_CHAIN_ID +export OPTIMISM_URL=$CERC_OPTIMISM_URL +export GETH_DEPLOYER_PK=$CERC_GETH_DEPLOYER_PK +export OPTIMISM_DEPLOYER_PK=$GETH_DEPLOYER_PK +export DISABLE_DETERMINISTIC_DEPLOYMENT=true -# Check and exit if a deployment already exists (on restarts) -if [ -f ${nitro_addresses_file} ]; then - echo "${nitro_addresses_file} already exists, skipping Nitro contracts deployment" - cat ${nitro_addresses_file} - exit -fi - -echo "Using ETH RPC endpoint ${CERC_ETH_RPC_ENDPOINT}" - -# Wait till ETH RPC endpoint is available with block number > 1 -retry_interval=5 -while true; do - block_number_hex=$(curl -s -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' ${CERC_ETH_RPC_ENDPOINT} | jq -r '.result') - - # Check if the request call was successful - if [ $? -ne 0 ]; then - echo "RPC endpoint not yet available, retrying in $retry_interval seconds..." - sleep $retry_interval - continue - fi - - # Convert hex to decimal - block_number_dec=$(printf %u ${block_number_hex}) - - # Check if block number is > 1 to avoid failures in the deployment - if [ "$block_number_dec" -ge 1 ]; then - echo "RPC endpoint is up" - break - else - echo "RPC endpoint not yet available, retrying in $retry_interval seconds..." - sleep $retry_interval - continue - fi -done - -echo "Using CERC_PRIVATE_KEY_DEPLOYER from env" - -# Use yarn contracts:deploy to deploy contracts -yarn test:deploy-contracts --chainUrl ${CERC_ETH_RPC_ENDPOINT} --key ${CERC_PRIVATE_KEY_DEPLOYER} --addressesFilePath ${nitro_addresses_file} -cat ${nitro_addresses_file} +yarn contracts:deploy-geth +yarn contracts:deploy-optimism \ No newline at end of file diff --git a/stack-orchestrator/container-build/cerc-go-nitro/Dockerfile b/stack-orchestrator/container-build/cerc-go-nitro/Dockerfile index 696c1fe..76e4326 100644 --- a/stack-orchestrator/container-build/cerc-go-nitro/Dockerfile +++ b/stack-orchestrator/container-build/cerc-go-nitro/Dockerfile @@ -4,6 +4,17 @@ FROM golang:1.21-bullseye AS builder WORKDIR /app COPY . . +RUN go mod tidy + +# Install mkcert +RUN curl -JLO "https://dl.filippo.io/mkcert/latest?for=linux/amd64" +RUN chmod +x mkcert-v*-linux-amd64 +RUN cp mkcert-v*-linux-amd64 /usr/local/bin/mkcert + +# Create certificates +RUN cd /app/tls && \ + make create-cert + # Build the binary RUN go build -v -o nitro . @@ -11,7 +22,10 @@ RUN go build -v -o nitro . # Reduce image size FROM debian:bullseye-slim RUN apt-get update -RUN apt-get install -y ca-certificates jq netcat +RUN apt-get install -y jq netcat RUN rm -rf /var/lib/apt/lists/* + WORKDIR /app + COPY --from=builder /app/nitro . +COPY --from=builder /app/tls /app/tls diff --git a/stack-orchestrator/container-build/cerc-go-nitro/build.sh b/stack-orchestrator/container-build/cerc-go-nitro/build.sh old mode 100644 new mode 100755 diff --git a/stack-orchestrator/container-build/cerc-nitro-rpc-client/Dockerfile b/stack-orchestrator/container-build/cerc-nitro-rpc-client/Dockerfile index 887673b..26f722d 100644 --- a/stack-orchestrator/container-build/cerc-nitro-rpc-client/Dockerfile +++ b/stack-orchestrator/container-build/cerc-nitro-rpc-client/Dockerfile @@ -10,4 +10,3 @@ RUN echo "Installing dependencies" && \ yarn & yarn build WORKDIR /app/packages/nitro-rpc-client -ENV NODE_EXTRA_CA_CERTS="$(mkcert -CAROOT)/rootCA.pem" diff --git a/stack-orchestrator/stack/go-nitro/README.md b/stack-orchestrator/stack/go-nitro/README.md index ad40743..28cf34f 100644 --- a/stack-orchestrator/stack/go-nitro/README.md +++ b/stack-orchestrator/stack/go-nitro/README.md @@ -1,19 +1,81 @@ # Go-nitro -## Clone the stack repo - +## Setup -## Clone required repositories +- Clone the stack repo + + ```bash + laconic-so fetch-stack git.vdb.to/cerc-io/nitro-stack + ``` + +- Clone required repositories + + ```bash + laconic-so --stack ~/cerc/nitro-stack/stack-orchestrator/stack/go-nitro setup-repositories + ``` + Note: Replace path to nitro stack with actual path + +- Build the container image + + ```bash + laconic-so --stack ~/cerc/nitro-stack/stack-orchestrator/stack/go-nitro build-containers + ``` + +## Create a deployment + +- Create a spec file + + ```bash + laconic-so --stack ~/cerc/nitro-stack/stack-orchestrator/stack/go-nitro deploy init --output go-nitro-spec.yml + ``` + +- TODO: Edit `network` in the spec file to map container ports to host ports as required: + + ```bash + network: + ports: + go-nitro: + - 3006:3006 + - 4006:4006 + - 5006:5006 + ``` + +- Create deployment + + ```bash + laconic-so --stack ~/cerc/nitro-stack/stack-orchestrator/stack/go-nitro deploy create --spec-file go-nitro-spec.yml --deployment-dir go-nitro-deployment + ``` + +## Configuration + +* Inside the `go-nitro-deployment` deployment directory, open `config.env` file and set following env variables: + + ```bash + # URL endpoint of the chain (Example: ws://host.docker.internal:8545) + CERC_NITRO_CHAIN_URL= + + # Private key of account to be used by Nitro node (derived address is used as Nitro address) + CERC_NITRO_PK= + + # Private key of the account on chain that is used for funding channels in Nitro node + CERC_NITRO_CHAIN_PK= + + # Port to be used by P2P message service (Example: 3009) + CERC_NITRO_MSG_PORT= + + # Port to be used by RPC server (Example: 4009) + CERC_NITRO_RPC_PORT=4009 + + # Optional + # Contract addresses of NitroAdjudicator, VirtualPaymentApp and ConsensusApp + # TODO: Contracts are deployed if addresses are not set + CERC_NA_ADDRESS= + CERC_VPA_ADDRESS= + CERC_CA_ADDRESS= + ``` + +## Start the deployment - ```bash -laconic-so --stack //nitro-stack/stack-orchestrator/stack/go-nitro setup-repositories +laconic-so deployment --dir go-nitro-deployment start ``` -Note: Replace path to nitro stack with actual path - -## Build the container image - - -```bash -laconic-so --stack //nitro-stack/stack-orchestrator/stack/go-nitro build-containers -``` \ No newline at end of file diff --git a/stack-orchestrator/stack/go-nitro/stack.yml b/stack-orchestrator/stack/go-nitro/stack.yml index 51649bd..02f4b23 100644 --- a/stack-orchestrator/stack/go-nitro/stack.yml +++ b/stack-orchestrator/stack/go-nitro/stack.yml @@ -5,10 +5,9 @@ repos: - github.com/cerc-io/go-nitro containers: - cerc/nitro-contracts - - cerc/nitro-rpc-client - # TODO: Uncomment after building container image - # - cerc/go-nitro + - cerc/go-nitro + # - cerc/nitro-rpc-client pods: - - nitro-contracts - go-nitro - - nitro-rpc-client + - nitro-contracts + # - nitro-rpc-client