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: Add infrastructure to run node #110

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.vscode
.env
.DS_Store
build/deployments
**/.idea/
build/devnet/anvil_state.json
33 changes: 33 additions & 0 deletions build/devnet/deploy-rollups-anvil.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash


# Run Anvil in the background
cmd="anvil --host 0.0.0.0 --dump-state anvil_state.json"
eval "${cmd}" &>/dev/null & disown;


# Check Anvil is ready
retries=0
max_retries=50
while [ $retries -lt $max_retries ]; do

ready=$(curl -X POST -s -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":"1","method":"net_listening","params":[]}' http://127.0.0.1:8545 | jq '.result')

if [ "$ready" == "true" ]; then
echo "Anvil is ready"
break
else
sleep 3
fi
done


# Deploy contracts with Foundry
### DEPLOY CONTRACTS HERE ###

# TEMP : To help debug
# read -n 1 -s -r -p "Press any key to continue"

sleep 3
#Kill Anvil ( just exit )
exit 0
38 changes: 38 additions & 0 deletions build/devnet/devnet.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# syntax=docker.io/docker/dockerfile:1.4
FROM debian:bookworm-20230814-slim as devnet-base

# install curl and jq (for healthcheck support)
RUN <<EOF
apt-get update
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends ca-certificates curl git jq xxd
rm -rf /var/lib/apt/lists/*
EOF

# download pre-compiled binaries
RUN curl -sSL https://github.com/foundry-rs/foundry/releases/download/nightly/foundry_nightly_linux_$(dpkg --print-architecture).tar.gz | \
tar -zx -C /usr/local/bin

# healthcheck script using net_listening JSON-RPC method
COPY eth_isready /usr/local/bin

HEALTHCHECK CMD eth_isready
CMD ["anvil"]


FROM devnet-base as devnet-deploy

WORKDIR /usr/share/cartesi
COPY deploy-rollups-anvil.sh .

RUN ./deploy-rollups-anvil.sh .


FROM devnet-base as devnet

WORKDIR /usr/share/cartesi
COPY ./entrypoint.sh /
COPY --from=devnet-deploy /usr/share/cartesi/anvil_state.json .
COPY --from=devnet-deploy /usr/share/cartesi/localhost.json .

ENTRYPOINT ["/entrypoint.sh"]
CMD ["anvil", "--load-state", "/usr/share/cartesi/state.json"]
3 changes: 3 additions & 0 deletions build/devnet/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
jq -r '.contracts | to_entries | .[] | "\(.value.address) \(.key)"' < /usr/share/cartesi/localhost.json
exec "$@"
4 changes: 4 additions & 0 deletions build/devnet/eth_isready
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh

# https://ethereum.org/en/developers/docs/apis/json-rpc/#net_listening
curl -X POST -s -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":"1","method":"net_listening","params":[]}' ${RPC_URL:-http://127.0.0.1:8545} | jq '.result'
12 changes: 12 additions & 0 deletions build/docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ target "rollups-node" {
target = "rollups-node"
context = ".."
}

target "machine" {
dockerfile = "./machine.Dockerfile"
target = "server-stage"
context = "."
}

target "devnet" {
dockerfile = "./devnet.Dockerfile"
target = "devnet"
context = "./devnet"
}
177 changes: 177 additions & 0 deletions build/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
version: "3.9"

name: rollups-node
services:
rollups-node:
build:
context: ..
dockerfile: build/Dockerfile
tags:
- "cartesi/rollups-node:devel"
entrypoint: ["cartesi-rollups-node", "validator"]
ports:
- "4000:4000"
depends_on:
hardhat:
condition: service_healthy
deployer:
condition: service_completed_successfully
hardhat_set_interval:
condition: service_completed_successfully
server_manager:
condition: service_healthy
database:
condition: service_healthy
redis:
condition: service_healthy
environment:
RUST_LOG: info
CARTESI_LOG_LEVEL: info
POSTGRES_ENDPOINT: postgres://postgres:password@database:5432/postgres

#GraphQL Server
GRAPHQL_HEALTHCHECK_PORT: 8082
GRAPHQL_HOST: "0.0.0.0"
GRAPHQL_PORT: "4000"

#Indexer
INDEXER_HEALTHCHECK_PORT: 8083
DAPP_CONTRACT_ADDRESS_FILE: /deployments/localhost/dapp.json
REDIS_ENDPOINT: redis://redis:6379
CHAIN_ID: 31337

volumes:
- machine:/var/opt/cartesi/machine-snapshots
- blockchain-data:/opt/cartesi/share/deployments:ro
- ./deployments:/deployments:ro

hardhat:
image: cartesi/rollups-hardhat:1.0.0
command:
[
"node",
"--network",
"hardhat",
"--export",
"/opt/cartesi/share/deployments/localhost.json",
]
init: true
ports:
- "8545:8545"
healthcheck:
test:
["CMD", "test", "-f", "/opt/cartesi/share/deployments/localhost.json"]
interval: 30s
timeout: 30s
retries: 5
volumes:
- blockchain-data:/opt/cartesi/share/deployments
- ./deployments:/app/rollups/deployments

deployer:
image: cartesi/rollups-cli:1.0.0
restart: on-failure
depends_on:
hardhat:
condition: service_healthy
server_manager:
condition: service_healthy
command:
[
"create",
"--rpc",
"http://hardhat:8545",
"--deploymentFile",
"/opt/cartesi/share/deployments/localhost.json",
"--mnemonic",
"test test test test test test test test test test test junk",
"--templateHashFile",
"/var/opt/cartesi/machine-snapshots/0_0/hash",
"--outputFile",
"/deployments/localhost/dapp.json",
]
volumes:
- blockchain-data:/opt/cartesi/share/deployments:ro
- machine:/var/opt/cartesi/machine-snapshots:ro
- ./deployments:/deployments

hardhat_stop_automine:
image: curlimages/curl:7.84.0
restart: on-failure
depends_on:
hardhat:
condition: service_healthy
deployer:
condition: service_completed_successfully
command:
[
"--data",
'{"id":1337,"jsonrpc":"2.0","method":"evm_setAutomine","params":[false]}',
"http://hardhat:8545",
]

hardhat_set_interval:
image: curlimages/curl:7.84.0
restart: on-failure
depends_on:
hardhat:
condition: service_healthy
hardhat_stop_automine:
condition: service_completed_successfully
command:
[
"--data",
'{"id":1337,"jsonrpc":"2.0","method":"evm_setIntervalMining","params":[5000]}',
"http://hardhat:8545",
]

server_manager:
build:
context: ..
dockerfile: build/machine.Dockerfile
restart: always
ports:
- "5001:5001"
healthcheck:
test: ["CMD-SHELL", 'bash -c ''echo "" > /dev/tcp/127.0.0.1/5001;''']
interval: 10s
timeout: 5s
retries: 5
volumes:
- machine:/var/opt/cartesi/machine-snapshots
environment:
- SERVER_MANAGER_LOG_LEVEL=warning
- REMOTE_CARTESI_MACHINE_LOG_LEVEL=info

database:
image: postgres:13-alpine
ports:
- 5432:5432
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres || exit 1"]
interval: 10s
timeout: 5s
retries: 5
environment:
- POSTGRES_PASSWORD=password
volumes:
- database-data:/var/lib/postgresql/data

redis:
image: redis:6-alpine
ports:
- 6379:6379
restart: always
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 5
volumes:
- redis-data:/data

volumes:
blockchain-data: {}
machine: {}
database-data: {}
redis-data: {}
30 changes: 30 additions & 0 deletions build/machine.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# (c) Cartesi and individual authors (see AUTHORS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR should be rebased on top of next.

# SPDX-License-Identifier: Apache-2.0 (see LICENSE)

FROM cartesi/server-manager:0.8.2 as build-server-stage

USER root

# Install system dependencies
RUN apt update && \
apt install -y wget

# Download rootfs, linux and rom
ENV IMAGES_PATH /usr/share/cartesi-machine/images
RUN wget -O ${IMAGES_PATH}/rootfs.ext2 https://github.com/cartesi/image-rootfs/releases/download/v0.18.0/rootfs-v0.18.0.ext2 && \
wget -O ${IMAGES_PATH}/linux.bin https://github.com/cartesi/image-kernel/releases/download/v0.17.0/linux-5.15.63-ctsi-2-v0.17.0.bin && \
wget -O ${IMAGES_PATH}/rom.bin https://github.com/cartesi/machine-emulator-rom/releases/download/v0.17.0/rom-v0.17.0.bin

# Generate machine with echo and store it
ENV SNAPSHOT_DIR=/tmp/dapp-bin
RUN cartesi-machine \
--ram-length=128Mi \
--rollup \
--store=$SNAPSHOT_DIR \
-- "ioctl-echo-loop --vouchers=1 --notices=1 --reports=1 --verbose=1"

FROM cartesi/server-manager:0.8.2 as server-stage

WORKDIR /opt/cartesi/bin
COPY --from=build-server-stage --chown=cartesi:cartesi /tmp/dapp-bin /var/opt/cartesi/machine-snapshots/0_0
RUN ln -s /var/opt/cartesi/machine-snapshots/0_0 /var/opt/cartesi/machine-snapshots/latest
26 changes: 26 additions & 0 deletions devnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# Run Node Devnet infrastrucuture

## Run infrastructure with sample echo DApp

```
docker compose up
```

## Send input

```
INPUT=0x68656C6C6F206E6F6465 ;\
INPUT_BOX_ADDRESS=0x59b22D57D4f067708AB0c00552767405926dc768 ;\
DAPP_ADDRESS=0x70ac08179605AF2D9e75782b8DEcDD3c22aA4D0C ;\
cast send $INPUT_BOX_ADDRESS "addInput(address,bytes)(bytes32)" $DAPP_ADDRESS $INPUT --mnemonic "test test test test test test test test test test test junk" --rpc-url "http://localhost:8545"
```

## query input

Access `http://localhost:4000/graphql` and execute query
```
query{
notices {edges{node{index,payload,input{payload}}}}
}
```
Loading
Loading