Skip to content

refactor(node): lazy on-demand origin directory (delete whole-directory mirror + watcher) - #1737

Merged
alpergundogdu merged 6 commits into
mainfrom
claude/lazy-origin-directory
Aug 18, 2026
Merged

alpergundogdu merged 6 commits into
mainfrom
claude/lazy-origin-directory

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Summary

Second of the eth_call-efficiency PRs (follows #1727). Replaces the whole-directory on-chain origin mirror with a lazy, bounded TTL cache, so a node's origin-related chain reads scale with the namespaces it actually serves — not the permissionless global namespace count.

The problem

ChainOriginDirectory mirrored the entire OriginAssignment directory in memory: a background watcher enumerated every namespace at boot (one getOrigins each), re-read on every event, and fanned out a nodeIdOf per operator per namespace. Namespace creation is permissionless and free (PublisherRegistry.createNamespace — no fee, no stake), so anyone can create thousands of namespaces and bloat every node in the network. And the whole mirror exists to serve a fallback path (node_origin::discover on a DHT find_providers miss).

The change

  • Delete the whole-directory mirror + its background watcher outright — no more boot enumeration, per-event re-reads, or nodeIdOf fan-out.
  • LazyOriginCache: a bounded LRU keyed by namespaceId with a positive TTL (default 300s) for populated namespaces and a shorter negative TTL (default 30s) for empty/nonexistent ones. A single getOrigins(ns) fires only on a cache miss for a namespace the node is actually asked to resolve. The negative TTL is the DoS bound: bogus/attacker-chosen request-namespaces can't force one getOrigins per request.
  • operator → NodeId resolves locally, with zero nodeIdOf chain calls: a new always-on Address → NodeId reverse projection on the capacity-bond registry, derived from the getRegisteredNodes page the registry already reads (no new chain call). An operator with no reverse-map binding is dropped (no chain fallback) — which also closes the "seat a namespace with bogus operators to force RPCs" vector. Liveness (StakerSet) is applied live at read time, so a stale cache entry can't resolve a just-deregistered node.
  • OriginDirectory::lookup_origins becomes async (its sole consumer is already async). On an RPC error it fails closed (resolves nothing) and does not cache, so a transient failure isn't frozen for a TTL.

Net effect

Origin resolution costs one getOrigins per cold-namespace miss and zero per-operator reads — down from O(all namespaces) at boot + O(operators × namespaces) fan-out + a perpetual event tail.

Config (new [blockchain] knobs)

  • origin_directory_positive_ttl_sec (default 300)
  • origin_directory_negative_ttl_sec (default 30 — the DoS bound)
  • origin_directory_cache_capacity (default 4096, LRU)

Testing

  • LazyOriginCache unit tests: split-TTL, negative-vs-miss distinction, no-TTL-refresh-on-read, LRU eviction/bump, cap clamp.
  • resolve_active: drops unbound operators, filters inactive, sorted+deduped.
  • ChainOriginDirectory async lookup: cold-miss-one-RPC (proven via call counter), cache-hit-no-RPC, negative caching, RPC-failure-fails-closed-and-not-cached, ns-0 short-circuit, live liveness re-application.
  • Registry reverse projection: always-on (built even with pull-through off), ejection keeps the entry, deregister removes it.
  • Full decdn-node + decdn-common suites green (1289 tests) on latest main.

Observability

Origin-watcher metrics/alerts removed (the watcher is gone); replaced with origin_directory_get_origins_failures_total (Counter) and origin_directory_cache_size (Gauge), with a matching alert. ADR appendix-observability.md updated (four chain-event watchers, not five).

Notes

🤖 Generated with Claude Code

Copilot AI lite review requested due to automatic review settings August 18, 2026 14:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Refactors the node’s on-chain origin resolution to avoid mirroring the entire OriginAssignment directory at boot and on events, replacing it with a lazy, bounded TTL cache keyed by namespaceId. This reduces eth_call load to only the namespaces the node is actually asked to resolve, and removes the origin-directory watcher/metrics in favor of cache/miss metrics.

Changes:

  • Replace the whole-directory origin mirror + background watcher with a lazy on-demand getOrigins(namespaceId) TTL/LRU cache (LazyOriginCache) and async OriginDirectory::lookup_origins.
  • Add an always-on operator address → NodeId reverse projection to the shared CapacityBond registry watcher, eliminating per-operator nodeIdOf chain calls.
  • Update config schema/resolution, tests, and observability docs/metrics to reflect the new cache behavior and removed watcher.

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
monitoring/prometheus-alerts.yml Removes the origin-directory watcher stalled alert rule.
crates/node/tests/sighup_signal.rs Updates resolved config seeding to include origin-directory cache TTL/capacity defaults.
crates/node/tests/node_origin_pull.rs Updates test OriginDirectory impl to async.
crates/node/tests/anvil_bringup_shutdown_e2e.rs Updates resolved config seeding to include origin-directory cache TTL/capacity defaults.
crates/node/src/runtime/reload.rs Updates runtime reload config defaults to include origin-directory cache TTL/capacity.
crates/node/src/runtime/mod.rs Switches runtime wiring to the new lazy ChainOriginDirectory (no watcher handle) and passes cache params + reverse map.
crates/node/src/node_origin/mod.rs Awaits async origin_directory.lookup_origins on DHT miss fallback.
crates/node/src/metrics.rs Removes origin-directory watcher metrics; adds get_origins_failures counter and cache_size gauge.
crates/node/src/dht/origin.rs Makes OriginDirectory::lookup_origins async and updates implementations/tests.
crates/node/src/dht/mod.rs Exposes new lazy_origin_cache module.
crates/node/src/dht/lazy_origin_cache.rs Adds bounded LRU cache with split TTLs and pure resolve_active helper.
crates/node/src/dht/chain_projection.rs Widens with_read visibility; removes watcher() accessor and documents watcher handle retention.
crates/node/src/dht/chain_origin_directory.rs Replaces event-fed mirror with lazy TTL cache, on-demand getOrigins, and reverse-map resolution.
crates/node/src/dht/capacity_bond_registry.rs Adds always-on operator_to_node reverse map projection maintained by the shared registry watcher.
crates/e2e/tests/cli_publish.rs Removes node-side watcher/metric assertions; keeps on-chain getOrigins effect assertions.
crates/common/src/config/types.rs Adds [blockchain] config knobs for origin-directory TTLs and cache capacity.
crates/common/src/config/resolved.rs Adds resolved fields for origin-directory TTLs and cache capacity.
crates/common/src/config/mod.rs Defines defaults and resolves the new config knobs (with unit tests).
crates/cli/tests/config_validate.rs Updates sample resolved config with new origin-directory defaults.
crates/cli/src/commands/config.rs Updates default config template and related tests to include new knobs.
adr/appendix-observability.md Updates watcher count/docs; documents origin-directory metrics/alerting recommendations for lazy cache.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread monitoring/prometheus-alerts.yml Resolved
@github-actions

github-actions Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 80.58% (was 80.35%, ▲ +0.23%) — 57989/71967 lines

File Coverage Lines Δ
crates/cli/src/commands/config.rs 86.07% 519/603 ▲ +0.33%
crates/common/src/config/mod.rs 97.04% 6534/6733 ▼ 0.01%
crates/common/src/config/resolved.rs 100.00% 33/33 ● 0.00%
crates/node/src/dht/capacity_bond_registry.rs 77.24% 543/703 ▲ +2.91%
crates/node/src/dht/chain_origin_directory.rs 88.19% 254/288 ▲ +17.76%
crates/node/src/dht/chain_projection.rs 74.63% 50/67 ▲ +3.20%
crates/node/src/dht/lazy_origin_cache.rs 94.92% 168/177 new
crates/node/src/dht/origin.rs 100.00% 38/38 ● 0.00%
crates/node/src/metrics.rs 95.09% 968/1018 ▼ 0.14%
crates/node/src/node_origin/mod.rs 91.07% 1051/1154 ▼ 0.06%
crates/node/src/runtime/mod.rs 48.02% 1082/2253 ▲ +0.43%
crates/node/src/runtime/reload.rs 96.74% 1394/1441 ▲ +0.02%
History
Commit Total Δ vs base
1150f61 80.58% ▲ +0.23%
a615ee2 (HEAD) 80.58% ▲ +0.23%

Adds DecdnOriginDirectoryLookupFailing (warning) on
decdn_origin_directory_get_origins_failures_total. The lazy origin
directory replaced the origin watcher, so the removed watcher-stalled
alert is replaced by a fail-closed failure-rate alert rather than left
with no coverage.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu merged commit 3807143 into main Aug 18, 2026
26 checks passed
@alpergundogdu
alpergundogdu deleted the claude/lazy-origin-directory branch August 18, 2026 14:40
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.

2 participants