Repository navigation
Description
Context
Raised on PR #663 review feedback (gemini-code-assist, marked high-priority security): the in-memory `HashMap<Address, ClientReputation>` in `crates/incentive/src/client_reputation.rs` is unbounded.
Threat model framing
The keyspace is structurally bounded today: entries enter only via `record_voucher_received` / `record_voucher_withheld`, which the future `cdn/client/v1` handler (#317, currently `blocked`) drives only on stream completion. A stream requires an open payment channel — itself an L2 transaction with USDC deposit and gas cost (ADR 003 §Payment Channel Open / Close). "Sybil resistance is structural" via channel-open cost is exactly the framing in #482; each new ledger key costs the attacker an on-chain channel open.
So the unbounded `HashMap` is not exploitable as a network-input DoS until the wiring lands — and even then, growth is rate-limited by the per-encounter channel-open cost.
Why this still belongs as a follow-up
Defense-in-depth on the map size is still right for the runtime-wiring PR (the one that adds the redb-backed `DiskClientReputationStore` + runtime instantiation alongside #317). The eviction policy benefits from real signals — `last_seen_us`, `completion_ratio`, `total_bytes_delivered` — that the leaf-primitive PR has no live consumer for. Shipping the cap with the wiring also lets us size `max_tracked_clients` against the actual on-chain channel-open rate observed on the testnet.
Scope
- Cap knob. Add `max_tracked_clients: usize` to `ClientReputationConfig` (default ~100_000; tunable per operator).
- Eviction. On insertion at capacity, evict the entry with the smallest `last_seen_us` (oldest observation). Linear-scan eviction is acceptable at this N; matches the pattern from `crates/node/src/dispatch.rs` `ConnectionLimiter::gc_per_source`.
- Throttle/lock-hygiene. Per the review note, do the eviction-candidate selection while holding the write lock briefly; bulk shrinking (if ever needed) uses `select_nth_unstable_by_key`.
- Metrics. `decdn_client_reputation_tracked_clients` gauge; `decdn_client_reputation_evictions_total` counter.
- Tests.
- Bounded growth under flood: insert > cap distinct addresses, assert map.len() == cap.
- LRU correctness: oldest entry is the one evicted.
- Cap=0 / cap=1 edge cases.
Dependencies / ordering
Should ship alongside or immediately before the `cdn/client/v1` admission wiring (so the cap is in place the first time the ledger sees live network input). Not blocking on the wiring PR — can land independently.
References
- ADR 003 §Corrupted delivery — "Sybil resistance is structural" framing.
- #482 — parent issue; this is the deferred hardening piece.
- #317 — `cdn/client/v1` handler (will be the live consumer).
- #645 — related: same keyspace-bound work for the DHT rate limiter.
🤖 Filed in response to PR #663 review feedback.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
fix(incentive): tighten ClientReputationLedger per PR #663 review
github-actions commentedon May 22, 2026
🎯 Triage report
Defense-in-depth hardening for
ClientReputationLedger: bound the in-memoryHashMap<Address, ClientReputation>with a configurable cap and LRU eviction bylast_seen_us. Filed from PR #663 review; the threat is not immediately exploitable (growth requires per-address on-chain channel-opens), but the cap should land before thecdn/client/v1wiring (#317) makes the ledger reachable from live network input. Scope is fully specified: config knob, eviction algorithm, lock-hygiene, metrics, and test cases.📊 Assessment
security,technical,rust,testing(added)testingadded because tests are explicitly in scopelast_seen_us), explicit metrics names, concrete test cases, and a prior pattern to follow (ConnectionLimiter::gc_per_sourceindispatch.rs)🔗 Similar issues
💡 Notes and suggestions
Implementation notes:
last_seen_us; useHashMap::iter().min_by_key(|(_, v)| v.last_seen_us)to find the candidate, thenHashMap::remove. This is acceptable at the stated default of 100k entries but worth a comment explaining the deliberate O(N) choice.select_nth_unstable_by_keyon aVecof keys avoids repeatedminscans.ConnectionLimiter::gc_per_source.max_tracked_clients: 0is an edge case — decide whether 0 means "unlimited" (disable cap) or "reject all insertions". The issue implies 0 should be a valid test edge case, so define the semantic explicitly inClientReputationConfigdocs.decdn_probe_rate_limit_rejections_totalfor consistency — include areasonorlayerlabel dimension so dashboards stay uniform.Suggested sub-tasks:
max_tracked_clients: usizetoClientReputationConfigwith default 100_000record_voucher_received/record_voucher_withheld(whichever does the insert)decdn_client_reputation_tracked_clientsgauge anddecdn_client_reputation_evictions_totalcounterDiskClientReputationStorepath to honour the same cap when rehydrating from redbOrdering: Can land independently of #317, but should be merged before the
cdn/client/v1handler PR that wires the ledger to live streams.Add this agentic workflows to your repo
To install this agentic workflow, run
refactor(incentive): apply ClientReputationLedger review-agent fixes
feat(incentive): add ClientReputationLedger building block (#482) (#663)
feat(incentive): bound ClientReputationLedger HashMap with LRU evicti…