Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
95th committed Jul 22, 2020
1 parent f8e82a4 commit 27375a5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
11 changes: 0 additions & 11 deletions dht/src/contact.rs
@@ -1,7 +1,6 @@
use crate::id::NodeId;
use ben::{Encode, Encoder};
use std::net::SocketAddr;
use std::time::Instant;

bitflags! {
pub struct ContactStatus: u8 {
Expand Down Expand Up @@ -32,9 +31,6 @@ impl ContactRef<'_> {
pub struct Contact {
pub id: NodeId,
pub addr: SocketAddr,
pub last_updated: Instant,
pub last_queried: Instant,
pub token: Vec<u8>,
pub status: ContactStatus,
timeout_count: Option<u8>,
}
Expand All @@ -44,9 +40,6 @@ impl Contact {
Self {
id,
addr,
last_updated: Instant::now(),
last_queried: Instant::now(),
token: vec![],
timeout_count: None,
status: ContactStatus::INITIAL,
}
Expand All @@ -59,10 +52,6 @@ impl Contact {
}
}

pub fn touch(&mut self) {
self.last_updated = Instant::now();
}

pub fn is_pinged(&self) -> bool {
self.timeout_count.is_some()
}
Expand Down
4 changes: 4 additions & 0 deletions dht/src/server/traversal.rs
Expand Up @@ -54,6 +54,10 @@ impl Traversal {
}
}

/// Handle an incoming response and return `true` if it
/// was handled in this traversal.
/// Returning `false` means that the response didn't belong
/// to this traversal.
pub fn handle_reply(
&mut self,
resp: &Response,
Expand Down
10 changes: 8 additions & 2 deletions dht/src/server/traversal/bootstrap.rs
Expand Up @@ -37,7 +37,7 @@ impl BootstrapTraversal {
if self.nodes.len() < 3 {
for node in &table.router_nodes {
self.nodes.push(TraversalNode {
id: NodeId::gen(),
id: NodeId::new(),
addr: *node,
status: Status::INITIAL | Status::NO_ID,
});
Expand Down Expand Up @@ -65,7 +65,13 @@ impl BootstrapTraversal {
) -> bool {
if let Some(req) = self.txns.remove(&resp.txn_id) {
if req.has_id {
table.heard_from(&req.id);
if &req.id == resp.id {
table.heard_from(&req.id);
} else {
warn!("ID mismatch from {}", addr);
table.failed(&req.id);
return true;
}
}
} else {
return false;
Expand Down
10 changes: 8 additions & 2 deletions dht/src/server/traversal/get_peers.rs
Expand Up @@ -45,7 +45,7 @@ impl GetPeersTraversal {
if self.nodes.len() < 3 {
for node in &table.router_nodes {
self.nodes.push(TraversalNode {
id: NodeId::gen(),
id: NodeId::new(),
addr: *node,
status: Status::INITIAL | Status::NO_ID,
});
Expand Down Expand Up @@ -73,7 +73,13 @@ impl GetPeersTraversal {
) -> bool {
if let Some(req) = self.txns.remove(&resp.txn_id) {
if req.has_id {
table.heard_from(&req.id);
if &req.id == resp.id {
table.heard_from(&req.id);
} else {
warn!("ID mismatch from {}", addr);
table.failed(&req.id);
return true;
}
}
} else {
return false;
Expand Down
7 changes: 6 additions & 1 deletion dht/src/table.rs
Expand Up @@ -114,14 +114,19 @@ impl RoutingTable {
self.buckets[idx].live.iter_mut().find(|c| c.id == *id)
}

pub fn failed(&mut self, id: &NodeId) {
if let Some(c) = self.find_contact(id) {
c.timed_out();
}
}

pub fn heard_from(&mut self, id: &NodeId) {
let idx = self.find_bucket(id);
let bucket = &mut self.buckets[idx];

if let Some(c) = bucket.live.iter_mut().find(|c| c.id == *id) {
c.status = ContactStatus::ALIVE | ContactStatus::QUERIED;
c.clear_timeout();
c.last_updated = Instant::now();
bucket.last_updated = Instant::now();
}
}
Expand Down

0 comments on commit 27375a5

Please sign in to comment.