Skip to content

Commit b8f0ec2

Browse files
committed
Better upgrade txsub handling:
- actually switch/case on status code - retries on TRY_AGAIN_LATER - decompose into functions
1 parent 08a9338 commit b8f0ec2

1 file changed

Lines changed: 132 additions & 40 deletions

File tree

start

Lines changed: 132 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -663,58 +663,150 @@ function kill_supervisor() {
663663
kill -3 $(cat "/var/run/supervisord.pid")
664664
}
665665

666+
function fail_soroban_config_upgrade() {
667+
echo "!!!!! $1. Stopping all services. !!!!!"
668+
kill_supervisor
669+
return 1
670+
}
671+
672+
function get_ledger_transaction_count() {
673+
curl -s http://localhost:11626/metrics | jq -r '.metrics."ledger.transaction.count".count'
674+
}
675+
676+
function wait_for_ledger_transaction_count() {
677+
while [ "$(get_ledger_transaction_count)" -lt "$1" ]; do
678+
sleep 1
679+
done
680+
}
681+
682+
function submit_soroban_config_tx() {
683+
local label="$1"
684+
local tx="$2"
685+
local txid="$3"
686+
local attempt=1
687+
local max_attempts=30
688+
689+
while true; do
690+
local response
691+
local status
692+
693+
response="$(curl -sG 'http://localhost:11626/tx' --data-urlencode "blob=$tx")"
694+
status="$(echo "$response" | jq -r '.status')"
695+
696+
echo "upgrades: soroban config: $label: $txid .. $status"
697+
698+
case "$status" in
699+
PENDING|DUPLICATE)
700+
return 0
701+
;;
702+
TRY_AGAIN_LATER)
703+
if [ "$attempt" -ge "$max_attempts" ]; then
704+
echo "$response"
705+
fail_soroban_config_upgrade "Unable to submit Soroban config transaction '$label' after $attempt attempts"
706+
fi
707+
708+
attempt=$((attempt+1))
709+
sleep 1
710+
;;
711+
ERROR|FILTERED)
712+
echo "$response"
713+
fail_soroban_config_upgrade "Unable to submit Soroban config transaction '$label'"
714+
;;
715+
*)
716+
echo "$response"
717+
fail_soroban_config_upgrade "Unexpected status '$status' while submitting Soroban config transaction '$label'"
718+
;;
719+
esac
720+
done
721+
}
722+
723+
function apply_soroban_config_tx() {
724+
submit_soroban_config_tx "$1" "$2" "$3"
725+
wait_for_ledger_transaction_count "$4"
726+
}
727+
728+
function set_soroban_config_upgrade() {
729+
local key="$1"
730+
local attempt=1
731+
local max_attempts=10
732+
733+
while true; do
734+
local output
735+
736+
output="$(curl -sG 'http://localhost:11626/upgrades?mode=set&upgradetime=1970-01-01T00:00:00Z' --data-urlencode "configupgradesetkey=$key")"
737+
echo "$output"
738+
739+
if [ "$output" != "Error setting configUpgradeSet" ]; then
740+
return 0
741+
fi
742+
743+
if [ "$attempt" -ge "$max_attempts" ]; then
744+
fail_soroban_config_upgrade "Unable to upgrade Soroban Config Settings"
745+
fi
746+
747+
attempt=$((attempt+1))
748+
sleep 1
749+
done
750+
}
751+
666752
function upgrade_soroban_config() {
667753
local config_file_path="$1"
668754
local seq_num="$2"
755+
local upgrade_output
756+
local line_count
669757

670758
# Generate txs for installing, deploying and executing the contract that
671759
# uploads a new config. Use the network root account to submit the txs.
672-
upgrade_output="$(echo $NETWORK_ROOT_SECRET_KEY \
760+
upgrade_output="$(echo "$NETWORK_ROOT_SECRET_KEY" \
673761
| stellar-core get-settings-upgrade-txs \
674762
"$NETWORK_ROOT_ACCOUNT_ID" \
675763
"$seq_num" \
676764
"$NETWORK_PASSPHRASE" \
677-
--xdr `stellar-xdr encode --type ConfigUpgradeSet < "$config_file_path"` \
765+
--xdr "$(stellar-xdr encode --type ConfigUpgradeSet < "$config_file_path")" \
678766
--signtxs)"
679767

680-
let line_count=$(echo "$upgrade_output" | wc -l)
681-
682-
echo "$upgrade_output" | { \
683-
TX_COUNT="`curl -s http://localhost:11626/metrics | jq -r '.metrics."ledger.transaction.count".count'`"
684-
TX_COUNT=$((TX_COUNT+1))
685-
# If the line count is 9 instead of 7, a version of core is being used where the restore op is being returned
686-
if [ $line_count = 9 ] ; then
687-
read tx;
688-
read txid;
689-
echo "upgrades: soroban config: restore contract: $txid .. $(curl -sG 'http://localhost:11626/tx' --data-urlencode "blob=$tx" | jq -r '.status')";
690-
while [ "`curl -s http://localhost:11626/metrics | jq -r '.metrics."ledger.transaction.count".count'`" != "$TX_COUNT" ]; do sleep 1; done
691-
TX_COUNT=$((TX_COUNT+1))
692-
fi
693-
read tx; \
694-
read txid; \
695-
echo "upgrades: soroban config: install contract: $txid .. $(curl -sG 'http://localhost:11626/tx' --data-urlencode "blob=$tx" | jq -r '.status')"; \
696-
while [ "`curl -s http://localhost:11626/metrics | jq -r '.metrics."ledger.transaction.count".count'`" != "$TX_COUNT" ]; do sleep 1; done
697-
TX_COUNT=$((TX_COUNT+1)); \
698-
read tx; \
699-
read txid; \
700-
echo "upgrades: soroban config: deploy contract: $txid .. $(curl -sG 'http://localhost:11626/tx' --data-urlencode "blob=$tx" | jq -r '.status')"; \
701-
while [ "`curl -s http://localhost:11626/metrics | jq -r '.metrics."ledger.transaction.count".count'`" != "$TX_COUNT" ]; do sleep 1; done
702-
TX_COUNT=$((TX_COUNT+1)); \
703-
read tx; \
704-
read txid; \
705-
echo "upgrades: soroban config: upload config: $txid .. $(curl -sG 'http://localhost:11626/tx' --data-urlencode "blob=$tx" | jq -r '.status')"; \
706-
while [ "`curl -s http://localhost:11626/metrics | jq -r '.metrics."ledger.transaction.count".count'`" != "$TX_COUNT" ]; do sleep 1; done
707-
TX_COUNT=$((TX_COUNT+1)); \
708-
read key; \
709-
echo "upgrades: soroban config: set config with key: $key";
710-
OUTPUT="$(curl -sG 'http://localhost:11626/upgrades?mode=set&upgradetime=1970-01-01T00:00:00Z' --data-urlencode "configupgradesetkey=$key")"
711-
echo "$OUTPUT"; \
712-
713-
if [ "$OUTPUT" == "Error setting configUpgradeSet" ]; then
714-
echo "!!!!! Unable to upgrade Soroban Config Settings. Stopping all services. !!!!!"
715-
kill_supervisor
716-
fi
717-
}
768+
line_count="$(echo "$upgrade_output" | wc -l | tr -d ' ')"
769+
770+
if [ "$line_count" -ne 7 ] && [ "$line_count" -ne 9 ]; then
771+
echo "$upgrade_output"
772+
fail_soroban_config_upgrade "Unexpected output from stellar-core get-settings-upgrade-txs"
773+
fi
774+
775+
echo "$upgrade_output" | {
776+
local expected_tx_count
777+
local tx
778+
local txid
779+
local key
780+
781+
expected_tx_count=$(( $(get_ledger_transaction_count) + 1 ))
782+
783+
# If 9 lines are returned instead of 7, core included a restore transaction.
784+
if [ "$line_count" -eq 9 ]; then
785+
read tx
786+
read txid
787+
apply_soroban_config_tx "restore contract" "$tx" "$txid" "$expected_tx_count"
788+
expected_tx_count=$((expected_tx_count+1))
789+
fi
790+
791+
read tx
792+
read txid
793+
apply_soroban_config_tx "install contract" "$tx" "$txid" "$expected_tx_count"
794+
expected_tx_count=$((expected_tx_count+1))
795+
796+
read tx
797+
read txid
798+
apply_soroban_config_tx "deploy contract" "$tx" "$txid" "$expected_tx_count"
799+
expected_tx_count=$((expected_tx_count+1))
800+
801+
read tx
802+
read txid
803+
apply_soroban_config_tx "upload config" "$tx" "$txid" "$expected_tx_count"
804+
805+
read key
806+
echo "upgrades: soroban config: set config with key: $key"
807+
set_soroban_config_upgrade "$key"
808+
}
809+
718810
echo "upgrades: soroban config done"
719811
}
720812

0 commit comments

Comments
 (0)