Fix blockchain hash chain race conditions and integrity verification gaps#521
Draft
Copilot wants to merge 2 commits into
Draft
Conversation
- Always query DB for prev_hash within the transaction (eliminates the per-process cache as source-of-truth, fixing multi-worker race) - Update blockchain_last_hash_cache only after save_issue_db succeeds; invalidate it on failure so stale hashes are never chained off - verify_blockchain_integrity now always fetches the predecessor from DB and rejects records whose previous_integrity_hash disagrees with the actual DB predecessor, detecting chain tampering or creation races Co-authored-by: RohanExploit <178623867+RohanExploit@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Optimize blockchain verification and admin stats
Fix blockchain hash chain race conditions and integrity verification gaps
Mar 8, 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.
Description
Three correctness bugs in the blockchain integrity feature: the in-process cache was used as the source of truth for
prev_hash(broken under multi-worker deployments), the cache was advanced before the DB commit (stale hash persisted on failure), andverify_blockchain_integritytrusted the storedprevious_integrity_hashwithout cross-checking the actual DB predecessor.Changes
Atomic
prev_hashderivation: Remove the cache lookup from the issue creation path entirely. Always query the DB for the lastintegrity_hashwithin the same transaction — every Gunicorn/Uvicorn worker now sees the same chain tail.Cache updated post-commit only:
blockchain_last_hash_cache.set()moved to aftersave_issue_dbsucceeds. Exception handler callsblockchain_last_hash_cache.invalidate()so a failed commit never leaves a hash in cache that future issues chain off.Verification cross-checks DB predecessor:
verify_blockchain_integritynow always fetches the actual predecessor from DB and compares it against the storedprevious_integrity_hash. A mismatch returnsis_valid=Falseimmediately with the computed hash (using the authoritative DB predecessor) so callers can see the expected value.Type of Change
Related Issue
Closes #
Testing Done
Screenshots (if applicable)
Checklist
Co-Authors
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.
Summary by cubic
Fixes multi-worker race conditions in blockchain hashing and strengthens integrity checks, while keeping admin stats fast. Follows up on #516 feedback to improve correctness and responsiveness.
blockchain_last_hash_cacheonly after a successful commit; invalidate on error to prevent stale chaining.verify_blockchain_integrity, always fetch the predecessor from the DB and flag mismatches with the storedprevious_integrity_hashto detect tampering or creation races.Written for commit bb24abb. Summary will update on new commits.