Skip to content

[UTXO-BUG] Ignore expired UTXO mempool claims - #4235

Closed
bkallday1122 wants to merge 1 commit into
Scottcjn:mainfrom
bkallday1122:codex/mempool-expiry-revalidate
Closed

bkallday1122 wants to merge 1 commit into
Scottcjn:mainfrom
bkallday1122:codex/mempool-expiry-revalidate

Conversation

@bkallday1122

Copy link
Copy Markdown
Contributor

Summary

Fixes an expired UTXO mempool lock issue for bounty Scottcjn/rustchain-bounties#2819.

Expired mempool rows were only removed when callers explicitly invoked mempool_clear_expired(). Normal admission, block-candidate selection, and double-spend checks did not clear or ignore expired rows. As a result, an expired transaction could still be returned to block producers and could keep its input marked as claimed, blocking a valid replacement until some separate cleanup path happened to run.

Bounty Classification

Root Cause

utxo_mempool.expires_at was written during mempool_add(), but the other public mempool boundaries did not enforce it:

  • mempool_get_block_candidates() selected all rows regardless of expires_at.
  • mempool_check_double_spend() treated expired utxo_mempool_inputs claims as active locks.
  • mempool_add() checked pool size and existing input claims before pruning expired entries.

Expected vs Actual

Expected:

  • Expired mempool transactions should not be eligible block candidates.
  • Expired input claims should not block fresh valid transactions.
  • Expired rows should not keep the pool full.

Actual before this patch:

  • A transaction with expires_at in the past was still returned by mempool_get_block_candidates().
  • The same expired transaction still made mempool_check_double_spend(box_id) return True.
  • A fresh replacement spending the same input was rejected until cleanup was called separately.

Fix

  • Add a transaction-scoped _mempool_clear_expired_locked() helper.
  • Prune expired rows under BEGIN IMMEDIATE before mempool admission checks.
  • Prune expired rows before returning block candidates.
  • Prune expired rows before reporting whether a box is locked by the mempool.
  • Keep the existing explicit mempool_clear_expired() API, now using the shared helper.

Validation

$ python3 node/test_utxo_db.py
.....................................................
----------------------------------------------------------------------
Ran 53 tests in 0.249s

OK

$ git diff --check HEAD~1..HEAD
$ python3 -m compileall -q node/utxo_db.py node/test_utxo_db.py

python3 -m pytest node/test_utxo_db.py -q was not available because pytest is not installed in this local environment.

Refs Scottcjn/rustchain-bounties#2819

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

@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 10, 2026

@gl11tchy gl11tchy 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 expired-mempool-lock path and the fix covers the important public boundaries: mempool_add clears expired rows before pool-size and input-claim checks, mempool_get_block_candidates prunes before selecting transactions for mining, and mempool_check_double_spend no longer treats expired claims as active locks.

Validation performed locally:

  • python3 node/test_utxo_db.py => 53 passed
  • python3 -m py_compile node/utxo_db.py node/test_utxo_db.py => OK
  • git diff --check origin/main...HEAD -- node/utxo_db.py node/test_utxo_db.py => OK

The new regression tests cover both halves of the issue: expired transactions are not returned as block candidates, and a fresh replacement spend can enter after the old claim expires. The shared transaction-scoped clear helper also avoids the prior split cleanup behavior.

@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! Nice work on this contribution. 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.

LGTM! Thanks for contributing. Approved.

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

The expiry fix itself is directionally correct and the two new regressions pass locally, but I am requesting changes because the branch is stale/conflicting and its advertised full validation no longer passes on this platform.

Local validation:

  • python -m pytest node\test_utxo_db.py::TestUtxoDB::test_mempool_expired_tx_not_returned_or_locked node\test_utxo_db.py::TestUtxoDB::test_mempool_add_releases_expired_input_claim -q -> 2 passed
  • python -m py_compile node\utxo_db.py node\test_utxo_db.py -> passed
  • git diff --check origin/main...HEAD -- node\utxo_db.py node\test_utxo_db.py -> passed

Blocking validation failure:

  • python node\test_utxo_db.py -> failed: PermissionError: [WinError 32] in tearDown() for test_mining_reward_over_cap_rejected
  • python -m pytest node\test_utxo_db.py -q -> failed: 1 failed, 52 passed, same Windows temp DB lock

This looks like the branch predates the later DB-handle cleanup for rejected minting rewards, so it needs to be rebased onto current main and revalidated. Once rebased, the actual mempool-expiry changes should be easy to re-check: clearing expired rows before admission, candidate selection, and double-spend checks is the right shape for the reported stale-lock issue.

No production service testing was performed.

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

Code review: LGTM! Thanks for contributing to RustChain. Approved.

@loganoe loganoe 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 after validating expired UTXO mempool claim handling.

What I checked:

  • Expired mempool rows are cleared inside the same SQLite transaction before pool-size checks, double-spend checks, candidate selection, and double-spend queries.
  • Expired transactions no longer appear in block candidates and no longer keep their input boxes locked.
  • A fresh replacement transaction using the same input is accepted after the prior claim expires.

Validation run locally:

  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 timeout 240 uv run --no-project --with pytest python -m pytest -q node/test_utxo_db.py -> 53 passed
  • python3 -m py_compile node/utxo_db.py node/test_utxo_db.py -> passed
  • git diff --check origin/main...HEAD -- node/utxo_db.py node/test_utxo_db.py -> passed
  • python3 tools/bcos_spdx_check.py --base-ref origin/main -> BCOS SPDX check: OK

No blocker found.

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

Hermes Agent review — APPROVED

I reviewed the diff for PR #4235: [UTXO-BUG] Ignore expired UTXO mempool claims

Validation proof

  • git diff --check: PASS
  • BCOS SPDX check: PASS
  • added-line secret-pattern scan: PASS
  • python py_compile: PASS
  • focused changed-test pytest: PASS

Scope checked
node/test_utxo_db.py (+63/-0), node/utxo_db.py (+60/-23)

Review note
The changed surface is bounded and the local gates above did not expose whitespace, SPDX, syntax, focused-test, or added-secret regressions. This approval is for the submitted diff at the reviewed head; payout/merge authority remains with the maintainers.

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

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! Great contribution to the RustChain ecosystem. Thanks for keeping the codebase clean and well-tested. 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!

@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

Copy link
Copy Markdown
Owner

Closing as stale branch — would cause destructive deletions if merged.

Your branch is 1084 commits behind current main. Filed during the May 11-13 contributor burst, the codebase has since moved substantially. GitHub's mergeable=clean indicator doesn't detect this; only an explicit git diff main..your-branch --shortstat does.

Bounty credit acknowledged where applicable

Most of the canonical fixes from your work-period have already shipped via other contributors' parallel PRs that landed earlier this week. Specific cases credited via the Codex audit batches:

If you want fresh review

Rebase against current main and verify your diff matches the size of your original changes:

git fetch upstream main
git rebase upstream/main
git diff main..HEAD --shortstat

If the deletion count is much higher than what you intended, the branch is still picking up stale assumptions — recreate from a fresh main.

Thanks for the contribution work.

@Scottcjn Scottcjn closed this May 18, 2026
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.

8 participants