From 97e2741d0f6b1901eddd5c6ee6c67e8ae28ee9b6 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Wed, 11 Dec 2024 12:09:32 +1100 Subject: [PATCH] ibcsim-bcd --- Makefile | 7 + contrib/images/Makefile | 17 +++ contrib/images/ibcsim-bcd/Dockerfile | 66 ++++++++++ contrib/images/ibcsim-bcd/docker-compose.yml | 67 ++++++++++ contrib/images/ibcsim-bcd/pre-deployment.sh | 22 ++++ contrib/images/ibcsim-bcd/setup-bcd.sh | 128 +++++++++++++++++++ contrib/images/ibcsim-bcd/wrapper.sh | 101 +++++++++++++++ 7 files changed, 408 insertions(+) create mode 100644 contrib/images/Makefile create mode 100644 contrib/images/ibcsim-bcd/Dockerfile create mode 100644 contrib/images/ibcsim-bcd/docker-compose.yml create mode 100755 contrib/images/ibcsim-bcd/pre-deployment.sh create mode 100755 contrib/images/ibcsim-bcd/setup-bcd.sh create mode 100755 contrib/images/ibcsim-bcd/wrapper.sh diff --git a/Makefile b/Makefile index f7b1cf5..c5eed45 100644 --- a/Makefile +++ b/Makefile @@ -78,3 +78,10 @@ proto-lint: .PHONY: all install \ build build-linux-static test test-all test-e2e \ proto-all proto-format proto-swagger-gen proto-lint + +############################################################################### +### Images ### +############################################################################### + +build-ibcsim-bcd: + $(MAKE) -C contrib/images build-ibcsim-bcd diff --git a/contrib/images/Makefile b/contrib/images/Makefile new file mode 100644 index 0000000..c3c0d1a --- /dev/null +++ b/contrib/images/Makefile @@ -0,0 +1,17 @@ +BABYLON_SDK_FULL_PATH := $(shell git rev-parse --show-toplevel) + +build-ibcsim-bcd: + docker build --tag babylonlabs-io/ibcsim-bcd -f ibcsim-bcd/Dockerfile ${BABYLON_SDK_FULL_PATH} + +ibcsim-bcd-rmi: + docker rmi babylonlabs-io/ibcsim-bcd 2>/dev/null; true + +start-bcd-consumer-integration: stop-bcd-consumer-integration + ibcsim-bcd/pre-deployment.sh + docker compose -f ibcsim-bcd/docker-compose.yml up -d + +stop-bcd-consumer-integration: + docker compose -f ibcsim-bcd/docker-compose.yml down + rm -rf ibcsim-bcd/.testnets + +.PHONY: build-ibcsim-bcd ibcsim-bcd-rmi start-bcd-consumer-integration stop-bcd-consumer-integration diff --git a/contrib/images/ibcsim-bcd/Dockerfile b/contrib/images/ibcsim-bcd/Dockerfile new file mode 100644 index 0000000..3945a77 --- /dev/null +++ b/contrib/images/ibcsim-bcd/Dockerfile @@ -0,0 +1,66 @@ +FROM debian:bullseye-slim AS build-env + +RUN apt-get update && apt-get install -y git make gcc wget + +WORKDIR /work + +ARG TARGETARCH + +# Download and install Go +ENV GOLANG_VERSION 1.21.4 +RUN wget -q https://golang.org/dl/go${GOLANG_VERSION}.linux-${TARGETARCH}.tar.gz && \ + tar -C /usr/local -xzf go${GOLANG_VERSION}.linux-${TARGETARCH}.tar.gz && \ + rm go${GOLANG_VERSION}.linux-${TARGETARCH}.tar.gz +# Set Go environment variables +ENV PATH /usr/local/go/bin:$PATH +ENV GOPATH /go +ENV PATH $GOPATH/bin:$PATH + +ENV GO111MODULE on +ENV RELAYER_TAG v2.5.2 + +# Install the relayer +RUN git clone https://github.com/cosmos/relayer.git +RUN cd relayer && git fetch origin && git checkout ${RELAYER_TAG} && make install && cd - + +# build bcd +COPY . /work/babylon-sdk +RUN cd babylon-sdk && \ + make build && \ + cd - + +FROM debian:bullseye-slim AS run + +RUN apt-get update && apt-get install -y bash curl jq wget + +# Install libraries +# Cosmwasm - Download correct libwasmvm version +COPY --from=build-env /work/babylon-sdk/demo/go.mod /tmp +RUN WASMVM_VERSION=$(grep github.com/CosmWasm/wasmvm /tmp/go.mod | cut -d' ' -f2) && \ + wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm.$(uname -m).so \ + -O /lib/libwasmvm.$(uname -m).so && \ + # verify checksum + wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/checksums.txt -O /tmp/checksums.txt && \ + sha256sum /lib/libwasmvm.$(uname -m).so | grep $(cat /tmp/checksums.txt | grep libwasmvm.$(uname -m) | cut -d ' ' -f 1) +RUN rm -f /tmp/go.mod + +# Copy binaries +COPY --from=build-env /go/bin/rly /usr/bin/rly +COPY --from=build-env /work/babylon-sdk/demo/build/bcd /usr/bin/bcd + +WORKDIR /ibcsim-bcd +COPY contrib/images/ibcsim-bcd/wrapper.sh /ibcsim-bcd/wrapper.sh +COPY contrib/images/ibcsim-bcd/setup-bcd.sh /ibcsim-bcd/setup-bcd.sh +COPY --from=build-env /work/babylon-sdk/tests/testdata/babylon_contract.wasm /ibcsim-bcd/babylon_contract.wasm +COPY --from=build-env /work/babylon-sdk/tests/testdata/btc_staking.wasm /ibcsim-bcd/btc_staking.wasm +COPY --from=build-env /work/babylon-sdk/tests/testdata/btc_finality.wasm /ibcsim-bcd/btc_finality.wasm + +ENV BABYLON_HOME=/data/node1/babylond +ENV BABYLON_NODE_RPC="http://babylondnode1:26657" +ENV RELAYER_CONF_DIR=/data/relayer +ENV CONSUMER_CONF=/data/bcd +ENV UPDATE_CLIENTS_INTERVAL=20s + +ENTRYPOINT ["/ibcsim-bcd/wrapper.sh"] +CMD [] +STOPSIGNAL SIGTERM diff --git a/contrib/images/ibcsim-bcd/docker-compose.yml b/contrib/images/ibcsim-bcd/docker-compose.yml new file mode 100644 index 0000000..5ec84ac --- /dev/null +++ b/contrib/images/ibcsim-bcd/docker-compose.yml @@ -0,0 +1,67 @@ +services: + babylondnode0: + container_name: babylondnode0 + image: "babylonlabs-io/babylond" + command: > + babylond --home /babylondhome start --log_level trace --trace --log_format 'plain' 2>&1 | tee /babylondhome/babylond.log + cap_add: + - SYS_PTRACE + security_opt: + - seccomp:unconfined + ports: + - "26656-26657:26656-26657" + - "1317:1317" + - "9090:9090" + - "2345:2345" + volumes: + - .testnets/node0/babylond:/babylondhome:Z + networks: + localnet: + ipv4_address: 192.168.10.2 + + babylondnode1: + container_name: babylondnode1 + image: "babylonlabs-io/babylond" + command: > + babylond --home /babylondhome start --log_level trace --trace --log_format 'plain' 2>&1 | tee /babylondhome/babylond.log + cap_add: + - SYS_PTRACE + security_opt: + - seccomp:unconfined + ports: + - "26666-26667:26656-26657" + - "1318:1317" + - "9091:9090" + - "2346:2345" + volumes: + - .testnets/node1/babylond:/babylondhome:Z + networks: + localnet: + ipv4_address: 192.168.10.3 + + ibcsim-bcd: + container_name: ibcsim-bcd + image: babylonlabs-io/ibcsim-bcd + ports: + - "5183:5183" + - "26676-26677:26656-26657" + - "1319:1317" + - "9092:9090" + - "2347:2345" + volumes: + - .testnets:/data:Z + networks: + localnet: + ipv4_address: 192.168.10.17 + depends_on: + - babylondnode0 + - babylondnode1 + restart: unless-stopped + +networks: + localnet: + driver: bridge + ipam: + driver: default + config: + - subnet: 192.168.10.0/25 diff --git a/contrib/images/ibcsim-bcd/pre-deployment.sh b/contrib/images/ibcsim-bcd/pre-deployment.sh new file mode 100755 index 0000000..89a0750 --- /dev/null +++ b/contrib/images/ibcsim-bcd/pre-deployment.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +# Create new directory that will hold node and services' configuration +mkdir -p ibcsim-bcd/.testnets && chmod -R 777 ibcsim-bcd/.testnets +echo "Creating and configuring testnet directory..." +docker run --rm -v $(pwd)/ibcsim-bcd/.testnets:/data babylonlabs-io/babylond \ + babylond testnet init-files --v 2 -o /data \ + --starting-ip-address 192.168.10.2 --keyring-backend=test \ + --chain-id chain-test --epoch-interval 10 \ + --btc-finalization-timeout 2 --btc-confirmation-depth 1 \ + --minimum-gas-prices 0.000006ubbn \ + --btc-base-header 0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000 \ + --btc-network regtest --additional-sender-account \ + --slashing-pk-script "76a914010101010101010101010101010101010101010188ab" \ + --slashing-rate 0.1 \ + --min-commission-rate 0.05 \ + --covenant-quorum 1 \ + --covenant-pks "bb50e2d89a4ed70663d080659fe0ad4b9bc3e06c17a227433966cb59ceee020d" # should be updated if `covenant-keyring` dir is changed` + +# Create separate subpaths for each component and copy relevant configuration +chmod -R 777 ibcsim-bcd/.testnets +echo "Testnet directory created and configured successfully." diff --git a/contrib/images/ibcsim-bcd/setup-bcd.sh b/contrib/images/ibcsim-bcd/setup-bcd.sh new file mode 100755 index 0000000..4780181 --- /dev/null +++ b/contrib/images/ibcsim-bcd/setup-bcd.sh @@ -0,0 +1,128 @@ +#!/bin/bash + +display_usage() { + echo "Missing parameters. Please check if all parameters were specified." + echo "Usage: setup-bcd.sh [CHAIN_ID] [CHAIN_DIR] [RPC_PORT] [P2P_PORT] [PROFILING_PORT] [GRPC_PORT] [BABYLON_CONTRACT_CODE_FILE] [BTCSTAKING_CONTRACT_CODE_FILE] [BTCFINALITY_CONTRACT_CODE_FILE] [INSTANTIATING_CFG]" + echo "Example: setup-bcd.sh test-chain-id ./data 26657 26656 6060 9090 ./babylon_contract.wasm '{"btc_confirmation_depth":1,"checkpoint_finalization_timeout":2,"network":"Regtest","babylon_tag":"bbn0", "notify_cosmos_zone":false, "btc_staking_code_id":2}'" + exit 1 +} + +BINARY=bcd +DENOM=stake +BASEDENOM=ustake +KEYRING=--keyring-backend="test" +SILENT=1 + +redirect() { + if [ "$SILENT" -eq 1 ]; then + "$@" >/dev/null 2>&1 + else + "$@" + fi +} + +if [ "$#" -lt "9" ]; then + display_usage + exit 1 +fi + +CHAINID=$1 +CHAINDIR=$2 +RPCPORT=$3 +P2PPORT=$4 +PROFPORT=$5 +GRPCPORT=$6 +BABYLON_CONTRACT_CODE_FILE=$7 +BTCSTAKING_CONTRACT_CODE_FILE=$8 +BTCFINALITY_CONTRACT_CODE_FILE=$9 +INSTANTIATING_CFG=${10} + +# ensure the binary exists +if ! command -v $BINARY &>/dev/null; then + echo "$BINARY could not be found" + exit +fi + +# Delete chain data from old runs +echo "Deleting $CHAINDIR/$CHAINID folders..." +rm -rf $CHAINDIR/$CHAINID &>/dev/null +rm $CHAINDIR/$CHAINID.log &>/dev/null + +echo "Creating $BINARY instance: home=$CHAINDIR | chain-id=$CHAINID | p2p=:$P2PPORT | rpc=:$RPCPORT | profiling=:$PROFPORT | grpc=:$GRPCPORT" + +# Add dir for chain, exit if error +if ! mkdir -p $CHAINDIR/$CHAINID 2>/dev/null; then + echo "Failed to create chain folder. Aborting..." + exit 1 +fi +# Build genesis file incl account for passed address +coins="100000000000$DENOM,100000000000$BASEDENOM" +delegate="100000000000$DENOM" + +redirect $BINARY --home $CHAINDIR/$CHAINID --chain-id $CHAINID init $CHAINID +sleep 1 +$BINARY --home $CHAINDIR/$CHAINID keys add validator $KEYRING --output json > $CHAINDIR/$CHAINID/validator_seed.json 2>&1 +sleep 1 +$BINARY --home $CHAINDIR/$CHAINID keys add user $KEYRING --output json > $CHAINDIR/$CHAINID/key_seed.json 2>&1 +sleep 1 +redirect $BINARY --home $CHAINDIR/$CHAINID genesis add-genesis-account $($BINARY --home $CHAINDIR/$CHAINID keys $KEYRING show user -a) $coins +sleep 1 +redirect $BINARY --home $CHAINDIR/$CHAINID genesis add-genesis-account $($BINARY --home $CHAINDIR/$CHAINID keys $KEYRING show validator -a) $coins +sleep 1 +redirect $BINARY --home $CHAINDIR/$CHAINID genesis gentx validator $delegate $KEYRING --chain-id $CHAINID +sleep 1 +redirect $BINARY --home $CHAINDIR/$CHAINID genesis collect-gentxs +sleep 1 + +# Set proper defaults and change ports +echo "Change settings in config.toml and genesis.json files..." +sed -i 's#"tcp://127.0.0.1:26657"#"tcp://0.0.0.0:'"$RPCPORT"'"#g' $CHAINDIR/$CHAINID/config/config.toml +sed -i 's#"tcp://0.0.0.0:26656"#"tcp://0.0.0.0:'"$P2PPORT"'"#g' $CHAINDIR/$CHAINID/config/config.toml +sed -i 's#"localhost:6060"#"localhost:'"$PROFPORT"'"#g' $CHAINDIR/$CHAINID/config/config.toml +sed -i 's/timeout_commit = "5s"/timeout_commit = "1s"/g' $CHAINDIR/$CHAINID/config/config.toml +sed -i 's/max_body_bytes = 1000000/max_body_bytes = 1000000000/g' $CHAINDIR/$CHAINID/config/config.toml +sed -i 's/minimum-gas-prices = ""/minimum-gas-prices = "0.00001ustake"/g' $CHAINDIR/$CHAINID/config/app.toml +sed -i 's/timeout_propose = "3s"/timeout_propose = "1s"/g' $CHAINDIR/$CHAINID/config/config.toml +sed -i 's/index_all_keys = false/index_all_keys = true/g' $CHAINDIR/$CHAINID/config/config.toml +sed -i 's#"tcp://0.0.0.0:1317"#"tcp://0.0.0.0:1318"#g' $CHAINDIR/$CHAINID/config/app.toml # ensure port is not conflicted with Babylon +sed -i 's/"bond_denom": "stake"/"bond_denom": "'"$DENOM"'"/g' $CHAINDIR/$CHAINID/config/genesis.json +# sed -i '' 's#index-events = \[\]#index-events = \["message.action","send_packet.packet_src_channel","send_packet.packet_sequence"\]#g' $CHAINDIR/$CHAINID/config/app.toml + +## Script for getting contract addresses +## TODO(euphrates): pass a gov prop on setting the Babylon / BTC staking contract addresses +# babylonContractAddr=$(bcd query wasm list-contract-by-code 1 -o json | jq -r '.contracts[0]') +# btcStakingContractAddr=$(bcd query wasm list-contract-by-code 2 -o json | jq -r '.contracts[0]') +# echo "babylonContractAddr is $babylonContractAddr" +# echo "btcStakingContractAddr is $btcStakingContractAddr" + +# update contract address in genesis +babylonContractAddr=bbnc14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9syx25zf +btcStakingContractAddr=bbnc1nc5tatafv6eyq7llkr2gv50ff9e22mnf70qgjlv737ktmt4eswrqgn0kq0 +btcFinalityContractAddr=bbnc17p9rzwnnfxcjp32un9ug7yhhzgtkhvl9jfksztgw5uh69wac2pgssg3nft +sed -i 's/"babylon_contract_address": ""/"babylon_contract_address": "'"$babylonContractAddr"'"/g' $CHAINDIR/$CHAINID/config/genesis.json +sed -i 's/"btc_staking_contract_address": ""/"btc_staking_contract_address": "'"$btcStakingContractAddr"'"/g' $CHAINDIR/$CHAINID/config/genesis.json +sed -i 's/"btc_finality_contract_address": ""/"btc_finality_contract_address": "'"$btcFinalityContractAddr"'"/g' $CHAINDIR/$CHAINID/config/genesis.json + +# Start +echo "Starting $BINARY..." +$BINARY --home $CHAINDIR/$CHAINID start --pruning=nothing --grpc-web.enable=false --grpc.address="0.0.0.0:$GRPCPORT" --log_level trace --trace --log_format 'plain' 2>&1 | tee $CHAINDIR/$CHAINID.log & +sleep 20 + +# upload contract code +echo "Uploading babylon contract code $BABYLON_CONTRACT_CODE_FILE..." +$BINARY --home $CHAINDIR/$CHAINID tx wasm store "$BABYLON_CONTRACT_CODE_FILE" $KEYRING --from user --chain-id $CHAINID --gas 20000000000 --gas-prices 0.01ustake --node http://localhost:$RPCPORT -y +sleep 10 + +# upload contract code +echo "Uploading btcstaking contract code $BTCSTAKING_CONTRACT_CODE_FILE..." +$BINARY --home $CHAINDIR/$CHAINID tx wasm store "$BTCSTAKING_CONTRACT_CODE_FILE" $KEYRING --from user --chain-id $CHAINID --gas 20000000000 --gas-prices 0.01ustake --node http://localhost:$RPCPORT -y +sleep 10 + +# upload contract code +echo "Uploading btcfinality contract code $BTCFINALITY_CONTRACT_CODE_FILE..." +$BINARY --home $CHAINDIR/$CHAINID tx wasm store "$BTCFINALITY_CONTRACT_CODE_FILE" $KEYRING --from user --chain-id $CHAINID --gas 20000000000 --gas-prices 0.01ustake --node http://localhost:$RPCPORT -y +sleep 10 + +# Echo the command with expanded variables +echo "Instantiating contract $BABYLON_CONTRACT_CODE_FILE..." +$BINARY --home $CHAINDIR/$CHAINID tx wasm instantiate 1 "$INSTANTIATING_CFG" --admin=$(bcd --home $CHAINDIR/$CHAINID keys show user --keyring-backend test -a) --label "v0.0.1" $KEYRING --from user --chain-id $CHAINID --gas 20000000000 --gas-prices 0.001ustake --node http://localhost:$RPCPORT -y --amount 100000stake diff --git a/contrib/images/ibcsim-bcd/wrapper.sh b/contrib/images/ibcsim-bcd/wrapper.sh new file mode 100755 index 0000000..2ebd636 --- /dev/null +++ b/contrib/images/ibcsim-bcd/wrapper.sh @@ -0,0 +1,101 @@ +#!/usr/bin/env sh + +# 0. Define configuration +BABYLON_KEY="babylon-key" +BABYLON_CHAIN_ID="chain-test" +CONSUMER_KEY="bcd-key" +CONSUMER_CHAIN_ID="bcd-test" + +# 1. Create a bcd testnet with Babylon contract +./setup-bcd.sh $CONSUMER_CHAIN_ID $CONSUMER_CONF 26657 26656 6060 9090 ./babylon_contract.wasm ./btc_staking.wasm ./btc_finality.wasm '{ + "network": "regtest", + "babylon_tag": "01020304", + "btc_confirmation_depth": 1, + "checkpoint_finalization_timeout": 2, + "notify_cosmos_zone": false, + "btc_staking_code_id": 2, + "consumer_name": "Test Consumer", + "consumer_description": "Test Consumer Description", + "btc_finality_code_id": 3 +}' + +sleep 10 + +CONTRACT_ADDRESS=$(bcd query wasm list-contract-by-code 1 | grep bbnc | cut -d' ' -f2) +CONTRACT_PORT="wasm.$CONTRACT_ADDRESS" +echo "bcd started. Status of bcd node:" +bcd status +echo "Contract port: $CONTRACT_PORT" + +# 2. Set up the relayer +mkdir -p $RELAYER_CONF_DIR +rly --home $RELAYER_CONF_DIR config init +RELAYER_CONF=$RELAYER_CONF_DIR/config/config.yaml + +cat <$RELAYER_CONF +global: + api-listen-addr: :5183 + max-retries: 20 + timeout: 20s + memo: "" + light-cache-size: 10 +chains: + babylon: + type: cosmos + value: + key: $BABYLON_KEY + chain-id: $BABYLON_CHAIN_ID + rpc-addr: $BABYLON_NODE_RPC + account-prefix: bbn + keyring-backend: test + gas-adjustment: 1.5 + gas-prices: 0.002ubbn + min-gas-amount: 1 + debug: true + timeout: 10s + output-format: json + sign-mode: direct + extra-codecs: [] + bcd: + type: cosmos + value: + key: $CONSUMER_KEY + chain-id: $CONSUMER_CHAIN_ID + rpc-addr: http://localhost:26657 + account-prefix: bbnc + keyring-backend: test + gas-adjustment: 1.5 + gas-prices: 0.002ustake + min-gas-amount: 1 + debug: true + timeout: 10s + output-format: json + sign-mode: direct + extra-codecs: [] +paths: + bcd: + src: + chain-id: $BABYLON_CHAIN_ID + dst: + chain-id: $CONSUMER_CHAIN_ID +EOT + +echo "Inserting the consumer key" +CONSUMER_MEMO=$(cat $CONSUMER_CONF/$CONSUMER_CHAIN_ID/key_seed.json | jq .mnemonic | tr -d '"') +rly --home $RELAYER_CONF_DIR keys restore bcd $CONSUMER_KEY "$CONSUMER_MEMO" + +echo "Inserting the babylond key" +BABYLON_MEMO=$(cat $BABYLON_HOME/key_seed.json | jq .secret | tr -d '"') +rly --home $RELAYER_CONF_DIR keys restore babylon $BABYLON_KEY "$BABYLON_MEMO" + +sleep 10 + +# 3. Start relayer +echo "Creating an IBC light clients, connection, and channel between the two CZs" +rly --home $RELAYER_CONF_DIR tx link bcd --src-port zoneconcierge --dst-port $CONTRACT_PORT --order ordered --version zoneconcierge-1 +echo "Created IBC channel successfully!" + +sleep 10 + +echo "Start the IBC relayer" +rly --home $RELAYER_CONF_DIR start bcd --debug-addr "" --flush-interval 30s