Skip to content

feat: add per-chain request budgets and streaming inputs to multi-chain scans - #288

Open
Bolajiomo99 wants to merge 4 commits into
wraith-protocol:developfrom
Bolajiomo99:fix/275-per-chain-budgets
Open

Bolajiomo99 wants to merge 4 commits into
wraith-protocol:developfrom
Bolajiomo99:fix/275-per-chain-budgets

Conversation

@Bolajiomo99

Copy link
Copy Markdown

Closes #275

Summary

MultichainScannerPool limited how many chain scans run at once, but each chain
still took a fully materialized array and had no request, buffer or memory bound.
A slow or rate-limited provider could therefore pin arbitrary memory, and there
was no way to cancel work that had already been handed to the pool.

This adds async-iterable inputs, per-chain request/in-flight/buffer budgets,
backpressure, cancellation and progress accounting underneath the existing
global chain-concurrency scheduler. The global concurrency option is unchanged
and still the outer scheduler; per-chain budgets never replace it.

Changes

Bounded streaming (src/scanner/boundedStream.ts, new)

  • BoundedAnnouncementStream wraps any AsyncIterable and guarantees:
    • at most maxInFlightRequests outstanding pulls;
    • at most maxBufferedAnnouncements buffered values, with in-flight pulls
      counted against the buffer so prefetch cannot overshoot;
    • at most maxRequests pulls, after which the scan fails with
      ScanBudgetExceededError instead of silently truncating;
    • close() drains the buffer, rejects pending reads and calls the source's
      return() so a cancelled scan releases references immediately.
  • ChainRequestBudget defaults are finite: 10 000 requests, 2 in-flight,
    256 buffered. 0, negative and non-finite limits are rejected.
  • resolveChainRequestBudget() and DEFAULT_CHAIN_REQUEST_BUDGET are exported.

Pool (src/scanner-pool.ts)

  • Scan inputs accept source?: AsyncIterable<...> in addition to
    announcements?; ckb uses the same shape with cells. Providing both, or
    neither, is rejected.
  • Budgets resolve in order: scan-input budget → pool budgets[chain] →
    pool requestBudget → finite defaults.
  • The chain adapters consume the bounded stream directly, so announcements are
    scanned in their existing small batches with no full materialization.
  • One internal AbortController per scanAll: caller abort rejects with the
    signal's reason, and failFast failures cancel sibling chains. With
    failFast: false, other chains still run to completion.
  • ProgressEvent now also reports requests, buffered and streaming;
    mid-stream events are throttled, with a final processed/buffered: 0 event
    per chain on success, error or cancellation.

Errors (src/errors.ts)

  • ScanBudgetExceededError (WRAITH/INPUT/SCAN_BUDGET_EXCEEDED) with
    { chain, kind, limit, processed } and a describe() hint for resuming.

Public API

  • Root entry now exports MultichainScannerPool, its options/input/result
    types, ProgressEvent, ChainRequestBudget, DEFAULT_CHAIN_REQUEST_BUDGET
    and resolveChainRequestBudget.

Tests (test/scanner-pool-streaming.test.ts, 19 tests)

Stress and resilience coverage:

  • arrays, streams and mixed inputs across chains; input shape validation
  • in-flight peak stays ≤ maxInFlightRequests across repeated scans
  • buffered peak stays ≤ maxBufferedAnnouncements, and pull-ahead is bounded
  • a stream needing more pulls than maxRequests fails after exactly that many
    pulls and closes the source
  • array inputs are not charged against the request budget
  • per-chain budgets are independent: one chain exhausts its budget while the
    other completes with a larger one
  • invalid limits fail instead of running unbounded
  • a slow chain does not delay a fast chain, and concurrency: 1 still orders
    chains as before
  • abort rejects with AbortError, calls return() on every source, and no
    further pulls happen afterwards
  • failFast cancels siblings; an already-aborted signal touches no source
  • final per-chain progress event reports buffered: 0 and the full count

Benchmarks (test/bench/scanner-pool.bench.ts)

Same two-chain scan through simulated providers, run with pnpm bench:

Profile Throughput Peak buffered Peak in-flight
local source (no latency) 2 517 Hz 127 / 128 4 (2 per chain)
slow provider (10 ms/pull) 3.17 Hz 0 4 (2 per chain)
rate-limited (backoff every 5 pulls) 1.19 Hz 0 4 (2 per chain)
failing provider (error after 20 pulls) 82.3 Hz 0 4 (2 per chain)

Heap delta per profile is printed with the table (approximate, no forced GC).
The local profile shows the buffer filling to its budget while provider-bound
profiles never buffer, which is the intended backpressure behaviour.

Verification

  • pnpm build
  • pnpm test (1618 passed, 5 skipped; plus 17 Svelte)
  • pnpm test:types
  • pnpm format:check
  • pnpm api:check (etc/sdk.api.md regenerated)
  • pnpm size (Root ESM 36.80/37.1 KB, Stellar CJS 41.01/41.2 KB)
  • pnpm bench (table above)

Notes for reviewers

  • concurrency is untouched and remains the outer scheduler; all new limits are
    per chain, so one slow chain holds only its own slot and its own budget.
  • Reaching maxRequests raises instead of returning a partial list, because a
    truncated scan would look complete. Matched results collected so far are
    still surfaced through progress accounting.
  • The root bundle grows ~5.4 KB brotli now that the pool is publicly exported;
    budgets were raised minimally rather than re-baselined.

…er pool

Add async-iterable scan inputs, bounded prefetch with backpressure, and
per-chain request, in-flight and buffer budgets. Cancellation drains queues,
and progress accounting stays under the existing chain-concurrency scheduler.
@drips-wave

drips-wave Bot commented Nov 22, 2026

Copy link
Copy Markdown

@Bolajiomo99 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

@truthixify truthixify left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Head 5bb2f5c screened clean for the known malware indicators; linked issue #275 is assigned to PR author @Bolajiomo99. Changes requested: createBoundedAsyncIterable launches iterator.next() calls concurrently and enqueues values in promise-settlement order. A later pull can resolve done before an earlier value, causing the consumer to terminate and drop that value; otherwise values can be reordered. Preserve source-call order, defer completion until all earlier slots settle, and add a regression with out-of-order next() settlements.

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.

[Wave 11] Add per-chain request budgets and streaming inputs to multi-chain scans

3 participants