diff --git a/run_service.sh b/run_service.sh index 60b730ba..c0053e87 100755 --- a/run_service.sh +++ b/run_service.sh @@ -144,6 +144,14 @@ add_volume_to_service() { fi } +# Function to retrieve on-chain service state (requires env variables set to use --use-custom-chain) +get_on_chain_service_state() { + local service_id="$1" + local service_info=$(poetry run autonomy service --use-custom-chain info "$service_id") + local state="$(echo "$service_info" | grep -oP "Service State\s*\|\s*\K\S+")" + echo "$state" +} + # ------------------ # Script starts here @@ -436,101 +444,119 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then operator_pkey="${operator_pkey#0x}" echo -n "$operator_pkey" >"$operator_pkey_file" - # transfer the ownership of the Safe from the agent to the service owner - # (in a live service, this should be done by sending a 0 DAI transfer to its Safe) - echo "[Agent instance] Swapping Safe owner..." - output=$(poetry run python "../scripts/swap_safe_owner.py" "$service_safe_address" "$agent_pkey_file" "$operator_address" "$rpc") - if [[ $? -ne 0 ]]; then - echo "Swapping Safe owner failed.\n$output" - rm -f $agent_pkey_file - rm -f $operator_pkey_file - exit 1 - fi - echo "$output" - - # terminate current service - echo "[Service owner] Terminating on-chain service $service_id..." - output=$( - poetry run autonomy service \ - --use-custom-chain \ - terminate "$service_id" \ - --key "$operator_pkey_file" - ) - if [[ $? -ne 0 ]]; then - echo "Terminating service failed.\n$output" - rm -f $agent_pkey_file - rm -f $operator_pkey_file - exit 1 + if [ $(get_on_chain_service_state $service_id) == "DEPLOYED" ]; then + # transfer the ownership of the Safe from the agent to the service owner + # (in a live service, this should be done by sending a 0 DAI transfer to its Safe) + echo "[Agent instance] Swapping Safe owner..." + output=$(poetry run python "../scripts/swap_safe_owner.py" "$service_safe_address" "$agent_pkey_file" "$operator_address" "$rpc") + if [[ $? -ne 0 ]]; then + echo "Swapping Safe owner failed.\n$output" + rm -f $agent_pkey_file + rm -f $operator_pkey_file + exit 1 + fi + echo "$output" + + # terminate current service + echo "[Service owner] Terminating on-chain service $service_id..." + output=$( + poetry run autonomy service \ + --use-custom-chain \ + terminate "$service_id" \ + --key "$operator_pkey_file" + ) + if [[ $? -ne 0 ]]; then + echo "Terminating service failed.\n$output" + echo "Please, delete or rename the ./trader folder and try re-run this script again." + rm -f $agent_pkey_file + rm -f $operator_pkey_file + exit 1 + fi fi # unbond current service - echo "[Operator] Unbonding on-chain service $service_id..." - output=$( - poetry run autonomy service \ - --use-custom-chain \ - unbond "$service_id" \ - --key "$operator_pkey_file" - ) - if [[ $? -ne 0 ]]; then - echo "Unbonding service failed.\n$output" - rm -f $agent_pkey_file - rm -f $operator_pkey_file - exit 1 + if [ $(get_on_chain_service_state $service_id) == "TERMINATED_BONDED" ]; then + echo "[Operator] Unbonding on-chain service $service_id..." + output=$( + poetry run autonomy service \ + --use-custom-chain \ + unbond "$service_id" \ + --key "$operator_pkey_file" + ) + if [[ $? -ne 0 ]]; then + echo "Unbonding service failed.\n$output" + echo "Please, delete or rename the ./trader folder and try re-run this script again." + rm -f $agent_pkey_file + rm -f $operator_pkey_file + exit 1 + fi fi # update service - echo "[Service owner] Updating on-chain service $service_id..." - agent_id=12 - cost_of_bonding=10000000000000000 - nft="bafybeig64atqaladigoc3ds4arltdu63wkdrk3gesjfvnfdmz35amv7faq" - output=$( - poetry run autonomy mint \ - --skip-hash-check \ - --use-custom-chain \ - service packages/valory/services/trader/ \ - --key "$operator_pkey_file" \ - --nft $nft \ - -a $agent_id \ - -n $n_agents \ - --threshold $n_agents \ - -c $cost_of_bonding \ - --update "$service_id" - ) - if [[ $? -ne 0 ]]; then - echo "Updating service failed.\n$output" - rm -f $agent_pkey_file - rm -f $operator_pkey_file - exit 1 + if [ $(get_on_chain_service_state $service_id) == "PRE_REGISTRATION" ]; then + echo "[Service owner] Updating on-chain service $service_id..." + agent_id=12 + cost_of_bonding=10000000000000000 + nft="bafybeig64atqaladigoc3ds4arltdu63wkdrk3gesjfvnfdmz35amv7faq" + output=$( + poetry run autonomy mint \ + --skip-hash-check \ + --use-custom-chain \ + service packages/valory/services/trader/ \ + --key "$operator_pkey_file" \ + --nft $nft \ + -a $agent_id \ + -n $n_agents \ + --threshold $n_agents \ + -c $cost_of_bonding \ + --update "$service_id" + ) + if [[ $? -ne 0 ]]; then + echo "Updating service failed.\n$output" + echo "Please, delete or rename the ./trader folder and try re-run this script again." + rm -f $agent_pkey_file + rm -f $operator_pkey_file + exit 1 + fi fi # activate service - echo "[Service owner] Activating registration for on-chain service $service_id..." - output=$(poetry run autonomy service --use-custom-chain activate --key "$operator_pkey_file" "$service_id") - if [[ $? -ne 0 ]]; then - echo "Activating service failed.\n$output" - rm -f $agent_pkey_file - rm -f $operator_pkey_file - exit 1 + if [ $(get_on_chain_service_state $service_id) == "PRE_REGISTRATION" ]; then + echo "[Service owner] Activating registration for on-chain service $service_id..." + output=$(poetry run autonomy service --use-custom-chain activate --key "$operator_pkey_file" "$service_id") + if [[ $? -ne 0 ]]; then + echo "Activating service failed.\n$output" + echo "Please, delete or rename the ./trader folder and try re-run this script again." + rm -f $agent_pkey_file + rm -f $operator_pkey_file + exit 1 + fi fi # register agent instance - echo "[Operator] Registering agent instance for on-chain service $service_id..." - output=$(poetry run autonomy service --use-custom-chain register --key "$operator_pkey_file" "$service_id" -a $agent_id -i "$agent_address") - if [[ $? -ne 0 ]]; then - echo "Registering agent instance failed.\n$output" - rm -f $agent_pkey_file - rm -f $operator_pkey_file - exit 1 + if [ $(get_on_chain_service_state $service_id) == "ACTIVE_REGISTRATION" ]; then + echo "[Operator] Registering agent instance for on-chain service $service_id..." + output=$(poetry run autonomy service --use-custom-chain register --key "$operator_pkey_file" "$service_id" -a $agent_id -i "$agent_address") + if [[ $? -ne 0 ]]; then + echo "Registering agent instance failed.\n$output" + echo "Please, delete or rename the ./trader folder and try re-run this script again." + rm -f $agent_pkey_file + rm -f $operator_pkey_file + exit 1 + fi fi # deploy on-chain service - echo "[Service owner] Deploying on-chain service $service_id..." - output=$(poetry run autonomy service --use-custom-chain deploy "$service_id" --key "$operator_pkey_file" --reuse-multisig) - if [[ $? -ne 0 ]]; then - echo "Deploying service failed.\n$output" - rm -f $agent_pkey_file - rm -f $operator_pkey_file - exit 1 + if [ $(get_on_chain_service_state $service_id) == "FINISHED_REGISTRATION" ]; then + echo "[Service owner] Deploying on-chain service $service_id..." + output=$(poetry run autonomy service --use-custom-chain deploy "$service_id" --key "$operator_pkey_file" --reuse-multisig) + if [[ $? -ne 0 ]]; then + echo "Deploying service failed.\n$output" + echo "Please, delete or rename the ./trader folder and try re-run this script again." + rm -f $agent_pkey_file + rm -f $operator_pkey_file + exit 1 + fi fi # delete the pkey files diff --git a/scripts/fix_interrupted_service_update.sh b/scripts/fix_interrupted_service_update.sh index 52e242c4..9a75a87c 100755 --- a/scripts/fix_interrupted_service_update.sh +++ b/scripts/fix_interrupted_service_update.sh @@ -125,6 +125,17 @@ echo "" echo "This script fixes an interrupted on-chain service update by an Open Autonomy version <0.12.1.post4" echo "" +echo "WARNING: This script (fix_interrupted_service_update.sh) is deprecated." +echo "Please use the latest version of ./run_script.sh, which addresses the issues fixed in this script." +read -p "Do you want to continue? (y/n): " choice + +if [[ $choice == "y" || $choice == "Y" ]]; then + echo "Continuing with the script..." +else + echo "Exiting the script." + exit 1 +fi + # Check if user is inside a venv if [[ "$VIRTUAL_ENV" != "" ]] then diff --git a/stop_service.sh b/stop_service.sh new file mode 100755 index 00000000..d0cc6f85 --- /dev/null +++ b/stop_service.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# ------------------------------------------------------------------------------ +# +# Copyright 2023 Valory AG +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ------------------------------------------------------------------------------ + +cd trader; poetry run autonomy deploy stop --build-dir trader_service/abci_build; cd ..