Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit 68be552

Browse files
Merge pull request #38 from valory-xyz/fix/fix_minting_corner_cases
Ensure script is robust against on-chain registry failures
2 parents ec0e7b8 + ae3f031 commit 68be552

File tree

1 file changed

+102
-107
lines changed

1 file changed

+102
-107
lines changed

run_service.sh

Lines changed: 102 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ else
287287
exit 1
288288
fi
289289

290+
echo ""
291+
echo "-----------------------------------------"
292+
echo "Checking Autonolas Protocol service state"
293+
echo "-----------------------------------------"
294+
290295
gnosis_chain_id=100
291296
n_agents=1
292297

@@ -299,6 +304,7 @@ export CUSTOM_GNOSIS_SAFE_MULTISIG_ADDRESS="0x3C1fF68f5aa342D296d4DEe4Bb1cACCA91
299304
export CUSTOM_GNOSIS_SAFE_PROXY_FACTORY_ADDRESS="0x3C1fF68f5aa342D296d4DEe4Bb1cACCA912D95fE"
300305
export CUSTOM_GNOSIS_SAFE_SAME_ADDRESS_MULTISIG_ADDRESS="0x3d77596beb0f130a4415df3D2D8232B3d3D31e44"
301306
export CUSTOM_MULTISEND_ADDRESS="0x40A2aCCbd92BCA938b02010E17A5b8929b49130D"
307+
export AGENT_ID=12
302308

303309
if [ "$first_run" = "true" ]
304310
then
@@ -336,10 +342,9 @@ then
336342
suggested_amount=50000000000000000
337343
ensure_minimum_balance "$operator_address" $suggested_amount "operator's address"
338344

339-
echo "Minting your service on the Gnosis chain..."
345+
echo "[Service owner] Minting your service on the Gnosis chain..."
340346

341347
# create service
342-
agent_id=12
343348
cost_of_bonding=10000000000000000
344349
nft="bafybeig64atqaladigoc3ds4arltdu63wkdrk3gesjfvnfdmz35amv7faq"
345350
service_id=$(poetry run autonomy mint \
@@ -348,7 +353,7 @@ then
348353
service packages/valory/services/$directory/ \
349354
--key "$operator_pkey_file" \
350355
--nft $nft \
351-
-a $agent_id \
356+
-a $AGENT_ID \
352357
-n $n_agents \
353358
--threshold $n_agents \
354359
-c $cost_of_bonding
@@ -362,46 +367,25 @@ then
362367
exit 1
363368
fi
364369

365-
echo "[Service owner] Activating registration for service with id $service_id..."
366-
# activate service
367-
activation=$(poetry run autonomy service --use-custom-chain activate --key "$operator_pkey_file" "$service_id")
368-
# validate activation
369-
if ! [[ "$activation" = "Service activated succesfully" ]]
370-
then
371-
echo "Service registration activation failed: $activation"
372-
exit 1
373-
fi
374-
375-
echo "[Service owner] Registering agent instance for service with id $service_id..."
376-
# register service
377-
registration=$(poetry run autonomy service --use-custom-chain register --key "$operator_pkey_file" "$service_id" -a $agent_id -i "$agent_address")
378-
# validate registration
379-
if ! [[ "$registration" = "Agent instance registered succesfully" ]]
380-
then
381-
echo "Service registration failed: $registration"
382-
exit 1
383-
fi
384-
385-
echo "[Service owner] Deploying service with id $service_id..."
386-
# deploy service
387-
deployment=$(poetry run autonomy service --use-custom-chain deploy --key "$operator_pkey_file" "$service_id")
388-
# validate deployment
389-
if ! [[ "$deployment" = "Service deployed succesfully" ]]
390-
then
391-
echo "Service deployment failed: $deployment"
392-
exit 1
393-
fi
394-
395-
# delete the operator's pkey file
396-
rm $operator_pkey_file
397-
# store service id
398370
echo -n "$service_id" > "../$service_id_path"
399371
fi
400372

401-
# Update the on-chain service if required
373+
# generate private key files in the format required by the CLI tool
374+
agent_pkey_file="agent_pkey.txt"
375+
agent_pkey=$(get_private_key "../$keys_json_path")
376+
agent_pkey="${agent_pkey#0x}"
377+
echo -n "$agent_pkey" >"$agent_pkey_file"
378+
379+
operator_pkey_file="operator_pkey.txt"
380+
operator_pkey=$(get_private_key "../$operator_keys_file")
381+
operator_pkey="${operator_pkey#0x}"
382+
echo -n "$operator_pkey" >"$operator_pkey_file"
383+
384+
# Update the on-chain service if outdated
402385
packages="packages/packages.json"
403386
local_service_hash="$(grep 'service' $packages | awk -F: '{print $2}' | tr -d '", ' | head -n 1)"
404387
remote_service_hash=$(poetry run python "../scripts/service_hash.py")
388+
operator_address=$(get_address "../$operator_keys_file")
405389

406390
if [ "$local_service_hash" != "$remote_service_hash" ]; then
407391
echo ""
@@ -415,9 +399,6 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
415399
echo ""
416400

417401
# Check balances
418-
service_safe_address=$(<"../$service_safe_address_path")
419-
operator_address=$(get_address "../$operator_keys_file")
420-
421402
suggested_amount=50000000000000000
422403
ensure_minimum_balance "$operator_address" $suggested_amount "operator's address"
423404

@@ -433,20 +414,12 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
433414
echo "Cancelling the on-chain service update prematurely could lead to an inconsistent state of the Safe or the on-chain service state, which may require manual intervention to resolve."
434415
echo ""
435416

436-
# generate private key files in the format required by the CLI tool
437-
agent_pkey_file="agent_pkey.txt"
438-
agent_pkey=$(get_private_key "../$keys_json_path")
439-
agent_pkey="${agent_pkey#0x}"
440-
echo -n "$agent_pkey" >"$agent_pkey_file"
441-
442-
operator_pkey_file="operator_pkey.txt"
443-
operator_pkey=$(get_private_key "../$operator_keys_file")
444-
operator_pkey="${operator_pkey#0x}"
445-
echo -n "$operator_pkey" >"$operator_pkey_file"
446-
417+
# TODO this condition should be increased to be service_state=DEPLOYED && current_safe_owner=agent_address.
418+
# Otherwise the script will not recover the on-chain state in the (rare) case where this transaction succeeds but terminating transaction fails.
447419
if [ $(get_on_chain_service_state $service_id) == "DEPLOYED" ]; then
448420
# transfer the ownership of the Safe from the agent to the service owner
449421
# (in a live service, this should be done by sending a 0 DAI transfer to its Safe)
422+
service_safe_address=$(<"../$service_safe_address_path")
450423
echo "[Agent instance] Swapping Safe owner..."
451424
output=$(poetry run python "../scripts/swap_safe_owner.py" "$service_safe_address" "$agent_pkey_file" "$operator_address" "$rpc")
452425
if [[ $? -ne 0 ]]; then
@@ -456,8 +429,10 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
456429
exit 1
457430
fi
458431
echo "$output"
432+
fi
459433

460-
# terminate current service
434+
# terminate current service
435+
if [ $(get_on_chain_service_state $service_id) == "DEPLOYED" ]; then
461436
echo "[Service owner] Terminating on-chain service $service_id..."
462437
output=$(
463438
poetry run autonomy service \
@@ -467,7 +442,7 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
467442
)
468443
if [[ $? -ne 0 ]]; then
469444
echo "Terminating service failed.\n$output"
470-
echo "Please, delete or rename the ./trader folder and try re-run this script again."
445+
echo "Please, delete or rename the ./trader folder and try re-run this script again."
471446
rm -f $agent_pkey_file
472447
rm -f $operator_pkey_file
473448
exit 1
@@ -485,7 +460,7 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
485460
)
486461
if [[ $? -ne 0 ]]; then
487462
echo "Unbonding service failed.\n$output"
488-
echo "Please, delete or rename the ./trader folder and try re-run this script again."
463+
echo "Please, delete or rename the ./trader folder and try re-run this script again."
489464
rm -f $agent_pkey_file
490465
rm -f $operator_pkey_file
491466
exit 1
@@ -495,7 +470,6 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
495470
# update service
496471
if [ $(get_on_chain_service_state $service_id) == "PRE_REGISTRATION" ]; then
497472
echo "[Service owner] Updating on-chain service $service_id..."
498-
agent_id=12
499473
cost_of_bonding=10000000000000000
500474
nft="bafybeig64atqaladigoc3ds4arltdu63wkdrk3gesjfvnfdmz35amv7faq"
501475
output=$(
@@ -505,86 +479,107 @@ if [ "$local_service_hash" != "$remote_service_hash" ]; then
505479
service packages/valory/services/trader/ \
506480
--key "$operator_pkey_file" \
507481
--nft $nft \
508-
-a $agent_id \
482+
-a $AGENT_ID \
509483
-n $n_agents \
510484
--threshold $n_agents \
511485
-c $cost_of_bonding \
512486
--update "$service_id"
513487
)
514488
if [[ $? -ne 0 ]]; then
515489
echo "Updating service failed.\n$output"
516-
echo "Please, delete or rename the ./trader folder and try re-run this script again."
490+
echo "Please, delete or rename the ./trader folder and try re-run this script again."
517491
rm -f $agent_pkey_file
518492
rm -f $operator_pkey_file
519493
exit 1
520494
fi
521495
fi
522496

523-
# activate service
524-
if [ $(get_on_chain_service_state $service_id) == "PRE_REGISTRATION" ]; then
525-
echo "[Service owner] Activating registration for on-chain service $service_id..."
526-
output=$(poetry run autonomy service --use-custom-chain activate --key "$operator_pkey_file" "$service_id")
527-
if [[ $? -ne 0 ]]; then
528-
echo "Activating service failed.\n$output"
529-
echo "Please, delete or rename the ./trader folder and try re-run this script again."
530-
rm -f $agent_pkey_file
531-
rm -f $operator_pkey_file
532-
exit 1
533-
fi
497+
echo ""
498+
echo "Finished updating on-chain service $service_id."
499+
fi
500+
501+
echo ""
502+
echo "Ensuring on-chain service $service_id is in DEPLOYED state..."
503+
504+
if [ $(get_on_chain_service_state $service_id) != "DEPLOYED" ]; then
505+
suggested_amount=25000000000000000
506+
ensure_minimum_balance "$operator_address" $suggested_amount "operator's address"
507+
fi
508+
509+
# activate service
510+
if [ $(get_on_chain_service_state $service_id) == "PRE_REGISTRATION" ]; then
511+
echo "[Service owner] Activating registration for on-chain service $service_id..."
512+
output=$(poetry run autonomy service --use-custom-chain activate --key "$operator_pkey_file" "$service_id")
513+
if [[ $? -ne 0 ]]; then
514+
echo "Activating service failed.\n$output"
515+
echo "Please, delete or rename the ./trader folder and try re-run this script again."
516+
rm -f $agent_pkey_file
517+
rm -f $operator_pkey_file
518+
exit 1
534519
fi
520+
fi
535521

536-
# register agent instance
537-
if [ $(get_on_chain_service_state $service_id) == "ACTIVE_REGISTRATION" ]; then
538-
echo "[Operator] Registering agent instance for on-chain service $service_id..."
539-
output=$(poetry run autonomy service --use-custom-chain register --key "$operator_pkey_file" "$service_id" -a $agent_id -i "$agent_address")
540-
if [[ $? -ne 0 ]]; then
541-
echo "Registering agent instance failed.\n$output"
542-
echo "Please, delete or rename the ./trader folder and try re-run this script again."
543-
rm -f $agent_pkey_file
544-
rm -f $operator_pkey_file
545-
exit 1
546-
fi
522+
# register agent instance
523+
if [ $(get_on_chain_service_state $service_id) == "ACTIVE_REGISTRATION" ]; then
524+
echo "[Operator] Registering agent instance for on-chain service $service_id..."
525+
output=$(poetry run autonomy service --use-custom-chain register --key "$operator_pkey_file" "$service_id" -a $AGENT_ID -i "$agent_address")
526+
if [[ $? -ne 0 ]]; then
527+
echo "Registering agent instance failed.\n$output"
528+
echo "Please, delete or rename the ./trader folder and try re-run this script again."
529+
rm -f $agent_pkey_file
530+
rm -f $operator_pkey_file
531+
exit 1
547532
fi
533+
fi
548534

549-
# deploy on-chain service
550-
if [ $(get_on_chain_service_state $service_id) == "FINISHED_REGISTRATION" ]; then
551-
echo "[Service owner] Deploying on-chain service $service_id..."
552-
output=$(poetry run autonomy service --use-custom-chain deploy "$service_id" --key "$operator_pkey_file" --reuse-multisig)
553-
if [[ $? -ne 0 ]]; then
554-
echo "Deploying service failed.\n$output"
555-
echo "Please, delete or rename the ./trader folder and try re-run this script again."
556-
rm -f $agent_pkey_file
557-
rm -f $operator_pkey_file
558-
exit 1
559-
fi
535+
# deploy on-chain service
536+
service_state=$(get_on_chain_service_state $service_id)
537+
if [ "$service_state" == "FINISHED_REGISTRATION" ] && [ "$first_run" = "true" ]; then
538+
echo "[Service owner] Deploying on-chain service $service_id..."
539+
output=$(poetry run autonomy service --use-custom-chain deploy "$service_id" --key "$operator_pkey_file")
540+
if [[ $? -ne 0 ]]; then
541+
echo "Deploying service failed.\n$output"
542+
echo "Please, delete or rename the ./trader folder and try re-run this script again."
543+
rm -f $agent_pkey_file
544+
rm -f $operator_pkey_file
545+
exit 1
546+
fi
547+
elif [ "$service_state" == "FINISHED_REGISTRATION" ]; then
548+
echo "[Service owner] Deploying on-chain service $service_id..."
549+
output=$(poetry run autonomy service --use-custom-chain deploy "$service_id" --key "$operator_pkey_file" --reuse-multisig)
550+
if [[ $? -ne 0 ]]; then
551+
echo "Deploying service failed.\n$output"
552+
echo "Please, delete or rename the ./trader folder and try re-run this script again."
553+
rm -f $agent_pkey_file
554+
rm -f $operator_pkey_file
555+
exit 1
560556
fi
557+
fi
561558

562-
# delete the pkey files
563-
rm -f $agent_pkey_file
564-
rm -f $operator_pkey_file
565-
echo ""
566-
echo "Finished update of on-chain service $service_id."
559+
# delete the pkey files
560+
rm -f $agent_pkey_file
561+
rm -f $operator_pkey_file
562+
563+
# check state
564+
service_state=$(get_on_chain_service_state $service_id)
565+
if [ "$service_state" != "DEPLOYED" ]; then
566+
echo "Something went wrong while deploying on-chain service. The service's state is $service_state."
567+
echo "Please check the output of the script and the on-chain registry for more information."
568+
exit 1
567569
fi
568570

571+
echo ""
572+
echo "Finished checking Autonolas Protocol service $service_id state."
573+
574+
569575
echo ""
570576
echo "------------------------------"
571577
echo "Starting the trader service..."
572578
echo "------------------------------"
573579
echo ""
574580

575-
# check state
576-
expected_state="| Service State | DEPLOYED |"
577-
service_info=$(poetry run autonomy service --use-custom-chain info "$service_id")
578-
service_state=$(echo "$service_info" | grep "Service State")
579-
if [ "$service_state" != "$expected_state" ]
580-
then
581-
echo "Something went wrong while deploying the service. The service's state is:"
582-
echo "$service_state"
583-
echo "Please check the output of the script for more information."
584-
exit 1
585-
fi
586-
587581
# Get the deployed service's Safe address from the contract
582+
service_info=$(poetry run autonomy service --use-custom-chain info "$service_id")
588583
safe=$(echo "$service_info" | grep "Multisig Address")
589584
address_start_position=31
590585
safe=$(echo "$safe" |

0 commit comments

Comments
 (0)