-
Notifications
You must be signed in to change notification settings - Fork 35
β‘ Bolt: Implement blockchain integrity for ClosureConfirmation #655
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
Open
RohanExploit
wants to merge
3
commits into
main
Choose a base branch
from
bolt-closure-blockchain-1527181114373412597
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import pytest | ||
| from sqlalchemy.orm import Session | ||
| from fastapi.testclient import TestClient | ||
| from datetime import datetime, timezone, timedelta | ||
|
|
||
| from backend.main import app | ||
| from backend.database import Base, get_db, engine | ||
| from backend.models import Grievance, GrievanceStatus, SeverityLevel, GrievanceFollower, Jurisdiction, JurisdictionLevel, ClosureConfirmation | ||
| from backend.closure_service import ClosureService | ||
|
|
||
| # Setup test database | ||
| @pytest.fixture(name="db_session") | ||
| def fixture_db_session(): | ||
| Base.metadata.create_all(bind=engine) | ||
| session = Session(bind=engine) | ||
| yield session | ||
| session.close() | ||
| Base.metadata.drop_all(bind=engine) | ||
|
|
||
|
RohanExploit marked this conversation as resolved.
|
||
| @pytest.fixture(name="client") | ||
| def fixture_client(db_session): | ||
| def override_get_db(): | ||
| try: | ||
| yield db_session | ||
| finally: | ||
| pass | ||
| app.dependency_overrides[get_db] = override_get_db | ||
| with TestClient(app) as c: | ||
| yield c | ||
| app.dependency_overrides.clear() | ||
|
|
||
| def test_closure_confirmation_blockchain_chaining(client, db_session): | ||
| # 1. Setup a grievance and followers | ||
| jurisdiction = Jurisdiction( | ||
| level=JurisdictionLevel.LOCAL, | ||
| geographic_coverage={"cities": ["Mumbai"]}, | ||
| responsible_authority="BMC", | ||
| default_sla_hours=24 | ||
| ) | ||
| db_session.add(jurisdiction) | ||
| db_session.flush() | ||
|
|
||
| grievance = Grievance( | ||
| unique_id="TEST-123", | ||
| category="Water", | ||
| severity=SeverityLevel.HIGH, | ||
| current_jurisdiction_id=jurisdiction.id, | ||
| assigned_authority="BMC", | ||
| sla_deadline=datetime.now(timezone.utc) + timedelta(hours=24), | ||
| status=GrievanceStatus.OPEN | ||
| ) | ||
| db_session.add(grievance) | ||
| db_session.flush() | ||
|
|
||
| followers = [ | ||
| GrievanceFollower(grievance_id=grievance.id, user_email=f"user{i}@example.com") | ||
| for i in range(5) | ||
| ] | ||
| for f in followers: | ||
| db_session.add(f) | ||
| db_session.commit() | ||
|
|
||
| # 2. Request closure | ||
| ClosureService.request_closure(grievance.id, db_session) | ||
|
|
||
| # 3. Submit multiple confirmations and verify chaining | ||
| emails = [f"user{i}@example.com" for i in range(3)] | ||
| conf_ids = [] | ||
|
|
||
| for email in emails: | ||
| result = ClosureService.submit_confirmation( | ||
| grievance_id=grievance.id, | ||
| user_email=email, | ||
| confirmation_type="confirmed", | ||
| reason="Verified resolved", | ||
| db=db_session | ||
| ) | ||
|
|
||
| # Get the record to check hashes | ||
| conf = db_session.query(ClosureConfirmation).filter( | ||
| ClosureConfirmation.user_email == email, | ||
| ClosureConfirmation.grievance_id == grievance.id | ||
| ).first() | ||
| conf_ids.append(conf.id) | ||
|
|
||
| # Verify the chain | ||
| conf1 = db_session.query(ClosureConfirmation).filter(ClosureConfirmation.id == conf_ids[0]).first() | ||
| conf2 = db_session.query(ClosureConfirmation).filter(ClosureConfirmation.id == conf_ids[1]).first() | ||
| conf3 = db_session.query(ClosureConfirmation).filter(ClosureConfirmation.id == conf_ids[2]).first() | ||
|
|
||
| assert conf1.previous_integrity_hash == "" | ||
| assert conf2.previous_integrity_hash == conf1.integrity_hash | ||
| assert conf3.previous_integrity_hash == conf2.integrity_hash | ||
|
|
||
| # 4. Verify via API endpoint | ||
| for cid in conf_ids: | ||
| response = client.get(f"/api/closure-confirmation/{cid}/blockchain-verify") | ||
| assert response.status_code == 200 | ||
| data = response.json() | ||
| assert data["is_valid"] is True | ||
| assert "Integrity verified" in data["message"] | ||
|
|
||
| # 5. Tamper with data and verify failure | ||
| conf2.confirmation_type = "disputed" | ||
| db_session.commit() | ||
|
|
||
| response = client.get(f"/api/closure-confirmation/{conf_ids[1]}/blockchain-verify") | ||
| assert response.status_code == 200 | ||
| assert response.json()["is_valid"] is False | ||
| assert "Integrity check failed" in response.json()["message"] | ||
|
|
||
| # Subsequent record should still be valid if its OWN data and recorded previous_integrity_hash match. | ||
| # Blockchain integrity check for a single record only verifies that IT matches its seal. | ||
| # To detect a break in the chain, you would need to verify the previous record as well. | ||
| # This is consistent with O(1) single-record verification. | ||
| response = client.get(f"/api/closure-confirmation/{conf_ids[2]}/blockchain-verify") | ||
| assert response.status_code == 200 | ||
| assert response.json()["is_valid"] is True | ||
|
RohanExploit marked this conversation as resolved.
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.