Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: script not waiting for chain to start if query block command fails #7

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name: "Test action"

on:
pull_request:

Expand All @@ -19,8 +21,10 @@ jobs:
version: v4.4.1

- name: Test chain started
run: "[[ \"$(./desmos q block | jq '.block')\" != \"null\" ]]"
run: "[[ \"$($DESMOS_BIN q block | jq '.block')\" != \"null\" ]]"
shell: bash
env:
DESMOS_BIN: ${{ steps.start-chain.outputs.desmos-bin }}

- name: Check outputs
run: '[[ ! -z "$DESMOS_BIN" ]] && [[ ! -z "$DESMOS_HOME" ]]'
Expand Down
36 changes: 24 additions & 12 deletions start-chain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ log() {
fi
}

stderr_echo() {
>&2 echo "$1"
}


# Function to download the desmos binary at a specific version.
# * `version` - The version to do download, must be in the format vX.X.X.
Expand Down Expand Up @@ -57,10 +61,10 @@ prepare_chain() {
# Get chain id from genesis file
user_chain_id=$(jq -r '.chain_id' "$user_genesis_file")
$DESMOS_BIN testnet --v 1 --keyring-backend=test --chain-id="$user_chain_id" \
--gentx-coin-denom="stake" --minimum-gas-prices="0.000006stake" > /dev/null 2>&1
--gentx-coin-denom="stake" --minimum-gas-prices="0stake" --output-dir "$SCRIPT_DIR/mytestnet" > /dev/null 2>&1
else
$DESMOS_BIN testnet --v 1 --keyring-backend=test \
--gentx-coin-denom="stake" --minimum-gas-prices="0.000006stake" > /dev/null 2>&1
--gentx-coin-denom="stake" --minimum-gas-prices="0stake" --output-dir "$SCRIPT_DIR/mytestnet" > /dev/null 2>&1
fi

# Generated genesis file path
Expand Down Expand Up @@ -111,19 +115,29 @@ run_script() {
fi

# Run the script
bash "$script_path"
output=$(bash "$script_path")
# On failure write script output to stderr
if [ $? != 0 ]; then
>&2 echo "$output"
fi
fi
}

wait_chain_start() {
log "Waiting for chain to start..."
# Give time to the binary to start
sleep 2
block="$($DESMOS_BIN q block 2> /dev/null | jq '.block')"
attempts=0
while [ "$block" == "null" ] || [ -z "$block" ]; do
# Fail if the chain don't come online
if [ "$attempts" == 10 ] ; then
stderr_echo "Chain start failed"
>&2 cat ./start-chain.log
break
fi

block="$($DESMOS_BIN q block | jq '.block')"
while [ "$block" == "null" ]; do
sleep 1
block="$($DESMOS_BIN q block | jq '.block')"
block="$($DESMOS_BIN q block 2> /dev/null | jq '.block')"
((attempts=attempts+1))
done

log "Chain started!"
Expand All @@ -141,8 +155,7 @@ download_desmos "$1"
prepare_chain "$2"

# Run the pre run script
pre_run_script_output=$(DESMOS_HOME="$DESMOS_HOME" DESMOS_BIN="$DESMOS_BIN" run_script "$3")
log "$pre_run_script_output"
DESMOS_HOME="$DESMOS_HOME" DESMOS_BIN="$DESMOS_BIN" run_script "$3"

# Sart the chain as background process
run_chain
Expand All @@ -151,5 +164,4 @@ run_chain
wait_chain_start

# Run the post run script
post_run_script_output=$(DESMOS_HOME="$DESMOS_HOME" DESMOS_BIN="$DESMOS_BIN" run_script "$4")
log "$post_run_script_output"
DESMOS_HOME="$DESMOS_HOME" DESMOS_BIN="$DESMOS_BIN" run_script "$4"