Skip to content

[UTXO-BUG] Reject oversized UTXO output lists - #5192

Closed
anansutiawan wants to merge 1 commit into
Scottcjn:mainfrom
anansutiawan:utxo-output-index-limit-2819
Closed

anansutiawan wants to merge 1 commit into
Scottcjn:mainfrom
anansutiawan:utxo-output-index-limit-2819

Conversation

@anansutiawan

Copy link
Copy Markdown

Summary

Fixes an oversized-output UTXO validation gap covered by Scottcjn/rustchain-bounties#2819.

compute_box_id() serializes output_index with output_index.to_bytes(2, 'little'), so output indexes above 65535 cannot be encoded. Before this patch, a transaction with 65,537 outputs could pass mempool_add() and then raise OverflowError: int too big to convert inside apply_transaction() during block candidate application.

Impact

A malformed transaction can be admitted to the mempool even though it cannot be applied into a block. That leaves the input claimed in utxo_mempool_inputs until expiry and exposes an unmineable candidate to block production.

Fix

  • Add MAX_OUTPUTS_PER_TX = 1 << 16, matching the two-byte output index encoding.
  • Reject transactions with more than 65,536 outputs in both apply_transaction() and mempool_add().
  • Move the unauthorized mining_reward guard into the transaction cleanup path so rejected calls close/rollback consistently.
  • Add regression tests for direct apply and mempool admission.

Validation

Run locally from a clean worktree based on upstream main:

python -m py_compile node\utxo_db.py node\test_utxo_db.py
python node\test_utxo_db.py
# Ran 57 tests in 2.015s - OK
python tools\bcos_spdx_check.py --base-ref main
# BCOS SPDX check: OK

Payout details intentionally omitted from the PR; the operator can provide them manually if the bounty maintainer asks on the authorized bounty issue.

@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 and removed BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) node Node server related labels May 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to RustChain! Thanks for your first pull request.

Before we review, please make sure:

  • Your PR has a BCOS-L1 or BCOS-L2 label
  • 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!

@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.

Approved. I focused this review on the serialization boundary the patch is trying to protect: compute_box_id() encodes output_index into two bytes, so an oversized output list has to be rejected before block application can try to assign box IDs.

Validation performed on the PR branch:

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
PYTHONPATH=node python3 -B -m pytest -q -o addopts='' \
  node/test_utxo_db.py::TestUtxoDB::test_apply_transaction_rejects_too_many_outputs \
  node/test_utxo_db.py::TestUtxoDB::test_mempool_rejects_too_many_outputs

Result: the focused pytest run passed with 2 passed in 0.19s; git diff --check and py_compile also passed.

The important behavior is covered in both paths: apply_transaction() now rejects len(outputs) > MAX_OUTPUTS_PER_TX before output box-ID assignment, and mempool_add() rejects the same oversized candidate before it can mark the spent input as a mempool double-spend. The regression tests also verify that the original UTXO balance is preserved and the mempool remains empty after the rejected oversized transaction.

@shuibui shuibui 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: Oversized UTXO output rejection - security hardening against malformed transactions.

@Guzzzzzzzz Guzzzzzzzz 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.

Security-focused review of the MAX_OUTPUTS_PER_TX guard

I reviewed this from the UTXO security audit perspective (bounty #2819). The serialization boundary fix is correct -- compute_box_id() encodes output_index in two bytes, so >= 65536 outputs would cause silent ID collisions.

Findings:

  1. _allow_minting guard improvement -- Moving the minting check inside the transaction block is actually a security improvement: the old code returned False without rolling back an open transaction. The new code uses abort() which properly rolls back. Nice.
  2. Missing MAX_INPUTS_PER_TX guard -- If outputs are bounded to prevent box_id overflow, inputs should also be bounded. An unbounded input list causes:
    • Mempool DoS via O(n) duplicate-check loops
    • SQLite INSERT INTO utxo_mempool_inputs with 100k+ rows per tx
  3. This should be a follow-up PR.
  4. Mempool vs apply_transaction rollback inconsistency -- mempool_add() uses inline rollback while apply_transaction() uses abort(). Not a bug but a consistency opportunity.
    Validation:
  • py_compile passes on both changed files
    • The two focused tests pass (verified locally)
      • The MAX_OUTPUTS_PER_TX = 1 << 16 = 65536 correctly bounds 0-indexed output_index to fit in 2 bytes
        Verdict: Approve. Core fix is correct and well-tested.

Wallet: Guzzzzzzzz
Bounty: #73 (security-focused review)

@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.

@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.

Great contribution! LGTM.

@shuibui shuibui 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.

Code review: Rejecting oversized UTXO output lists is a good DoS protection. Consider also limiting total output value to prevent dust attacks.

@anansutiawan

Copy link
Copy Markdown
Author

Closing this PR as superseded by upstream main.

After this branch was opened, #5112 merged a stricter MAX_OUTPUTS_PER_TX = 100 guard plus regression coverage for excessive output counts. That cap fully covers the two-byte output_index serialization boundary this PR targeted, and keeping this branch open now only creates a conflict against the merged validation.

Thanks for the reviews.

@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!

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

Labels

size/M PR: 51-200 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants