Skip to content

[UTXO-BUG] Fix dual-write transfer rejection on sub-micro absorbed fee dust (bounty #2819) - #8483

Open
TomerGamerTV wants to merge 2 commits into
Scottcjn:mainfrom
TomerGamerTV:fix/dual-write-absorbed-fee-precision
Open

TomerGamerTV wants to merge 2 commits into
Scottcjn:mainfrom
TomerGamerTV:fix/dual-write-absorbed-fee-precision

Conversation

@TomerGamerTV

@TomerGamerTV TomerGamerTV commented Sep 19, 2026 •

Copy link
Copy Markdown

Summary

Fixes a bug in /utxo/transfer (node/utxo_endpoints.py) where legitimate transfer requests under _dual_write=True are rejected with HTTP 400 (Invalid amount: effective_fee_nrtc cannot be mirrored by dual-write account model (max 6 decimal places)) if UTXO coin selection absorbs sub-micro dust remainder into the effective transaction fee.

Vulnerability & Root Cause Analysis

  1. Remote Caller Path: Public HTTP POST /utxo/transfer (production caller on main).
  2. Mechanics:
    • A user submits a valid transfer with standard 6-decimal values (e.g. amount_rtc = 10.0, fee_rtc = 0).
    • The user holds UTXO boxes carrying 8-decimal precision (common for mining rewards, migrated boxes, or change boxes, such as 33,870,945 nRTC or any remainder value_nrtc % 100 != 0).
    • coin_select() selects the UTXO boxes. If the unspent change is below DUST_THRESHOLD (1,000 nRTC / 10 uRTC), coin_select absorbs the change into the transaction fee (change = 0, absorbed_fee_nrtc = total - target_nrtc).
    • In node/utxo_endpoints.py:
      effective_fee_nrtc = fee_nrtc + absorbed_fee_nrtc
    • If effective_fee_nrtc % 100 != 0 (e.g. 45 nRTC), calling _nrtc_to_account_i64(effective_fee_nrtc, 'effective_fee_nrtc') raises ValueError: effective_fee_nrtc cannot be mirrored by dual-write account model (max 6 decimal places).
    • The endpoint intercepts this and aborts with HTTP 400.
  3. Impact:
    • Valid transfer requests from legitimate users fail unexpectedly.
    • Senders whose UTXO balances contain boxes with sub-micro dust remain unable to spend them under dual-write mode.

Fix

  • The user's explicit fee_rtc is already validated upfront (_decimal_to_account_i64(fee_rtc, 'fee_rtc')) to have at most 6 decimal places.
  • Any absorbed dust fee (< 1,000 nRTC) is a UTXO-side dust absorption. In dual-write mode, derive the shadow fee using integer division:
    effective_fee_i64_for_dual_write = effective_fee_nrtc // (UNIT // ACCOUNT_UNIT)
    This mirrors the whole microRTC fee units to the shadow account balances and ledger, while safely allowing sub-micro dust fee absorption on the UTXO side.

Verification & Tests

  • Added two regression tests in node/test_utxo_endpoints.py:
    • test_dual_write_allows_sub_micro_absorbed_dust_fee: verifies transfer with 45 nRTC absorbed fee succeeds (HTTP 200), balances update properly, and /utxo/integrity models agree.
    • test_dual_write_allows_sub_micro_absorbed_dust_fee_with_multiple_micros: verifies absorbed fee with mixed microRTC and nanoRTC dust remainder (345 nRTC).
  • All 43 tests in node/test_utxo_endpoints.py PASS.
  • All 107 tests in node/test_utxo_db.py PASS.
  • Zero regressions across existing test suites.

Scottcjn#2819)

Under UTXO dual-write mode (_dual_write=True), transfer requests with clean
6-decimal amounts and fees were rejected if coin selection absorbed sub-micro
dust change (< DUST_THRESHOLD) into the transaction fee.

Because UTXO boxes (e.g. from mining rewards or genesis migration) carry
8-decimal precision, absorbed dust often has non-zero 7th/8th decimals (such
as 45 nRTC). Previously, _nrtc_to_account_i64 strictly enforced that the
effective fee be an exact multiple of 100 nRTC, throwing ValueError and
returning HTTP 400 'effective_fee_nrtc cannot be mirrored by dual-write
account model', preventing legitimate senders from spending their funds.

Fix this by deriving the dual-write shadow fee via integer division
(effective_fee_nrtc // (UNIT // ACCOUNT_UNIT)), mirroring the whole microRTC
fee units to the shadow account model while safely allowing sub-micro dust
fee absorption on the UTXO side. Added comprehensive regression tests in
node/test_utxo_endpoints.py.
@github-actions github-actions Bot added the BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) label Sep 19, 2026
@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-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 19, 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.

@TomerGamerTV — this one is a real finding, and I want to say that plainly given I rejected your last claim. The difference: this defect is reachable in the code, your two tests fail when I revert only utxo_endpoints.py, and the full test_utxo_endpoints.py + test_utxo_db.py run stays green (150 passed). The test-hygiene tidy is correct too: with sqlite3.connect(...) does not close the connection, so the explicit close() fixes a real fd leak.

I also checked the guard you might have weakened, and you didn't: the user-supplied fee_rtc is still validated to 6 dp up front (#8443 intact, test_dual_write_rejects_sub_micro_amounts still passes). Only the ≤99 nRTC coin-selection remainder is dropped, which converges a pre-existing divergence rather than creating one.

Reachability, measured on production just now:

SELECT COUNT(*) FROM utxo_boxes WHERE spent_at IS NULL AND value_nrtc % 100 != 0;  -- 0
SELECT COUNT(*) FROM utxo_boxes WHERE value_nrtc % 100 != 0;                       -- 0

All 1,697 live boxes are micro-aligned (genesis derives them from balances ×100, and reward mints are ×100 too). So the bug is latent today, not live. That's why this is a change request rather than a rush merge: dual-write is now enabled in production, so this is live settlement/transfer code.

One change before merge: keep the truncation, but when effective_fee_nrtc % (UNIT // ACCOUNT_UNIT) != 0, log it (wallet, tx, remainder in nRTC). As written the truncation is silent, which would mask a genuine model divergence instead of surfacing it. Everything else looks right.

@TomerGamerTV

Copy link
Copy Markdown
Author

You want me to apply the one change you suggested?

@Scottcjn

Scottcjn commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Queue sweep (2026-11-06): the branch conflicts with main after the recent UTXO endpoint changes. Please rebase so Scott can re-review the dust handling against current code. — Sophia Elya (reviewing for Scott)

@github-actions

Copy link
Copy Markdown
Contributor

This PR has been inactive for 21 days. It will be closed in 14 days unless updated.
Need help finishing? Ask in the PR comments — we're happy to assist!

@github-actions github-actions Bot added the stale Inactive — will close soon label Nov 30, 2026

This branch has not been deployed

No deployments
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 stale Inactive — will close soon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants