Skip to content

Commit

Permalink
Merge branch 'feat/fix-babylon-deployment' into feat/ci-workflow-bitc…
Browse files Browse the repository at this point in the history
…oinsim
  • Loading branch information
lesterli committed Oct 19, 2024
2 parents 46e6e39 + da96793 commit a6c9a2e
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 93 deletions.
4 changes: 2 additions & 2 deletions deployments/finality-gadget-integration-op-l2/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### Bitcoin
BITCOIN_NETWORK=testnet
BITCOIN_NETWORK=signet
BITCOIN_RPC_PORT=18332
WALLET_PASS=walletpass
# btcstaker private key in WIF format
Expand All @@ -8,7 +8,7 @@ SLASHING_PK_SCRIPT=76a914010101010101010101010101010101010101010188ab

### Babylon
BABYLON_HOME_DIR=/babylondhome
TEST_SPENDING_KEY_NAME=test-spending-key
TEST_SPENDING_KEY_NAME=test-spending-key # this is hardcoded in the babylon repo
BABYLON_CHAIN_ID=chain-test

### CW contract
Expand Down
1 change: 1 addition & 0 deletions deployments/finality-gadget-integration-op-l2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ make stop-deployment-finality-gadget-integration-op-l2
1. `BASE_HEADER_HEIGHT`, initialization for the babylon network, it should use the latest Bitcoin difficulty adjustment height
2. `btc-cache-size`, in the Vigilante reporter, it should not be less than the Bitcoin difficulty epoch 2016
3. `btc-confirmation-depth`, in the Vigilante moniter, it should not be less than 6
4. `min-staking-amount-sat`, in the init-babylon-accounts.sh, it should not be less than 10000 (same as Euphrates v0.5.0)
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ monitor:
checkpoint-buffer-size: 1000
btc-block-buffer-size: 1000
btc-cache-size: 2018
btc-confirmation-depth: 6
btc-confirmation-depth: 6 # it cannot be less than 6
liveness-check-interval-seconds: 100
max-live-btc-heights: 200
enable-liveness-checker: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
set -euo pipefail

# For signet, load environment variables from .env file
echo "Load environment variables from .env file..."
if [ -f .env ]; then
export $(cat .env | grep -v '^#' | xargs)
fi
set -a
source $(pwd)/.env
set +a

if [ -z "$(echo ${CONSUMER_ID})" ]; then
echo "Error: CONSUMER_ID environment variable is not set"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ function init_babylon_account() {
# If account not yet funded, fund it
if [ "$account_balance" = "null" ] || [ -z "$account_balance" ]; then
echo "account not yet funded, funding it"
# TODO: 1000000000ubbn should be configurable
# TODO: we cannot do this when running against Euphrates.
local fund_tx_hash=$(docker exec babylondnode0 /bin/sh -c "
/bin/babylond tx bank send \
$TEST_SPENDING_KEY_NAME \
Expand Down
103 changes: 25 additions & 78 deletions deployments/finality-gadget-integration-op-l2/init-testnets-dir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if [ ! -d ".testnets" ]; then
# Create new directory that will hold node and services' configuration
mkdir -p .testnets && chmod -R 777 .testnets

if [[ -z "$BITCOIN_NETWORK" || "$BITCOIN_NETWORK" == "regtest" ]]; then
if [[ "$BITCOIN_NETWORK" == "regtest" ]]; then
FINALIZATION_TIMEOUT=2
CONFIRMATION_DEPTH=1
BASE_HEADER=0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000
Expand All @@ -32,28 +32,6 @@ if [ ! -d ".testnets" ]; then
exit 1
fi

echo "Base header: $BASE_HEADER"
elif [[ "$BITCOIN_NETWORK" == "testnet" ]]; then
FINALIZATION_TIMEOUT=20
CONFIRMATION_DEPTH=1
# Get the next target difficulty adjustment height
NEXT_RETARGET_HEIGHT=$(curl -sSL "https://mempool.space/testnet/api/v1/difficulty-adjustment" | jq -r '.nextRetargetHeight')
echo "Next retarget height: $NEXT_RETARGET_HEIGHT"

# Calculate the previous difficulty adjustment height
# Each 2016-block interval is known as a difficulty epoch
BASE_HEADER_HEIGHT=$((NEXT_RETARGET_HEIGHT - 2016))
echo "Base header height: $BASE_HEADER_HEIGHT"

# Get the base header hash and header
BASE_HEIGHT_HASH=$(curl -sSL "https://mempool.space/testnet/api/block-height/$BASE_HEADER_HEIGHT")
BASE_HEADER=$(curl -sSL "https://mempool.space/testnet/api/block/$BASE_HEIGHT_HASH/header")

if [ -z "$BASE_HEADER" ]; then
echo "Error: Failed to retrieve base header"
exit 1
fi

echo "Base header: $BASE_HEADER"
else
echo "Unsupported bitcoin network: $BITCOIN_NETWORK"
Expand All @@ -63,11 +41,12 @@ if [ ! -d ".testnets" ]; then

# Initialize files for a babylon testnet
# `covenant-pks` should be updated if `covenant-keyring` dir is changed`
# TODO: --min-staking-amount-sat and --max-staking-time-blocks can be a ENV variable
docker run --rm -v $(pwd)/.testnets:/data babylonlabs/babylond:a98269d178879f22b136760701950d8929cc2093 \
babylond testnet init-files --v 2 -o /data \
--starting-ip-address 192.168.10.2 \
--keyring-backend=test \
--chain-id chain-test \
--chain-id $BABYLON_CHAIN_ID \
--epoch-interval 10 \
--btc-finalization-timeout $FINALIZATION_TIMEOUT \
--btc-confirmation-depth $CONFIRMATION_DEPTH \
Expand All @@ -87,6 +66,10 @@ if [ ! -d ".testnets" ]; then
sudo chown -R $(whoami):$(whoami) .testnets
sudo chmod -R 777 .testnets

# TODO: we need to decouple the babylon system and our finality system.
# only put the configs that are needed for the babylon system in the .testnets dir
# and for the finality gadget system in a separate dir.
# it might also make sense to split this script into two.
# Create separate subpaths for each component and copy relevant configuration
mkdir -p .testnets/vigilante/bbnconfig
mkdir -p .testnets/btc-staker
Expand All @@ -101,73 +84,36 @@ if [ ! -d ".testnets" ]; then

# for btc-staker, replace placeholders with env variables
cp artifacts/stakerd.conf .testnets/btc-staker/stakerd.conf
if [[ "$(uname)" == "Darwin" ]]; then
# macOS version
sed -i '' "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/btc-staker/stakerd.conf
sed -i '' "s|\${BITCOIN_RPC_PORT}|$BITCOIN_RPC_PORT|g" .testnets/btc-staker/stakerd.conf
sed -i '' "s|\${WALLET_PASS}|$WALLET_PASS|g" .testnets/btc-staker/stakerd.conf
sed -i '' "s|\${BABYLON_CHAIN_ID}|$BABYLON_CHAIN_ID|g" .testnets/btc-staker/stakerd.conf
else
# Linux version
sed -i "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/btc-staker/stakerd.conf
sed -i "s|\${BITCOIN_RPC_PORT}|$BITCOIN_RPC_PORT|g" .testnets/btc-staker/stakerd.conf
sed -i "s|\${WALLET_PASS}|$WALLET_PASS|g" .testnets/btc-staker/stakerd.conf
sed -i "s|\${BABYLON_CHAIN_ID}|$BABYLON_CHAIN_ID|g" .testnets/btc-staker/stakerd.conf
fi
sed -i.bak "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/btc-staker/stakerd.conf
sed -i.bak "s|\${BITCOIN_RPC_PORT}|$BITCOIN_RPC_PORT|g" .testnets/btc-staker/stakerd.conf
sed -i.bak "s|\${WALLET_PASS}|$WALLET_PASS|g" .testnets/btc-staker/stakerd.conf
sed -i.bak "s|\${BABYLON_CHAIN_ID}|$BABYLON_CHAIN_ID|g" .testnets/btc-staker/stakerd.conf
rm .testnets/btc-staker/stakerd.conf.bak

# for vigilante, replace placeholders with env variables
cp artifacts/vigilante.yml .testnets/vigilante/vigilante.yml
if [[ "$(uname)" == "Darwin" ]]; then
# macOS version
sed -i '' "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/vigilante/vigilante.yml
sed -i '' "s|\${BITCOIN_RPC_PORT}|$BITCOIN_RPC_PORT|g" .testnets/vigilante/vigilante.yml
sed -i '' "s|\${WALLET_PASS}|$WALLET_PASS|g" .testnets/vigilante/vigilante.yml
else
# Linux version
sed -i "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/vigilante/vigilante.yml
sed -i "s|\${BITCOIN_RPC_PORT}|$BITCOIN_RPC_PORT|g" .testnets/vigilante/vigilante.yml
sed -i "s|\${WALLET_PASS}|$WALLET_PASS|g" .testnets/vigilante/vigilante.yml
fi
# copy genesis file to vigilante config directory
cp .testnets/node0/babylond/config/genesis.json .testnets/vigilante/bbnconfig/genesis.json
sed -i.bak "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/vigilante/vigilante.yml
sed -i.bak "s|\${BITCOIN_RPC_PORT}|$BITCOIN_RPC_PORT|g" .testnets/vigilante/vigilante.yml
sed -i.bak "s|\${WALLET_PASS}|$WALLET_PASS|g" .testnets/vigilante/vigilante.yml
rm .testnets/vigilante/vigilante.yml.bak

cp artifacts/eotsd.conf .testnets/eotsmanager/eotsd.conf
cp artifacts/fpd.conf .testnets/finality-provider/fpd.conf
if [[ "$(uname)" == "Darwin" ]]; then
# macOS version
sed -i '' "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/finality-provider/fpd.conf
else
# Linux version
sed -i "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/finality-provider/fpd.conf
fi
sed -i.bak "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/finality-provider/fpd.conf
rm .testnets/finality-provider/fpd.conf.bak

cp artifacts/opfgd.toml .testnets/finality-gadget/opfgd.toml
if [[ "$(uname)" == "Darwin" ]]; then
# macOS version
sed -i '' "s|\${BITCOIN_RPC_PORT}|$BITCOIN_RPC_PORT|g" .testnets/finality-gadget/opfgd.toml
else
# Linux version
sed -i "s|\${BITCOIN_RPC_PORT}|$BITCOIN_RPC_PORT|g" .testnets/finality-gadget/opfgd.toml
fi
sed -i.bak "s|\${BITCOIN_RPC_PORT}|$BITCOIN_RPC_PORT|g" .testnets/finality-gadget/opfgd.toml
rm .testnets/finality-gadget/opfgd.toml.bak

cp artifacts/consumer-eotsd.conf .testnets/consumer-eotsmanager/eotsd.conf
cp artifacts/consumer-fpd.conf .testnets/consumer-finality-provider/fpd.conf
if [[ "$(uname)" == "Darwin" ]]; then
# macOS version
sed -i '' "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/consumer-finality-provider/fpd.conf
else
# Linux version
sed -i "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/consumer-finality-provider/fpd.conf
fi
sed -i.bak "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/consumer-finality-provider/fpd.conf
rm .testnets/consumer-finality-provider/fpd.conf.bak

cp artifacts/covd.conf .testnets/covenant-emulator/covd.conf
if [[ "$(uname)" == "Darwin" ]]; then
# macOS version
sed -i '' "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/covenant-emulator/covd.conf
else
# Linux version
sed -i "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/covenant-emulator/covd.conf
fi
sed -i.bak "s|\${BITCOIN_NETWORK}|$BITCOIN_NETWORK|g" .testnets/covenant-emulator/covd.conf
rm .testnets/covenant-emulator/covd.conf.bak

cp -R artifacts/covenant-keyring .testnets/covenant-emulator/keyring-test
cp .testnets/covenant-emulator/keyring-test/* .testnets/node0/babylond/covenant-emulator/keyring-test/
Expand All @@ -177,6 +123,7 @@ if [ ! -d ".testnets" ]; then
echo
fi

# Only run if .bitcoin directory does not exist
if [ ! -d ".bitcoin" ]; then
echo "Creating .bitcoin directory..."
mkdir -p .bitcoin && chmod -R 777 .bitcoin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#!/bin/bash
set -euo pipefail

# check if the Babylon FP already exists
EXISTING_BBN_FP_MONIKER=$(docker exec finality-provider /bin/sh \
-c "/bin/fpd list-finality-providers" \
| jq '.finality_providers[0].description.moniker')
EXISTING_BBN_FP_EOTS_PK_HEX=$(docker exec finality-provider /bin/sh \
-c "/bin/fpd list-finality-providers" \
| jq -r '.finality_providers[0].btc_pk_hex')
if [ "$EXISTING_BBN_FP_MONIKER" == "$BBN_FP_MONIKER" ]; then
echo "Babylon finality provider already exists with \
moniker: $EXISTING_BBN_FP_MONIKER and \
EOTS PK: $EXISTING_BBN_FP_EOTS_PK_HEX"
exit 0
fi

# create FP for Babylon
echo "Creating Babylon finality provider..."
BBN_FP_EOTS_PK_HEX=$(docker exec finality-provider /bin/sh -c "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#!/bin/bash
set -euo pipefail

# check if the OP consumer FP already exists
EXISTING_OP_FP_MONIKER=$(docker exec consumer-finality-provider /bin/sh \
-c "/bin/fpd list-finality-providers" \
| jq '.finality_providers[0].description.moniker')
EXISTING_OP_FP_EOTS_PK_HEX=$(docker exec consumer-finality-provider /bin/sh \
-c "/bin/fpd list-finality-providers" \
| jq -r '.finality_providers[0].btc_pk_hex')
if [ "$EXISTING_OP_FP_MONIKER" == "$OP_FP_MONIKER" ]; then
echo "OP consumer finality provider already exists with \
moniker: $EXISTING_OP_FP_MONIKER and \
EOTS PK: $EXISTING_OP_FP_EOTS_PK_HEX"
exit 0
fi

# create FP for the consumer chain
echo "Creating OP consumer finality provider..."
OP_FP_EOTS_PK_HEX=$(docker exec consumer-finality-provider /bin/sh -c "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
#!/bin/bash
set -euo pipefail

# Get all registered consumer IDs and store them in an array
# TODO: this adds another coupling between our system and the Babylon chain.
# we should use RPC calls
CONSUMER_IDS=($(docker exec babylondnode0 /bin/sh -c "
/bin/babylond query btcstkconsumer registered-consumers \
--home $BABYLON_HOME_DIR \
--chain-id $BABYLON_CHAIN_ID \
-o json" | jq -r '.consumer_ids[]'))

# Check if the OP consumer chain is already registered
if [[ "${CONSUMER_IDS[@]}" =~ "${CONSUMER_ID}" ]]; then
echo "OP consumer chain already registered with ID: $CONSUMER_ID"
exit 0
fi

# Register op consumer chain
echo "Registering OP consumer chain..."
REGISTER_TX_HASH=$(docker exec babylondnode0 /bin/sh -c "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/bin/bash
set -euo pipefail

# Load environment variables from .env file
echo "Load environment variables from .env file..."
if [ -f .env ]; then
export $(cat .env | grep -v '^#' | xargs)
fi
# Load environment variables from the .env file
set -a
source $(pwd)/.env
set +a

if [ -z "$(echo ${WALLET_PASS})" ] || [ -z "$(echo ${BTCSTAKER_PRIVKEY})" ]; then
echo "Error: WALLET_PASS or BTCSTAKER_PRIVKEY environment variable is not set"
Expand All @@ -21,7 +20,7 @@ SYNCED=$(docker exec bitcoindsim /bin/sh -c "
-rpcuser=rpcuser \
-rpcpassword=rpcpass \
getblockchaininfo" | jq -r '.verificationprogress')
if [ $(echo "$SYNCED < 0.999" | bc -l) -eq 1 ]; then
if (( $(awk -v synced="$SYNCED" 'BEGIN {print (synced < 0.999)}') )); then
echo "Error: Bitcoin node is not fully synced. Expected at least 99.9%, got ${SYNCED}"
exit 1
fi
Expand Down Expand Up @@ -83,9 +82,9 @@ BALANCE_BTC=$(docker exec bitcoindsim /bin/sh -c "
-rpcpassword=rpcpass \
-rpcwallet=btcstaker \
listunspent" | jq -r '[.[] | .amount] | add')
if [ $(echo "$BALANCE_BTC < 0.01" | bc -l) -eq 1 ]; then
if (( $(awk -v balance="$BALANCE_BTC" 'BEGIN {print (balance < 0.01)}') )); then
echo "Warning: BTCStaker balance is less than 0.01 BTC. You may need to fund this address for ${BITCOIN_NETWORK}."
else
echo "BTCStaker balance is sufficient: ${BALANCE_BTC} BTC"
fi
echo
echo

0 comments on commit a6c9a2e

Please sign in to comment.