Skip to content

[UTXO-BUG] Reject non-string transfer memo before dual-write - #4833

Merged
Scottcjn merged 2 commits into
Scottcjn:mainfrom
saim256:bounty-utxo-reject-non-string-memo-saim256
May 15, 2026
Merged

Scottcjn merged 2 commits into
Scottcjn:mainfrom
saim256:bounty-utxo-reject-non-string-memo-saim256

Conversation

@saim256

@saim256 saim256 commented May 12, 2026

Copy link
Copy Markdown
Contributor

/claim #2819

Summary

  • Reject non-string /utxo/transfer memo values before signature verification, nonce reservation, UTXO commit, or dual-write shadow writes.
  • Add a dual-write regression proving a malformed memo cannot commit UTXO state and then skip account/ledger updates.

Bug / Impact

With dual_write=True, the current endpoint accepts a JSON object memo. The UTXO transaction commits first, then the account-model shadow write tries to slice memo[:30], raises TypeError, catches it in the broad dual-write exception handler, and still returns HTTP 200.

That leaves the UTXO model debited/credited while balances and ledger remain unchanged. In the local repro, a 10 RTC transfer with memo: {bad:memo} returned 200, moved UTXO balances to sender=90 RTC and recipient=10 RTC, but left balances at sender=100 RTC and inserted zero ledger rows.

Validation

  • git diff --check -- node/utxo_endpoints.py node/test_utxo_endpoints.py -> passed
  • python -m py_compile node\utxo_endpoints.py node\test_utxo_endpoints.py -> passed
  • python -m pytest node\test_utxo_endpoints.py -q -> 19 passed
  • python -m ruff check node\utxo_endpoints.py node\test_utxo_endpoints.py --select E9,F821,F811 --output-format=concise -> passed
  • python tools\bcos_spdx_check.py --base-ref origin/main -> OK

Note: python -m pytest node\test_utxo_endpoints.py tests\test_utxo_transfer_replay.py -q runs my focused suite successfully, then hits existing Windows cleanup locks in tests/test_utxo_transfer_replay.py while unlinking temp DB files. I did not bundle that unrelated cleanup here because it is outside this fix.

Wallet/miner ID: RTC253255d034065a839cd421811ec589ae5b694ffc

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

@strongkeep-debug strongkeep-debug 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 current head 9fbf4618a5e0a819333b3504a40e2a68276d50d7.

This is the right fix for the dual-write partial-commit case: the endpoint now rejects a non-string JSON memo before amount parsing, signature checks, nonce reservation, UTXO writes, or the dual-write shadow ledger path that slices memo[:30]. The new regression also exercises the important failure mode directly by seeding both UTXO and account state, sending an object memo, and confirming the request returns 400 while UTXO balances, balances, and ledger remain unchanged.

Validation performed:

  • git diff --check origin/main...HEAD -- node\utxo_endpoints.py node\test_utxo_endpoints.py -> passed
  • python -m py_compile node\utxo_endpoints.py node\test_utxo_endpoints.py -> passed
  • python -m pytest node\test_utxo_endpoints.py -q -> 19 passed
  • python -m ruff check node\utxo_endpoints.py node\test_utxo_endpoints.py --select E9,F821,F811 --output-format=concise -> passed
  • python tools\bcos_spdx_check.py --base-ref origin/main -> OK

GitHub checks are green on this head and the PR merge state is clean.

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

@chenzf534403209 chenzf534403209 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 the non-string memo guard for /utxo/transfer.

The fix is narrowly placed before amount parsing, signature verification, nonce reservation, UTXO mutation, and dual-write shadow writes, which is the right point for this bug: malformed memo values now fail before any state can be committed. The new dual-write regression is also strong because it checks both ledgers after rejection: UTXO balances stay unchanged, the account-model balance remains at the seeded value, the recipient is not created, and no ledger rows are inserted.

Validation I ran locally on this PR:

  • python -m py_compile node\utxo_endpoints.py node\test_utxo_endpoints.py -> passed
  • git diff --check origin/main...HEAD -- node\utxo_endpoints.py node\test_utxo_endpoints.py -> passed
  • python -m pytest node\test_utxo_endpoints.py -q -> 19 passed
  • python tools\bcos_spdx_check.py --base-ref origin/main -> OK
  • python -m pytest node\test_utxo_endpoints.py tests\test_utxo_transfer_replay.py -q -> 19 passed before the two known Windows temp-DB unlink failures in tests/test_utxo_transfer_replay.py

I did not find a blocker in this patch. ruff is not installed in my local Python environment, so I could not independently rerun that check.

Review bounty context: Scottcjn/rustchain-bounties#73

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

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

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

@godd-ctrl godd-ctrl 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 current head 9fbf461 after independently reviewing the non-string memo guard. The check rejects malformed optional memo values before signature verification, nonce reservation, UTXO mutation, or dual-write account/ledger shadow writes. The regression covers the important partial-commit failure mode by asserting the object-memo request returns 400 while sender/recipient UTXO state, account balances, and ledger rows remain unchanged. Validation: python -m pytest node\test_utxo_endpoints.py -q -> 19 passed; python -m py_compile node\utxo_endpoints.py node\test_utxo_endpoints.py -> passed; git diff --check origin/main...HEAD -- node\utxo_endpoints.py node\test_utxo_endpoints.py -> passed; python tools\bcos_spdx_check.py --base-ref origin/main -> OK; uv run --no-project --with ruff python -m ruff check node\utxo_endpoints.py node\test_utxo_endpoints.py --select E9,F821,F811 --output-format=concise -> passed. No live node, wallet, or production endpoint was exercised.

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

Code Review: Reject Non-String Transfer Memo Before UTXO Commit

Summary

Validates that the memo field in UTXO transfers is a string before committing any database state. This prevents a partial-write bug where non-string memos could commit UTXO boxes but fail on the shadow/ledger write, leaving the database in an inconsistent state.

What Works Well

  1. Prevents partial writes: Validates memo before any database mutation
  2. 71-line test: Comprehensive test that verifies rejection and that no state was committed
  3. Correct order: Validation → dual write → commit, not commit → validate
  4. Database consistency check: Test verifies sender balance is unchanged after rejection

Issues Found

1. Low — What other fields could cause partial writes?
If any other user-supplied field (amount, address, signature) has a type mismatch that fails after UTXO commit, the same partial-write bug could occur. Consider a comprehensive validation step before any database writes:

# Validate ALL fields before touching the database
errors = validate_all_fields(payload)
if errors:
    return jsonify({"error": errors}), 400
# Only now begin database operations

2. Nit — Test uses direct SQL to set up state
The test manually creates ledger table and inserts data. This is fine for testing but fragile if the schema changes. Consider using the application's own setup functions.

Verdict: Approve

Important data integrity fix. The comprehensive validation suggestion is worth considering.

@iice257

iice257 commented May 12, 2026

Copy link
Copy Markdown
Contributor

Review for Scottcjn/rustchain-bounties#73.

I reviewed the non-string memo guard and the dual-write regression. The placement is good: the endpoint rejects memo before amount parsing, signature reconstruction, nonce reservation, UTXO apply, and the account-model shadow write. The test also checks the important state invariants: sender balance unchanged, recipient absent, and no ledger row.

Actionable coverage notes:

  1. Please add explicit cases for memo: null and memo: []. Current code rejects null because data.get('memo', '') returns None when the key is present, which may be intended, but optional UI fields commonly serialize empty values as null. A test should lock whether null means "omitted" or "invalid".

  2. If clients consume structured validation errors, consider returning a field/code shape such as {'error': 'memo must be a string', 'field': 'memo'}. Not a blocker, but it would make this validation branch easier to handle alongside the existing required-field response.

Overall this fixes the commit-before-shadow-write failure mode cleanly. I would merge with the additional null/array coverage if the endpoint wants a strict string-only memo contract.

@saim256

saim256 commented May 12, 2026

Copy link
Copy Markdown
Contributor Author

Updated the PR to address the coverage note about other non-string memo shapes.

New head: b05c69e2d4d68732e5491643697c523f349a57c2

Change:

  • Expanded the dual-write regression to cover object, JSON null, and array memo values.
  • Production behavior is unchanged: any present non-string memo is rejected before nonce reservation, UTXO mutation, or dual-write shadow writes.

Validation rerun:

  • git diff --check -- node/utxo_endpoints.py node/test_utxo_endpoints.py -> passed
  • python -m py_compile node\utxo_endpoints.py node\test_utxo_endpoints.py -> passed
  • python tools\bcos_spdx_check.py --base-ref origin/main -> OK
  • python -m ruff check node\utxo_endpoints.py node\test_utxo_endpoints.py --select E9,F821,F811 --output-format=concise -> passed
  • python -m pytest node\test_utxo_endpoints.py -q -> 19 passed

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

Code Review — #4833

1. Correct fix — validates memo type before dual-write

Rejecting non-string memos before the UTXO commit prevents state corruption where the primary database commits but the shadow/ledger write fails due to type mismatch.

2. Test coverage is thorough

The test test_dual_write_rejects_non_string_memo_before_utxo_commit directly verifies that:

  • A non-string memo is rejected
  • The UTXO balance is not changed (sender still has 100 UNIT)
  • No partial commit occurs

3. Defense-in-depth approach

This fix follows the "validate early, reject before mutation" pattern which is the correct approach for financial transaction systems. Never let invalid data reach the commit stage.

4. Consider also validating memo content

Beyond type checking, consider:

  • Length limiting (prevent arbitrarily long memos)
  • Character set validation (reject control characters, null bytes)
  • Encoding validation (ensure valid UTF-8)

— Xeophon

@godd-ctrl godd-ctrl 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 current head b05c69e.

The updated guard rejects non-string memo values before signature verification, nonce reservation, UTXO mutation, or dual-write account/ledger writes. The expanded regression covers object, null, and list memo payloads and confirms the sender/recipient UTXO balances, account balance row, and ledger row count remain unchanged after each rejected request.

Validation performed:

  • git diff --name-status origin/main...HEAD -> node/utxo_endpoints.py, node/test_utxo_endpoints.py
  • git diff --check origin/main...HEAD -> passed
  • python tools\bcos_spdx_check.py --base-ref origin/main -> passed
  • python -m py_compile node\utxo_endpoints.py node\test_utxo_endpoints.py -> passed
  • python -m pytest node\test_utxo_endpoints.py -q -> 19 passed, 3 subtests passed

No blocking issues found in the reviewed diff.

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

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

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

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

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

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

@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. The memo type guard runs before the UTXO commit path, so object/list/null memos are rejected cleanly and do not leave partial UTXO or dual-write ledger state behind.

Validation performed:

  • git diff --check origin/main...HEAD
  • python3 -m py_compile node/utxo_endpoints.py node/test_utxo_endpoints.py
  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 /tmp/rustchain-flask-venv/bin/python -m pytest -q node/test_utxo_endpoints.py -> 19 passed, 3 subtests passed

@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: Good Fix

Approve.

**Verdict: Approve.

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

Reviewed the UTXO non-string memo guard.

Validation run on the PR head:

  • python3 -m py_compile node/utxo_endpoints.py node/test_utxo_endpoints.py -> passed
  • Focused regression with node on PYTHONPATH passed:
    PYTHONPATH=node PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 uv run --no-project --with flask python -m unittest test_utxo_endpoints.TestUtxoEndpoints.test_dual_write_rejects_non_string_memo_before_utxo_commit -> OK
  • Local static smoke check confirmed the guard is before transfer processing and returns memo must be a string -> passed
  • git diff --check origin/main...HEAD -> passed
  • python3 tools/bcos_spdx_check.py --base-ref origin/main -> OK

The regression covers dict, null, and list memo values and verifies no UTXO, balance, or ledger mutation occurs on rejection. This is the right failure point before dual-write side effects.

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

Reviewed PR #4833 against the current diff. The patch rejects non-string memo values immediately after parsing the request body, before signature verification, nonce reservation, UTXO application, or dual-write shadow ledger updates. That directly prevents the reported partial-commit path where UTXO state could change and the account-model write later failed on memo[:30].

The regression test covers object, null, and list memo values and verifies no UTXO balance, account balance, or ledger mutation occurs. No blocking issues found.

@Scottcjn
Scottcjn merged commit b25e681 into Scottcjn:main May 15, 2026
11 checks passed
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.