-
Notifications
You must be signed in to change notification settings - Fork 37
⚡ Bolt: Optimized Blockchain Verification to O(1) #430
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -175,8 +175,10 @@ async def create_issue( | |
| ) | ||
| prev_hash = prev_issue[0] if prev_issue and prev_issue[0] else "" | ||
|
|
||
| # Simple but effective SHA-256 chaining | ||
| hash_content = f"{description}|{category}|{prev_hash}" | ||
| # Explicit cryptographic link to previous report and geographic context | ||
| lat_str = f"{latitude:.7f}" if latitude is not None else "0.0000000" | ||
| lon_str = f"{longitude:.7f}" if longitude is not None else "0.0000000" | ||
| hash_content = f"{description}|{category}|{lat_str}|{lon_str}|{prev_hash}" | ||
| integrity_hash = hashlib.sha256(hash_content.encode()).hexdigest() | ||
|
Comment on lines
+178
to
182
|
||
|
|
||
| # RAG Retrieval (New) | ||
|
|
@@ -196,7 +198,8 @@ async def create_issue( | |
| longitude=longitude, | ||
| location=location, | ||
| action_plan=initial_action_plan, | ||
| integrity_hash=integrity_hash | ||
| integrity_hash=integrity_hash, | ||
| previous_integrity_hash=prev_hash | ||
| ) | ||
|
|
||
| # Offload blocking DB operations to threadpool | ||
|
|
@@ -615,28 +618,31 @@ def get_user_issues( | |
| async def verify_blockchain_integrity(issue_id: int, db: Session = Depends(get_db)): | ||
| """ | ||
| Verify the cryptographic integrity of a report using the blockchain-style chaining. | ||
| Optimized: Uses column projection to fetch only needed data. | ||
| Optimized: O(1) retrieval of all data needed for link validation. | ||
| """ | ||
| # Fetch current issue data | ||
| # Performance Boost: O(1) retrieval of all data needed for link validation | ||
| # We store the previous_integrity_hash directly to avoid an extra query. | ||
| current_issue = await run_in_threadpool( | ||
| lambda: db.query( | ||
| Issue.id, Issue.description, Issue.category, Issue.integrity_hash | ||
| Issue.description, | ||
| Issue.category, | ||
| Issue.latitude, | ||
| Issue.longitude, | ||
| Issue.integrity_hash, | ||
| Issue.previous_integrity_hash | ||
| ).filter(Issue.id == issue_id).first() | ||
| ) | ||
|
|
||
| if not current_issue: | ||
| raise HTTPException(status_code=404, detail="Issue not found") | ||
|
|
||
| # Fetch previous issue's integrity hash to verify the chain | ||
| prev_issue_hash = await run_in_threadpool( | ||
| lambda: db.query(Issue.integrity_hash).filter(Issue.id < issue_id).order_by(Issue.id.desc()).first() | ||
| ) | ||
|
|
||
| prev_hash = prev_issue_hash[0] if prev_issue_hash and prev_issue_hash[0] else "" | ||
| # Recompute hash using stored previous hash and geographic context | ||
| # Chaining logic: hash(description|category|lat|lon|prev_hash) | ||
| lat_str = f"{current_issue.latitude:.7f}" if current_issue.latitude is not None else "0.0000000" | ||
| lon_str = f"{current_issue.longitude:.7f}" if current_issue.longitude is not None else "0.0000000" | ||
| prev_hash = current_issue.previous_integrity_hash or "" | ||
|
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. P2: Security regression: denormalizing Prompt for AI agents |
||
|
|
||
| # Recompute hash based on current data and previous hash | ||
| # Chaining logic: hash(description|category|prev_hash) | ||
| hash_content = f"{current_issue.description}|{current_issue.category}|{prev_hash}" | ||
| hash_content = f"{current_issue.description}|{current_issue.category}|{lat_str}|{lon_str}|{prev_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: Breaking change: verification will report false tampering for all pre-existing records. Old records were hashed as Prompt for AI agents |
||
| computed_hash = hashlib.sha256(hash_content.encode()).hexdigest() | ||
|
|
||
| is_valid = (computed_hash == current_issue.integrity_hash) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,26 +23,36 @@ def client(db_session): | |
|
|
||
| def test_blockchain_verification_success(client, db_session): | ||
| # Create first issue | ||
| hash1_content = "First issue|Road|" | ||
| lat1, lon1 = 19.0760, 72.8777 | ||
| lat1_str, lon1_str = f"{lat1:.7f}", f"{lon1:.7f}" | ||
| hash1_content = f"First issue|Road|{lat1_str}|{lon1_str}|" | ||
| hash1 = hashlib.sha256(hash1_content.encode()).hexdigest() | ||
|
|
||
| issue1 = Issue( | ||
| description="First issue", | ||
| category="Road", | ||
| integrity_hash=hash1 | ||
| latitude=lat1, | ||
| longitude=lon1, | ||
| integrity_hash=hash1, | ||
| previous_integrity_hash="" | ||
| ) | ||
| db_session.add(issue1) | ||
| db_session.commit() | ||
| db_session.refresh(issue1) | ||
|
|
||
| # Create second issue chained to first | ||
| hash2_content = f"Second issue|Garbage|{hash1}" | ||
| lat2, lon2 = 19.0761, 72.8778 | ||
| lat2_str, lon2_str = f"{lat2:.7f}", f"{lon2:.7f}" | ||
| hash2_content = f"Second issue|Garbage|{lat2_str}|{lon2_str}|{hash1}" | ||
| hash2 = hashlib.sha256(hash2_content.encode()).hexdigest() | ||
|
|
||
| issue2 = Issue( | ||
| description="Second issue", | ||
| category="Garbage", | ||
| integrity_hash=hash2 | ||
| latitude=lat2, | ||
| longitude=lon2, | ||
| integrity_hash=hash2, | ||
| previous_integrity_hash=hash1 | ||
| ) | ||
| db_session.add(issue2) | ||
| db_session.commit() | ||
|
|
@@ -64,10 +74,14 @@ def test_blockchain_verification_success(client, db_session): | |
|
|
||
| def test_blockchain_verification_failure(client, db_session): | ||
| # Create issue with tampered hash | ||
| lat, lon = 19.0760, 72.8777 | ||
| issue = Issue( | ||
| description="Tampered issue", | ||
| category="Road", | ||
| integrity_hash="invalidhash" | ||
| latitude=lat, | ||
| longitude=lon, | ||
| integrity_hash="invalidhash", | ||
| previous_integrity_hash="" | ||
| ) | ||
| db_session.add(issue) | ||
| db_session.commit() | ||
|
Comment on lines
75
to
87
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1:
bcrypt<4.0.0is incompatible with Python 3.12+ (the project's target runtime per the README). bcrypt 3.x fails to build on Python 3.12 (pyca/bcrypt#666); Python 3.12 support was added in bcrypt 4.1.1. This constraint will cause installation failures on Render deployment. Consider usingbcrypt>=4.0.1and ensuring passlib compatibility, or switching to a maintained passlib fork likepasslib>=1.7.4(orpwdlib) that works with bcrypt 4.x.Prompt for AI agents