Skip to content

fix(utxo): reject invalid values in add_box and flag them in integrity_check - #8516

Merged
Scottcjn merged 1 commit into
Scottcjn:mainfrom
pujitha24:auto/issue-8513
Sep 25, 2026
Merged

Scottcjn merged 1 commit into
Scottcjn:mainfrom
pujitha24:auto/issue-8513

Conversation

@pujitha24

Copy link
Copy Markdown
Contributor

BCOS Checklist (Required For Non-Doc PRs)

  • Add a tier label: BCOS-L1 or BCOS-L2 (also accepted: bcos:l1, bcos:l2). Suggested: BCOS-L1. I can't add labels from a fork, so a maintainer will need to add it.
  • If adding new code files, include SPDX header near the top (example: # SPDX-License-Identifier: MIT). N/A: no new files.
  • Provide test evidence (commands + output or screenshots)

What Changed

Fixes the validation gap in UtxoDB.add_box() and the blind spot in integrity_check() reported in #8513.

  • node/utxo_db.py add_box(): raises ValueError unless value_nrtc is a positive int64. It reuses _is_positive_int64, the same check apply_transaction() already runs on outputs. The check happens before a connection is opened, so nothing gets written. I left the dust threshold out on purpose: add_box is the low-level insert, and dust is a transaction-policy rule.
  • node/utxo_db.py integrity_check(): the SELECT that computes the totals now also counts unspent boxes whose value is not a positive integer (typeof(value_nrtc) != 'integer' OR value_nrtc <= 0). Because it's the same statement, the count comes from the same snapshot as the totals. If any are found, it sets ok=False and reports invalid_value_boxes. Before this, a -50 box and a +200 box summed to the expected 150 and got ok=True, models_agree=True.
  • node/test_utxo_db.py: two new tests covering both changes.

Impact: this is defense-in-depth. I found no production caller of add_box() in node/. Its only callers are tests. The production writers (apply_transaction, the account-mirror path, genesis migration) already reject non-positive values, and their behaviour is unchanged. The integrity_check change applies to any unspent row, whichever path wrote it, so it would also flag a bad row written by older code or by hand.

Testing / Evidence

Run in a local venv with pytest, flask and ruff installed.

The new targeted tests fail without the utxo_db.py change and pass with it:

$ cd node && python -m pytest test_utxo_db.py -q -k "invalid_value or non_positive"
# without the fix: 8 failed, 107 deselected   (2 tests + 6 subtests)
# with the fix:    2 passed, 107 deselected, 6 subtests passed

UTXO suites that call add_box / integrity_check:

$ cd node && python -m pytest test_utxo_db.py test_utxo_endpoints.py test_utxo_add_box_conflict_poc.py \
    test_utxo_a9_a10_a11_poc.py test_spendbox_validation.py test_mempool_rollback.py \
    tests/test_account_utxo_pending_confirm_ownership.py tests/test_utxo_integrity_single_snapshot.py \
    ../tests/test_utxo_dual_write_double_spend.py ../tests/test_spend_box_rollback_bug2.py \
    ../tests/test_utxo_mempool_atomicity_bug1_bug4.py -q
197 passed, 1 warning, 48 subtests passed

CI lint subset:

$ python -m ruff check node/utxo_db.py node/test_utxo_db.py --select E9,F63,F7,F82
All checks passed!

Note: tests/security_audit_real_code.py::test_vuln1_mining_reward_minting fails on main both with and without this change. It is unrelated, and the tests/ CI job doesn't collect it.

This change was prepared with AI assistance. I reviewed it and ran the tests above locally.

🤖 Generated with Claude Code


AI assistance: this change was drafted with Claude Code.

Fixes #8513

…y_check

Motivation
UtxoDB.add_box() inserted value_nrtc without validation, so a negative,
zero or non-integer value could be persisted. integrity_check() only
compared the aggregate sum, so a -50 box and a +200 box together matched
an expected total of 150 and reported ok=True / models_agree=True.
No production caller of add_box() exists in node/ (only tests), and the
production writers (apply_transaction, account mirror, genesis
migration) already reject non-positive values; this is defense-in-depth.

Approach
- add_box() raises ValueError unless value_nrtc passes the existing
  _is_positive_int64 check that apply_transaction() applies to outputs.
  The check runs before any connection is opened. Dust threshold is not
  enforced here, since it is transaction policy, not a box invariant.
- integrity_check() counts unspent boxes whose value is not a positive
  integer in the same SELECT as the totals (same snapshot); if any exist
  it sets ok=False and reports invalid_value_boxes. This applies to any
  such row, whichever path wrote it.

Validation (local venv with pytest, flask, ruff)
- cd node && python -m pytest test_utxo_db.py -q -k "invalid_value or non_positive"
  without the utxo_db.py change: 8 failed (2 tests + 6 subtests);
  with it: 2 passed, 6 subtests passed.
- cd node && python -m pytest test_utxo_db.py test_utxo_endpoints.py
  test_utxo_add_box_conflict_poc.py test_utxo_a9_a10_a11_poc.py
  test_spendbox_validation.py test_mempool_rollback.py
  tests/test_account_utxo_pending_confirm_ownership.py
  tests/test_utxo_integrity_single_snapshot.py
  ../tests/test_utxo_dual_write_double_spend.py
  ../tests/test_spend_box_rollback_bug2.py
  ../tests/test_utxo_mempool_atomicity_bug1_bug4.py -q
  -> 197 passed, 48 subtests passed.
- python -m ruff check node/utxo_db.py node/test_utxo_db.py
  --select E9,F63,F7,F82 -> All checks passed.

Report: #8513

Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
Assisted-by: claude-opus-5-5 (via Claude Code)
@github-actions

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) BCOS-L2 Beacon Certified Open Source tier BCOS-L2 (required for non-doc PRs) node Node server related size/M PR: 51-200 lines labels Sep 24, 2026

@Scottcjn Scottcjn left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pujitha24. This is a clean, well-scoped fix for #8513, and the PR description is precise.

  • node/utxo_db.py:341: add_box reuses _is_positive_int64, the same invariant apply_transaction applies to outputs, and checks it before any connection is opened. Leaving dust policy out of this low-level insert is the right boundary.
  • node/utxo_db.py:1256-1278: the invalid-value count comes from the same SELECT/snapshot as the totals, so it keeps the #2819 single-snapshot guarantee. Because ok=False is set before the expected_total branch, a matching total can't flip it back to True. The typeof(value_nrtc) != 'integer' clause also catches REAL and NULL rows. Good.
  • Tests: I checked out the branch and ran the full UTXO set you listed. Result: 197 passed, 48 subtests passed. The two new tests exercise both the negative/+200 masking case and the REAL-typed row.

Deploy note for the maintainer, not for you: integrity_check is now stricter for every unspent row, however it got written. Before deploying, run SELECT COUNT(*) FROM utxo_boxes WHERE spent_at IS NULL AND (typeof(value_nrtc)!='integer' OR value_nrtc<=0) on the live DB, so a pre-existing legacy row doesn't make /utxo/integrity go red unexpectedly. If one does turn up, it's something we'd want to find anyway.

Verdict: approve. CI didn't run for this first-time fork; the local results are above.

@Scottcjn
Scottcjn merged commit 74ec1d4 into Scottcjn:main Sep 25, 2026
3 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

RTC Reward

This merged PR earned 5 RTC — sent to pujitha24.

RustChain Bounty Program

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) BCOS-L2 Beacon Certified Open Source tier BCOS-L2 (required for non-doc PRs) node Node server related size/M PR: 51-200 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[UTXO-BUG] add_box accepts invalid values that integrity_check can miss

2 participants