Skip to content

fix: keep block save and tx confirmation in one transaction#2157

Merged
Scottcjn merged 1 commit intoScottcjn:mainfrom
createkr:feat/issue-block-save-atomicity
Apr 7, 2026
Merged

fix: keep block save and tx confirmation in one transaction#2157
Scottcjn merged 1 commit intoScottcjn:mainfrom
createkr:feat/issue-block-save-atomicity

Conversation

@createkr
Copy link
Copy Markdown
Contributor

@createkr createkr commented Apr 6, 2026

Makes BlockProducer.save_block() and TransactionPool.confirm_transaction() share a single SQLite connection so that block insertion and all transaction confirmations are a single atomic unit.

Problem

save_block() opened its own DB connection and called confirm_transaction() for each transaction in the block. confirm_transaction() opened a separate connection and committed independently. This meant:

  • A crash mid-save could leave a block saved with partially-confirmed transactions
  • Failed confirmations were silently ignored — the block was saved anyway
  • No way to roll back the entire block if any transaction confirmation failed

Fix

  • rustchain_tx_handler.py: Added optional conn parameter to confirm_transaction(). When provided, the method uses the caller's connection and does not manage its own transaction boundary. The core logic is extracted into _do_confirm(cursor) to avoid duplication.
  • rustchain_block_producer.py: save_block() now passes its connection to each confirm_transaction() call. If any confirmation returns False, the entire block save is aborted and rolled back.

Changes

  • node/rustchain_tx_handler.pyconfirm_transaction() accepts optional conn parameter; extracted _do_confirm() inner function
  • node/rustchain_block_producer.pysave_block() passes conn=conn to confirm_transaction() and aborts on failure
  • node/tests/test_f10_block_save_atomicity.py — 5 new tests:
    • test_confirm_with_shared_connection_succeeds
    • test_confirm_rollback_on_shared_connection
    • test_confirm_fails_insufficient_balance_shared_conn
    • test_standalone_confirm_still_works (backward compat)
    • test_multi_tx_atomic_on_shared_conn

Backward Compatibility

The conn parameter is optional with a default of None. Existing callers that don't pass a connection continue to use the standalone path with their own transaction management. No API breakage.

@github-actions github-actions bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) node Node server related tests Test suite changes labels Apr 6, 2026
@createkr
Copy link
Copy Markdown
Contributor Author

createkr commented Apr 6, 2026

For bounty payout, please use RTC wallet: RTC1d48d848a5aa5ecf2c5f01aa5fb64837daaf2f35.

@github-actions github-actions bot added the size/L PR: 201-500 lines label Apr 6, 2026
@Scottcjn Scottcjn merged commit c9f528f into Scottcjn:main Apr 7, 2026
7 of 9 checks passed
@Scottcjn
Copy link
Copy Markdown
Owner

Scottcjn commented Apr 7, 2026

✅ Merged! 75 RTC awarded for atomicity fix — block save.

Payment will be sent to wallet RTC1d48d848a5aa5ecf2c5f01aa5fb64837daa as requested.

Total this batch: 200 RTC. Thank you @createkr — your 3 fixes today touched security, data integrity, and epoch correctness. This is core contributor work.

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/L PR: 201-500 lines tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants