aboutsummaryrefslogtreecommitdiff
path: root/p2p/src/routing_table/entry.rs
blob: 1427c2bc2c8d90374a7416c47b0a2c433cdee304 (plain)
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
use bincode::{Decode, Encode};

use karyon_net::{Addr, Port};

/// Specifies the size of the key, in bytes.
pub const KEY_SIZE: usize = 32;

/// The unique key identifying the peer.
pub type Key = [u8; KEY_SIZE];

/// An Entry represents a peer in the routing table.
#[derive(Encode, Decode, Clone, Debug)]
pub struct Entry {
    /// The unique key identifying the peer.
    pub key: Key,
    /// The IP address of the peer.
    pub addr: Addr,
    /// TCP port
    pub port: Port,
    /// UDP/TCP port
    pub discovery_port: Port,
}

impl PartialEq for Entry {
    fn eq(&self, other: &Self) -> bool {
        // XXX: should we compare both self.addr and other.addr???
        self.key == other.key
    }
}

/// Calculates the XOR distance between two provided keys.
///
/// The XOR distance is a metric used in Kademlia to measure the closeness
/// of keys.
pub fn xor_distance(key: &Key, other: &Key) -> Key {
    let mut res = [0; 32];
    for (i, (k, o)) in key.iter().zip(other.iter()).enumerate() {
        res[i] = k ^ o;
    }
    res
}