subspace_networking/utils/
key_with_distance.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use libp2p::kad::KBucketDistance;
pub use libp2p::kad::RecordKey;
pub use libp2p::PeerId;
use std::cmp::Ordering;
use std::hash::Hash;

type KademliaBucketKey<T> = libp2p::kad::KBucketKey<T>;

/// Helper structure. It wraps Kademlia distance to a given peer for heap-metrics.
#[derive(Debug, Clone, Eq)]
pub struct KeyWithDistance {
    key: RecordKey,
    distance: KBucketDistance,
}

impl KeyWithDistance {
    /// Creates a new [`KeyWithDistance`] instance with the given `PeerId` and `K` key.
    ///
    /// The `distance` is calculated as the distance between the `KademliaBucketKey` derived
    /// from the `PeerId` and the `KademliaBucketKey` derived from the `K` key.
    pub fn new<K>(peer_id: PeerId, key: K) -> Self
    where
        RecordKey: From<K>,
    {
        Self::new_with_record_key(peer_id, RecordKey::from(key))
    }

    /// Creates a new [`KeyWithDistance`] instance with the given `PeerId` and `RecordKey`.
    pub fn new_with_record_key(peer_id: PeerId, key: RecordKey) -> Self {
        let peer_key = KademliaBucketKey::from(peer_id);
        let distance = KademliaBucketKey::new(key.as_ref()).distance(&peer_key);
        Self { key, distance }
    }

    /// Returns a reference to the record key.
    pub fn record_key(&self) -> &RecordKey {
        &self.key
    }
}

impl PartialEq for KeyWithDistance {
    fn eq(&self, other: &Self) -> bool {
        self.key == other.key
    }
}

impl PartialOrd for KeyWithDistance {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for KeyWithDistance {
    fn cmp(&self, other: &Self) -> Ordering {
        self.distance.cmp(&other.distance)
    }
}

impl Hash for KeyWithDistance {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.key.hash(state);
    }
}