-
Notifications
You must be signed in to change notification settings - Fork 35
⚡ Bolt: Optimized blockchain verification to O(1) using previous_integrity_hash column #454
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
6a3c245
33f1fd8
1caa642
df83ddb
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 |
|---|---|---|
|
|
@@ -31,3 +31,4 @@ pydub | |
| googletrans==4.0.2 | ||
| langdetect | ||
| indic-nlp-library | ||
| async-lru | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -97,3 +97,48 @@ def test_upvote_optimization(client, db_session): | |||||
| # Verify in DB | ||||||
| db_session.refresh(issue) | ||||||
| assert issue.upvotes == 11 | ||||||
|
|
||||||
| def test_blockchain_o1_optimization(client, db_session): | ||||||
| # This test verifies that the previous_integrity_hash is stored | ||||||
| # and used to avoid the extra query. | ||||||
|
|
||||||
| # Create first issue | ||||||
| response = client.post( | ||||||
| "/api/issues", | ||||||
| data={ | ||||||
| "description": "First issue for O1 test", | ||||||
| "category": "Road" | ||||||
| } | ||||||
| ) | ||||||
| assert response.status_code == 201 | ||||||
| id1 = response.json()["id"] | ||||||
|
|
||||||
| issue1 = db_session.query(Issue).filter(Issue.id == id1).first() | ||||||
| hash1 = issue1.integrity_hash | ||||||
| # The very first issue in an empty DB will have empty previous hash | ||||||
| assert issue1.previous_integrity_hash == "" | ||||||
|
|
||||||
| # Create second issue | ||||||
| response = client.post( | ||||||
| "/api/issues", | ||||||
| data={ | ||||||
| "description": "Second issue for O1 test", | ||||||
| "category": "Garbage" | ||||||
| } | ||||||
| ) | ||||||
| assert response.status_code == 201 | ||||||
| id2 = response.json()["id"] | ||||||
|
|
||||||
| issue2 = db_session.query(Issue).filter(Issue.id == id2).first() | ||||||
| # Check that it stored the hash of the first issue | ||||||
| assert issue2.previous_integrity_hash == hash1 | ||||||
|
|
||||||
| # Verify the chain re-calculation logic matches | ||||||
| expected_hash2_content = f"Second issue for O1 test|Garbage|{hash1}" | ||||||
| expected_hash2 = hashlib.sha256(expected_hash2_content.encode()).hexdigest() | ||||||
| assert issue2.integrity_hash == expected_hash2 | ||||||
|
|
||||||
| # Verify endpoint still works (it will use the O1 path internally) | ||||||
| response = client.get(f"/api/issues/{id2}/blockchain-verify") | ||||||
| assert response.status_code == 200 | ||||||
| assert response.json()["is_valid"] == True | ||||||
|
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. Fix 🔧 Proposed fix- assert response.json()["is_valid"] == True
+ assert response.json()["is_valid"]📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.15.1)[error] 144-144: Avoid equality comparisons to Replace with (E712) 🤖 Prompt for AI Agents |
||||||
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.
No schema migration included — deployment will fail on existing databases.
Adding a column to a SQLAlchemy model does not automatically alter the live database table. Without a migration (Alembic revision or raw
ALTER TABLE issues ADD COLUMN previous_integrity_hash VARCHAR;), any deployment against a pre-existing database will raisesqlalchemy.exc.OperationalError(or a silent query error depending on the driver) the first timeprevious_integrity_hashis projected in a query — which happens on every call to/api/issues/{id}/blockchain-verifyafter this PR.The column is
nullable=True, so no backfill is strictly required, but theALTER TABLEDDL must be applied before the new code goes live.🤖 Prompt for AI Agents