Skip to content

Commit

Permalink
Ensure accounts are taken offline when switching networks (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsoerensen authored Aug 17, 2024
1 parent e2d2732 commit a160160
Showing 1 changed file with 80 additions and 7 deletions.
87 changes: 80 additions & 7 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ wallet_password=""
new_user_setup=0
new_network=0
network_status_url=""
container_id=""

voi_logo="MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
Expand Down Expand Up @@ -222,12 +223,31 @@ wait_for_stack_to_be_ready() {
display_banner "Stack is ready!"
}
function get_container_id() {
case ${VOINETWORK_PROFILE} in
"relay")
container_id=$(docker ps -q -f name=voinetwork_relay)
;;
"developer")
container_id=$(docker ps -q -f name=voinetwork_developer)
;;
"archiver")
container_id=$(docker ps -q -f name=voinetwork_archiver)
;;
"participation")
container_id=$(docker ps -q -f name=voinetwork_algod)
;;
*)
abort "Invalid profile. Exiting the program."
;;
esac
}
verify_node_is_running() {
local retries=0
local max_retries=5
while [[ $retries -lt $max_retries ]]; do
container_id=$(execute_sudo "docker ps -q -f name=voinetwork_algod")
if [[ -n "$container_id" ]]; then
execute_sudo "docker exec ${container_id} bash -c \"curl -sS http://localhost:8080/health > /dev/null\""
local exit_code=$?
Expand Down Expand Up @@ -309,6 +329,7 @@ catchup_node() {
get_node_status
sleep 5
done
echo ""
display_banner "Caught up with the network!"
}
Expand Down Expand Up @@ -475,6 +496,37 @@ generate_new_key() {
execute_interactive_docker_command "/node/bin/goal account addpartkey -a ${address} --roundFirstValid ${start_block} --roundLastValid ${end_block}"
}
ensure_accounts_are_offline() {
account_addresses=$(get_account_addresses)
if [[ -z ${account_addresses} ]]; then
return
fi
if [[ ${container_id} == "" ]]; then
echo "Skipping account offline check as container not found."
return
fi
display_banner "Migrating to new network. Ensuring all accounts are offline."
echo "Checking if accounts are online and have a balance of 1,000 microVoi or more."
echo "Accounts with a balance of 1,000 microVoi or more will be taken offline."
for account in ${account_addresses}; do
local balance
balance=$(get_account_balance "${account}")
account_status=$(execute_docker_command "/node/bin/goal account dump -a ${account}" | jq -r .onl)
if [[ ${balance} -ge 1000 && ${account_status} -eq 1 ]]; then
echo ""
echo "Balance is ${balance} which is above 1,000 microVoi. Taking account ${account} offline."
execute_interactive_docker_command "/node/bin/goal account changeonlinestatus -a ${account} -o=false"
echo "Account ${account} is now offline!"
fi
done
}
generate_participation_key() {
display_banner "Generating/Updating participation key"
Expand Down Expand Up @@ -504,12 +556,12 @@ generate_participation_key() {
return 1
fi
local balance
balance=$(get_account_balance "${account}")
if [[ ${account_addresses_length} -eq 1 ]]; then
generate_new_key "${account}"
else
local balance
balance=$(get_account_balance "${account}")
if [[ ${balance} -ge 1000 ]]; then
echo "Balance is equal/above 1,000 microVoi. Generating participation key for account ${account}"
generate_new_key "${account}"
Expand All @@ -524,6 +576,7 @@ generate_participation_key() {
else
echo "Balance is below 1,000 microVoi. Skipping participation key generation for account ${account}"
fi
echo ""
fi
elif [[ $((active_key_last_valid_round-last_committed_block)) -le 417104 ]]; then
local existing_expiration_date
Expand Down Expand Up @@ -625,10 +678,10 @@ joined_network_instructions() {
echo "Account setup skipped. Multiple accounts detected in your wallet."
else
echo "Account setup skipped. Detected existing account with address: ${account_addr}"
fi

echo "To see network participation status use ${HOME}/voi/bin/get-participation-status ${account_addr}"
echo "To go online use ${HOME}/voi/bin/go-online ${account_addr}"
echo "To see network participation status use ${HOME}/voi/bin/get-participation-status ${account_addr}"
echo "To go online use ${HOME}/voi/bin/go-online ${account_addr}"
fi
fi

# Display information informing the user that the network will catch up in the background if used in non-interactive mode
Expand Down Expand Up @@ -676,10 +729,23 @@ change_account_online_status() {

join_as_new_user() {
new_user_setup=1
local account_addresses
account_addresses=$(get_account_addresses)

display_banner "Joining network"

generate_participation_key

# If there are multiple accounts, we don't want to go online automatically, unless previously done
# as part of participation key generation.
if [[ $(echo "$account_addresses" | wc -l) -gt 1 ]]; then
display_banner "Welcome to Voi!"

joined_network_instructions

exit
fi

busy_wait_until_balance_is_sufficient

change_account_online_status "${account}"
Expand Down Expand Up @@ -1084,6 +1150,11 @@ fi

check_minimum_requirements

if [[ ${new_network} -eq 1 ]]; then
get_container_id
ensure_accounts_are_offline
fi

get_telemetry_name
set_telemetry_name

Expand Down Expand Up @@ -1149,6 +1220,7 @@ start_stack
wait_for_stack_to_be_ready

if [[ ${VOINETWORK_PROFILE} == "participation" ]]; then
get_container_id
verify_node_is_running
fi

Expand Down Expand Up @@ -1219,6 +1291,7 @@ if [[ ${skip_account_setup} -eq 0 || ${new_network} -eq 1 ]]; then
else
generate_participation_key
participation_key_generation_status=$?

## Catch cases where an install was aborted / user didn't succeed in going online
## This can happen where there are no part keys present on the machine, or where there's multiple part keys but
## no key is active.
Expand Down

0 comments on commit a160160

Please sign in to comment.