karyon_p2p/routing_table/
entry.rs

1use bincode::{Decode, Encode};
2
3use karyon_net::{Addr, Port};
4
5/// Specifies the size of the key, in bytes.
6pub const KEY_SIZE: usize = 32;
7
8/// The unique key identifying the peer.
9pub type Key = [u8; KEY_SIZE];
10
11/// An Entry represents a peer in the routing table.
12#[derive(Encode, Decode, Clone, Debug)]
13pub struct Entry {
14    /// The unique key identifying the peer.
15    pub key: Key,
16    /// The IP address of the peer.
17    pub addr: Addr,
18    /// TCP port
19    pub port: Port,
20    /// UDP/TCP port
21    pub discovery_port: Port,
22}
23
24impl PartialEq for Entry {
25    fn eq(&self, other: &Self) -> bool {
26        // XXX: should we compare both self.addr and other.addr???
27        self.key == other.key
28    }
29}
30
31/// Calculates the XOR distance between two provided keys.
32///
33/// The XOR distance is a metric used in Kademlia to measure the closeness
34/// of keys.
35pub fn xor_distance(key: &Key, other: &Key) -> Key {
36    let mut res = [0; 32];
37    for (i, (k, o)) in key.iter().zip(other.iter()).enumerate() {
38        res[i] = k ^ o;
39    }
40    res
41}