Skip to content

feat: Implement Reorg-Aware Event Indexer with zk-SNARK Quality Attestation Pipeline (#1319) - #1330

Merged
Nitya-003 merged 7 commits into
Nitya-003:mainfrom
Neelr1912:feature/zk-reorg-indexer-1319
Aug 16, 2026
Merged

Nitya-003 merged 7 commits into
Nitya-003:mainfrom
Neelr1912:feature/zk-reorg-indexer-1319

Conversation

@Neelr1912

Copy link
Copy Markdown
Contributor

Closes #1319

📌 Context & Problem Statement

Currently, CropChain relies on basic real-time event listeners which are vulnerable to state desynchronization from blockchain reorganizations (reorgs) and socket drops. Furthermore, confidential crop quality compliance data (pesticide PPM, moisture percentage, organic grading) needs to be attested on-chain without revealing sensitive farm trade secrets.

This PR implements a resilient Reorg-Aware Blockchain Event Indexer Engine combined with a Zero-Knowledge Proof (zk-SNARK) quality verification pipeline.


🏗️ Summary of Changes

1. Circom zk-SNARK Quality Attestation Circuit (smart-contracts/circuits/)

  • Created QualityAttestation.circom circuit to confidentially enforce:
    • Pesticide_PPM < Threshold
    • Moisture_Percentage within acceptable bounds [MIN, MAX]
    • Certified Grade == CertifiedGrade
  • Created compile-circuit.js script to automate .wasm, .zkey, and Groth16 Solidity verifier artifact exports.

2. On-Chain zk-SNARK Verification & Replay Protection (smart-contracts/contracts/)

  • Updated Verifier.sol with standard Groth16Verifier contract implementation.
  • Updated CropChain.sol with verifyQualityAttestation() to validate ZK proofs before state transitions.
  • Integrated spentProofHashes nullifier map to prevent proof replay attacks.

3. Reorg-Aware Event Indexer Engine (backend/)

  • Created BlockHeader and IndexedEvent Mongoose models to track block parent hash continuity and event status (UNFINALIZED vs FINALIZED).
  • Implemented configurable block confirmation depth ($N = 5$ blocks).
  • Built automatic rollback to common ancestor block upon detecting chain forks up to 5 blocks deep.
  • Implemented catch-up self-healing chunked block pagination (chunkSize = 100) via eth_getLogs for RPC recovery.

4. Dead Letter Queue (DLQ) & Admin Reconciliation API (backend/)

  • Created DeadLetterEvent model and indexerDlqService.js to push unprocessable events to DLQ with exponential backoff retries.
  • Exposed protected admin REST API POST /api/v1/indexer/reconcile for manual forced state synchronization.

🧪 Edge Cases Handled

  • 5-Block Chain Reorgs: Orphan records rolled back to common ancestor block and canonical chain re-indexed cleanly without leaving orphan DB state.
  • Proof Replay Attacks: Spent ZK proof hashes tracked on-chain to revert duplicate proof submissions.
  • RPC Failures & WS Disconnects: Chunked pagination catch-up recovers logs automatically after disconnection.

✅ Testing & Verification

  • Smart Contract Tests: 67/67 passing (npm run test:ci in smart-contracts/)
  • Backend Indexer & DLQ Tests: 6/6 passing (npx jest tests/reorgIndexer.test.js)
  • Circuit Build Pipeline: node scripts/compile-circuit.js executed cleanly.

@vercel

vercel Bot commented Aug 13, 2026

Copy link
Copy Markdown

@Neelr1912 is attempting to deploy a commit to the Nitya Gosain's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Nitya-003 Nitya-003 added enhancement New feature or request gssoc:approved gssoc'26 approved level:beginner 20 pts for contributor 10 pts for mentor quality:clean *1.2 for contributor +5 pts for mentor level:advanced 55 pts for contributors 30 pts for mentors type:feature and removed level:beginner 20 pts for contributor 10 pts for mentor labels Aug 16, 2026
@Nitya-003
Nitya-003 merged commit e1d4bc2 into Nitya-003:main Aug 16, 2026
15 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request gssoc:approved gssoc'26 approved level:advanced 55 pts for contributors 30 pts for mentors quality:clean *1.2 for contributor +5 pts for mentor type:feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE/HARD]: Implement Reorg-Aware Blockchain Event Indexer with zk-SNARK Quality Attestation Pipeline

2 participants