Skip to content

Commit

Permalink
ibcsim-bcd
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Dec 11, 2024
1 parent b6bbc02 commit 97e2741
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions contrib/images/Makefile
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions contrib/images/ibcsim-bcd/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
67 changes: 67 additions & 0 deletions contrib/images/ibcsim-bcd/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions contrib/images/ibcsim-bcd/pre-deployment.sh
Original file line number Diff line number Diff line change
@@ -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."
128 changes: 128 additions & 0 deletions contrib/images/ibcsim-bcd/setup-bcd.sh
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 97e2741

Please sign in to comment.