fix: prevent negative balances during transaction confirmation#2094
Merged
Scottcjn merged 1 commit intoScottcjn:mainfrom Apr 5, 2026
Merged
fix: prevent negative balances during transaction confirmation#2094Scottcjn merged 1 commit intoScottcjn:mainfrom
Scottcjn merged 1 commit intoScottcjn:mainfrom
Conversation
Contributor
Author
|
For bounty payout, please use RTC wallet: |
Owner
|
Merged. 50 RTC. CHECK(balance_urtc >= 0) constraint + confirm-time balance recheck prevents negative-balance minting between submit and confirm. |
This was referenced Apr 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: re-check sender balance in confirm_transaction() + add CHECK constraint
What
Prevents negative-balance minting in
TransactionPool.confirm_transaction()by adding a balance re-check before deduction, plus a schema-levelCHECK(balance_urtc >= 0)constraint with automatic migration.Why
confirm_transaction()previously deducted from the sender's balance without verifying sufficient funds at confirmation time. If the balance dropped betweensubmit_transaction()andconfirm_transaction()(e.g., another tx confirmed in the same block, direct DB mutation), the sender's balance would go negative — creating funds from nothing.Changes
node/rustchain_tx_handler.pyconfirm_transaction()— Added balance re-check before the sender deduction:SELECT balance_urtc FROM balances WHERE wallet = ?before the UPDATEFalsewith error log ifsender_balance < tx.amount_urtc_ensure_schema()— Added automatic migration to addCHECK(balance_urtc >= 0):sqlite_masterinspectionALTER TABLE ADD CHECK)balance_urtc >= 0(preserves non-negative data)node/tests/test_confirm_balance_recheck.py(new)5 focused tests:
test_confirm_rejects_when_balance_insufficient— balance drained before confirm → rejectedtest_confirm_succeeds_when_balance_sufficient— normal path still workstest_confirm_rejects_exact_balance— confirming for exact balance → succeeds (balance → 0)test_confirm_rejects_unknown_sender— no balance row → rejectedtest_check_constraint_prevents_negative_balance— direct negative INSERT blocked by DBTest Results
Risk