Don't ping all nodes on start (#1656)

* Don't ping all nodes on start

* Fixed test
This commit is contained in:
Arkadiy Paronyan 2016-07-19 09:24:19 +02:00 committed by Gav Wood
parent 4e447ccc68
commit 340f0b6f58
2 changed files with 12 additions and 10 deletions

View File

@ -119,10 +119,12 @@ impl Discovery {
} }
/// Add a new node to discovery table. Pings the node. /// Add a new node to discovery table. Pings the node.
pub fn add_node(&mut self, e: NodeEntry) { pub fn add_node(&mut self, e: NodeEntry, new: bool) {
let endpoint = e.endpoint.clone(); let endpoint = e.endpoint.clone();
self.update_node(e); self.update_node(e);
self.ping(&endpoint); if new {
self.ping(&endpoint);
}
} }
/// Add a list of known nodes to the table. /// Add a list of known nodes to the table.
@ -549,10 +551,10 @@ mod tests {
let node1 = Node::from_str("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@127.0.0.1:7770").unwrap(); let node1 = Node::from_str("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@127.0.0.1:7770").unwrap();
let node2 = Node::from_str("enode://b979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@127.0.0.1:7771").unwrap(); let node2 = Node::from_str("enode://b979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@127.0.0.1:7771").unwrap();
discovery1.add_node(NodeEntry { id: node1.id.clone(), endpoint: node1.endpoint.clone() }); discovery1.add_node(NodeEntry { id: node1.id.clone(), endpoint: node1.endpoint.clone() }, true);
discovery1.add_node(NodeEntry { id: node2.id.clone(), endpoint: node2.endpoint.clone() }); discovery1.add_node(NodeEntry { id: node2.id.clone(), endpoint: node2.endpoint.clone() }, true);
discovery2.add_node(NodeEntry { id: key1.public().clone(), endpoint: ep1.clone() }); discovery2.add_node(NodeEntry { id: key1.public().clone(), endpoint: ep1.clone() }, true);
discovery2.refresh(); discovery2.refresh();
for _ in 0 .. 10 { for _ in 0 .. 10 {
@ -579,7 +581,7 @@ mod tests {
let ep = NodeEndpoint { address: SocketAddr::from_str("127.0.0.1:40446").unwrap(), udp_port: 40447 }; let ep = NodeEndpoint { address: SocketAddr::from_str("127.0.0.1:40446").unwrap(), udp_port: 40447 };
let mut discovery = Discovery::new(&key, ep.address.clone(), ep.clone(), 0); let mut discovery = Discovery::new(&key, ep.address.clone(), ep.clone(), 0);
for _ in 0..1200 { for _ in 0..1200 {
discovery.add_node(NodeEntry { id: NodeId::random(), endpoint: ep.clone() }); discovery.add_node(NodeEntry { id: NodeId::random(), endpoint: ep.clone() }, true);
} }
assert!(Discovery::nearest_node_entries(&NodeId::new(), &discovery.node_buckets).len() <= 16); assert!(Discovery::nearest_node_entries(&NodeId::new(), &discovery.node_buckets).len() <= 16);
let removed = discovery.check_expired(true).len(); let removed = discovery.check_expired(true).len();

View File

@ -419,7 +419,7 @@ impl Host {
self.nodes.write().add_node(n); self.nodes.write().add_node(n);
if let Some(ref mut discovery) = *self.discovery.lock() { if let Some(ref mut discovery) = *self.discovery.lock() {
discovery.add_node(entry); discovery.add_node(entry, true);
} }
} }
} }
@ -433,7 +433,7 @@ impl Host {
self.nodes.write().add_node(Node::new(entry.id.clone(), entry.endpoint.clone())); self.nodes.write().add_node(Node::new(entry.id.clone(), entry.endpoint.clone()));
if let Some(ref mut discovery) = *self.discovery.lock() { if let Some(ref mut discovery) = *self.discovery.lock() {
discovery.add_node(entry); discovery.add_node(entry, false);
} }
Ok(()) Ok(())
@ -550,7 +550,7 @@ impl Host {
if let Some(mut discovery) = discovery { if let Some(mut discovery) = discovery {
discovery.init_node_list(self.nodes.read().unordered_entries()); discovery.init_node_list(self.nodes.read().unordered_entries());
for n in self.nodes.read().unordered_entries() { for n in self.nodes.read().unordered_entries() {
discovery.add_node(n.clone()); discovery.add_node(n.clone(), false);
} }
*self.discovery.lock() = Some(discovery); *self.discovery.lock() = Some(discovery);
io.register_stream(DISCOVERY).expect("Error registering UDP listener"); io.register_stream(DISCOVERY).expect("Error registering UDP listener");
@ -788,7 +788,7 @@ impl Host {
self.nodes.write().add_node(Node::new(entry.id.clone(), entry.endpoint.clone())); self.nodes.write().add_node(Node::new(entry.id.clone(), entry.endpoint.clone()));
let mut discovery = self.discovery.lock(); let mut discovery = self.discovery.lock();
if let Some(ref mut discovery) = *discovery.deref_mut() { if let Some(ref mut discovery) = *discovery.deref_mut() {
discovery.add_node(entry); discovery.add_node(entry, true);
} }
} }
} }