Skip to content

DHT: implement BatchStore handler-side admission (ACs 17-20 of ADR 022) #648

Description

@thiras

Context

PR #642 (now landed in the #320 stack) defined the BatchStoreRequest / BatchStoreAck wire types at their ADR-022 canonical discriminants (4 and 5) so the wire format wouldn't need a future shuffle. The handler-side admission was deferred: today the handler rejects BatchStore requests with APP_ERR_UNSUPPORTED_MESSAGE (`0x01`) so publishers fall back to per-hash `Store` per ADR 022 §Schema Evolution.

This issue is to implement the handler-side admission so publishers can use the bandwidth optimization at scale (ADR 022 §DHT Bandwidth Analysis: ~80% wire-bytes reduction at `C ≥ 10k` cached blobs).

Acceptance criteria (ACs 17-20 from issue #320)

  • AC 17 — `BatchStoreRequest { n ≤ 256 hashes }` from publisher P produces `BatchStoreAck { results }` with one bool per request hash in request order. The batch-level `holder` is checked once against the authenticated QUIC NodeId; the remaining admission decisions (rate limit, per-publisher quota with two-tier eviction, active-staker check) are applied per-hash and match those of `n` separate `StoreRequest` RPCs.
  • AC 18 — A `BatchStoreRequest` consumes between 1 and `n` tokens from each rate-limit bucket: stage 1 charges 1 token before deserialization; stage 2 charges up to `n-1` more after reading `n = hashes.len()`, bounded by remaining budget. The first `k` admitted hashes pass per-hash processing; the remaining `n-k` are marked `false` in the ack without further processing.
  • AC 19 — A `BatchStoreRequest` with `hashes.len() > 256` is rejected by closing the stream with `MALFORMED_MESSAGE` (`0x03`); the publisher MUST split larger publish sets across multiple batches.
  • AC 20 — A publisher whose `BatchStoreRequest` to a given receiver results in stream-close-without-ack MUST fall back to per-hash `StoreRequest` for subsequent publications to that receiver; the publisher SHOULD cache the negative result for some bounded duration to avoid repeated drop-and-fallback cycles.

Implementation notes

  • The handler's `read_dht_request` currently has a dedicated arm for `BatchStore` that rejects with `APP_ERR_UNSUPPORTED_MESSAGE`. Replace it with a real handler that routes `BatchStore` through the existing `RecordStore::insert_at` per-hash with shared receiver-anchored `receive_us`.
  • Two-stage rate-limit accounting needs a new method on `DhtRateLimiter` that consumes up to `n` tokens with a partial-admit signal.
  • Publisher-side fallback negotiation needs a new outbound `client::batch_store` primitive and a small per-receiver `unsupported_until` cache.

References

  • ADR 022 §STORE Flow (BatchStore + two-stage rate-limit accounting): `adr/022-content-discovery.md`
  • Wire types: `crates/protocol/src/dht.rs` (`BatchStoreRequest` / `BatchStoreAck`, `MAX_BATCH_STORE_HASHES`)
  • Current handler arm: `crates/node/src/handlers/dht.rs::read_dht_request` (BatchStore → `APP_ERR_UNSUPPORTED_MESSAGE`)

Tracks the BatchStore split-out from #320.

Activity

  1. self-assigned this
    on May 21, 2026
  2. added
    rust
    on May 21, 2026
  3. github-actions commented on May 21, 2026

    @github-actions

    🎯 Triage report

    This issue tracks the handler-side implementation of BatchStore admission in the DHT ALPN handler, split out from the parent #320 tracking issue. The wire types (BatchStoreRequest/BatchStoreAck) are already defined (landed in PR #642), but the handler currently short-circuits with APP_ERR_UNSUPPORTED_MESSAGE. Full implementation requires three distinct pieces: handler admission logic, two-stage rate-limit accounting on DhtRateLimiter, and a publisher-side fallback cache.

    📊 Assessment

    Dimension Value Reasoning
    Type Task Implementation work with clear, enumerated ACs; not a bug or new design
    Labels technical, poc-blocking, rust Core DHT implementation path; bandwidth reduction is PoC-critical per ADR 022 §DHT Bandwidth Analysis; pure Rust crate work
    Coding agent Suitable Well-defined ACs (17-20), concrete file/function references, clear success criteria; self-contained to crates/node and crates/protocol with no unresolved design questions

    🔗 Similar issues

    💡 Notes and suggestions

    Implementation decomposition:

    • Handler arm (crates/node/src/handlers/dht.rs): Replace the BatchStore → APP_ERR_UNSUPPORTED_MESSAGE arm with real logic. Validate hashes.len() ≤ 256 (return MALFORMED_MESSAGE 0x03 on violation per AC 19). Check holder once against the authenticated QUIC NodeId. Loop over hashes calling RecordStore::insert_at with a shared receive_us timestamp; collect per-hash bool results into BatchStoreAck.
    • Two-stage rate-limit (DhtRateLimiter): New method admit_batch(n) -> (admitted: usize, remaining_budget: usize). Stage 1 deducts 1 token before deserialization. Stage 2 deducts up to n-1 more after reading the hash count; first k hashes proceed, rest are false in the ack (AC 18). Coordinate with cdn/dht/v1 DoS hardening — per-publisher quota, DHT rate limit, lookup invariants #625 to ensure the quota semantics stay consistent.
    • Publisher-side fallback (client::batch_store): New outbound primitive that sends BatchStoreRequest and handles stream-close-without-ack by populating an unsupported_until: HashMap<NodeId, Instant> cache. When a node is in the unsupported window, the publisher falls back to individual StoreRequest RPCs (AC 20). Cache expiry should be bounded (e.g., 5–30 min configurable).
    • Tests: Unit tests for the partial-admit boundary (exactly 256 hashes admitted vs 257 rejected), the two-stage token accounting, and the fallback cache TTL behaviour.

    Key invariant to verify: The receive_us timestamp used for the whole batch must be anchored to the receiver's clock at the time of the BatchStoreRequest arrival, not per-hash insertion time — this prevents a batch from having internally inconsistent TTLs.

    Watch out for anti-panic policy: All slice indexing into the hashes vec must use .get() or iterators — direct hashes[i] will fail clippy's indexing_slicing deny.

    Generated by Agentic Triage for issue #648 · ● 2.1M · ◷

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/issue-triage.md@94ec5db57374c5b04a4b8eef8b4413f9af44d63f
    
  4. added a commit that references this issue on May 29, 2026
    441a676
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

rust

Type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions