check-health.sh: add retry logic for hash comparison to fix false-positive forked status

This commit is contained in:
rob
2026-06-18 14:49:34 +00:00
parent a3a78cb3be
commit e9ed1c0cd3

View File

@@ -104,9 +104,51 @@ if [ $? -eq 0 ]; then
rm "$response_file"
if [ -n "$ref" ]; then
response_file2=$(mktemp)
MAX_RETRIES=3
attempt=1
sleep 3 # to give the reference node more time to import the block if it is very current
while [ $attempt -le $MAX_RETRIES ]; do
# Re-fetch local latest block for retries > 1
if [ $attempt -gt 1 ]; then
sleep 3
# Re-query local latest block and update variables
response_file=$(mktemp)
http_status_code=$(curl -L --ipv4 -m $timeout -s -X POST -w "%{http_code}" -o "$response_file" -H "Content-Type: application/json" --data "$rpc_method" $RPC_URL)
if [ $? -eq 0 ] && [[ $http_status_code -eq 200 ]]; then
response=$(cat "$response_file")
if $is_starknet; then
latest_block_timestamp_decimal=$(echo "$response" | jq -r '.result.timestamp')
latest_block_number=$(echo "$response" | jq -r '.result.block_number')
latest_block_hash=$(echo "$response" | jq -r '.result.block_hash')
elif $is_aztec; then
latest_block_number=$(echo "$response" | jq -r '.result.header.globalVariables.blockNumber')
latest_block_timestamp_decimal=$(echo "$response" | jq -r '.result.header.globalVariables.timestamp')
latest_block_hash=$(echo "$response" | jq -r '.result.blockHash')
if [ "$latest_block_number" = "null" ] || [ "$latest_block_timestamp_decimal" = "null" ] || [ -z "$latest_block_timestamp_decimal" ]; then
echo "error"
exit 1
fi
else
latest_block_timestamp=$(echo "$response" | jq -r '.result.timestamp')
latest_block_timestamp_decimal=$((16#${latest_block_timestamp#0x}))
latest_block_number=$(echo "$response" | jq -r '.result.number')
latest_block_hash=$(echo "$response" | jq -r '.result.hash')
fi
current_time=$(date +%s)
time_difference=$((current_time - latest_block_timestamp_decimal))
rm "$response_file"
else
rm "$response_file"
echo "error"
exit 1
fi
fi
response_file2=$(mktemp)
if [ $attempt -eq 1 ]; then
sleep 3 # to give the reference node more time to import the block if it is very current
fi
if $is_starknet; then
# Starknet uses block_id object with block_number
@@ -248,14 +290,23 @@ if [ $? -eq 0 ]; then
fi
fi
else
echo "forked"
exit 1
# Hash mismatch - retry if we have attempts left
if [ $attempt -lt $MAX_RETRIES ]; then
rm "$response_file2"
attempt=$((attempt + 1))
continue
else
rm "$response_file2"
echo "forked"
exit 1
fi
fi
else
echo "unverified ($http_status_code2)"
exit 1
fi
fi
done
echo "unverified ($curl_code)"
exit 0