From 01a83e603140e82faf9586c4c824fececa69a791 Mon Sep 17 00:00:00 2001 From: arkpar Date: Mon, 15 Feb 2016 20:28:27 +0100 Subject: [PATCH] Populate discovery from node table --- util/src/network/discovery.rs | 10 +++++++++- util/src/network/host.rs | 1 + util/src/network/node_table.rs | 8 +++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/util/src/network/discovery.rs b/util/src/network/discovery.rs index da8ce9e34..08f5e5cf1 100644 --- a/util/src/network/discovery.rs +++ b/util/src/network/discovery.rs @@ -110,12 +110,20 @@ impl Discovery { } } - pub fn add_node(&mut self, e: NodeEntry) { + /// Add a new node to discovery table. Pings the node. + pub fn add_node(&mut self, e: NodeEntry) { let endpoint = e.endpoint.clone(); self.update_node(e); self.ping(&endpoint); } + /// Add a list of known nodes to the table. + pub fn init_node_list(&mut self, mut nodes: Vec) { + for n in nodes.drain(..) { + self.update_node(n); + } + } + fn update_node(&mut self, e: NodeEntry) { trace!(target: "discovery", "Inserting {:?}", &e); let ping = { diff --git a/util/src/network/host.rs b/util/src/network/host.rs index 5f0bf19b9..c548d07cf 100644 --- a/util/src/network/host.rs +++ b/util/src/network/host.rs @@ -386,6 +386,7 @@ impl Host where Message: Send + Sync + Clone { for n in boot_nodes { host.add_node(&n); } + host.discovery.lock().unwrap().init_node_list(host.nodes.read().unwrap().unordered_entries()); host } diff --git a/util/src/network/node_table.rs b/util/src/network/node_table.rs index 40cc14743..9bce2d334 100644 --- a/util/src/network/node_table.rs +++ b/util/src/network/node_table.rs @@ -29,7 +29,7 @@ use hash::*; use rlp::*; use time::Tm; use error::*; -use network::discovery::TableUpdates; +use network::discovery::{TableUpdates, NodeEntry}; pub use rustc_serialize::json::Json; /// Node public key @@ -214,6 +214,12 @@ impl NodeTable { refs.iter().map(|n| n.id.clone()).collect() } + /// Unordered list of all entries + pub fn unordered_entries(&self) -> Vec { + // preserve failure counter + self.nodes.values().map(|n| NodeEntry { endpoint: n.endpoint.clone(), id: n.id.clone() }).collect() + } + /// Get particular node pub fn get_mut(&mut self, id: &NodeId) -> Option<&mut Node> { self.nodes.get_mut(id)