Repository navigation
refactor(node): newtype warming source ids and hashes at the warming-allowance seam - #1874
Merged
Merged
Conversation
…seam The warming-credit seam passed both content hashes and source node identities as bare [u8; 32], so the compiler could not tell them apart: debit_speculative(source, hash, units) had two adjacent same-typed parameters a caller could transpose silently, source_for returned a source of the same type as the hash it took, and the deferred-credit channel carried a (source, units) tuple shaped identically to the (hash, units) the sink above it carried. Introduce a SourceId newtype for the bonded upstream seller identity and use decdn_cache::Hash for the content hash across WarmingAllowance, WarmingCreditSink, and the background aggregator. The two sides are now distinct types, so transposing a source and a hash is a compile error. SourceId is repr(transparent) over [u8; 32] with from_bytes/as_bytes, matching the identity-newtype pattern already used on the wire. Call sites wrap at the boundary: the node-origin pull legs build a SourceId from the candidate node id and a Hash from the pulled hash bytes, and the eviction, blacklist, admin, and serve paths pass their existing Hash values through directly instead of unwrapping to bytes. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01JfEvDBn3S8R3H8KSDd7rV3
`Hash` names both the re-exported struct and the std derive macro in scope, so a bare `[`Hash`]` link is ambiguous and `cargo doc -D warnings` rejects it. Prefix the SourceId doc reference with `struct@`. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01JfEvDBn3S8R3H8KSDd7rV3
There was a problem hiding this comment.
Pull request overview
This PR improves type-safety at the node “warming allowance” seam by introducing a dedicated SourceId newtype for upstream seller identities and switching the content identifier to decdn_cache::Hash, preventing accidental transposition of two adjacent 32-byte parameters.
Changes:
- Introduces
SourceId([u8; 32])and updatesWarmingAllowance/WarmingCreditSinkAPIs to useSourceId+Hash. - Updates node-origin pull legs and serve/evict/blacklist/admin call sites to pass
Hashdirectly rather than raw[u8; 32]. - Adjusts integration and unit tests to construct and pass the new types.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/node/src/warming_allowance.rs | Adds SourceId and migrates the warming ledger/tag/channel plumbing to SourceId + decdn_cache::Hash. |
| crates/node/src/node_origin/mod.rs | Updates economic gate plumbing to key warming availability by SourceId. |
| crates/node/src/node_origin/pull_leg.rs | Wraps candidate node id/hash bytes into SourceId/Hash when debiting speculative warming buys. |
| crates/node/src/handlers/client/mod.rs | Passes Hash directly into the warming credit sink instead of raw bytes. |
| crates/node/src/runtime/eviction.rs | Forgets warming tags using Hash directly on eviction. |
| crates/node/src/blacklist_watcher.rs | Updates eviction + tests to use Hash and SourceId instead of [u8; 32]. |
| crates/node/src/admin.rs | Forgets warming tags using Hash directly in admin-initiated eviction. |
| crates/node/tests/client_loopback.rs | Updates loopback tests to use SourceId and Hash in warming allowance setup. |
| crates/node/tests/node_origin_pull.rs | Updates origin-pull integration test to use SourceId (naming cleanup suggested in comments). |
Suppressed comments (2)
crates/node/tests/node_origin_pull.rs:12693
src2_id_bytesis now aSourceId(not raw bytes), so the_bytessuffix is misleading in this test.
let src2_id_bytes =
decdn_node::warming_allowance::SourceId::from_bytes(*src2_sk.public().as_bytes());
anyhow::ensure!(
warming.available(src2_id_bytes),
"an independent source must read available before it is ever touched"
crates/node/tests/node_origin_pull.rs:12719
src3_id_bytesis now aSourceId(not raw bytes), so the_bytessuffix is misleading and makes the test harder to read.
let src3_id_bytes =
decdn_node::warming_allowance::SourceId::from_bytes(*src3_sk.public().as_bytes());
let (vindicated_admitted, _) = attack_b_attempt(
src3_sk,
0xD1,
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Address review nits on the warming source-id newtype:
- Give SourceId the same by-value `to_bytes()` accessor and symmetric
`From<SourceId> for [u8; 32]` the repo's other 32-byte identity
newtypes carry, so a call site never has to reach through
`*id.as_bytes()`.
- Rename the `src{1,2,3}_id_bytes` locals in the origin-pull test to
`src{1,2,3}_id`: they hold a SourceId now, and the `_bytes` suffix
read as raw `[u8; 32]`.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfEvDBn3S8R3H8KSDd7rV3
Coverage reportTotal: 82.12% (was 82.11%, ▲ +0.01%) — 68340/83223 lines
History
|
The new `to_bytes()` and `From<SourceId> for [u8; 32]` accessors had no exercising caller, leaving them uncovered. Add a round-trip unit test over from_bytes/as_bytes/to_bytes and both `From` directions. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01JfEvDBn3S8R3H8KSDd7rV3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1851.
Problem
The warming-credit seam passed both content hashes and source node identities as bare
[u8; 32], so the compiler could not tell them apart:debit_speculative(source: [u8; 32], hash: [u8; 32], units: u64)— two adjacent same-typed parameters a caller can transpose silently.source_for(hash: [u8; 32]) -> Option<[u8; 32]>took a hash and returned a source of the same type.mpsc::Sender<([u8; 32], u64)>(a source) with the same tuple shape as the hash thatWarmingCreditSink::creditabove it carried — nothing marked where the meaning flipped.Change
Introduce a
SourceIdnewtype for the bonded upstream seller identity and usedecdn_cache::Hashfor the content hash acrossWarmingAllowance,WarmingCreditSink, and the background aggregator. The two sides are now distinct types, so transposing a source and a hash is a compile error rather than a silent logic bug.SourceIdisrepr(transparent)over[u8; 32]withfrom_bytes/as_bytes, matching the identity-newtype pattern already used on the wire (decdn_protocol::identity).SourceId; the hash-to-source tag map isDashMap<Hash, SourceId>; the credit channel carries(SourceId, u64).SourceIdfrom the candidate node id and aHashfrom the pulled hash bytes, and the eviction, blacklist, admin, and serve paths pass their existingHashvalues through directly instead of unwrapping to*hash.as_bytes().This is the type-safety cleanup suggested in the #1803 review and filed as #1851.
Testing
cargo check -p decdn-node --lib— cleancargo clippy -p decdn-node --tests— cleancargo fmt -- --check— cleancargo test -p decdn-node --lib warming_allowance— 11 passedGenerated by Claude Code