API Heartbeat Watchdog #58569
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: API Heartbeat Watchdog | |
| on: | |
| workflow_dispatch: | |
| jobs: | |
| heartbeat: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Commit and Reveal Randomness | |
| env: | |
| API_KEY: ${{ secrets.WATCHDOG_API_KEY }} | |
| API_URL: "https://api.blockrand.net/api/v1" | |
| run: | | |
| echo "--- Starting Heartbeat ---" | |
| # 1. GENERATE DYNAMIC SECRET & HASH (Like your JS code) | |
| # We use a random string and hash it locally first | |
| SECRET="wd_$(date +%s)_$RANDOM" | |
| HASH=$(echo -n "$SECRET" | sha256sum | awk '{print $1}') | |
| echo "Generated Secret: $SECRET" | |
| echo "Generated Hash: $HASH" | |
| # 2. COMMIT | |
| COMMIT_RES=$(curl -s -X POST "$API_URL/commit" \ | |
| -H "X-API-KEY: $API_KEY" \ | |
| -H "Content-Type: application/json" \ | |
| -d "{\"player_id\": \"watchdog\", \"player_hash\": \"$HASH\", \"delay_sec\": 10}") | |
| echo "Commit Response: $COMMIT_RES" | |
| TARGET_ROUND=$(echo $COMMIT_RES | jq -r '.target_round') | |
| if [ "$TARGET_ROUND" == "null" ] || [ -z "$TARGET_ROUND" ]; then | |
| echo "Error: Failed to get target_round" | |
| exit 1 | |
| fi | |
| # 3. WAIT | |
| # 20s to be safe for the 10s delay + Drand publishing | |
| echo "Waiting 20s for Round $TARGET_ROUND..." | |
| sleep 20 | |
| # 4. REVEAL | |
| REVEAL_RES=$(curl -s -X POST "$API_URL/reveal" \ | |
| -H "X-API-KEY: $API_KEY" \ | |
| -H "Content-Type: application/json" \ | |
| -d "{\"player_id\": \"watchdog\", \"player_secret\": \"$SECRET\", \"target_round\": $TARGET_ROUND}") | |
| echo "Reveal Response: $REVEAL_RES" | |
| # 5. VALIDATION | |
| STATUS=$(echo $REVEAL_RES | jq -r '.status') | |
| if [[ "$STATUS" == "revealed" || "$STATUS" == "ready" ]]; then | |
| echo "--- Heartbeat Successful! ---" | |
| else | |
| echo "Heartbeat Failed: Status is $STATUS" | |
| exit 1 | |
| fi |