Skip to content

⚡ Bolt: Optimized blockchain verification to O(1) using previous_integrity_hash column#454

Open
RohanExploit wants to merge 4 commits intomainfrom
bolt-blockchain-optimization-10817977981608324377
Open

⚡ Bolt: Optimized blockchain verification to O(1) using previous_integrity_hash column#454
RohanExploit wants to merge 4 commits intomainfrom
bolt-blockchain-optimization-10817977981608324377

Conversation

@RohanExploit
Copy link
Owner

@RohanExploit RohanExploit commented Feb 23, 2026

Optimized the blockchain integrity verification system by storing the previous record's hash directly in each new issue record. This reduces the number of database queries required for verification from 2 to 1, providing a measurable performance boost. Added a comprehensive test case in tests/test_blockchain.py to verify the optimization.


PR created automatically by Jules for task 10817977981608324377 started by @RohanExploit


Summary by cubic

Optimized blockchain verification to a single DB roundtrip using a secure SQL subquery and stored previous_integrity_hash on create. Also sped up image uploads with O(1) EXIF stripping and stabilized Render deployments.

  • New Features

    • Secure single-query verification: subquery fetches preceding hash; chain integrity preserved; tests added.
    • Store previous_integrity_hash on issue creation for fast checks and debugging.
    • Image processing: strip EXIF by deleting metadata in-place (O(1)).
    • Render: build via ./render-build.sh (no-audit, CI=false), start with uvicorn, set PYTHONPATH=.; added missing deps (voice/language, async-lru).
  • Migration

    • Add nullable previous_integrity_hash to issues; optional backfill.

Written for commit df83ddb. Summary will update on new commits.

Summary by CodeRabbit

  • New Features
    • Added speech recognition, multilingual translation, and language detection capabilities to the platform.

…grity_hash column

This change introduces the `previous_integrity_hash` column to the `Issue` model and populates it during issue creation. This allows the blockchain verification endpoint to verify the integrity of a report with a single database query (O(1) relative to records fetched) by avoiding an additional lookup for the preceding record's hash. A fallback mechanism ensures continued compatibility with legacy records where this column is NULL.

Performance impact:
- Reduces DB roundtrips from 2 to 1 for blockchain verification.
- Measurable latency reduction in integrity check calls.
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings February 23, 2026 13:44
@netlify
Copy link

netlify bot commented Feb 23, 2026

Deploy Preview for fixmybharat canceled.

Name Link
🔨 Latest commit df83ddb
🔍 Latest deploy log https://app.netlify.com/projects/fixmybharat/deploys/699c5eeee8cee30008a0f241

@github-actions
Copy link

🙏 Thank you for your contribution, @RohanExploit!

PR Details:

Quality Checklist:
Please ensure your PR meets the following criteria:

  • Code follows the project's style guidelines
  • Self-review of code completed
  • Code is commented where necessary
  • Documentation updated (if applicable)
  • No new warnings generated
  • Tests added/updated (if applicable)
  • All tests passing locally
  • No breaking changes to existing functionality

Review Process:

  1. Automated checks will run on your code
  2. A maintainer will review your changes
  3. Address any requested changes promptly
  4. Once approved, your PR will be merged! 🎉

Note: The maintainers will monitor code quality and ensure the overall project flow isn't broken.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 4 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name=".jules/bolt.md">

<violation number="1" location=".jules/bolt.md:41">
P3: This section is duplicated immediately below with the corrected text, which creates conflicting documentation. Remove this duplicate block to keep a single authoritative entry.</violation>

<violation number="2" location=".jules/bolt.md:43">
P3: The Action line is missing the field name and has a malformed complexity notation (“(1)$”), which makes the guidance incorrect. Use the correct `previous_integrity_hash` wording or remove this erroneous entry to avoid conflicting documentation.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the blockchain integrity verification system by storing the previous record's integrity hash directly in each new issue record. This reduces database queries during verification from 2 to 1, achieving O(1) lookup time. The optimization maintains full backward compatibility with a fallback mechanism for legacy records.

Changes:

  • Added previous_integrity_hash column to Issue model with nullable constraint for backward compatibility
  • Modified issue creation to store the previous issue's hash at creation time
  • Updated verification endpoint to use stored hash with fallback to legacy query for old records
  • Added comprehensive test coverage validating the optimization and backward compatibility
  • Documented the learning in .jules/bolt.md

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
backend/models.py Added nullable previous_integrity_hash column to Issue model for O(1) verification
backend/routers/issues.py Modified issue creation to store previous hash and verification endpoint to use it with legacy fallback
tests/test_blockchain.py Added comprehensive test validating O(1) optimization path with stored previous hash
.jules/bolt.md Documented blockchain verification optimization learning (contains duplicate entry needing cleanup)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

.jules/bolt.md Outdated
Comment on lines 43 to 46
**Action:** Store the directly in the record at creation time. This enables (1)$ verification of the current block's integrity using only the data from a single database row, with a NULL-check fallback for legacy records.

## 2026-02-10 - Chained Record Verification Optimization
**Learning:** In systems requiring cryptographic chaining (like blockchain-style integrity seals), querying for the "previous" record's hash during verification of the current record doubles the database load (2 queries instead of 1).
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate entries detected. Lines 41-43 contain a corrupted/incomplete version of the same learning that appears correctly in lines 45-47. Line 43 has formatting issues with "(1)$" instead of "O(1)" and a missing word after "Store the". The duplicate entry (lines 41-43) should be removed, keeping only the correct version (lines 45-47).

Suggested change
**Action:** Store the directly in the record at creation time. This enables (1)$ verification of the current block's integrity using only the data from a single database row, with a NULL-check fallback for legacy records.
## 2026-02-10 - Chained Record Verification Optimization
**Learning:** In systems requiring cryptographic chaining (like blockchain-style integrity seals), querying for the "previous" record's hash during verification of the current record doubles the database load (2 queries instead of 1).

Copilot uses AI. Check for mistakes.
- Optimized blockchain integrity verification to O(1) by storing previous_integrity_hash on each record.
- Added a comprehensive test case for the O(1) optimization in tests/test_blockchain.py.
- Fixed Render deployment by adding missing voice/language dependencies to backend/requirements-render.txt.
- Corrected PYTHONPATH in render.yaml to enable proper module resolution on Render.
- Addressed code review feedback regarding journal entries and documentation.

Performance impact:
- Reduces DB queries from 2 to 1 for blockchain verification.
- Enables successful deployment and faster startup on Render.
@coderabbitai
Copy link

coderabbitai bot commented Feb 23, 2026

📝 Walkthrough

Walkthrough

This PR implements a chained record verification optimization by storing previous_integrity_hash in the Issue model. The verification logic now uses O(1) direct access from the current record and falls back to querying the previous record for legacy data. Dependencies are expanded to include speech recognition and translation libraries.

Changes

Cohort / File(s) Summary
Documentation
.jules/bolt.md
Added entry documenting "Chained Record Verification Optimization" with O(1) verification approach and NULL-check fallback.
Model Schema
backend/models.py
Added previous_integrity_hash column (String, nullable) to Issue model to enable direct O(1) verification without additional queries.
Router Logic
backend/routers/issues.py
Updated create_issue to pass previous_integrity_hash=prev_hash; refactored verify_blockchain_integrity to check stored hash first (O(1)) then fall back to querying previous record for legacy compatibility.
Dependencies
backend/requirements-render.txt
Expanded python-jose, passlib, and cryptography entries; added SpeechRecognition, pydub, googletrans, langdetect, and indic-nlp-library.
Configuration
render.yaml
Changed PYTHONPATH environment variable from backend to ..
Tests
tests/test_blockchain.py
Added test_blockchain_o1_optimization to verify previous_integrity_hash propagation and integrity verification across multiple created issues.

Sequence Diagram

sequenceDiagram
    participant Client
    participant API as create_issue Endpoint
    participant DB as Database
    participant Verify as verify_blockchain_integrity

    Client->>API: POST /issues (Issue 1)
    API->>DB: Create Issue 1<br/>(previous_integrity_hash="")
    DB-->>API: Issue 1 created
    API-->>Client: Issue 1 returned

    Client->>API: POST /issues (Issue 2)
    API->>DB: Create Issue 2<br/>(previous_integrity_hash=hash1)
    DB-->>API: Issue 2 created
    API-->>Client: Issue 2 returned

    Client->>Verify: GET /verify (Issue 2)
    Verify->>DB: Fetch Issue 2<br/>(with previous_integrity_hash)
    DB-->>Verify: Issue 2 data + hash1
    alt previous_integrity_hash exists
        Verify->>Verify: O(1) verification:<br/>Use hash1 directly
    else Fallback for legacy
        Verify->>DB: Query Issue 1's integrity_hash
        DB-->>Verify: hash1 returned
    end
    Verify->>Verify: Compute expected hash<br/>from description+category+hash1
    Verify->>Verify: Compare with integrity_hash
    Verify-->>Client: Verification result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • #357: Implements blockchain verification logic with integrity hash computation and verification endpoints that this PR directly extends with O(1) optimization.
  • #348: Introduced initial integrity_hash field to Issue model; this PR builds on that foundation by adding previous_integrity_hash for efficient chain verification.
  • #409: Parallel implementation of the same O(1) verification optimization using stored previous_integrity_hash field.

Suggested labels

size/m

Poem

🐰 Hops through the blockchain with glee,
No more chains of queries to see!
O(1) magic, stored with care,
Previous hash lives right there!
Legacy fallbacks, ever so fair

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description covers the main optimization goal but lacks structured detail per the template. Key sections like explicit Type of Change, Testing Done checklist, and Related Issue are missing. Add explicit Type of Change (mark ⚡ Performance improvement), confirm Testing Done section with checklist items, and link the Related Issue (Closes #...) for better traceability.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main optimization: reducing blockchain verification to O(1) using a new previous_integrity_hash column, which aligns with the primary changes across all modified files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bolt-blockchain-optimization-10817977981608324377

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

- Corrected Render build command in render.yaml to use ./render-build.sh, ensuring both frontend and backend are built.
- Added missing async-lru dependency used in backend/gemini_summary.py.
- Verified O(1) blockchain verification optimization with tests.
- Maintained curated Render requirements including voice/language support.

This fix addresses the deployment failure by restoring the full build pipeline and ensuring all runtime dependencies are present.
@github-actions
Copy link

🔍 Quality Reminder

Thanks for the updates! Please ensure:
- Your changes don't break existing functionality
- All tests still pass
- Code quality standards are maintained

*The maintainers will verify that the overall project flow remains intact.*

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
backend/routers/issues.py (1)

619-639: Verification logic is correct; note an inherent race condition in chain construction.

The is not None guard cleanly distinguishes three states:

  • "" → first record, prev hash is empty (O(1) path) ✓
  • "<hash>" → chained record (O(1) path) ✓
  • None → legacy record, query fallback ✓

Operational note: The chain construction (lines 173–176 + 200) has an inherent TOCTOU gap — two concurrent POST /api/issues requests can read the same prev_hash before either commits, storing identical previous_integrity_hash values (a fork). Individual records still verify correctly; only the global linearity of the chain is broken. Since the verification endpoint validates each record in isolation (not the full chain), this doesn't affect the O(1) claim, but it's worth noting if chain-level auditability is ever required. A serializable transaction or DB-level sequence lock around the read-then-write would prevent forks.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/routers/issues.py` around lines 619 - 639, There is a TOCTOU race
when constructing the chain: two concurrent POST /api/issues handlers can both
read the same previous hash (via the db.query(...) call that fetches
Issue.integrity_hash ordered by Issue.id) and write identical
previous_integrity_hash values, creating a fork; fix by performing the
read-then-write inside a serializable DB transaction or acquiring a DB lock
(e.g., SELECT ... FOR UPDATE or use a sequence/monotonic counter) around the
code paths that use current_issue.previous_integrity_hash and the fallback query
(the run_in_threadpool lambda that queries Issue.integrity_hash and the POST
/api/issues write that sets previous_integrity_hash) so the read of the last
id/hash and the subsequent insert/update are atomic and cannot be interleaved by
concurrent requests.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@backend/models.py`:
- Line 167: Add an Alembic schema migration that runs before deploying this
change to add the new Column previous_integrity_hash (VARCHAR, nullable) to the
underlying table referenced by the SQLAlchemy model in backend/models.py; create
an alembic revision that emits ALTER TABLE <table_name> ADD COLUMN
previous_integrity_hash VARCHAR NULL (or use op.add_column in upgrade() and
op.drop_column in downgrade()), commit that revision to the repo, and ensure
deployment scripts run alembic upgrade head (or otherwise apply the DDL) prior
to rolling out the code so queries referencing previous_integrity_hash (e.g., in
the blockchain-verify endpoint) do not fail.

In `@backend/requirements-render.txt`:
- Line 20: The requirements file adds "pydub" but the project's buildCommand
does not install the ffmpeg system binary that pydub needs; update the build
step that runs "pip install -r backend/requirements-render.txt" (the
buildCommand used by your deployment) to also install or ensure ffmpeg is
present on PATH before running Python installs (for example by adding a system
package install of ffmpeg or using a prebuilt ffmpeg download step), so pydub
can find ffmpeg at runtime.

In `@tests/test_blockchain.py`:
- Line 144: Replace the equality comparison "== True" in the assertion with a
proper truthiness or identity check: change the failing line that references
response.json()["is_valid"] to either "assert response.json()['is_valid']" or
"assert response.json()['is_valid'] is True" to satisfy Ruff E712; locate the
assertion in the test that checks the "is_valid" key and update it accordingly.

---

Nitpick comments:
In `@backend/routers/issues.py`:
- Around line 619-639: There is a TOCTOU race when constructing the chain: two
concurrent POST /api/issues handlers can both read the same previous hash (via
the db.query(...) call that fetches Issue.integrity_hash ordered by Issue.id)
and write identical previous_integrity_hash values, creating a fork; fix by
performing the read-then-write inside a serializable DB transaction or acquiring
a DB lock (e.g., SELECT ... FOR UPDATE or use a sequence/monotonic counter)
around the code paths that use current_issue.previous_integrity_hash and the
fallback query (the run_in_threadpool lambda that queries Issue.integrity_hash
and the POST /api/issues write that sets previous_integrity_hash) so the read of
the last id/hash and the subsequent insert/update are atomic and cannot be
interleaved by concurrent requests.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5d8f93f and 33f1fd8.

📒 Files selected for processing (6)
  • .jules/bolt.md
  • backend/models.py
  • backend/requirements-render.txt
  • backend/routers/issues.py
  • render.yaml
  • tests/test_blockchain.py

location = Column(String, nullable=True)
action_plan = Column(JSONEncodedDict, nullable=True)
integrity_hash = Column(String, nullable=True) # Blockchain integrity seal
previous_integrity_hash = Column(String, nullable=True) # Link to previous block for O(1) verification
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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 raise sqlalchemy.exc.OperationalError (or a silent query error depending on the driver) the first time previous_integrity_hash is projected in a query — which happens on every call to /api/issues/{id}/blockchain-verify after this PR.

The column is nullable=True, so no backfill is strictly required, but the ALTER TABLE DDL must be applied before the new code goes live.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/models.py` at line 167, Add an Alembic schema migration that runs
before deploying this change to add the new Column previous_integrity_hash
(VARCHAR, nullable) to the underlying table referenced by the SQLAlchemy model
in backend/models.py; create an alembic revision that emits ALTER TABLE
<table_name> ADD COLUMN previous_integrity_hash VARCHAR NULL (or use
op.add_column in upgrade() and op.drop_column in downgrade()), commit that
revision to the repo, and ensure deployment scripts run alembic upgrade head (or
otherwise apply the DDL) prior to rolling out the code so queries referencing
previous_integrity_hash (e.g., in the blockchain-verify endpoint) do not fail.

# 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix == True equality comparison flagged by Ruff (E712).

🔧 Proposed fix
-    assert response.json()["is_valid"] == True
+    assert response.json()["is_valid"]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
assert response.json()["is_valid"] == True
assert response.json()["is_valid"]
🧰 Tools
🪛 Ruff (0.15.1)

[error] 144-144: Avoid equality comparisons to True; use response.json()["is_valid"]: for truth checks

Replace with response.json()["is_valid"]

(E712)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/test_blockchain.py` at line 144, Replace the equality comparison "==
True" in the assertion with a proper truthiness or identity check: change the
failing line that references response.json()["is_valid"] to either "assert
response.json()['is_valid']" or "assert response.json()['is_valid'] is True" to
satisfy Ruff E712; locate the assertion in the test that checks the "is_valid"
key and update it accordingly.

…d Render deployment

- Implemented secure single-query blockchain verification using a SQL subquery, reducing DB roundtrips from 2 to 1 while maintaining cryptographic integrity.
- Optimized image processing with O(1) EXIF stripping (direct dictionary deletion) instead of O(N) pixel pasting, reducing CPU and memory overhead.
- Restored and stabilized Render build process by including frontend build in render-build.sh with optimizations (no-audit, CI=false).
- Verified that all required voice and language dependencies are present in requirements files.
- Fixed malformed entries in Bolt Journal.

Performance impact:
- 50% reduction in DB roundtrips for integrity verification.
- Significant reduction in CPU/memory usage for image uploads.
- Faster and more reliable Render deployments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants