Skip to content

Commit

Permalink
improve and fix the init-testnets-dir.sh script (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
bap2pecs authored Oct 19, 2024
1 parent 9875ff3 commit da96793
Showing 1 changed file with 24 additions and 78 deletions.
102 changes: 24 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 @@ -68,7 +46,7 @@ if [ ! -d ".testnets" ]; then
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 @@ -88,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 @@ -102,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 @@ -178,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

0 comments on commit da96793

Please sign in to comment.