Repository navigation
Description
Split out of the review on #1456. Pre-existing; that PR neither introduced nor worsened it, but it deleted the last test that touched the upper end of the reputation domain, so it is now entirely uncovered.
What
crates/node/src/selection.rs:
let rep = f64::from(reputation.max(REPUTATION_FLOOR));
rate * rtt / (rep * rep)Candidate.reputation is documented as domain [0.0, 1.0]. The clamp bounds it below only:
reputation = 10.0→ score divided by 100 → beats a perfect-reputation node by 100×.reputation = f32::INFINITY→ score0.0→ sorts first, unconditionally.
The lower half is well-defended (NaN and negatives both clamp to 0.1; NaN safety now pinned by score_nan_reputation_clamps_to_floor, added in #1456). The upper half has no defense and no test.
Reachability
Not reachable today. LocalReputation::fold ends in .clamp(0.0, 1.0) (crates/reputation/src/local.rs:349) and LocalReputation::new validates initial_score ∈ [0.0, 1.0], so the producer already enforces the domain.
The one place the type-level chain breaks is crates/node/src/node_origin.rs:2293:
fn peer_reputation(deps: &NodeOriginDeps, pk: PublicKey) -> f32 {
deps.local_rep.score(pk) as f32
}— an unchecked f64 → f32 narrowing cast. Safe given the producer's clamp, but the guarantee is carried by convention across a crate boundary rather than by a type.
Two ways to fix
(a) Cheap, local. Add the upper clamp in compute_score:
let rep = f64::from(reputation.max(REPUTATION_FLOOR).min(1.0));.clamp(REPUTATION_FLOOR, 1.0). f32::clamp propagates NaN (if self < min {min} else if self > max {max} else {self}), which would regress the NaN safety that score_nan_reputation_clamps_to_floor now pins. f32::max and f32::min both implement IEEE maxNum/minNum and ignore NaN, so the .max().min() form is the NaN-total one. It also avoids clamp's min > max panic shape, which the workspace anti-panic clippy lints do not catch.
(b) Structural. A Reputation newtype with a smart constructor, applied at peer_reputation's as f32 cast. That makes Candidate { reputation: <garbage> } unconstructible, retires the "defensive" framing in the score_* tests, and demotes REPUTATION_FLOOR from a safety clamp to a pure policy floor. Cost: ~six test helpers construct Candidate literals with arbitrary f32 (including -0.5) specifically to document the defensive behavior — a newtype makes those unwritable, which is an acceptable but real loss of expressed intent.
Suggest (a) now with a test at each end of the domain, and (b) as a separate call once someone is in node_origin anyway.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
fix: address review findings on the #1438/#1440 removal
refactor: drop two dead knobs — min-reputation floor and trusted-IP r…
refactor(selection): name the reputation ceiling, fix stale docs, add…