Repository navigation
fix(backfill): start paging at the window, not at the beginning (#653) - #807
Open
Neziahtech wants to merge 1 commit into
Open
Neziahtech wants to merge 1 commit into
Neziahtech wants to merge 1 commit into
Conversation
…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.
|
@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! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #653
The problem
Horizon's
/tradesendpoint has no time filter — only a paging cursor. Sonpm run backfillstarted from cursor'0'and paged the pair's entire trade history, throwing away everything before--starton 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.tslocates the ledger that closed at or before--startwith 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_MSpauses 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
fetchHorizonJsonand the SDEX path through the newcallHorizonWithRetry, 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'sON CONFLICT DO NOTHINGidentity 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.resolveStartCursorbuilds 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 onendare kept while the one beforestartis 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.exampledocuments the four new settings;README.mdgained 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
/ledgersindex 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.