pub struct PeerReputationGraph { /* private fields */ }Expand description
Graph-based peer reputation system with trust propagation.
The graph maintains directed weighted edges between peer identifiers and computes reputation scores via both direct interaction history and multi-hop trust propagation.
Implementations§
Source§impl PeerReputationGraph
impl PeerReputationGraph
Sourcepub fn new(config: GraphConfig) -> Self
pub fn new(config: GraphConfig) -> Self
Create a new graph with the given configuration.
Sourcepub fn add_peer(&mut self, peer_id: String) -> Result<(), GraphError>
pub fn add_peer(&mut self, peer_id: String) -> Result<(), GraphError>
Add a peer to the graph.
Returns GraphError::MaxPeersExceeded if the configured limit would be
breached. Adding an already-existing peer is a no-op (returns Ok).
Sourcepub fn remove_peer(&mut self, peer_id: &str) -> Result<(), GraphError>
pub fn remove_peer(&mut self, peer_id: &str) -> Result<(), GraphError>
Remove a peer and all edges incident to it.
Sourcepub fn add_edge(
&mut self,
from: &str,
to: &str,
weight: f64,
) -> Result<(), GraphError>
pub fn add_edge( &mut self, from: &str, to: &str, weight: f64, ) -> Result<(), GraphError>
Add or update a directed trust edge.
§Errors
GraphError::PeerNotFoundif either peer is absent.GraphError::SelfLoopiffrom == to.GraphError::InvalidWeightifweightis outside[0.0, 1.0].
Sourcepub fn remove_edge(&mut self, from: &str, to: &str) -> Result<(), GraphError>
pub fn remove_edge(&mut self, from: &str, to: &str) -> Result<(), GraphError>
Remove a directed trust edge.
Sourcepub fn record_interaction(
&mut self,
peer_id: &str,
positive: bool,
magnitude: f64,
) -> Result<ReputationEvent, GraphError>
pub fn record_interaction( &mut self, peer_id: &str, positive: bool, magnitude: f64, ) -> Result<ReputationEvent, GraphError>
Record an interaction with a peer, updating its direct score via EMA.
Alpha = 0.1. Positive interactions add magnitude; negative ones
subtract it. The result is clamped to [0.0, 1.0].
Returns the corresponding ReputationEvent.
Sourcepub fn propagate_trust(&mut self) -> usize
pub fn propagate_trust(&mut self) -> usize
Propagate trust through the graph via BFS.
For each peer p, performs BFS up to propagation_depth hops.
The propagated score contribution of a path
p → n1 → n2 → … → nk is:
w(p→n1) × direct_score(p) × damping^1
+ w(n1→n2) × direct_score(n1) × damping^2 (if n2 is the target)
…Each peer’s propagated_score is set to the sum of all such
contributions clamped to [0.0, 1.0].
Returns the number of peers whose propagated score was updated.
Sourcepub fn decay_scores(&mut self) -> Vec<ReputationEvent>
pub fn decay_scores(&mut self) -> Vec<ReputationEvent>
Apply decay to all direct scores and prune weak edges.
Each direct_score is multiplied by trust_decay_factor. A
ReputationEvent::ScoreDecayed is emitted for any peer whose score
changes by more than 0.001. Edges whose weight drops below
min_edge_weight are also pruned.
Sourcepub fn reputation(&self, peer_id: &str) -> Result<ReputationScore, GraphError>
pub fn reputation(&self, peer_id: &str) -> Result<ReputationScore, GraphError>
Retrieve the full ReputationScore for a peer.
combined_score is a weighted sum of direct_score and
propagated_score normalized so the total weight sums to 1.
confidence is min(1.0, interaction_count / 10.0).
Sourcepub fn top_peers(&self, n: usize) -> Vec<ReputationScore>
pub fn top_peers(&self, n: usize) -> Vec<ReputationScore>
Return the top n peers by combined score, sorted descending.
Sourcepub fn peers_trusting(&self, peer_id: &str) -> Vec<String>
pub fn peers_trusting(&self, peer_id: &str) -> Vec<String>
Peers that have a trust edge pointing to peer_id (in-neighbours).
Sourcepub fn peers_trusted_by(&self, peer_id: &str) -> Vec<String>
pub fn peers_trusted_by(&self, peer_id: &str) -> Vec<String>
Peers that peer_id trusts — i.e. out-neighbours of peer_id.
Sourcepub fn stats(&self) -> GraphStats
pub fn stats(&self) -> GraphStats
Compute aggregate statistics for the graph.
Sourcepub fn prune_isolated(&mut self) -> usize
pub fn prune_isolated(&mut self) -> usize
Remove peers that are isolated (no edges) and have a combined score below 0.1.
Returns the number of peers removed.
Sourcepub fn edge(&self, from: &str, to: &str) -> Option<&TrustEdge>
pub fn edge(&self, from: &str, to: &str) -> Option<&TrustEdge>
Return the edge between two peers, if present.
Sourcepub fn contains_peer(&self, peer_id: &str) -> bool
pub fn contains_peer(&self, peer_id: &str) -> bool
Return whether a peer is present in the graph.
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Return the current number of peers.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Return the current number of edges.
Auto Trait Implementations§
impl Freeze for PeerReputationGraph
impl RefUnwindSafe for PeerReputationGraph
impl Send for PeerReputationGraph
impl Sync for PeerReputationGraph
impl Unpin for PeerReputationGraph
impl UnsafeUnpin for PeerReputationGraph
impl UnwindSafe for PeerReputationGraph
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more