#!/usr/bin/env bash
# WEIGHT=10
# CHECK: Bulk-sync masternode-mismatch fallback runs CompareSignersLists unconditionally
# REF: verifyHeader.go calcMasternodes-failure branch must NOT skip integrity checks
set -e
vh="$PATIENT/consensus/XDPoS/engines/engine_v2/verifyHeader.go"
[[ -f "$vh" ]] || exit 1
# Failing pattern: the CompareSignersLists block sits inside an `} else {` after a parents-fallback.
# Passing pattern: CompareSignersLists runs regardless of whether parents are present.
# Heuristic check: count occurrences of CompareSignersLists; check it's not nested inside an `if len(parents) > 0` followed by `else`.
# Crude but effective: look for the specific regression pattern.
if grep -qE 'len\(parents\)\s*>\s*0' "$vh"; then
  # If len(parents) > 0 is present, ensure CompareSignersLists is NOT inside an else branch of that conditional.
  # Look for: pattern of `} else {` followed within ~10 lines by CompareSignersLists.
  awk '
    /len\(parents\)\s*>\s*0/ { in_parents_branch = 1; next }
    in_parents_branch && /} else {/ { in_else = 1; next }
    in_else && /CompareSignersLists/ { found_inside_else = 1; exit }
    in_else && /^}/ { in_else = 0; in_parents_branch = 0 }
    END { exit (found_inside_else ? 1 : 0) }
  ' "$vh"
else
  # No parents-branch present; the regression doesn't exist.
  exit 0
fi
