Skip to content

[UTXO-BUG] Reject unpersistable UTXO outputs - #5451

Merged
Scottcjn merged 1 commit into
Scottcjn:mainfrom
galanime:codex/utxo-output-normalization
May 18, 2026
Merged

Scottcjn merged 1 commit into
Scottcjn:mainfrom
galanime:codex/utxo-output-normalization

Conversation

@galanime

Copy link
Copy Markdown
Contributor

Summary

  • add shared UTXO output normalization for both apply_transaction() and mempool_add()
  • reject outputs that cannot be safely converted into propositions or SQLite TEXT metadata
  • add regressions proving malformed outputs cannot enter the mempool, lock inputs, remain as block candidates, or mutate balances through direct apply

Security impact

Current main accepts several JSON-serializable output payloads into the mempool even though block application cannot persist or apply them. The admitted transaction claims the input in utxo_mempool_inputs, appears in mempool_get_block_candidates(), then fails during apply_transaction() and remains available as an unmineable candidate until expiry.

Confirmed pre-fix cases on current main:

missing_address admitted=True candidates=1 locked=True apply_exception=KeyError: 'address' after_candidates=1
none_address admitted=True candidates=1 locked=True apply_exception=AttributeError: 'NoneType' object has no attribute 'encode' after_candidates=1
dict_tokens_json admitted=True candidates=1 locked=True apply_exception=ProgrammingError: Error binding parameter 8: type 'dict' is not supported after_candidates=1
list_registers_json admitted=True candidates=1 locked=True apply_exception=ProgrammingError: Error binding parameter 9: type 'list' is not supported after_candidates=1

Duplicate boundary

This overlaps the same UTXO mempool-hardening area as older reports, but it is not only the missing-address case. This patch centralizes output normalization and covers additional unpersistable shapes that still reproduce on main: non-string/blank addresses and metadata payloads whose JSON shape cannot be persisted consistently (tokens_json must be JSON text decoding to a list; registers_json must be JSON text decoding to a dict). The goal is to keep mempool admission and direct application on the same validation rules.

Verification

  • python -B -m unittest test_utxo_db.TestUtxoDB.test_mempool_rejects_unpersistable_outputs_without_locking_input test_utxo_db.TestUtxoDB.test_apply_transaction_rejects_unpersistable_outputs -v -> 2 passed
  • python -m py_compile node\utxo_db.py node\test_utxo_db.py -> passed
  • python -B node\test_utxo_db.py -> 73 passed
  • git diff --check -- node\utxo_db.py node\test_utxo_db.py -> passed
  • python tools\bcos_spdx_check.py --base-ref origin/main -> BCOS SPDX check: OK

Bounty

Claiming under Scottcjn/rustchain-bounties#2819.

Requested severity: Medium, mempool DoS / invalid block-candidate manipulation.

Wallet/miner ID: RTC74b80ab40602e5ae31819912b2fca974484e5dab

@github-actions github-actions Bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) node Node server related size/M PR: 51-200 lines labels May 16, 2026
@galanime

Copy link
Copy Markdown
Contributor Author

CI note after checking the failed test job:

The blocking failure is unrelated to this UTXO diff. The job reaches the full pytest tests/ collection step and stops on missing baseline dependencies outside the touched files:

  • tests/test_agent_economy_sdk.py -> ModuleNotFoundError: No module named 'aiohttp'
  • tests/test_faucet_service_wallet_validation.py -> ModuleNotFoundError: No module named 'flask_cors'
  • tests/test_hardware_visualizer.py and tests/test_pse_analyze_results.py -> ModuleNotFoundError: No module named 'matplotlib'

The PR-specific validation remains green locally:

python -B -m unittest test_utxo_db.TestUtxoDB.test_mempool_rejects_unpersistable_outputs_without_locking_input test_utxo_db.TestUtxoDB.test_apply_transaction_rejects_unpersistable_outputs -v -> 2 passed
python -B node/test_utxo_db.py -> 73 passed
python -m py_compile node/utxo_db.py node/test_utxo_db.py -> passed
git diff --check -- node/utxo_db.py node/test_utxo_db.py -> passed
python tools/bcos_spdx_check.py --base-ref origin/main -> OK

@GoodMorC GoodMorC left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved. I reviewed the UTXO output normalization change and did not find a blocker.

What I checked:

  • mempool_add() and apply_transaction() now share the same output-shape validation for address, dust/value type, and tokens_json/registers_json persistence shape.
  • Invalid JSON-serializable outputs are rejected before mempool insertion, so they do not claim utxo_mempool_inputs or remain as unmineable block candidates.
  • Direct apply_transaction() fails closed before spending the input or changing balances for malformed outputs.
  • Valid outputs without explicit metadata remain compatible because the normalizer supplies the existing default [] / {} metadata shape during application.

Validation on head a271fb2e859facb45298c16b3a1146ae72ae99d7:

  • PYTHONPATH=review-5451/node:/tmp/rustchain-testdeps python3 -B -m unittest review-5451/node/test_utxo_db.py -v -> 73 passed
  • python3 -m py_compile review-5451/node/utxo_db.py review-5451/node/test_utxo_db.py -> passed

No blocking issue found in this scope.

@kekehanshujun kekehanshujun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved. I reviewed the UTXO output-normalization change and did not find a blocker.

What I checked:

  • apply_transaction() and mempool_add() now share _normalize_outputs(...), so mempool admission rejects output shapes that direct application cannot persist safely.
  • The validator rejects missing/non-string/blank address values and metadata fields that are not SQLite-safe JSON text with the expected decoded shape (tokens_json as list, registers_json as dict).
  • The mempool regression verifies invalid outputs do not enter block candidates and do not lock the input UTXO.
  • The direct-apply regression verifies invalid output shapes fail before mutating balances.

Validation on head a271fb2e859facb45298c16b3a1146ae72ae99d7:

  • python -B -m unittest node.test_utxo_db.TestUtxoDB.test_mempool_rejects_unpersistable_outputs_without_locking_input node.test_utxo_db.TestUtxoDB.test_apply_transaction_rejects_unpersistable_outputs -v -> 2 passed
  • python -B node\test_utxo_db.py -> 73 passed
  • python -m py_compile node\utxo_db.py node\test_utxo_db.py -> passed

No blocking issue found in this scope.

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thanks for contributing!

@ZacharyZhang-NY ZacharyZhang-NY left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed PR #5451 at head a271fb2.

Validation performed:

  • git fetch origin pull/5451/head:review-pr-5451 --force
  • git diff --check origin/main...review-pr-5451 -- node/utxo_db.py node/test_utxo_db.py -> passed.
  • python -m py_compile node/utxo_db.py node/test_utxo_db.py on an extracted PR-head tree -> passed.
  • PYTHONPATH=node python -B -m unittest node.test_utxo_db.TestUtxoDB.test_mempool_rejects_unpersistable_outputs_without_locking_input node.test_utxo_db.TestUtxoDB.test_apply_transaction_rejects_unpersistable_outputs -v -> 2 passed.
  • PYTHONPATH=node python -B node/test_utxo_db.py -> 73 tests passed.
  • Checked bounty #2819 and the PR's duplicate-boundary statement for this mempool DoS / invalid block-candidate class.
  • Ran an additional UtxoDB probe covering missing address, non-string address, non-string metadata, and wrong-shaped metadata JSON. Each invalid output was rejected by mempool_add(), block candidates stayed empty, the input stayed unlocked, and the original balance remained unchanged.

The shared _normalize_outputs() path aligns mempool admission with direct transaction application for persistable output shape, dust/value validation, and metadata JSON shape. This closes the issue-scoped invalid-candidate path while preserving valid default metadata behavior. Approving.

@TJCurnutte TJCurnutte left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Validated locally and this looks good to merge.

Proof I ran on head a271fb2e859f:

git diff --check origin/main...HEAD -- node/utxo_db.py node/test_utxo_db.py
python3 -B -m py_compile node/utxo_db.py node/test_utxo_db.py
python3 -B -m pytest -q node/test_utxo_db.py
# 73 passed, 11 subtests passed in 0.51s

I also ran a runtime probe against temporary initialized UTXO databases. It covered seven invalid output shapes through mempool_add() — missing/blank/non-string address, non-string tokens_json/registers_json, and JSON strings with the wrong container type — and confirmed each one returns False, leaves mempool_get_block_candidates() empty, and does not set a double-spend lock on the input. A direct apply_transaction() probe for bad persisted metadata shapes also returned False while preserving Alice/Bob balances.

The important bit is that _normalize_outputs() is now shared by direct application and mempool admission, so the candidate set cannot accept an output shape that later fails while deriving propositions or binding the TEXT metadata columns. That closes the unmineable-mempool-lock path without weakening the existing dust/bool/non-int checks.

No blocking findings from this pass.

@ZacharyZhang-NY ZacharyZhang-NY left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed PR #5451 at head a271fb2.

Validation performed:

  • Checked rustchain-bounties#2819. This is a UTXO mempool/validation hardening finding: malformed but JSON-serializable outputs could enter mempool, lock inputs, and remain as unmineable block candidates.
  • git fetch origin pull/5451/head:review-pr-5451 --force
  • git diff --check origin/main...review-pr-5451 -- node/utxo_db.py node/test_utxo_db.py -> passed.
  • python -m py_compile node/utxo_db.py node/test_utxo_db.py on an extracted PR-head tree -> passed.
  • From the extracted PR-head node/ directory: python -B -m unittest test_utxo_db.TestUtxoDB.test_mempool_rejects_unpersistable_outputs_without_locking_input test_utxo_db.TestUtxoDB.test_apply_transaction_rejects_unpersistable_outputs -v -> 2 passed.
  • From the extracted PR-head node/ directory: python -B test_utxo_db.py -> 73 passed.
  • python tools/bcos_spdx_check.py --base-ref origin/main from a detached PR-head worktree -> OK.

The shared output normalization now keeps mempool admission and direct transaction application aligned for address shape, dust/value checks, and JSON metadata persistence. Approving.

@jaxint jaxint left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thanks for contributing. Approved.

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@508704820 508704820 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reject unpersistable UTXO outputs. Verify: (1) Unpersistable outputs cannot be created even through internal API. (2) Rejection criteria cover all edge cases: null fields, invalid types, oversized data. (3) Existing unpersistable outputs in DB are identified and handled. - Xeophon (security review, UTXO)

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@508704820

Copy link
Copy Markdown
Contributor

Security Review ✅ IMPORTANT

Reject unpersistable UTXO outputs. Malformed outputs that cannot be converted to propositions or SQLite TEXT metadata could enter the mempool, lock inputs, and persist as broken block candidates — effectively a state corruption DoS. Fix adds shared normalization for both apply_transaction() and mempool_add().

Reviewed by Xeophon - Solana: Lt9nERv6VHsojw15LpFeiaabuphAggzfLF9sM9UXRrZ

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@Scottcjn
Scottcjn merged commit 25f33b2 into Scottcjn:main May 18, 2026
11 of 12 checks passed
@Scottcjn

Copy link
Copy Markdown
Owner

Payment: 25 RTC (Medium tier, audit batch 4)

Audit summary: UTXO output normalization (mempool + apply_transaction).

Dispatched to wallet RTC74b80ab40602e5ae31819912b2fca974484e5dab as part of a combined 60 RTC transfer (pending_id 1591, tx 9ad7d63e529bd2a493e81a7d780f7454) covering all three of your batch 4 merged PRs. Confirms in ~24 hours.

— Codex gpt-5.4 xhigh audit, Sophia/Claude synthesis

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) node Node server related size/M PR: 51-200 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants