Skip to content

fix(indexer): bound NFT metadata lookups with a worker pool and per-cycle budget - #217

Merged
Miracle656 merged 2 commits into
Miracle656:mainfrom
ezedike-evan:fix/nft-metadata-concurrency
Sep 30, 2026
Merged

Miracle656 merged 2 commits into
Miracle656:mainfrom
ezedike-evan:fix/nft-metadata-concurrency

Conversation

@ezedike-evan

Copy link
Copy Markdown
Collaborator

closes #205

Summary

pollOnce awaited getNftMetadata (and the metadata fetch) one token at a time, so N new tokens cost N serial round trips and a hung metadata source could stall the cursor.

Changes

  • New src/indexer/nft-metadata.ts with enrichNftMetadata: a worker pool (default 8, NFT_METADATA_CONCURRENCY) under a per-cycle time budget (default 10000 ms, NFT_METADATA_BUDGET_MS). No lookup starts after the budget is spent and an in-flight lookup that outlives it is abandoned, so the loop returns in roughly the budget regardless of source speed.
  • Failures are per token (cache read, fetch, upsert). Skipped or timed-out tokens are not written, so they stay uncached and are retried next cycle.
  • src/indexer.ts now calls it in place of the serial loop. Logs never include the raw error object (may carry DB errors or provider URLs); no metric labels added.
  • .env.example documents the two env vars.

Before / after

Unit test: 20 tokens with 50 ms lookups. Serial: at least 1000 ms. Pool of 5: about 200 ms (asserted under 700 ms, peak concurrency asserted at most 5).

Verification

  • npx jest src/__tests__/nftMetadata.test.ts: 10 passed (slow source, failing source, mixed batch, dedupe/cache, budget, env parsing). The module did not exist before, so these tests cannot pass on the original code.
  • npx jest src/__tests__/multiNetworkIndexer.test.ts src/__tests__/nft.test.ts: 35 passed.
  • npm run typecheck: clean.
  • Full suite not run (shared, heavily loaded machine).

Caveats

Timing tests use real timers with generous margins. Abandoned in-flight fetches are not cancelled (the RPC client has no abort here); they finish in the background and their result is discarded.

@drips-wave

drips-wave Bot commented Sep 30, 2026

Copy link
Copy Markdown

@ezedike-evan 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

@Miracle656

Copy link
Copy Markdown
Owner

Merged. Verified locally: typecheck clean and the full jest suite green at 552 passed / 46 suites.

enrichNftMetadata delivers all three things #205 asked for, and the tests actually demonstrate each rather than asserting the function runs: a worker pool with a measured peak in-flight count, a budget that a never-settling fetch cannot outlive, and per-token isolation shown by the exact set of tokens that still got stored around a thrower. The detail I'd call out is expect(errors).toEqual(["NFT metadata lookup failed"]) on an error carrying https://secret.provider/url — that pins the repo rule that a provider URL never reaches the log, not just that an error was counted. Deferring rather than writing an empty row on timeout is also the right choice: those tokens stay uncached and get retried instead of being poisoned.

I pushed a merge commit to your branch with two changes:

  1. Rebased onto fix(indexer): share one per-batch pipeline between the single and parallel ingest paths #219, which landed first. fix(indexer): share one per-batch pipeline between the single and parallel ingest paths #219 moved the whole per-batch pipeline out of src/indexer.ts and into src/indexer/batch.ts so the single and sharded ingest paths share one body, so your serial loop was no longer where you left it. I resolved src/indexer.ts to main's version and ported your enrichNftMetadata call into batch.ts. This is strictly better than the original placement: the bound now applies to the parallel path too, which previously did no metadata work at all. fix(indexer): share one per-batch pipeline between the single and parallel ingest paths #219's ingestParity.test.ts passes against the result, which is real evidence the move preserved behaviour — NftMetadata rows are part of the snapshot it compares across both paths. One consequence worth knowing: the budget is per batch, so a sharded run with N workers can spend up to N budgets concurrently. Fine at the defaults, but it means NFT_METADATA_CONCURRENCY is per worker, not global.
  2. src/__tests__/nftMetadata.test.ts hardcoded CBC42KFZO33TYVFDOUXFRWXYYXHFGH7W5GM4IJQSXKGFINKL2XPP4XTE as CONTRACT. It is 56 characters but fails StrKey.isValidContract — the checksum is wrong. Nothing broke, because the deps are mocked and no decode happens, but a fabricated address in a fixture has bitten this repo before, including one that threw at import and ran zero tests while the PR reported green. Replaced with StrKey.encodeContract(Buffer.alloc(32, 11)), valid by construction.

One pre-existing behaviour I left alone: fetch: ... .catch(() => ({})) turns a failed lookup into an empty metadata object which is then upserted, so a transient RPC failure caches an empty record permanently and it is never retried. That came from the original code, and your deferred path correctly avoids introducing a second instance of it, but it is worth its own issue.

https://claude.ai/code/session_01USgemLt4Rnz4SGB1Srf3GB

@Miracle656
Miracle656 merged commit 1edd880 into Miracle656:main Sep 30, 2026
3 of 4 checks passed
Miracle656 added a commit that referenced this pull request Sep 30, 2026
* Use token decimals for display amounts

* docs(wave): W094-W098 batch draft (published as #203-207)

Five issues, 800 points. The headline is W094: `src/indexer.ts:491` switches
between pollOnce and pollParallel on INGEST_WORKERS, and `indexer/parallel.ts`
imports only fetchEventsSafe, parseEvents, upsertTransfers, setLastIndexedLedger
and emitTransfer — no NFT parsing, no metadata, no account summaries. Raising
the worker count for throughput silently stops indexing whole categories, with
no error to notice.

Also: /offramp/orders/:orderId serves an order with no authorization, NFT
metadata is fetched one serial round trip at a time, and every paged query pays
for a full COUNT.

Claude-Session: https://claude.ai/code/session_01USgemLt4Rnz4SGB1Srf3GB

* Docs: Mainnet deployment guide (#166) (#211)

* Docs: Mainnet deployment guide (#166)

* docs(mainnet): correct the native SAC ids, fix the backup link, add DIRECT_DATABASE_URL

Review fixes applied on top of #211:

- The mainnet block used CDLZFC3SY…, which is the *testnet* native XLM SAC
  (Asset.native().contractId(Networks.TESTNET)). Mainnet is CAS3J7GY…
  (Networks.PUBLIC). The testnet block used CDMLFMKMM…, which is not the
  native SAC on either network. Both corrected, with the derivation inlined
  so the next reader can check rather than trust.
- Added a warning that SAC_CONTRACT_IDS must be set explicitly on mainnet,
  because the built-in fallback in src/indexer.ts is the wrong address.
- ../W076 did not resolve to anything; pointed at ./backup-restore.md.
- Added DIRECT_DATABASE_URL to both env blocks — prisma/schema.prisma
  declares directUrl, and boot-time schema sync fails without it.
- Linked ./DUAL_NETWORK.md for the NETWORKS=testnet,mainnet option.

Claude-Session: https://claude.ai/code/session_01USgemLt4Rnz4SGB1Srf3GB

---------

Co-authored-by: Miracle656 <iupacnumen2020@gmail.com>

* fix: narrow XDR error classification (#209)

* fix(offramp): require a bearer token to read an order (#216)

* fix(offramp): require a bearer token to read an order

GET /offramp/orders/:orderId served any order to whoever held its id: the
bank payout amount, the deposit address and the rate.

Orders now get a random public id (ofr_ + 128 bits) in place of the
provider's, and the creator is handed a bearer token (oft_ + 256 bits) once,
in the create response. Only its SHA-256 is stored. Lookups need
Authorization: Bearer; a missing header is 401, and an unknown id, a
malformed id and a wrong token are all the same 404.

Failed lookups are rate-limited separately from the app-wide limiter (10 per
15 minutes per IP by default); successful polling does not count.

Replaying a creation with the same idempotencyKey now also needs the same
walletAddress (409 otherwise) and re-issues the token, since only its hash is
kept. The lookup response no longer names the provider (source: live) or
returns its id, and a database failure returns a generic 500 instead of
reaching the global handler.

* test(offramp): use a valid strkey for the second wallet fixture

GBBB...SAM was 56 characters but failed StrKey.isValidEd25519PublicKey.
The test passes either way today, because the route does not validate the
address, but a fixture that is not a real strkey breaks the moment it is
handed to anything that decodes one.

Claude-Session: https://claude.ai/code/session_01USgemLt4Rnz4SGB1Srf3GB

---------

Co-authored-by: blockchain-maxis <267648998+blockchain-maxis@users.noreply.github.com>
Co-authored-by: Miracle656 <iupacnumen2020@gmail.com>

* fix: skip malformed events in parseEvents instead of wedging the indexer (#197)

Co-authored-by: Miracle656 <iupacnumen2020@gmail.com>

* fix(indexer): share one per-batch pipeline between the single and parallel ingest paths (#219)

Co-authored-by: DevTobis <232918735+DevTobis@users.noreply.github.com>

* Bring docker-compose.yml in line with env contract (#194) (#220)

* Bring docker-compose.yml in line with env contract (#194)

- Remove obsolete version: "3.9" key
- Add all missing env keys from .env.example:
  - DIRECT_DATABASE_URL
  - STELLAR_NETWORK
  - SOROBAN_RPC_URL (with STELLAR_RPC_URL as backward-compat alias)
  - NETWORKS
  - SAC_CONTRACT_IDS (and per-network variants)
  - NFT_CONTRACT_IDS (and per-network variants)
  - RETENTION_DAYS
  - CACHE_ENABLED and all Redis cache config
  - TOMBSTONE_CHECK_EVERY_CYCLES
  - LP_POOL_CONTRACT_IDS variants
  - SKIP_INDEXER
- Keep CONTRACT_IDS as documented backward-compat alias
- Add optional redis service with cache profile for CACHE_ENABLED support
- Update README to use SAC_CONTRACT_IDS and add cache profile instructions
- Add test to validate compose file meets requirements

* test(compose): derive the env-contract check from .env.example

The drift test built envKeys from .env.example and then never used it,
asserting against a hand-maintained list instead — so a key added to
.env.example and forgotten in docker-compose.yml still passed, which is the
one failure #194 is about. The assertions were also substring matches on the
whole file: toContain("SAC_CONTRACT_IDS") is satisfied by
SAC_CONTRACT_IDS_TESTNET, and toContain("CONTRACT_IDS") by either, so they
held on a file declaring none of them.

Now it parses the wraith service's own environment block (anchored on the
service, since db has an environment block too) and compares declared keys
against every uncommented key in .env.example, with an explicit, empty
exclusion list. Verified it fails on an added key and passes without one.

Also: the docker compose config case ran unconditionally and called fail(),
which is not defined under jest-circus, so on a machine or CI runner without
Docker it failed with a ReferenceError. Unit tests here do not require
Docker (the integration suite is vitest + Docker), so it now skips instead.

Claude-Session: https://claude.ai/code/session_01USgemLt4Rnz4SGB1Srf3GB

---------

Co-authored-by: Miracle656 <iupacnumen2020@gmail.com>

* Fix/cache network key (#202)

* fix(cache): include resolved network in Redis cache key (#182)

defaultKeyFn now prepends req.network (set by networkMiddleware) to the cache key so mainnet and testnet requests never collide, even when the network is supplied via the X-Network header rather than ?network=.

The ?network= query param is filtered from the query segment since it is already captured by req.network, ensuring ?network=mainnet and X-Network: mainnet produce identical keys.

Existing keys change shape and will expire naturally over their TTL.

* fix(cache): include resolved network in Redis cache key (#182)

* chore: drop the unrelated package-lock.json change

The branch re-resolved fsevents and dropped its "dev": true marker. Nothing
in this PR touches dependencies, so restore the lockfile to main's.

Claude-Session: https://claude.ai/code/session_01USgemLt4Rnz4SGB1Srf3GB

---------

Co-authored-by: Miracle656 <iupacnumen2020@gmail.com>

* Add npm run db:seed with deterministic fixtures (#221)

- Create shared fixture module in src/fixtures.ts with deterministic test data
  across two addresses (ALICE, BOB, CAROL) and two contracts (CONTRACT_A, CONTRACT_B)
- Add seed script at scripts/seed.ts with --network flag support (testnet/mainnet)
- Add db:seed script to package.json
- Update integration tests to use shared fixture module instead of local copy
- Update README Quick Start with seed step and real sample output
- Add fixture validation test to verify data structure and coverage

The seed script uses skipDuplicates on unique constraints, making it safe to
run multiple times without duplicating rows. Fixtures include TokenTransfer,
NftTransfer, and AccountSummary rows with deterministic eventId values.

Resolves #193

* fix(seed): accept --network mainnet as well as --network=mainnet

Follow-up to #221: this fix was pushed to the PR branch but did not make it
into the squash. The docstring advertised the space-separated form while
parseArgs matched only --network=, so 'npm run db:seed -- --network mainnet'
silently seeded testnet. Both spellings now parse; an unknown value still
exits 1.

Claude-Session: https://claude.ai/code/session_01USgemLt4Rnz4SGB1Srf3GB

* Fix token decimals review issues

* perf(api): drop the default COUNT from list queries, add hasMore and opt-in includeTotal (#218)

Co-authored-by: royalTreasure <295874283+royalTreasure@users.noreply.github.com>

* feat: instrument HTTP surface in Prometheus (#189) (#212)

* feat: instrument HTTP surface in Prometheus

* fix(metrics): instrument before networkMiddleware and the rate limiter

Mounted after them, the HTTP middleware never saw the requests those two
reject, so 429s and invalid-?network= 400s were absent from
http_requests_total. Moved to the top of the chain, right after cors().

Also documents the two new metrics in the README table and records why the
route label must stay req.route.path: req.baseUrl is the matched mount path,
and src/api/accounts.ts mounts a router at "/:address/transfers", so baseUrl
carries the real address and would make the label unbounded.

Claude-Session: https://claude.ai/code/session_01USgemLt4Rnz4SGB1Srf3GB

---------

Co-authored-by: Miracle656 <iupacnumen2020@gmail.com>

* fix(indexer): bound NFT metadata lookups with a worker pool and per-cycle budget (#217)

Co-authored-by: Miracle656 <iupacnumen2020@gmail.com>

---------

Co-authored-by: Miracle656 <iupacnumen2020@gmail.com>
Co-authored-by: Gloria <glorious217@gmail.com>
Co-authored-by: Jemimah <ekongjemimah@gmail.com>
Co-authored-by: blockchain-maxis <blockchainmaxis@gmail.com>
Co-authored-by: blockchain-maxis <267648998+blockchain-maxis@users.noreply.github.com>
Co-authored-by: Apulupie <167634780+Frun1na@users.noreply.github.com>
Co-authored-by: Tobiz <deborahayoola2000@gmail.com>
Co-authored-by: DevTobis <232918735+DevTobis@users.noreply.github.com>
Co-authored-by: BOA <97275013+boalambo@users.noreply.github.com>
Co-authored-by: Bathoul Mohammed <funds0033@gmail.com>
Co-authored-by: royaldev <chiditreasure15@gmail.com>
Co-authored-by: royalTreasure <295874283+royalTreasure@users.noreply.github.com>
Co-authored-by: Collins Ezedike-egwom <62267326+collinsezedike@users.noreply.github.com>
Co-authored-by: That guy <120946193+ezedike-evan@users.noreply.github.com>
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.

NFT metadata is fetched one round trip at a time

2 participants