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