Skip to content

Commit

Permalink
feat: build rollups-devnet image
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelstanley committed Dec 14, 2023
1 parent 0dd4bdb commit 3ba3649
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 65 deletions.
48 changes: 47 additions & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ ARG GO_BUILD_PATH=${GO_BASE_PATH}/cartesi-rollups-node
ARG GO_BIN_PATH=${GO_BASE_PATH}/bin
ARG RUNTIME_DIR=/var/opt/cartesi
ARG MACHINE_SNAPSHOT_PATH=/var/opt/cartesi/machine-snapshots
ARG DEVNET_SHARE_PATH=/usr/share/devnet
ARG DEVNET_ANVIL_TIMEOUT=10

#
# Machine snapshot generation
Expand Down Expand Up @@ -54,6 +56,50 @@ RUN ln -s ${MACHINE_SNAPSHOT_PATH}/0_0 ${MACHINE_SNAPSHOT_PATH}/latest
WORKDIR ${MACHINE_SNAPSHOT_PATH}
CMD [ "/bin/bash" ]

#
# Create devnet for local tests
#
FROM ghcr.io/foundry-rs/foundry:latest as devnet

RUN apk add --no-cache \
bash \
curl \
jq

ARG RUNTIME_DIR
WORKDIR ${RUNTIME_DIR}
COPY scripts scripts

ARG DEVNET_SHARE_PATH
WORKDIR ${DEVNET_SHARE_PATH}

# Copy machine snapshot hash
ARG MACHINE_SNAPSHOT_PATH
COPY --from=machine-snapshot ${MACHINE_SNAPSHOT_PATH}/0_0/hash 0_0_hash

# Generate devnet
ARG DEVNET_ANVIL_TIMEOUT
ENV DEVNET_ANVIL_TIMEOUT=${DEVNET_ANVIL_TIMEOUT}
RUN ${RUNTIME_DIR}/scripts/devnet/gen-devnet.sh \
-t 0_0_hash

# Install utilities
RUN ln -s ${RUNTIME_DIR}/scripts/devnet/cmd/anvil_net_listening.sh \
/usr/local/bin/anvil_net_listening.sh
RUN ln -s ${RUNTIME_DIR}/scripts/devnet/cmd/anvil_up.sh \
/usr/local/bin/anvil_up.sh

HEALTHCHECK --start-period=20s --interval=10s --timeout=1s --retries=5 \
CMD anvil_net_listening.sh
#CMD ${RUNTIME_DIR}/scripts/devnet/cmd/anvil_net_listening.sh

ENTRYPOINT ["anvil_up.sh"]}
#CMD ["/usr/share/devnet/anvil_state.hex"]

#CMD ["anvil", "--block-time", "5"]
#CMD ${RUNTIME_DIR}/scripts/devnet/cmd/anvil_loadState.sh \
# "${DEVNET_SHARE_PATH}/anvil_state.hex"

#
# Cargo chef
#
Expand Down Expand Up @@ -90,7 +136,7 @@ RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY ./offchain/ .
# needed to log commit hash in runtime
COPY ./.git ../
COPY ./.git ../
RUN cargo build --release

#
Expand Down
44 changes: 1 addition & 43 deletions build/deps-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,17 @@ version: "3.9"
name: rollups-node
services:
devnet:
image: sunodo/devnet:1.1.1
command:
[
"anvil",
"--block-time",
"${BLOCK_TIME:-5}",
"--load-state",
"/usr/share/sunodo/anvil_state.json",
]
image: cartesi/rollups-devnet:devel
ports:
- 8545:8545
healthcheck:
test: ["CMD", "eth_isready"]
interval: 10s
timeout: 1s
retries: 5
environment:
ANVIL_IP_ADDR: 0.0.0.0
volumes:
- blockchain-data:/usr/share/sunodo

machine_snapshot_setup:
image: cartesi/rollups-machine-snapshot:devel
volumes:
- machine:/var/opt/cartesi/machine-snapshots

dapp_deployer:
image: cartesi/rollups-cli:1.0.2
restart: on-failure
depends_on:
devnet:
condition: service_started
machine_snapshot_setup:
condition: service_completed_successfully
command:
[
"create",
"--rpc",
"http://devnet:8545",
"--deploymentFile",
"/usr/share/sunodo/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",
"/usr/share/sunodo/dapp.json",
]
volumes:
- machine:/var/opt/cartesi/machine-snapshots:ro
- blockchain-data:/usr/share/sunodo

database:
image: postgres:13-alpine
ports:
Expand All @@ -68,5 +27,4 @@ services:
- POSTGRES_PASSWORD=password

volumes:
blockchain-data: {}
machine: {}
9 changes: 8 additions & 1 deletion build/docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ target "docker-platforms" {}

group "default" {
targets = [
"rollups-node", "machine-snapshot"
"rollups-node", "machine-snapshot", "devnet"
]
}

Expand All @@ -23,3 +23,10 @@ target "machine-snapshot" {
target = "machine-snapshot"
context = ".."
}

target "devnet" {
inherits = ["docker-platforms"]
dockerfile = "./build/Dockerfile"
target = "devnet"
context = ".."
}
4 changes: 4 additions & 0 deletions build/docker-bake.override.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ target "rollups-node" {
target "machine-snapshot" {
tags = ["${DOCKER_ORGANIZATION}/rollups-machine-snapshot:${TAG}"]
}

target "devnet" {
tags = ["${DOCKER_ORGANIZATION}/rollups-devnet:${TAG}"]
}
10 changes: 4 additions & 6 deletions build/node-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ services:
depends_on:
devnet:
condition: service_healthy
dapp_deployer:
condition: service_completed_successfully
machine_snapshot_setup:
condition: service_completed_successfully
database:
Expand All @@ -29,11 +27,11 @@ services:
CARTESI_BLOCKCHAIN_IS_LEGACY: "false"
CARTESI_BLOCKCHAIN_GENESIS_BLOCK: "1"
CARTESI_BLOCKCHAIN_READ_DEPTH: "1"
CARTESI_CONTRACTS_DAPP_ADDRESS: "0x70ac08179605AF2D9e75782b8DEcDD3c22aA4D0C"
CARTESI_CONTRACTS_DAPP_ADDRESS: "0x4e5BE025E498EF85FfAD28124Ffc74fE8ad10c4c"
CARTESI_CONTRACTS_DAPP_DEPLOYMENT_BLOCK_NUMBER: "1"
CARTESI_CONTRACTS_HISTORY_ADDRESS: "0x4FF8BD9122b7D91d56Dd5c88FE6891Fb3c0b5281"
CARTESI_CONTRACTS_AUTHORITY_ADDRESS: "0x5050F233F2312B1636eb7CF6c7876D9cC6ac4785"
CARTESI_CONTRACTS_INPUT_BOX_ADDRESS: "0x59b22D57D4f067708AB0c00552767405926dc768"
CARTESI_CONTRACTS_HISTORY_ADDRESS: "0x35861Aa3cf766C8E9b83924e1Ad6053b4039d6d6"
CARTESI_CONTRACTS_AUTHORITY_ADDRESS: "0xc2A807938914398ccD5F8Df2d36fD64C9F78B3C0"
CARTESI_CONTRACTS_INPUT_BOX_ADDRESS: "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9"
CARTESI_SNAPSHOT_DIR: "/var/opt/cartesi/machine-snapshots"
CARTESI_AUTH_MNEMONIC: "test test test test test test test test test test test junk"
CARTESI_POSTGRES_ENDPOINT: "postgres://postgres:password@database:5432/postgres"
Expand Down
13 changes: 11 additions & 2 deletions scripts/devnet/cmd/anvil_up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
# SPDX-License-Identifier: Apache-2.0 (see LICENSE)
ANVIL_IP_ADDR=${ANVIL_IP_ADDR:-"0.0.0.0"}

hex_state_file="$1"
#hex_state_file="$1"
hex_state_file="/usr/share/devnet/anvil_state.hex"

anvil \
nohup anvil \
--block-time 5 &

timeout=5
sleep "$timeout"
hex_state_str="\"$(cat $hex_state_file)\""
curl -X \
POST \
-s \
-H 'Content-Type: application/json' \
-d "{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"method\":\"anvil_loadState\",\"params\":[${hex_state_str}]}" \
"http://$ANVIL_IP_ADDR:8545"

while true
do
# loop infinitely
sleep 5
done
7 changes: 4 additions & 3 deletions scripts/devnet/gen-devnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DEVNET_FOUNDRY_ACCOUNT_0_ADDRESS="0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
DEVNET_FOUNDRY_ACCOUNT_0_PRIVATE_KEY="0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
# keccak256("salt")
DEVNET_DEFAULT_SALT="0xa05e334153147e75f3f416139b5109d1179cb56fef6a4ecb4c4cbc92a7c37b70"
DEVNET_ANVIL_TIMEOUT=5
DEVNET_ANVIL_TIMEOUT=${DEVNET_ANVIL_TIMEOUT:-5}
readonly ROLLUPS_CONTRACTS_VERSION \
DEVNET_RPC_URL \
DEVNET_FOUNDRY_ACCOUNT_0_ADDRESS \
Expand Down Expand Up @@ -339,8 +339,7 @@ verbose "created $work_dir"

anvil_pid=""
anvil_up \
anvil_pid\
"$devnet_anvil_state_file"
anvil_pid
check_error $? "failed to start anvil"
log "started anvil (pid=$anvil_pid)"

Expand All @@ -365,6 +364,8 @@ create_dapp \
"$dapp_factory_address"
log "created CartesiDApp"

anvil_dump_state \
"$devnet_anvil_state_file"
generate_deployment_file \
"$devnet_deployment_file"

Expand Down
14 changes: 5 additions & 9 deletions scripts/devnet/lib/anvil.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ lib_dir="$( cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
_is_anvil_up() {
local -n ready=$1

local result=$(. $lib_dir/../cmd/anvil_net_listening.sh)
local result
result=$("$lib_dir"/../cmd/anvil_net_listening.sh)

ready="false"
if [[ -n "$result" ]]; then
Expand All @@ -18,8 +19,6 @@ _is_anvil_up() {

anvil_up() {
local -n ret="$1"
shift
local anvil_state_file="$1"

# check if there's another instance of anvil up listening on the same port
_is_anvil_up is_up
Expand All @@ -29,13 +28,12 @@ anvil_up() {
fi

anvil \
--host $ANVIL_IP_ADDR \
--dump-state "$anvil_state_file" \
--host "$ANVIL_IP_ADDR" \
> /dev/null &
local pid=$!

# check if anvil is up
sleep $DEVNET_ANVIL_TIMEOUT
sleep "$DEVNET_ANVIL_TIMEOUT"
_is_anvil_up is_up
if [[ "$is_up" != "true" ]]; then
err "anvil has not started"
Expand All @@ -48,9 +46,7 @@ anvil_up() {
anvil_down() {
local anvil_pid="$1"

verbose "waiting $DEVNET_ANVIL_TIMEOUT seconds before killing anvil..."
sleep $DEVNET_ANVIL_TIMEOUT
kill "$anvil_pid"
check_error $? "failed to kill anvil"
check_error "$?" "failed to kill anvil"
verbose "killed anvil (pid=$anvil_pid)"
}

0 comments on commit 3ba3649

Please sign in to comment.