Repository navigation
Description
Context
The type-design review on PR #643 flagged that `NodeId = [u8; 32]` is a type alias, not a newtype, and that this is the single biggest structural improvement available across the DHT cluster. Subsequent PRs (#646, #647) added a `Hash` type alias under the same constraint.
The routing-table-poisoning bug caught in PR #643 review (an authenticated peer A passing `req.requester = B` and having B inserted into A's routing table) would have been a compile error if `AuthenticatedNodeId` and `WireNodeId` (or even just `NodeId(#[repr(transparent)] [u8; 32])`) were distinct types: the handler would have had to perform an explicit `from_authenticated_quic(...)` conversion at exactly the boundary ADR 022 cares about.
Scope
Protocol crate
In `crates/protocol/src/lib.rs` (or a new `identity.rs`):
```rust
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
#[repr(transparent)]
#[serde(transparent)]
pub struct NodeId(pub [u8; 32]);
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
#[repr(transparent)]
#[serde(transparent)]
pub struct ContentHash(pub [u8; 32]);
```
`#[repr(transparent)]` + `#[serde(transparent)]` preserve the wire format — postcard sees the same 32-byte layout, so no discriminant tests move and no ALPN bump is required.
Use sites to migrate
- `decdn_protocol::dht` — `FindValueRequest.hash`, `StoreRequest.hash`, `StoreRequest.holder`, `FindNodeRequest.target`, `FindNodeRequest.requester`, `FindNodeResponse.target`, `FindNodeResponse.closer_nodes`, `FindValueResponse.providers`, `FindValueResponse.closer_nodes`, `BatchStoreRequest.holder`.
- `crates/node/src/dht/{routing,records,publish,bucket_refresh,bootstrap,client}.rs` — every place currently using `[u8; 32]` or the local type alias.
- `crates/node/src/handlers/dht.rs` — `handle_store` becomes `fn handle_store(&self, peer: AuthenticatedNodeId, req: StoreRequest)` (where `AuthenticatedNodeId` is a separate newtype constructed only from `conn.remote_id()`).
- `crates/common/src/config/resolved.rs` — `ResolvedDht.static_active_nodes` switches from `HashSet<[u8; 32]>` to `HashSet`.
Auxiliary newtypes
Consider also:
- `AuthenticatedNodeId` — wraps `NodeId`, only constructible from an authenticated QUIC connection. The handler accepts this, the wire types accept `NodeId`. Forces the conversion at exactly the spot where the trust boundary is.
- `CloserNodes` — wraps `Vec` with the `MAX_CLOSER_NODES` invariant enforced at construction time.
Compatibility
- Wire: unchanged thanks to `#[repr(transparent)]` + `#[serde(transparent)]`.
- ABI: unchanged (newtype around `[u8; 32]` lays out identically).
- Source breakage: every use site needs to either wrap with `NodeId(...)` or `unwrap` with `.0`. The migration is mechanical but touches many call sites.
References
- Type-design review on PR feat(node): cdn/dht/v1 handler + routing table + 3-layer rate limiter (PR 2/4 of #320) #643 (feat(node): cdn/dht/v1 handler + routing table + 3-layer rate limiter (PR 2/4 of #320) #643 review feedback, "if you fix only one thing")
- Bug class caught at `crates/node/src/handlers/dht.rs::handle_find_node` in PR feat(node): cdn/dht/v1 handler + routing table + 3-layer rate limiter (PR 2/4 of #320) #643 commit `cd6e074`
Splits out from #320 review feedback.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
github-actions commentedon May 21, 2026
🎯 Triage report
This is a well-scoped refactoring task to promote
NodeIdandHashfrom type aliases ([u8; 32]) to newtypes (NodeId(pub [u8; 32])), motivated directly by a routing-table-poisoning bug class identified in PR #643 review. The#[repr(transparent)]+#[serde(transparent)]approach ensures zero wire-format impact. Work spanscrates/protocol,crates/node/src/dht/*,crates/node/src/handlers/dht.rs, andcrates/common. Splits out from #320.📊 Assessment
technical,rust,security,highAuthenticatedNodeId,CloserNodes) specified🔗 Similar issues
No similar issues found.
💡 Notes and suggestions
Security note: The core motivation is making the
AuthenticatedNodeId↔WireNodeIdconfusion a compile error. TheAuthenticatedNodeIdnewtype should be constructible only viaconn.remote_id()— do not addimpl From<NodeId> for AuthenticatedNodeId. This is what closes the routing-table-poisoning class.Suggested sub-tasks:
NodeIdandContentHashnewtypes incrates/protocol/src/lib.rs(or a newidentity.rsmodule)AuthenticatedNodeIdnewtype incrates/protocol(orcrates/node), constructible only from QUIC connection identitydecdn_protocol::dhtmessage types (FindValueRequest,StoreRequest,FindNodeRequest,FindNodeResponse,FindValueResponse,BatchStoreRequest)crates/node/src/dht/{routing,records,publish,bucket_refresh,bootstrap,client}.rshandle_storeandhandle_find_nodeincrates/node/src/handlers/dht.rsto acceptAuthenticatedNodeIdcrates/common/src/config/resolved.rs(ResolvedDht.static_active_nodes)CloserNodeswrapper enforcingMAX_CLOSER_NODESat constructionImplementation tips:
impl From<[u8; 32]> for NodeId+impl From<NodeId> for [u8; 32]for ergonomic migrationclippywill catch remaining[u8; 32]literals in DHT contexts once the types are in place — use it as a migration guidecargo nextest run -p decdn-protocolandcargo nextest run -p decdn-nodethat no tests failNodeId([0u8; 32])serializes to the same bytes as[0u8; 32]