karyon_p2p/discovery/kademlia/routing_table/
entry.rs1use bincode::{Decode, Encode};
2
3use crate::{bloom::Bloom, discovery::kademlia::messages::PeerMsg, message::PeerAddr, PeerID};
4
5pub const KEY_SIZE: usize = 32;
7
8pub type Key = [u8; KEY_SIZE];
10
11#[derive(Encode, Decode, Clone, Debug)]
13pub struct Entry {
14 pub key: Key,
16 pub addrs: Vec<PeerAddr>,
18 pub discovery_addrs: Vec<PeerAddr>,
20 pub protocols: Bloom,
23}
24
25impl Entry {
26 pub fn primary_addr(&self) -> Option<&karyon_net::Addr> {
28 self.addrs.first().map(|a| &a.addr)
29 }
30}
31
32impl PartialEq for Entry {
33 fn eq(&self, other: &Self) -> bool {
34 self.key == other.key
35 }
36}
37
38impl From<Entry> for PeerMsg {
39 fn from(entry: Entry) -> PeerMsg {
40 PeerMsg {
41 peer_id: PeerID(entry.key),
42 addrs: entry.addrs,
43 discovery_addrs: entry.discovery_addrs,
44 protocols: entry.protocols,
45 }
46 }
47}
48
49impl From<PeerMsg> for Entry {
50 fn from(peer: PeerMsg) -> Entry {
51 Entry {
52 key: peer.peer_id.0,
53 addrs: peer.addrs,
54 discovery_addrs: peer.discovery_addrs,
55 protocols: peer.protocols,
56 }
57 }
58}
59
60pub fn xor_distance(key: &Key, other: &Key) -> Key {
65 let mut res = [0; 32];
66 for (i, (k, o)) in key.iter().zip(other.iter()).enumerate() {
67 res[i] = k ^ o;
68 }
69 res
70}