Skip to content

Commit 965711d

Browse files
Simplify routing table design
Instead of doing dynamic table resizing, just allocate all the memory upfront.
1 parent 1f8cac2 commit 965711d

File tree

6 files changed

+127
-144
lines changed

6 files changed

+127
-144
lines changed

src/dht_service.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ impl DhtV4 {
137137
while let Some(_) = bootstrap_join_set.join_next().await {}
138138
drop(bootstrap_join_set);
139139

140-
println!("I should be done");
141140
info!("DHT bootstrapped, routing table has {} nodes", peer_guide.node_count());
142141

143142
Ok(dht)
@@ -159,11 +158,13 @@ impl DhtV4 {
159158
let our_id = dht.our_id.clone();
160159

161160
info!("bootstrapping with {peer}");
162-
let response = timeout(Duration::from_secs(5), async {
161+
let _response = timeout(Duration::from_secs(5), async {
163162
let node_id = dht.ping(peer).await?;
164163
dht.routing_table.add(node_id, peer);
165164

166-
dht.find_node(our_id).await;
165+
// the find node only obviously we know ourselves, this only serves us to get us info
166+
// about other nodes
167+
let _ = dht.find_node(our_id).await;
167168
println!("done finding node");
168169

169170
Ok::<(), eyre::Report>(())

src/dht_service/dht_client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl DhtHandle {
7474
pub async fn find_node(self: Arc<Self>, target: NodeId) -> Result<NodeInfo, OurError> {
7575
// if we already know the node, then no need for any network requests
7676
if let Some(node) = (&self).routing_table.find(target) {
77-
return Ok(node.contact);
77+
return Ok(node);
7878
}
7979

8080
let mut queried: HashSet<NodeInfo> = HashSet::new();
@@ -180,7 +180,7 @@ impl DhtHandle {
180180
//
181181
// if we already know the node, then no need for any network requests
182182
if let Some(node) = (&self).routing_table.find(resonsible) {
183-
let (token, _nodes, peers) = self.send_get_peers_rpc(node.contact.contact().0, info_hash).await?;
183+
let (token, _nodes, peers) = self.send_get_peers_rpc(node.contact().0, info_hash).await?;
184184
return Ok((
185185
token.expect("A node directly responsible for a piece would return a token"),
186186
peers,

src/dht_service/dht_server.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
use crate::{
22
domain_knowledge::{InfoHash, NodeId, PeerContact, Token},
33
message::{
4-
announce_peer_query::AnnouncePeerQuery,
5-
find_node_get_peers_response::{self, FindNodeGetPeersResponse},
6-
find_node_query::{self, FindNodeQuery},
7-
get_peers_query::GetPeersQuery,
8-
ping_query::PingQuery,
9-
Krpc,
4+
announce_peer_query::AnnouncePeerQuery, find_node_query::FindNodeQuery, get_peers_query::GetPeersQuery,
5+
ping_query::PingQuery, Krpc,
106
},
117
};
128
use rand::RngCore;
@@ -24,7 +20,7 @@ use tokio::{
2420
task::Builder as TskBuilder,
2521
time::Instant,
2622
};
27-
use tracing::{error, info, info_span, trace, Instrument};
23+
use tracing::{info, info_span, trace, Instrument};
2824

2925
use super::{peer_guide::PeerGuide, MessageBroker};
3026
#[derive(Debug)]

src/dht_service/peer_guide.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tokio::sync::mpsc;
88
use crate::{
99
domain_knowledge::{NodeId, NodeInfo},
1010
message::Krpc,
11-
routing::{Node, RoutingTable},
11+
routing::RoutingTable,
1212
};
1313

1414
#[derive(Debug)]
@@ -57,7 +57,7 @@ impl PeerGuide {
5757
routing_table.find_closest(target)
5858
}
5959

60-
pub fn find(&self, target: NodeId) -> Option<Node> {
60+
pub fn find(&self, target: NodeId) -> Option<NodeInfo> {
6161
let routing_table = self.routing_table.lock().unwrap();
6262
routing_table.find(target)
6363
}

src/domain_knowledge.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use num::{traits::ops::bytes, BigUint};
33
use smallvec::SmallVec;
44
use std::{fmt::Debug, net::SocketAddrV4};
55

6-
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
6+
const NODE_ID_LEN: usize = 20;
7+
pub const ZERO_DIST: [u8; NODE_ID_LEN] = [0; NODE_ID_LEN];
8+
9+
#[derive(PartialEq, Eq, Hash, Clone, Copy, PartialOrd, Ord)]
710
pub struct NodeId(pub [u8; 20]);
811

912
impl Debug for NodeId {
@@ -36,6 +39,14 @@ impl NodeId {
3639
let node_id = BigUint::from_bytes_be(rhs.as_bytes());
3740
our_id ^ node_id
3841
}
42+
43+
pub fn dist(&self, rhs: &Self) -> [u8; 20] {
44+
let mut dist = [0u8; 20];
45+
for i in 0..20 {
46+
dist[i] = self.0[i] ^ rhs.0[i]
47+
}
48+
dist
49+
}
3950
}
4051

4152
impl ToBencode for NodeId {
@@ -46,6 +57,25 @@ impl ToBencode for NodeId {
4657
}
4758
}
4859

60+
// impl PartialOrd for NodeId {
61+
// fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
62+
// for (lhs, rhs) in self.0.iter().zip(other.0.iter()) {
63+
// match lhs.cmp(rhs) {
64+
// Ordering::Equal => continue,
65+
// Ordering::Less => return Some(Ordering::Less),
66+
// Ordering::Greater => return Some(Ordering::Greater),
67+
// }
68+
// }
69+
// Some(Ordering::Equal)
70+
// }
71+
// }
72+
//
73+
// impl Ord for NodeId {
74+
// fn cmp(&self, other: &Self) -> Ordering {
75+
// self.partial_cmp(other).unwrap()
76+
// }
77+
// }
78+
4979
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
5080
pub struct InfoHash(pub [u8; 20]);
5181

0 commit comments

Comments
 (0)