Skip to main content

rings_core/measure/
quality.rs

1use crate::dht::Did;
2
3/// Local peer-quality class derived from recent reliability evidence.
4pub type PeerQuality = rings_measure::ReliabilityClass;
5
6/// Failure limits used to classify recent local reliability evidence.
7pub type PeerQualityThresholds = rings_measure::ReliabilityThresholds;
8
9/// Recent local logical transport outcomes.
10pub type PeerQualityEvidence = rings_measure::ReliabilityEvidence;
11
12/// Projected local measurement for one Rings peer.
13///
14/// Reliability remains advisory DHT ordering evidence. Credit is a separate
15/// local resource-priority relation and is never a Chord membership proof.
16#[derive(Debug, Clone, Copy, PartialEq)]
17pub struct PeerMeasurement {
18    /// Peer DID described by this projection.
19    pub did: Did,
20    /// Recent logical transport evidence.
21    pub evidence: PeerQualityEvidence,
22    /// Persistent useful-byte totals for this retained peer.
23    pub credit: rings_measure::CreditRecord,
24    /// Local aMule-compatible resource-priority multiplier.
25    pub credit_score: rings_measure::CreditScore,
26    /// Advisory reliability class derived at query time.
27    pub quality: PeerQuality,
28}
29
30/// One bounded page of local peer measurements in DID order.
31#[derive(Debug, Clone, PartialEq, Default)]
32pub struct PeerMeasurementPage {
33    /// Successfully projected measurements in the scanned page.
34    pub measurements: Vec<PeerMeasurement>,
35    /// Exclusive DID cursor for the next page, absent at the end.
36    pub next_cursor: Option<Did>,
37}
38
39impl PeerMeasurement {
40    /// Convert a pure generic ledger projection into the Rings DTO boundary.
41    pub fn from_projected(projected: rings_measure::PeerMeasurement<Did>) -> Self {
42        Self {
43            did: projected.peer,
44            evidence: projected.reliability,
45            credit: projected.credit,
46            credit_score: projected.credit_score,
47            quality: projected.reliability_class,
48        }
49    }
50}
51
52/// Stably order DHT candidates by advisory reliability.
53///
54/// Invariant: the output is a stable permutation of the input candidate set.
55pub fn order_peers_by_quality(
56    candidates: impl IntoIterator<Item = (Did, PeerQuality)>,
57) -> Vec<Did> {
58    rings_measure::order_peers_by_reliability(candidates)
59}