Skip to main content

TrustEngine

Struct TrustEngine 

Source
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

Source

pub fn new() -> Self

Create a new TrustEngine

Source

pub fn update_node_stats(&self, node_id: &PeerId, update: NodeStatisticsUpdate)

Record a peer interaction outcome

Source

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.

Source

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.

Source

pub fn remove_node(&self, node_id: &PeerId)

Remove a peer from the trust system entirely

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for TrustEngine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TrustEngine

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more