Repository navigation
Description
Problem
-
The sync cursor is last-writer-wins.
run()readscursor_blockonce, indexes chunks, then writescursor_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_blockhas the same issue: a machine that read a lower head can lower it. -
A failed full poll is not deduplicated.
pollLaunchesdedupes per-chainrun(), butpollAll()still runs the heals, backfills, and attribution pass for every concurrent caller. The 15s loop andPOST /api/launch/syncwithout a tx can overlap and duplicate the expensive work. -
Trader healing can starve rows.
healSwapTradersselects the newest 100trader IS NULLrows every poll. If the newest 100 transactions cannot be read (RPC refusal, dropped or reorged tx), they are put inhealRestUntiland skipped. The query still returns the same 100 rows, so rows behind them are never reached. The 5,000-entry rest map callsclear()on overflow, which retries every parked transaction at once. -
The missing-trader scan has no matching index. The query filters on
chain_idandtrader IS NULLand orders by block; the existing partial indexes are ontrader_via, nottrader. -
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})andhead_block = GREATEST(COALESCE(head_block, 0), ${head}); clearlast_erroronly when this run reached the stored cursor. - Wrap the whole
pollAllpass in a single-flight promise (keep the per-chainpollLaunchesdedupe). - Page
healSwapTraderswith 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 NULLindb/concurrent-indexes.sql. - Prune expired entries from
healRestUntilandattributionTriesinstead of clearing them.
Acceptance criteria
- Two overlapping syncs cannot move the cursor or head backwards.
- Concurrent
pollAllcallers share one pass. healSwapTradersuses keyset pagination, notLIMIT 100newest-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