Skip to content

Fix blockchain integrity cache advancing before DB commit#518

Draft
Copilot wants to merge 2 commits into
bolt-optimized-blockchain-and-admin-stats-v2-3885603136094984226from
copilot/sub-pr-516
Draft

Fix blockchain integrity cache advancing before DB commit#518
Copilot wants to merge 2 commits into
bolt-optimized-blockchain-and-admin-stats-v2-3885603136094984226from
copilot/sub-pr-516

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 8, 2026

Description

blockchain_last_hash_cache was updated with the new integrity_hash before save_issue_db was called. A failed DB commit would leave the cache pointing to a hash that was never persisted, causing subsequent issues to chain off a phantom hash and silently corrupting the blockchain integrity chain.

Fix:

  • Move blockchain_last_hash_cache.set(...) to after save_issue_db succeeds — cache only advances on confirmed persist
  • Add blockchain_last_hash_cache.invalidate("last_hash") in the except block — forces a fresh DB lookup on the next request instead of chaining off a stale/uncommitted value
# Before (broken): cache updated before DB write
integrity_hash = hashlib.sha256(hash_content.encode()).hexdigest()
blockchain_last_hash_cache.set(data=integrity_hash, key="last_hash")  # ← too early
await run_in_threadpool(save_issue_db, db, new_issue)

# After (fixed): cache updated only on success, invalidated on failure
integrity_hash = hashlib.sha256(hash_content.encode()).hexdigest()
await run_in_threadpool(save_issue_db, db, new_issue)
blockchain_last_hash_cache.set(data=integrity_hash, key="last_hash")  # ← after commit
# ...
except Exception as e:
    blockchain_last_hash_cache.invalidate("last_hash")  # ← force re-fetch from DB

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📝 Documentation update
  • 🎨 Code style update (formatting, renaming)
  • ♻️ Refactoring (no functional changes)
  • ⚡ Performance improvement
  • ✅ Test update

Related Issue

Closes #

Testing Done

  • Tested locally
  • Added/updated tests
  • All tests passing

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Co-Authors


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.


Summary by cubic

Strengthens blockchain integrity and speeds up Admin Stats. Tightens validation, fixes the last-hash cache race, and adds fast, indexed rollups with short-lived caching to cut query load and fix incorrect counts.

  • Bug Fixes

    • Block acceptance now checks prev-hash continuity and transaction merkle root before commit to prevent orphan/invalid blocks.
    • Fixed last-hash cache race: update only after a successful DB commit and invalidate on errors to avoid stale reads.
    • Admin Stats totals corrected by reading from the canonical chain state and excluding pending/orphaned records.
  • Refactors

    • Centralized validation into small utilities with a startup integrity pass for the existing chain.
    • Admin Stats now use aggregated queries with new DB indexes and a 60s cache (stale-while-revalidate).

Written for commit 59daf4c. Summary will update on new commits.

…ul DB commit

Co-authored-by: RohanExploit <178623867+RohanExploit@users.noreply.github.com>
Copilot AI changed the title [WIP] WIP address feedback on blockchain integrity and admin stats optimization Fix blockchain integrity cache advancing before DB commit Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants