pub struct LocalReputation { /* private fields */ }Expand description
In-memory store of per-peer EWMA reputation scores keyed by NodeId.
Cheap to share across tasks via std::sync::Arc; reads use a
read-lock, writes a write-lock. Idle scores decay toward neutral lazily at
read time (ADR 008 §Score Decay). No persistence and no network
aggregation per ADR 008 §14a.
The peer map is split into SHARD_COUNT independently-locked shards so
per-candidate reads during ranking and the periodic maintenance sweep do not
serialize on one global lock.
Implementations§
Source§impl LocalReputation
impl LocalReputation
Sourcepub fn new(config: LocalReputationConfig) -> Result<Self, ConfigError>
pub fn new(config: LocalReputationConfig) -> Result<Self, ConfigError>
Build a new score store backed by the system clock, validating the config.
Validation rejects non-finite or out-of-range parameters so a single bad config write cannot poison every future score with NaN.
Sourcepub fn with_clock(
config: LocalReputationConfig,
clock: Arc<dyn Clock>,
) -> Result<Self, ConfigError>
pub fn with_clock( config: LocalReputationConfig, clock: Arc<dyn Clock>, ) -> Result<Self, ConfigError>
Sourcepub fn record(&self, peer: NodeId, outcome: Outcome) -> f64
pub fn record(&self, peer: NodeId, outcome: Outcome) -> f64
Fold an outcome into the peer’s score and return the new value.
The stored score is first decayed to now (ADR 008 §Score Decay), then
EWMA-folded with this interaction’s sample. The returned value matches a
subsequent Self::score call taken at the same instant so callers
logging both can avoid a re-lock.
Sourcepub fn score(&self, peer: NodeId) -> f64
pub fn score(&self, peer: NodeId) -> f64
Current score for peer, decayed toward neutral for idle time (ADR 008
§Score Decay), or LocalReputationConfig::initial_score if unseen.
Sourcepub fn snapshot(&self) -> Vec<(NodeId, f64)>
pub fn snapshot(&self) -> Vec<(NodeId, f64)>
Snapshot of all observed (peer, score) pairs, each decayed to now
(ADR 008 §Score Decay). Allocates.
Sourcepub fn evict(&self)
pub fn evict(&self)
Drop entries whose decayed score has converged within EVICT_NEUTRAL_BAND
(0.05) of neutral AND that have been idle for more than EVICT_IDLE_WEEKS
(26), so the map stays bounded under peer churn without
discarding any opinion that still reads as non-neutral. A re-seen peer
simply re-inserts at neutral, so eviction is information-lossless for
anything that has already decayed to neutral. Best-effort maintenance —
call periodically; correctness never depends on it.