Skip to content

refactor(node): newtype warming source ids and hashes at the warming-allowance seam - #1874

Merged
alpergundogdu merged 4 commits into
mainfrom
claude/issue-1851-pyrhm8
Aug 30, 2026
Merged

alpergundogdu merged 4 commits into
mainfrom
claude/issue-1851-pyrhm8

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

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.
  • The deferred-credit channel carried mpsc::Sender<([u8; 32], u64)> (a source) with the same tuple shape as the hash that WarmingCreditSink::credit above it carried — nothing marked where the meaning flipped.

Change

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 rather than a silent logic bug.

  • SourceId is repr(transparent) over [u8; 32] with from_bytes/as_bytes, matching the identity-newtype pattern already used on the wire (decdn_protocol::identity).
  • Ledger buckets key on SourceId; the hash-to-source tag map is DashMap<Hash, SourceId>; the credit channel carries (SourceId, u64).
  • 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 *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 — clean
  • cargo clippy -p decdn-node --tests — clean
  • cargo fmt -- --check — clean
  • cargo test -p decdn-node --lib warming_allowance — 11 passed

Generated by Claude Code

…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
Copilot AI lite review requested due to automatic review settings August 30, 2026 15:18
@alpergundogdu alpergundogdu changed the title node: newtype warming source ids and hashes at the warming-allowance seam refactor(node): newtype warming source ids and hashes at the warming-allowance seam Aug 30, 2026
`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

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

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 updates WarmingAllowance / WarmingCreditSink APIs to use SourceId + Hash.
  • Updates node-origin pull legs and serve/evict/blacklist/admin call sites to pass Hash directly 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_bytes is now a SourceId (not raw bytes), so the _bytes suffix 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_bytes is now a SourceId (not raw bytes), so the _bytes suffix 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.

Comment thread crates/node/tests/node_origin_pull.rs Resolved
Comment thread crates/node/src/warming_allowance.rs Resolved
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
@github-actions

github-actions Bot commented Aug 30, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 82.12% (was 82.11%, ▲ +0.01%) — 68340/83223 lines

File Coverage Lines Δ
crates/node/src/admin.rs 92.52% 977/1056 ● 0.00%
crates/node/src/blacklist_watcher.rs 74.78% 925/1237 ● 0.00%
crates/node/src/handlers/client/mod.rs 92.92% 1574/1694 ▲ +0.18%
crates/node/src/node_origin/mod.rs 92.86% 963/1037 ● 0.00%
crates/node/src/node_origin/pull_leg.rs 85.61% 577/674 ● 0.00%
crates/node/src/runtime/eviction.rs 79.66% 372/467 ● 0.00%
crates/node/src/warming_allowance.rs 98.17% 321/327 ▲ +0.14%
History
Commit Total Δ vs base
4981165 82.11% ● 0.00%
dcd5467 (HEAD) 82.12% ▲ +0.01%

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
@alpergundogdu
alpergundogdu merged commit b28e8ec into main Aug 30, 2026
25 checks passed
@alpergundogdu
alpergundogdu deleted the claude/issue-1851-pyrhm8 branch August 30, 2026 16:14
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.

node: newtype hashes and source node ids instead of raw [u8; 32]

3 participants