From e9ed1c0cd354b2a3086b8cc53f0ce5e6ef3b1472 Mon Sep 17 00:00:00 2001 From: rob Date: Thu, 18 Jun 2026 14:49:34 +0000 Subject: [PATCH] check-health.sh: add retry logic for hash comparison to fix false-positive forked status --- check-health.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/check-health.sh b/check-health.sh index 02db18f3..6402770a 100755 --- a/check-health.sh +++ b/check-health.sh @@ -103,10 +103,52 @@ if [ $? -eq 0 ]; then rm "$response_file" - if [ -n "$ref" ]; then - response_file2=$(mktemp) + if [ -n "$ref" ]; then + MAX_RETRIES=3 + attempt=1 + + 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) - sleep 3 # to give the reference node more time to import the block if it is very current + 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