Skip to content

Feature/ledger backfill 419#502

Open
Babigdk wants to merge 3 commits into
Pulsefy:mainfrom
Babigdk:feature/ledger-backfill-419
Open

Feature/ledger backfill 419#502
Babigdk wants to merge 3 commits into
Pulsefy:mainfrom
Babigdk:feature/ledger-backfill-419

Conversation

@Babigdk
Copy link
Copy Markdown

@Babigdk Babigdk commented May 28, 2026

Ledger Backfill Service for Testnet Contract Events (#419)

Closes #419

Overview

This PR implements a comprehensive ledger backfill service that scans historical contract events from the Stellar Testnet and populates analytics and timelines. The implementation follows best practices for idempotency, resumability, and observability.

✨ Key Features

1. Configurable Ledger Backfill

  • Start and end ledger sequence number configuration
  • Adjustable batch size for processing
  • Support for campaign-specific filtering
  • Input validation (non-negative, valid ranges)

2. Progress Checkpoint System

  • Persists backfill progress in database
  • Tracks exact ledger position after each batch
  • Enables resume from failure without reprocessing
  • Tracks error information and retry attempts
  • Support for pausing and resuming jobs

3. Idempotent Event Storage

  • Composite unique key on ContractEvent model
  • Unique constraint: (contractId, ledgerSequence, transactionHash, eventIndex)
  • Prevents duplicate events in database
  • Automatic detection and skipping of existing events
  • Tracked metrics (processed, skipped, errors)

4. Resilient Job Processing

  • BullMQ queue integration with exponential backoff
  • Max 3 retry attempts per batch
  • Dead Letter Queue (DLQ) support for permanently failed jobs
  • Checkpoint updates after each batch for resume capability

📦 Database Changes

New Models

BackfillCheckpoint

  • Tracks progress for each backfill job
  • Fields: id, jobId, contractId, startLedger, endLedger, lastProcessedLedger
  • Status tracking: pending → processing → completed/failed/paused
  • Running counters: processed, skipped, errors
  • Resume support: resumeCount, maxRetries
  • Metadata storage for batch configuration

ContractEvent

  • Stores contract events fetched from blockchain
  • Composite unique key prevents duplicates
  • Event metadata: type, topics, data payload
  • Processing tracking: processedAt, processedBy
  • Ledger reference: ledgerSequence, transactionHash, eventIndex

🔌 API Endpoints

All endpoints require admin role and API authentication.

POST /v1/admin/ledger/backfill

Trigger a new backfill job

Request:

{
  "contractId": "CCONTRACTID...",
  "startLedger": 1000,
  "endLedger": 2000,
  "campaignId": "campaign_123",
  "batchSize": 100
}

Babigdk added 3 commits May 28, 2026 06:59
…and idempotency

- Add BackfillCheckpoint model to track backfill progress
- Add ContractEvent model to store contract events with composite unique key
- Implement LedgerBackfillService with full backfill logic:
  * triggerBackfill: Start new backfill job with checkpoint
  * resumeBackfill: Resume from last processed ledger
  * processBackfillBatch: Process batches of ledgers
  * processLedgerRange: Handle event fetching and storage
  * getBackfillStatus: Query checkpoint status
  * listCheckpoints: List and filter checkpoints
  * pauseBackfill: Pause running jobs
- Add LedgerBackfillProcessor for BullMQ job processing
- Enhanced LedgerAdminController with new endpoints:
  * POST /admin/ledger/backfill - Trigger backfill
  * GET /admin/ledger/backfill - List checkpoints
  * GET /admin/ledger/backfill/:checkpointId - Get status
  * POST /admin/ledger/backfill/:checkpointId/resume - Resume job
  * POST /admin/ledger/backfill/:checkpointId/pause - Pause job
- Add comprehensive tests for LedgerBackfillService
- Ensure idempotency through composite unique keys on ContractEvent
- Support configurable batch sizes and max retry attempts

Closes Pulsefy#419
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 28, 2026

@Babigdk Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ledger Backfill Service for Testnet Contract Events

1 participant