From 34ff0179396204a51a5b1fa1faf24fee31eb6c6d Mon Sep 17 00:00:00 2001 From: lesterli Date: Sun, 3 Nov 2024 16:18:24 +0800 Subject: [PATCH] feat: set finality contract enabled value (#35) --- Makefile | 6 ++- docker/docker-compose-babylon-integration.yml | 20 +++++-- .../toggle-cw-killswitch.sh | 6 +++ scripts/babylon-integration/utils/Dockerfile | 4 +- scripts/babylon-integration/utils/common.sh | 34 ++++++++++++ .../utils/deploy-cw-contract.sh | 34 +----------- .../utils/toggle-cw-killswitch.sh | 53 +++++++++++++++++++ 7 files changed, 120 insertions(+), 37 deletions(-) create mode 100755 scripts/babylon-integration/toggle-cw-killswitch.sh create mode 100755 scripts/babylon-integration/utils/common.sh create mode 100755 scripts/babylon-integration/utils/toggle-cw-killswitch.sh diff --git a/Makefile b/Makefile index f90eee6..d9edad8 100644 --- a/Makefile +++ b/Makefile @@ -92,4 +92,8 @@ set-babylon-keys: register-consumer-chain: @./scripts/babylon-integration/register-consumer-chain.sh -.PHONY: register-consumer-chain \ No newline at end of file +.PHONY: register-consumer-chain + +toggle-cw-killswitch: + @./scripts/babylon-integration/toggle-cw-killswitch.sh +.PHONY: toggle-cw-killswitch \ No newline at end of file diff --git a/docker/docker-compose-babylon-integration.yml b/docker/docker-compose-babylon-integration.yml index 4bbf3de..24207f3 100644 --- a/docker/docker-compose-babylon-integration.yml +++ b/docker/docker-compose-babylon-integration.yml @@ -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: @@ -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: @@ -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: @@ -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 diff --git a/scripts/babylon-integration/toggle-cw-killswitch.sh b/scripts/babylon-integration/toggle-cw-killswitch.sh new file mode 100755 index 0000000..3580f71 --- /dev/null +++ b/scripts/babylon-integration/toggle-cw-killswitch.sh @@ -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 \ No newline at end of file diff --git a/scripts/babylon-integration/utils/Dockerfile b/scripts/babylon-integration/utils/Dockerfile index d541f20..503f9da 100644 --- a/scripts/babylon-integration/utils/Dockerfile +++ b/scripts/babylon-integration/utils/Dockerfile @@ -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 \ No newline at end of file +COPY common.sh /common.sh +COPY deploy-cw-contract.sh /deploy-cw-contract.sh +COPY toggle-cw-killswitch.sh /toggle-cw-killswitch.sh \ No newline at end of file diff --git a/scripts/babylon-integration/utils/common.sh b/scripts/babylon-integration/utils/common.sh new file mode 100755 index 0000000..0b125c4 --- /dev/null +++ b/scripts/babylon-integration/utils/common.sh @@ -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 +} \ No newline at end of file diff --git a/scripts/babylon-integration/utils/deploy-cw-contract.sh b/scripts/babylon-integration/utils/deploy-cw-contract.sh index cf41e16..e8f427c 100755 --- a/scripts/babylon-integration/utils/deploy-cw-contract.sh +++ b/scripts/babylon-integration/utils/deploy-cw-contract.sh @@ -1,6 +1,8 @@ #!/bin/bash set -uo pipefail +source "./common.sh" + # Set keyring directory KEYRING_DIR=/home/.babylond # Set contract address output directory @@ -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" diff --git a/scripts/babylon-integration/utils/toggle-cw-killswitch.sh b/scripts/babylon-integration/utils/toggle-cw-killswitch.sh new file mode 100755 index 0000000..6dfdbde --- /dev/null +++ b/scripts/babylon-integration/utils/toggle-cw-killswitch.sh @@ -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 \ No newline at end of file