Skip to content

Indexer durability: monotonic cursor, single-flight polls, and fair trader healing #112

Closed

Description

@Ayush7614

Problem

  1. The sync cursor is last-writer-wins. run() reads cursor_block once, indexes chunks, then writes cursor_block = ${to}. Two machines that start from the same cursor can finish out of order: A reaches block 200, B reaches block 190, and B writes 190 after A. The cursor goes backwards. The next pass rescans 190..200, holder backfill and health lag are computed from a stale cursor, and the regression repeats under concurrency. head_block has the same issue: a machine that read a lower head can lower it.

  2. A failed full poll is not deduplicated. pollLaunches dedupes per-chain run(), but pollAll() still runs the heals, backfills, and attribution pass for every concurrent caller. The 15s loop and POST /api/launch/sync without a tx can overlap and duplicate the expensive work.

  3. Trader healing can starve rows. healSwapTraders selects the newest 100 trader IS NULL rows every poll. If the newest 100 transactions cannot be read (RPC refusal, dropped or reorged tx), they are put in healRestUntil and skipped. The query still returns the same 100 rows, so rows behind them are never reached. The 5,000-entry rest map calls clear() on overflow, which retries every parked transaction at once.

  4. The missing-trader scan has no matching index. The query filters on chain_id and trader IS NULL and orders by block; the existing partial indexes are on trader_via, not trader.

  5. The attribution retry map also clears at 20k, resetting all backoff state at once.

Impact

  • Repeated range scans and a cursor that can go backwards; health lag can be wrong.
  • Concurrent web syncs can multiply the most expensive work in the app.
  • Some swaps can never get a trader, so holder, ranking, and points facts silently omit them.
  • Retry storms after a map overflow.

Proposed fix

  • Write cursor_block = GREATEST(cursor_block, ${to}) and head_block = GREATEST(COALESCE(head_block, 0), ${head}); clear last_error only when this run reached the stored cursor.
  • Wrap the whole pollAll pass in a single-flight promise (keep the per-chain pollLaunches dedupe).
  • Page healSwapTraders with a keyset (block_number, log_index) > cursor, oldest first, and advance past visited transaction groups. Wrap at the end so parked rows are retried later. This removes head-of-line blocking and visits every row.
  • Add a partial index on (chain_id, block_number, log_index) WHERE trader IS NULL in db/concurrent-indexes.sql.
  • Prune expired entries from healRestUntil and attributionTries instead of clearing them.

Acceptance criteria

  • Two overlapping syncs cannot move the cursor or head backwards.
  • Concurrent pollAll callers share one pass.
  • healSwapTraders uses keyset pagination, not LIMIT 100 newest-first; a page of parked rows cannot block the rest.
  • The partial index is present and validated by the schema test.
  • Unit and source-contract tests cover the cursor, single-flight, paging, and pruning; full checks pass.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions