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

Build devnet image for testing purposes #204

Merged
merged 3 commits into from
Jan 9, 2024
Merged
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
14 changes: 14 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ jobs:
with:
go-version-file: 'go.mod'

- uses: depot/setup-action@v1

- name: Build and load devnet image
uses: depot/bake-action@v1
with:
files: |
./docker-bake.hcl
./docker-bake.override.hcl
./docker-bake.platforms.hcl
targets: rollups-devnet
project: ${{ vars.DEPOT_PROJECT }}
workdir: build
load: true

- name: Run Go tests
working-directory: ${{ github.workspace }}
run: go test ./...
Expand Down
51 changes: 50 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 ANVIL_IP_ADDR=0.0.0.0

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

#
# Devnet generation stage
#
FROM debian:bookworm-slim as rollups-devnet

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
apt-get install -y --no-install-recommends \
ca-certificates \
curl \
git \
jq \
wget \
xxd

# Install foundry
RUN curl -L https://foundry.paradigm.xyz | bash
ENV PATH="~/.foundry/bin:${PATH}"
RUN bash -c foundryup
RUN ln -s ~/.foundry/bin/anvil /usr/local/bin/anvil

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
ENV ANVIL_IP_ADDR="${ANVIL_IP_ADDR}"
RUN ${RUNTIME_DIR}/scripts/devnet/gen-devnet.sh \
-t 0_0_hash

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

HEALTHCHECK --interval=10s --timeout=1s --retries=5 \
CMD anvil_net_listening.sh

ENTRYPOINT ["anvil"]
CMD ["--block-time", "5", "--load-state" , "/usr/share/devnet/anvil_state.json"]

#
# Cargo chef
#
Expand Down Expand Up @@ -96,7 +145,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
43 changes: 2 additions & 41 deletions build/deps-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,19 @@ 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
- blockchain-data:/usr/share/devnet

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 Down
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", "rollups-devnet"
]
}

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

target "rollups-devnet" {
inherits = ["docker-platforms"]
dockerfile = "./build/Dockerfile"
target = "rollups-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 "rollups-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_FINALITY_OFFSET: "1"
CARTESI_CONTRACTS_DAPP_ADDRESS: "0x70ac08179605AF2D9e75782b8DEcDD3c22aA4D0C"
CARTESI_CONTRACTS_DAPP_ADDRESS: "0x180763470853cAF642Df79a908F9282c61692A45"
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: "0xbCc4067dfdeE8274E531951f0968EC895f397A44"
CARTESI_CONTRACTS_AUTHORITY_ADDRESS: "0xb090149a3cA43000681B74875500854B54355496"
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
18 changes: 9 additions & 9 deletions pkg/addresses/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ type Book struct {
// Get the addresses for the test environment.
func GetTestBook() *Book {
return &Book{
CartesiDAppFactory: common.HexToAddress("0x7122cd1221C20892234186facfE8615e6743Ab02"),
DAppAddressRelay: common.HexToAddress("0xF5DE34d6BbC0446E2a45719E718efEbaaE179daE"),
ERC1155BatchPortal: common.HexToAddress("0xedB53860A6B52bbb7561Ad596416ee9965B055Aa"),
ERC1155SinglePortal: common.HexToAddress("0x7CFB0193Ca87eB6e48056885E026552c3A941FC4"),
ERC20Portal: common.HexToAddress("0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB"),
ERC721Portal: common.HexToAddress("0x237F8DD094C0e47f4236f12b4Fa01d6Dae89fb87"),
EtherPortal: common.HexToAddress("0xFfdbe43d4c855BF7e0f105c400A50857f53AB044"),
InputBox: common.HexToAddress("0x59b22D57D4f067708AB0c00552767405926dc768"),
CartesiDApp: common.HexToAddress("0x70ac08179605AF2D9e75782b8DEcDD3c22aA4D0C"),
CartesiDAppFactory: common.HexToAddress("0x610178dA211FEF7D417bC0e6FeD39F05609AD788"),
DAppAddressRelay: common.HexToAddress("0x8A791620dd6260079BF849Dc5567aDC3F2FdC318"),
ERC1155BatchPortal: common.HexToAddress("0x0165878A594ca255338adfa4d48449f69242Eb8F"),
ERC1155SinglePortal: common.HexToAddress("0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6"),
ERC20Portal: common.HexToAddress("0xa513E6E4b8f2a923D98304ec87F64353C4D5C853"),
ERC721Portal: common.HexToAddress("0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9"),
EtherPortal: common.HexToAddress("0x5FC8d32690cc91D4c39d9d3abcBD16989F875707"),
InputBox: common.HexToAddress("0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9"),
CartesiDApp: common.HexToAddress("0x180763470853cAF642Df79a908F9282c61692A45"),
}
}

Expand Down
9 changes: 1 addition & 8 deletions pkg/ethutil/ethutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,7 @@ func TestEthUtilSuite(t *testing.T) {
// This image starts an anvil node with the Rollups contracts already deployed.
func newDevNetContainer(ctx context.Context) (testcontainers.Container, error) {
req := testcontainers.ContainerRequest{
Image: "sunodo/devnet:1.1.1",
Cmd: []string{
"anvil",
"--block-time",
"1",
"--load-state",
"/usr/share/sunodo/anvil_state.json",
},
Image: "cartesi/rollups-devnet:devel",
Env: map[string]string{
"ANVIL_IP_ADDR": "0.0.0.0",
},
Expand Down
29 changes: 15 additions & 14 deletions scripts/devnet/gen-devnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ ROLLUPS_CONTRACTS_VERSION="1.1.0"
DEVNET_RPC_URL="http://localhost:8545"
DEVNET_FOUNDRY_ACCOUNT_0_ADDRESS="0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
DEVNET_FOUNDRY_ACCOUNT_0_PRIVATE_KEY="0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
# keccak256("salt")
DEVNET_DEFAULT_SALT="0xa05e334153147e75f3f416139b5109d1179cb56fef6a4ecb4c4cbc92a7c37b70"
# Salt is the same as hardhat's create2Salt
# See https://github.com/wighawag/hardhat-deploy/blob/a611466906282969cee601e4f6cd53438fefa2b3/src/helpers.ts#L559
DEVNET_DEFAULT_SALT="0x0000000000000000000000000000000000000000000000000000000000000000"
readonly ROLLUPS_CONTRACTS_VERSION \
DEVNET_RPC_URL \
DEVNET_FOUNDRY_ACCOUNT_0_ADDRESS \
Expand Down Expand Up @@ -70,8 +71,7 @@ finish() {

if [ -n "$work_dir" ]; then
rm -rf "$work_dir"
check_error $? "failed to remove $work_dir"
verbose "removing $work_dir"
check_error $? "failed to remove workdir $work_dir"
fi

if [[ -n "$anvil_pid" ]]; then
Expand Down Expand Up @@ -187,10 +187,10 @@ deploy_relay() {
################################################################################
# Create DApp factories
create_factories() {
local -n dapp_factory_addr="$1"
shift
local -n auth_hist_factory_addr="$1"
shift
local -n dapp_factory_addr="$1"
shift

local contract="src/dapp/CartesiDAppFactory.sol"
local name="CartesiDAppFactory"
Expand Down Expand Up @@ -245,16 +245,16 @@ create_factories() {
create_dapp() {
local -n ret="$1"
shift
factory_addr="$1"
local auth_hist_factory_addr="$1"
shift
dapp_factory_addr="$1"
local dapp_factory_addr="$1"
shift

local addresses
contract_create \
addresses \
block_number \
"$factory_addr" \
"$auth_hist_factory_addr" \
"newAuthorityHistoryPair(address,bytes32)(address,address)" \
"$DEVNET_FOUNDRY_ACCOUNT_0_ADDRESS" \
"$DEVNET_DEFAULT_SALT"
Expand Down Expand Up @@ -325,12 +325,12 @@ trap finish EXIT ERR
log "starting devnet creation"
work_dir=$(mktemp -d)
readonly work_dir
check_error $? "faile to create temp dir"
verbose "created $work_dir"
check_error $? "failed to create temp dir"
verbose "created work dir at $work_dir"

anvil_pid=""
anvil_up \
anvil_pid\
anvil_pid \
"$devnet_anvil_state_file"
check_error $? "failed to start anvil"
log "started anvil (pid=$anvil_pid)"
Expand All @@ -341,13 +341,14 @@ forge_prepare \
log "prepared forge environment"

deploy_libraries

deploy_portals
deploy_relay
log "deployed contracts"

create_factories \
dapp_factory_address \
auth_hist_factory_address
auth_hist_factory_address \
dapp_factory_address
log "created factories"

create_dapp \
Expand Down
29 changes: 13 additions & 16 deletions scripts/devnet/lib/anvil.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
#!/usr/bin/env bash
# (c) Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: Apache-2.0 (see LICENSE)
anvil_lib_dir="$( cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEVNET_ANVIL_STATE_INTERVAL=5
DEVNET_ANVIL_TIMEOUT=$(expr $DEVNET_ANVIL_STATE_INTERVAL + 10)
readonly anvil_lib_dir DEVNET_ANVIL_STATE_INTERVAL DEVNET_ANVIL_TIMEOUT

readonly DEVNET_ANVIL_IP="0.0.0.0"
readonly DEVNET_ANVIL_TIMEOUT=3
ANVIL_IP_ADDR=${ANVIL_IP_ADDR:-"0.0.0.0"}

_is_anvil_up() {
local -n ready=$1

local result="$(
curl -X \
POST \
-s \
-H 'Content-Type: application/json' \
-d '{"jsonrpc":"2.0","id":"1","method":"net_listening","params":[]}' \
"http://$DEVNET_ANVIL_IP:8545"
)"
local result
result=$("$anvil_lib_dir"/anvil_net_listening.sh)

ready="false"
if [[ -n "$result" ]]; then
Expand All @@ -36,13 +33,14 @@ anvil_up() {
fi

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

sleep "$DEVNET_ANVIL_TIMEOUT"
# check if anvil is up
sleep $DEVNET_ANVIL_TIMEOUT
_is_anvil_up is_up
if [[ "$is_up" != "true" ]]; then
err "anvil has not started"
Expand All @@ -55,9 +53,8 @@ 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"
wait "$anvil_pid"
check_error "$?" "failed to kill anvil"
verbose "killed anvil (pid=$anvil_pid)"
}
Loading