pub struct TrustEngine { /* private fields */ }Expand description
Local trust engine based on direct peer observations.
Scores are an exponential moving average of success/failure observations
that decays toward neutral (0.5) when idle. Bounded by MIN_TRUST_SCORE
and MAX_TRUST_SCORE.
This is the sole authority on peer trust scores in the system.
Implementations§
Source§impl TrustEngine
impl TrustEngine
Sourcepub fn update_node_stats(&self, node_id: &PeerId, update: NodeStatisticsUpdate)
pub fn update_node_stats(&self, node_id: &PeerId, update: NodeStatisticsUpdate)
Record a peer interaction outcome
Sourcepub fn update_node_stats_weighted(
&self,
node_id: &PeerId,
update: NodeStatisticsUpdate,
weight: f64,
)
pub fn update_node_stats_weighted( &self, node_id: &PeerId, update: NodeStatisticsUpdate, weight: f64, )
Record a peer interaction outcome with an explicit weight.
Weight 1.0 is equivalent to a single internal event. Higher weights
amplify the observation’s influence on the EMA. The caller is responsible
for validating/clamping the weight before calling this method.
Sourcepub fn score(&self, node_id: &PeerId) -> f64
pub fn score(&self, node_id: &PeerId) -> f64
Get current trust score for a peer (synchronous).
Applies time decay lazily — no background task needed.
Returns DEFAULT_NEUTRAL_TRUST (0.5) for unknown peers.
Uses parking_lot::RwLock so this never falls back to a stale
neutral value during write contention — it briefly blocks until
the writer releases.
Sourcepub fn remove_node(&self, node_id: &PeerId)
pub fn remove_node(&self, node_id: &PeerId)
Remove a peer from the trust system entirely
Sourcepub fn export_snapshot(&self) -> TrustSnapshot
pub fn export_snapshot(&self) -> TrustSnapshot
Export current trust state as a serializable snapshot.
Applies decay to all scores before exporting so the snapshot reflects the current effective scores.
Sourcepub fn import_snapshot(&self, snapshot: &TrustSnapshot)
pub fn import_snapshot(&self, snapshot: &TrustSnapshot)
Import trust state from a persisted snapshot.
Scores are restored as-is with last_updated set to now. Decay does
not run while our node is offline — we can’t observe peer behavior
during downtime, so penalising peers for our absence would be wrong.
Decay resumes naturally from the moment the node restarts.