Skip to content

fix(backfill): start paging at the window, not at the beginning (#653) - #807

Open
Neziahtech wants to merge 1 commit into
Divinity-Labs:mainfrom
Neziahtech:fix/backfill-cursor-653
Open

Neziahtech wants to merge 1 commit into
Divinity-Labs:mainfrom
Neziahtech:fix/backfill-cursor-653

Conversation

@Neziahtech

Copy link
Copy Markdown
Contributor

Closes #653

The problem

Horizon's /trades endpoint has no time filter — only a paging cursor. So npm run backfill started from cursor '0' and paged the pair's entire trade history, throwing away everything before --start on the way. Backfilling one hour of history cost a full walk of the pair; a pair Lens had never seen paid for its whole life before it could be filled in. And because Horizon answers older and older pages until it runs out, an operator could not tell a slow run from a stuck one, or stop one without killing it.

What this changes

The run starts at the window, not at the beginning. src/backfill/ledgerCursor.ts locates the ledger that closed at or before --start with a bounded binary search over /ledgers/{seq} — about 26 requests whether the pair is a week old or a year old — and the first trades page is built from that ledger's paging token. A run that cannot reach the ledger index says so instead of guessing a starting ledger and paging from zero.

The boundary is read, not skipped. A ledger close and the trades inside it are not the same instant, so the start cursor is placed BACKFILL_LOOKBACK_LEDGERS (default 30) ledgers before the located one, and rows outside [start, end] are filtered as they always were. Nothing at the edge of the window is missed; raising the lookback costs requests, not correctness.

Bounded, paced, visible. BACKFILL_MAX_PAGES (default 500, across every pool for an AMM run) stops a run with a message naming the cursor it stopped at and a non-zero exit, so a resumable stop cannot look like a completed backfill. BACKFILL_PAGE_DELAY_MS pauses between pages so a backfill does not spend Horizon's whole per-IP budget on top of the live ingester. Each page reports progress.

429 is a retry, not the end of the data. The AMM path goes through fetchHorizonJson and the SDEX path through the new callHorizonWithRetry, both using the same backoff live ingestion already uses (src/ingest/venues/horizon.ts). A rate limit that cannot be got past is still treated as the failed page it is — it does not silently truncate the range.

What is deliberately unchanged

How a failed page is handled, how backfilled rows are timestamped, and the idempotency contract: a second run over the same range still inserts zero rows (src/backfill/backfillService.ts's ON CONFLICT DO NOTHING identity is untouched). scripts/backfill.ts's existing flags keep their meaning; the new settings are read the way the rest of the tool's settings are.

How it is proved

  • src/__tests__/ledgerCursor.test.ts — the search bisects to the greatest ledger at or before the target, its request count is logarithmic and does not grow with the range, it returns the tip for a future target and the first ledger for a pre-network one, it stops at its own request budget with a clear error, and it surfaces a Horizon failure rather than guessing. resolveStartCursor builds the first token of (located ledger − lookback), never before ledger 1, and points at the configured network.
  • src/__tests__/backfillPaging.test.ts — drives the real paging loop against stubbed Horizon responses: the run begins at the window and makes the same number of requests for a week-old and a year-old pair; the trade in the boundary ledger and a trade landing exactly on end are kept while the one before start is dropped; the page limit stops with the cursor named; pages pause; progress is reported per page; a 429 on a page and on a ledger lookup is retried and the run continues; a second identical run inserts nothing; and the AMM path pages each pool from the window and retries its own 429s.
  • src/__tests__/backfill.test.ts — the existing conflict-path and batching expectations still pass unchanged.
  • .env.example documents the four new settings; README.md gained the pointer so the flags and the env names are findable from the usage section.

No real network access was used anywhere: every test stubs the Horizon fetches, as the existing ingester tests do. No floating-point price maths is introduced — trades are counted, not priced. The three files above run with npx vitest run src/__tests__/backfillPaging.test.ts src/__tests__/ledgerCursor.test.ts src/__tests__/backfill.test.ts.

Known limit

The ledger search assumes ledger close times are monotonic with sequence, which is what Horizon's own /ledgers index reports. If the located ledger is a few seconds off the boundary, the lookback absorbs it — that is the reason it exists rather than starting exactly at the located ledger.

…nity-Labs#653)

Horizon has no time filter on /trades, so `npm run backfill` paged from
cursor '0' and threw away everything before --start: an hour of history
cost the pair's whole trade history, and a fresh pair paid for it too.

The run now locates the ledger that closed at --start with a bounded
binary search over /ledgers/{seq} (~26 requests whatever the pair's age,
capped by its own request budget) and pages trades from that ledger's
first paging token, minus a configurable lookback so a trade in the
boundary ledger is still read and filtered, never missed.

Paging is bounded, paced, and visible: --max-pages stops a run with the
cursor it stopped at and a non-zero exit, --page-delay-ms pauses between
pages, and each page reports progress so an operator can tell slow from
stuck. The AMM path goes through fetchHorizonJson and the SDEX path
through the new callHorizonWithRetry, so a 429 is retried with the same
backoff as live ingestion instead of looking like the end of the data.

What happens when a page fails and how backfilled rows are timestamped
are unchanged, and a second run over the same range still inserts zero
rows.
@drips-wave

drips-wave Bot commented Nov 23, 2026

Copy link
Copy Markdown

@Neziahtech 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.

Backfill walks every trade since the pair's first one to reach its window

1 participant