Skip to content

fix(scripts): distinct malformed-JSON/missing-jq errors + contract balance path for check_balance.sh (#7889) - #8647

Open
DSP-AID wants to merge 2 commits into
Scottcjn:mainfrom
DSP-AID:fix/7889-balance-error-gaps
Open

DSP-AID wants to merge 2 commits into
Scottcjn:mainfrom
DSP-AID:fix/7889-balance-error-gaps

Conversation

@DSP-AID

@DSP-AID DSP-AID commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Consistent error handling + exit codes — wallet balance check

Addresses #7889 (base fix was merged earlier in #8024; this PR closes the remaining inconsistencies that were still live on main).
Bounty issue: Scottcjn/rustchain-bounties#16253

Inconsistencies fixed (enumerated per acceptance criteria)

Pre-existing in the merged scripts/check_balance.sh on main:

  1. Malformed JSON was not reported distinctly — a non-JSON body (e.g. an nginx HTML error page behind a proxy) fell through to Error: response missing amount_rtc field. Issue Bug: Inconsistent Error Handling in Wallet Balance Check Script #7889 / bounty deliverable DOS Tools Initial Upload #1 explicitly requires malformed JSON to produce a clear, distinct message. → Now: Error: malformed JSON response from <URL> (exit 3), while the missing amount_rtc message is reserved for well-formed JSON that lacks the field.

  2. Missing jq silently misreported as a bad response — without jq, every jq call is swallowed by 2>/dev/null || true, so users on hosts without jq got Error: response missing amount_rtc field (exit 3) instead of a clear dependency error. → Now an explicit preflight check: Error: required dependency 'jq' is not installed (exit 1), documented in the header comment and --help.

  3. Default request path did not match the repo's own API contract — the script requested /api/balance?wallet=…, which returns HTTP 404 against the public RPC and against the documented read-only contract (compatibility_lab/read_only_api.openapi.json → getWalletBalance = GET /wallet/balance?miner_id=…). A balance tool whose default happy path always errors is exactly the Bug: Inconsistent Error Handling in Wallet Balance Check Script #7889 complaint ("can confuse users troubleshooting miner setup"). → Now uses the contract path /wallet/balance?miner_id=…; live probe:

    https://rustchain.org/api/balance?wallet=Ahqb…G1iG  -> HTTP 404 (old)
    https://rustchain.org/wallet/balance?address=Ahqb…G1iG -> HTTP 200 {"amount_i64":0,"amount_rtc":0.0,"miner_id":"Ahqb…"}
    

Exit-code scheme unchanged and now fully accurate in --help / header: 0 ok, 1 usage/environment (incl. missing jq), 2 network, 3 bad response.

Tests (no behavior change on the happy path)

New hermetic suite tests/test_check_balance_script_7889.py — runs the real bash script against a local HTTP stub (no external network; connection-refused via closed port), so it is collected by the existing pytest tests/ CI step. Skips cleanly on hosts without bash/jq (Windows). Covers every failure path: no args, --help, invalid wallet, missing jq, connection refused, non-200, empty body, malformed JSON (asserts the distinct message), missing field, non-numeric amount, wallet mismatch, and the happy path (stdout = balance only, exit 0).

$ python -m pytest tests/test_check_balance_script_7889.py -v   # python:3.11-bookworm + jq
12 passed in 4.33s

$ bash tests/scripts/test_check_balance.sh                      # existing suite, regression check
Results: 7 passed, 0 failed

Live transcript after the change (docker python:3.11-bookworm, curl+jq):

=== 1. live RPC happy path ===
0
exit: 0
=== 2. malformed JSON -> distinct error (was: missing amount_rtc) ===
Error: malformed JSON response from http://127.0.0.1:8099/wallet/balance?miner_id=Ahqb…G1iG
exit: 3
=== 3. missing jq dependency -> exit 1 ===
Error: required dependency 'jq' is not installed
exit: 1

Acceptance criteria

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Welcome to RustChain! Thanks for your first pull request.

Before we review, please make sure:

  • Non-doc PRs have a BCOS-L1 or BCOS-L2 label
  • Doc-only PRs are exempt from BCOS tier labels when they only touch docs/**, *.md, or common image/PDF files
  • New code files include an SPDX license header
  • You've tested your changes against the live node

Bounty tiers: Micro (1-10 RTC) | Standard (20-50) | Major (75-100) | Critical (100-150)

A maintainer will review your PR soon. Thanks for contributing!

@github-actions github-actions Bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) tests Test suite changes size/M PR: 51-200 lines labels Nov 8, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) size/M PR: 51-200 lines tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants