Skip to content

chore: code-review follow-up fixes - #33

Merged
nimrod-teich merged 25 commits into
mainfrom
chore/code-review-fixes
Apr 16, 2026
Merged

nimrod-teich merged 25 commits into
mainfrom
chore/code-review-fixes

Conversation

@nimrod-teich

Copy link
Copy Markdown
Contributor

Summary

Addresses items from the recent code review (_workspace/review-report.md). Each fix is a separate commit so they can be reviewed/reverted independently.

Done so far:

More to come on this branch: #1, #3, #5, #7, #8, #9, #10, #14 (skipping #6, #11 per request).

Test plan

  • pnpm typecheck passes across workspaces
  • pnpm --filter @info/api test — 97 passing
  • pnpm --filter @info/shared test — 46 passing (4 new for parseYMD, 4 new for weightedQos)
  • Manual sanity on /optimizer-metrics with an invalid date → 400
  • Check Content-Encoding: br header on /providers?limit=1000

Large JSON responses (e.g., /providers list ~150KB, /all_providers_apr
potentially 500KB+) were sent uncompressed. Register @fastify/compress
globally with a 1KB threshold so brotli/gzip is applied where it helps.
- Delete fetchProviderRewardPoolsAmount — zero callers anywhere.
- Delete fetchLavaUsdPriceAt — zero callers anywhere.
- Unexport fetchStakingPool — only used inside the module.
- Unexport fetchBondedValidators — only used inside the module.
optimizer-metrics used new Date(query.from) which accepts ISO, RFC-2822,
and timezone-spec strings — much looser than the YYYY-MM-DD the route
advertises. provider-rewards already had a strict parser; promote it to
@info/shared and use it in both routes. Invalid dates now return 400.

Includes tests covering calendar-invalid dates (Feb 30, month 13, day 0)
and leap-year handling.
Both /index/charts and /providers/:addr/charts computed QoS from
weighted-sum / weight. Extract weightedQos to @info/shared/utils so
there's one source of truth for the formula, including the excellence-QoS
variant (same shape, different weight).

Includes unit tests for zero/negative weight and null component sums.
Every gql() call was opening a fresh TCP+TLS connection to the indexer;
with 2-4 GraphQL calls per route this wasted 100-400ms on handshakes.
Register a pooled undici Agent as the global dispatcher so all outbound
fetch() (GraphQL + REST) benefit, and add AbortSignal.timeout() so slow
indexer responses don't stack up requests on the event loop.

INDEXER_TIMEOUT_MS env var (default 15000) controls the timeout.

Pinned undici to ^6 because v8 requires Node >=20 for File() and the
project still wants to support local dev on Node 18; prod runs Node 22+.
Both buildProbeTargets (fetching providers per spec) and the probe loop
itself were hardcoded to batches of 5 — stale since RPC_BATCH_SIZE was
introduced (default 25). On dedicated RPC endpoints this becomes the
bottleneck. Use the env-configured size instead.
/provider-rewards called fetchAllProviders() (which iterates every spec
on chain) just to build a Map<address, moniker>. Introduce a cached
fetchAllProviderMonikers() in the rpc module — 60-second in-process TTL
shared across routes. Routes that only need monikers skip re-enumeration
when another route populated the cache in the last minute.

/provider-rewards now uses the helper. Callers that need the full
provider list (e.g. provider-claimable-rewards) keep fetchAllProviders.
The types in packages/shared/src/types/api.ts were defined but imported
only by their own test file — so api and web each re-declared response
shapes locally and could drift silently. Add a ProviderListItem shape
that matches the /providers response, have the api route return
PaginatedResponse<ProviderListItem>, and have the /providers page
useApi<PaginatedResponse<ProviderListItem>>.

ApiError.message/statusCode made optional — callers (e.g. 404 replies)
already return shorter variants.
The 1,673-line rpc/lava.ts mixed ten distinct concerns (HTTP plumbing,
block lookup, specs, providers, supply, pricing, rewards, validators,
APR, Keybase). Extract each concern into its own file so changes stay
scoped and tests can target narrower surfaces:

- rpc/rest.ts        — fetchRest, RPC_BATCH_SIZE, inflight coalescing
- rpc/block.ts       — fetchLatestBlockHeight, fetchBlockAtTimestamp
- rpc/specs.ts       — fetchAllSpecs + chain display-name overrides
- rpc/supply.ts      — total/circulating supply, vesting, reward pools
- rpc/providers.ts   — spec/provider queries + moniker cache
- rpc/pricing.ts     — CoinGecko, denom registry, TVL + DEX sources
- rpc/rewards.ts     — multi-denom reward conversion helpers
- rpc/validators.ts  — validator RPC + rewards breakdown
- rpc/apr.ts         — APR math + weighted Redis history
- rpc/keybase.ts     — Keybase avatar lookup

rpc/lava.ts is now a thin barrel that re-exports the original public
surface so external callers don't need updating. New code should import
directly from the focused modules.

No behavior change — all 97 tests continue to pass.
The health probe enumerates every spec × provider on each 30s tick and
makes gRPC probe calls — all on the Fastify event loop, causing periodic
p95 latency spikes on the API during probe windows.

Extract the probe loop into services/health-probe-loop.ts and add a
bin/health-probe.ts entrypoint that runs it as its own process.
Production deploys should run this as a separate ECS task (or any
container) so probe traffic is isolated from the API's event loop.

The in-process plugins/health-probe.ts now delegates to the shared loop
and stays opt-in (ENABLE_HEALTH_PROBE=true) for local dev convenience.

Scripts:
  pnpm --filter @info/api probe:dev    # hot-reload dev
  pnpm --filter @info/api probe:start  # production
/supply/circulating (paginates ~53K accounts), /apr and
/all_providers_apr (per-entity estimated_* RPC calls), and
/validators-and-rewards (5 RPC × N validators) are all fine on warm
cache but pathological on cache miss / cold deploy. Add a standalone
worker that recomputes them on a 15-min interval and writes results to
Redis under precompute:<name>.

Routes now read from the precompute store first and fall back to live
compute if the worker hasn't populated the key yet — so the site still
works without the worker, it's just slower.

Scripts:
  pnpm --filter @info/api precompute:dev    # hot-reload dev
  pnpm --filter @info/api precompute:start  # production

Env:
  PRECOMPUTE_INTERVAL_MS — default 900000 (15 min)
API route coverage was at 10/16. Add focused happy-path + schema-rejection
tests for the six missing routes:
  - index.ts          (stats, charts)
  - lava.ts           (specs)
  - optimizer-metrics (both provider + spec variants — 503 when DB off,
                       400 on invalid YYYY-MM-DD)
  - providers.ts      (pagination/sort, addr schema, stakes)
  - search.ts         (case-insensitive match across providers+specs)
  - specs.ts          (sort-by-provider-count, :specId/health no-Redis path)

Add vitest to apps/web with unit tests for the two most-used utilities:
format.ts (formatNumber, formatNumberKMB, formatLava, formatLavaKMB) and
chain-icons.ts (alias resolution + case handling).

Test counts:
  apps/api     97 → 116
  apps/web      0 →  15
  shared       46       (unchanged)
Tighten TypeScript to flag silent undefined-from-indexed-access bugs
(arr[i] → T | undefined, Map.get → T | undefined). Produced 57 latent
issues — all fixed in this commit, mostly via explicit ! on indices the
surrounding loop guarantees are in-bounds and destructure defaults on
.split() outputs.
Network-wide provider count sits in the tens, so paginating the list
response at the HTTP layer buys nothing and makes client filtering/sorting
awkward. Return the full provider array from GET /providers and let the
frontend paginate with TanStack Table if it wants to.

Changes:
- Delete plugins/pagination.ts + the app.register call in main.ts.
- Strip request.pagination usage from routes/providers.ts (both GET / and
  GET /:addr/health) and simplify readHealthForProvider to return the
  full HealthRecord[] array.
- Remove PaginatedResponse/PaginationParams from @info/shared/types;
  ApiError stays.
- Drop the limit=10000 query param on the frontend /providers page.
- Update affected tests (11 files).
- Ignore .next.old dev artifact directories.
BigInt(1e6) round-trips through a Number at runtime — defeats the point
of BigInt and costs a few nanoseconds per call site. The literal 1_000_000n
is both clearer and slightly faster.
Four call sites were hand-rolling the same BigInt(value / 1_000_000n)
math: web/lib/format.ts (both formatLava + formatLavaKMB), api/routes/
supply.ts, api/rpc/pricing.ts, and api/bin/precompute.ts.

Keep the existing ulavaToLava helper in @info/shared/utils/formatting.ts
(returns a decimal string with no trailing zeros) and add a second
variant ulavaToLavaNumber that returns a Number for arithmetic — used by
the TVL math in pricing.ts and the display helpers in web/lib/format.ts.

No behavior change; tests still pass.
Route handlers were emitting at least three different error shapes:
  reply.status(400).send({ error: msg })
  reply.status(503); return { error: msg }
  reply.status(400).send({ error: 'Error - bad X: ...' })

Add sendApiError(reply, status, message) in plugins/error-handler.ts
that returns the canonical ApiError shape ({ error, message, statusCode })
matching what the global error handler already produces for thrown errors.
Every 4xx/5xx return now goes through this helper.

Callers updated: routes/optimizer-metrics.ts, provider-rewards.ts,
provider-estimated-rewards.ts, health.ts. Test assertions updated to
check body.message instead of body.error for the detail text.
The display-name override map (ETH1 → "Ethereum Mainnet" etc.) lived in
apps/api/src/rpc/specs.ts alongside fetchAllSpecs. Both the API and the
frontend routinely need to render a chain's human name, so promote the
map and a new chainDisplayName helper to @info/shared/constants where
both workspaces can import it. Keep rpc/specs.ts focused on the actual
RPC call.
The /all_providers_apr response mixes snake_case (e.g. rewards_last_month)
with camelCase (moniker, delegateTotal). It looks inconsistent but the
snake_case keys are load-bearing — they mirror jsinfo's exact output
shape which burn-ui and the lava-ops pipelines parse by name. Document
that so the next reader doesn't 'fix' it and break downstream consumers.
process.env was read ad-hoc at 22 call sites across rpc/, plugins/,
services/, bin/, routes/ and main.ts. Each one re-declared its own
default string. Centralize them in apps/api/src/config.ts so:

- the full env contract is visible in one place (mirrors CLAUDE.md's
  table)
- defaults are parsed once at startup (not on every request)
- helpers envInt/envBool give consistent NaN/parse handling

Every consumer now imports `config` and references typed slices:
  config.server.{port,host,corsOrigins,rateLimitMax}
  config.lava.{restUrl,rpcUrl,rpcBatchSize}
  config.indexer.{graphqlUrl,timeoutMs}
  config.external.{coingeckoApiUrl,keybaseApiUrl}
  config.redis.url, config.relaysDb.url
  config.healthProbe.{enabled,region,intervalMs}
  config.precompute.intervalMs
  config.isDev / config.isProd

No behavior change; tests green.
encodeURIComponent() was already applied so there's no live SSRF, but a
future refactor that dropped the encoding would reopen one. Add an
explicit regex gate: Keybase identity fingerprints are hex strings up
to 64 chars — reject anything else. Belt-and-suspenders against the
security review's concern that relying solely on downstream encoding
is fragile.
30 routes read cacheTTL from bare seconds — 10, 30, 300, 1800, 7200,
21600, 86_400 — making tiering changes a grep-and-pray exercise.

Add CACHE_TTL to config.ts with semantic names:
  REAL_TIME (10s), HEALTH_PROBE (30s), LIST (300s), SEARCH (600s),
  APR (1800s), SLOW_MOVING (7200s), OPTIMIZER (6h), HISTORICAL (24h).

Every route config + the request.cacheTTL overrides in provider-rewards
and provider-estimated-rewards now reference the named values.
The web app never calls it — the route exists for burn-ui and lava-ops
pipelines. A grep-driven "dead route" cleanup would remove it; the
comment makes that decision explicit.
useApi configured refreshInterval inline on every hook instance, so two
components pointing at the same URL inside a few ms could still make two
HTTP requests and revalidateOnFocus was forcing an extra fetch whenever
the user re-focused the tab.

Move the defaults to a SwrProvider at the root layout:
  - dedupingInterval: 10s  — coalesce concurrent callers of the same URL
  - refreshInterval: 5min  — matches API cache TTL
  - revalidateOnFocus: false
  - keepPreviousData: true — smooth network-toggle transitions

Per-call overrides are still honored (just none exist yet).
fetchLavaUsdPrice hit CoinGecko every call while fetchTokenUsdPrice for
the same 'lava' denom already cached through priceCache for 5 min. Route
handlers calling fetchLavaUsdPrice directly (e.g. /provider-estimated-
rewards, /validators-and-rewards, /all_providers_apr, TVL) each paid the
~150ms handshake instead of sharing.

Use the existing priceCache (key 'lava') for reads and writes, and add
an in-flight promise to coalesce concurrent callers on a miss so ten
parallel routes become one CoinGecko request.
@nimrod-teich
nimrod-teich merged commit 3e016c0 into main Apr 16, 2026
1 of 2 checks passed
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.

1 participant