Repository navigation
fix(utxo): reject invalid values in add_box and flag them in integrity_check - #8516
Conversation
…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)
|
Welcome to RustChain! Thanks for your first pull request. Before we review, please make sure:
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! |
Scottcjn
left a comment
There was a problem hiding this comment.
Thanks @pujitha24. This is a clean, well-scoped fix for #8513, and the PR description is precise.
node/utxo_db.py:341:add_boxreuses_is_positive_int64, the same invariantapply_transactionapplies 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. Becauseok=Falseis set before theexpected_totalbranch, a matching total can't flip it back to True. Thetypeof(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.
RTC RewardThis merged PR earned 5 RTC — sent to |
BCOS Checklist (Required For Non-Doc PRs)
BCOS-L1orBCOS-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.# SPDX-License-Identifier: MIT). N/A: no new files.What Changed
Fixes the validation gap in
UtxoDB.add_box()and the blind spot inintegrity_check()reported in #8513.node/utxo_db.pyadd_box(): raisesValueErrorunlessvalue_nrtcis a positive int64. It reuses_is_positive_int64, the same checkapply_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_boxis the low-level insert, and dust is a transaction-policy rule.node/utxo_db.pyintegrity_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 setsok=Falseand reportsinvalid_value_boxes. Before this, a -50 box and a +200 box summed to the expected 150 and gotok=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()innode/. 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. Theintegrity_checkchange 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.pychange and pass with it:UTXO suites that call
add_box/integrity_check:CI lint subset:
Note:
tests/security_audit_real_code.py::test_vuln1_mining_reward_mintingfails onmainboth with and without this change. It is unrelated, and thetests/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