Skip to content

Add reorg and ledger-gap handling to the Horizon indexer (lib/indexer.ts) #230

@greatest0fallt1me

Description

@greatest0fallt1me

Description

lib/indexer.ts advances its cursor purely forward and only deduplicates by network:id:type; it has no detection for ledger gaps or chain reorganizations, so a reorg silently leaves processed events that no longer exist on-chain. Add reorg/gap handling so indexed stream events stay consistent with Horizon truth. This is a backend on-chain indexing feature.

Requirements and Context

  • Detect gaps when event.ledger > cursor + 1 + overlapWindow and trigger a bounded backfill from the last safe ledger.
  • Detect reorgs by tracking a short window of recent ledger hashes and rolling the cursor back when a hash mismatch is observed; re-emit affected events idempotently.
  • Replace the console.error stall "alert" with structured logging via the project logger and a metric counter.
  • Must be secure, tested, and documented
  • Should be efficient and easy to review

Suggested Execution

  1. Fork the repo and create a branch
    git checkout -b feature/indexer-reorg-handling
  2. Implement changes
    • lib/indexer.ts — gap detection, reorg rollback, hash window
    • lib/indexer.test.ts — extend coverage
  3. Test and commit
    • npm test -- lib/indexer.test.ts
    • Cover edge cases: ledger gap, reorg rollback + replay, duplicate suppression
    • Include test output and notes in the PR

Example commit message

feat: handle ledger gaps and reorgs in Horizon indexer

Acceptance Criteria

  • Gaps trigger bounded backfill from a safe ledger
  • Reorg rolls cursor back and re-emits affected events idempotently
  • Stall detection uses structured logs + a counter, not console.error
  • Coverage ≥ 90% for lib/indexer.ts

Guidelines

  • Minimum 90% test coverage including reorg/gap simulations
  • Clear documentation and inline comments
  • Timeframe: 96 hours

Metadata

Metadata

Labels

Stellar WaveIssues in the Stellar wave programbackendBackend service workdatabaseDatabase schema/migrationsenhancementNew feature or improvement
No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions