-
Notifications
You must be signed in to change notification settings - Fork 35
⚡ Bolt: Blockchain Chaining and ORM Query Optimization #652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,29 +87,13 @@ def create_grievance(self, grievance_data: Dict[str, Any], db: Session = None) - | |
| unique_id = str(uuid.uuid4())[:8].upper() | ||
|
|
||
| # Blockchain integrity logic | ||
| # We cache both the last grievance ID and its integrity hash, and validate | ||
| # the cache against the current DB state to avoid chaining to stale hashes | ||
| cached_prev_hash = grievance_last_hash_cache.get("last_hash") | ||
| cached_last_id = grievance_last_hash_cache.get("last_id") | ||
|
|
||
| # Always check the actual last grievance in the DB | ||
| last_grievance = db.query(Grievance.id, Grievance.integrity_hash).order_by(Grievance.id.desc()).first() | ||
| if last_grievance: | ||
| db_last_id, db_last_hash = last_grievance | ||
| else: | ||
| db_last_id, db_last_hash = None, "" | ||
|
|
||
| # If cache is missing or inconsistent with DB, refresh from DB | ||
| if ( | ||
| cached_prev_hash is None | ||
| or cached_last_id != db_last_id | ||
| or cached_prev_hash != db_last_hash | ||
| ): | ||
| prev_hash = db_last_hash or "" | ||
| # Performance Boost: Use thread-safe cache to eliminate DB query for last hash | ||
| prev_hash = grievance_last_hash_cache.get("last_hash") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Removing the DB-validation step from the blockchain cache creates a silent chain-corruption risk. The old code always compared the cached Consider restoring the DB cross-check, or at minimum verifying Prompt for AI agents |
||
| if prev_hash is None: | ||
| # Cache miss: Fetch only the last hash from DB | ||
| last_grievance = db.query(Grievance.integrity_hash).order_by(Grievance.id.desc()).first() | ||
| prev_hash = last_grievance[0] if last_grievance and last_grievance[0] else "" | ||
| grievance_last_hash_cache.set(data=prev_hash, key="last_hash") | ||
|
RohanExploit marked this conversation as resolved.
RohanExploit marked this conversation as resolved.
|
||
| grievance_last_hash_cache.set(data=db_last_id, key="last_id") | ||
| else: | ||
| prev_hash = cached_prev_hash or "" | ||
|
|
||
| # Chaining: hash(unique_id|category|severity|prev_hash) | ||
| hash_content = f"{unique_id}|{grievance_data.get('category', 'general')}|{severity.value}|{prev_hash}" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.