Skip to content

Commit

Permalink
feat: set finality contract enabled value (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
lesterli authored Nov 3, 2024
1 parent cc363b3 commit 34ff017
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 37 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ set-babylon-keys:

register-consumer-chain:
@./scripts/babylon-integration/register-consumer-chain.sh
.PHONY: register-consumer-chain
.PHONY: register-consumer-chain

toggle-cw-killswitch:
@./scripts/babylon-integration/toggle-cw-killswitch.sh
.PHONY: toggle-cw-killswitch
20 changes: 17 additions & 3 deletions docker/docker-compose-babylon-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ services:
# This is a one-off container just for setting the Babylon keys
set-babylon-keys:
container_name: set-babylon-keys
image: snapchain/babylon-deployment-utils:1b34d77
image: snapchain/babylon-deployment-utils:a2c8f0e
env_file:
- "${PWD}/.env.babylon-integration"
volumes:
Expand All @@ -15,7 +15,7 @@ services:
# This is a one-off container just for registering the consumer chain
register-consumer-chain:
container_name: register-consumer-chain
image: snapchain/babylon-deployment-utils:1b34d77
image: snapchain/babylon-deployment-utils:a2c8f0e
env_file:
- "${PWD}/.env.babylon-integration"
volumes:
Expand All @@ -28,7 +28,7 @@ services:
# This is a one-off container just for deploying cw contract
deploy-cw-contract:
container_name: deploy-cw-contract
image: snapchain/babylon-deployment-utils:1b34d77
image: snapchain/babylon-deployment-utils:a2c8f0e
env_file:
- "${PWD}/.env.babylon-integration"
volumes:
Expand All @@ -39,6 +39,20 @@ services:
- -c
- /deploy-cw-contract.sh

# This is a one-off container just for setting the finality contract enabled value
toggle-cw-killswitch:
container_name: toggle-cw-killswitch
image: snapchain/babylon-deployment-utils:a2c8f0e
env_file:
- "${PWD}/.env.babylon-integration"
volumes:
- ${PWD}/.deploy/babylond:/home/.babylond
- ${PWD}/.deploy/contract:/home/.deploy
entrypoint:
- /bin/bash
- -c
- /toggle-cw-killswitch.sh

btc-staker:
container_name: btc-staker
# https://github.com/babylonlabs-io/btc-staker/commit/484bcb8fd9b7b0b525234d704dd049b1ef18e29f
Expand Down
6 changes: 6 additions & 0 deletions scripts/babylon-integration/toggle-cw-killswitch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -euo pipefail

# setting the finality contract enabled value
docker compose -f docker/docker-compose-babylon-integration.yml up -d toggle-cw-killswitch
docker logs -f toggle-cw-killswitch
4 changes: 3 additions & 1 deletion scripts/babylon-integration/utils/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ RUN rm -f /tmp/go.mod

COPY set-babylon-keys.sh /set-babylon-keys.sh
COPY register-consumer-chain.sh /register-consumer-chain.sh
COPY deploy-cw-contract.sh /deploy-cw-contract.sh
COPY common.sh /common.sh
COPY deploy-cw-contract.sh /deploy-cw-contract.sh
COPY toggle-cw-killswitch.sh /toggle-cw-killswitch.sh
34 changes: 34 additions & 0 deletions scripts/babylon-integration/utils/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
set -uo pipefail

# Function to handle pending transactions
wait_for_tx() {
local tx_hash=$1
local max_attempts=$2
local interval=$3
local attempt=0

while [ $attempt -lt $max_attempts ]; do
# Query with explicit error handling
if output=$(babylond query tx "$tx_hash" \
--chain-id "$BABYLON_CHAIN_ID" \
--node "$BABYLON_RPC_URL" -o json 2>&1); then
echo "Transaction found"
return 0
else
# Command failed, check if it's because tx is pending
if echo "$output" | grep -q "Internal error: tx ($tx_hash) not found"; then
echo "Transaction pending..."
sleep "$interval"
((attempt++))
continue
fi
# Other error occurred
echo "Query failed: $output"
return 1
fi
done

echo "Timeout after $max_attempts attempts waiting for transaction $tx_hash to be available."
return 1
}
34 changes: 2 additions & 32 deletions scripts/babylon-integration/utils/deploy-cw-contract.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash
set -uo pipefail

source "./common.sh"

# Set keyring directory
KEYRING_DIR=/home/.babylond
# Set contract address output directory
Expand All @@ -10,38 +12,6 @@ if [ ! -d "$CONTRACT_DIR" ]; then
mkdir -p $CONTRACT_DIR
fi

# Function to handle pending transactions
wait_for_tx() {
local tx_hash=$1
local max_attempts=$2
local interval=$3
local attempt=0

while [ $attempt -lt $max_attempts ]; do
# Query with explicit error handling
if output=$(babylond query tx "$tx_hash" \
--chain-id "$BABYLON_CHAIN_ID" \
--node "$BABYLON_RPC_URL" -o json 2>&1); then
echo "Transaction found"
return 0
else
# Command failed, check if it's because tx is pending
if echo "$output" | grep -q "Internal error: tx ($tx_hash) not found"; then
echo "Transaction pending..."
sleep "$interval"
((attempt++))
continue
fi
# Other error occurred
echo "Query failed: $output"
return 1
fi
done

echo "Timeout after $max_attempts attempts waiting for transaction $tx_hash to be available."
return 1
}

# Download the contract
echo "Downloading contract version $CONTRACT_VERSION..."
curl -SL "https://github.com/babylonlabs-io/babylon-contract/releases/download/$CONTRACT_VERSION/${CONTRACT_FILE}.zip" -o "${CONTRACT_FILE}.zip"
Expand Down
53 changes: 53 additions & 0 deletions scripts/babylon-integration/utils/toggle-cw-killswitch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash
set -uo pipefail

source "./common.sh"

# Set keyring directory
KEYRING_DIR=/home/.babylond
# Set contract address output directory
CONTRACT_DIR=/home/.deploy
# Get the IS_ENABLED environment variable
echo "Setting enabled value to $IS_ENABLED"

# Read the contract address
CONTRACT_ADDR=$(cat $CONTRACT_DIR/contract-address.txt | tr -d '[:space:]')
echo "Contract address: $CONTRACT_ADDR"

# Set the is_enabled value in the contract
SET_ENABLED_TX_OUTPUT=$(babylond tx wasm execute $CONTRACT_ADDR \
'{"set_enabled":{"enabled":'$IS_ENABLED'}}' \
--gas-prices 0.2ubbn \
--gas auto \
--gas-adjustment 1.3 \
--from $BABYLON_PREFUNDED_KEY \
--keyring-dir $KEYRING_DIR \
--chain-id $BABYLON_CHAIN_ID \
--node $BABYLON_RPC_URL \
--keyring-backend test \
-o json -y)
echo "$SET_ENABLED_TX_OUTPUT"
SET_ENABLED_TX_HASH=$(echo "$SET_ENABLED_TX_OUTPUT" | jq -r '.txhash')
echo "Set enabled tx hash: $SET_ENABLED_TX_HASH"

# Wait for the transaction to be included in a block
if ! wait_for_tx "$SET_ENABLED_TX_HASH" 10 3; then
echo "Failed to set enabled value in contract - transaction failed"
exit 1
fi

# Query and verify the enabled state
QUERY_ENABLED_VALUE=$(babylond query wasm contract-state smart $CONTRACT_ADDR \
'{"is_enabled":{}}' \
--chain-id $BABYLON_CHAIN_ID \
--node $BABYLON_RPC_URL \
-o json \
| jq -r '.data')
echo "Query enabled value: $QUERY_ENABLED_VALUE"

if [ "$QUERY_ENABLED_VALUE" != "$IS_ENABLED" ]; then
echo "Failed to set enabled value in contract - value mismatch (expected: $IS_ENABLED, got: $QUERY_ENABLED_VALUE)"
exit 1
fi
echo "Successfully set enabled value to $IS_ENABLED"
echo

0 comments on commit 34ff017

Please sign in to comment.