-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Improve P2P discovery #9526
Improve P2P discovery #9526
Changes from 3 commits
48cb9d4
e563f98
5e67cb5
0abffce
8d1cdbc
e0f61b1
973fd2c
d586beb
95d0b4a
2af281f
fe230b2
e4d5212
c38ff7c
bbf99bd
6ebdc56
e98c191
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,7 @@ pub struct Discovery<'a> { | |
id_hash: H256, | ||
secret: Secret, | ||
public_endpoint: NodeEndpoint, | ||
discovering: bool, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two bools look like their doing the same thing? Could we ever be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, much clearer now. Can't really think of a better name, maybe s/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds better, thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two booleans look like they're doing the same thing, i.e. could we ever be in a state where |
||
discovery_round: u16, | ||
discovery_id: NodeId, | ||
discovery_nodes: HashSet<NodeId>, | ||
|
@@ -141,6 +142,7 @@ impl<'a> Discovery<'a> { | |
id_hash: keccak(key.public()), | ||
secret: key.secret().clone(), | ||
public_endpoint: public, | ||
discovering: false, | ||
discovery_round: 0, | ||
discovery_id: NodeId::new(), | ||
discovery_nodes: HashSet::new(), | ||
|
@@ -224,11 +226,20 @@ impl<'a> Discovery<'a> { | |
/// Starts the discovery process at round 0 | ||
fn start(&mut self) { | ||
trace!(target: "discovery", "Starting discovery"); | ||
self.discovering = true; | ||
self.discovery_round = 0; | ||
self.discovery_id.randomize(); //TODO: use cryptographic nonce | ||
self.discovery_nodes.clear(); | ||
} | ||
|
||
/// Complete the discovery process | ||
fn stop(&mut self) { | ||
trace!(target: "discovery", "Completing discovery"); | ||
self.discovering = false; | ||
self.discovery_round = DISCOVERY_MAX_STEPS; | ||
self.discovery_nodes.clear(); | ||
} | ||
|
||
fn update_new_nodes(&mut self) { | ||
while self.in_flight_requests.len() < MAX_NODES_PING { | ||
match self.adding_nodes.pop() { | ||
|
@@ -239,8 +250,8 @@ impl<'a> Discovery<'a> { | |
} | ||
|
||
fn discover(&mut self) { | ||
self.update_new_nodes(); | ||
if self.discovery_round == DISCOVERY_MAX_STEPS { | ||
self.stop(); | ||
return; | ||
} | ||
trace!(target: "discovery", "Starting round {:?}", self.discovery_round); | ||
|
@@ -263,9 +274,7 @@ impl<'a> Discovery<'a> { | |
} | ||
|
||
if tried_count == 0 { | ||
trace!(target: "discovery", "Completing discovery"); | ||
self.discovery_round = DISCOVERY_MAX_STEPS; | ||
self.discovery_nodes.clear(); | ||
self.stop(); | ||
return; | ||
} | ||
self.discovery_round += 1; | ||
|
@@ -591,26 +600,24 @@ impl<'a> Discovery<'a> { | |
Ok(None) | ||
} | ||
|
||
fn check_expired(&mut self, time: Instant) -> HashSet<NodeId> { | ||
let mut removed: HashSet<NodeId> = HashSet::new(); | ||
fn check_expired(&mut self, time: Instant) { | ||
while let Some((node_id, sent_at)) = self.expiring_pings.pop_front() { | ||
if time.duration_since(sent_at) <= PING_TIMEOUT { | ||
self.expiring_pings.push_front((node_id, sent_at)); | ||
break; | ||
} | ||
self.expire_in_flight_request(node_id, sent_at, &mut removed); | ||
self.expire_in_flight_request(node_id, sent_at); | ||
} | ||
while let Some((node_id, sent_at)) = self.expiring_finds.pop_front() { | ||
if time.duration_since(sent_at) <= FIND_NODE_TIMEOUT { | ||
self.expiring_finds.push_front((node_id, sent_at)); | ||
break; | ||
} | ||
self.expire_in_flight_request(node_id, sent_at, &mut removed); | ||
self.expire_in_flight_request(node_id, sent_at); | ||
} | ||
removed | ||
ngotchac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
fn expire_in_flight_request(&mut self, node_id: NodeId, sent_at: Instant, removed: &mut HashSet<NodeId>) { | ||
fn expire_in_flight_request(&mut self, node_id: NodeId, sent_at: Instant) { | ||
if let Entry::Occupied(entry) = self.in_flight_requests.entry(node_id) { | ||
if entry.get().sent_at == sent_at { | ||
entry.remove(); | ||
|
@@ -631,7 +638,6 @@ impl<'a> Discovery<'a> { | |
&node.address, node.fail_count | ||
); | ||
} else { | ||
removed.insert(node_id); | ||
let node = bucket.nodes.remove(index).expect("index was located in if condition"); | ||
debug!(target: "discovery", "Removed expired node {:?}", &node.address); | ||
} | ||
|
@@ -640,16 +646,18 @@ impl<'a> Discovery<'a> { | |
} | ||
} | ||
|
||
pub fn round(&mut self) -> Option<TableUpdates> { | ||
let removed = self.check_expired(Instant::now()); | ||
self.discover(); | ||
if !removed.is_empty() { | ||
Some(TableUpdates { added: HashMap::new(), removed }) | ||
} else { None } | ||
pub fn round(&mut self) { | ||
self.check_expired(Instant::now()); | ||
self.update_new_nodes(); | ||
if self.discovering { | ||
self.discover(); | ||
} | ||
} | ||
|
||
pub fn refresh(&mut self) { | ||
self.start(); | ||
if !self.discovering { | ||
self.start(); | ||
} | ||
} | ||
|
||
pub fn any_sends_queued(&self) -> bool { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of a separate bool, I think making
discovery_round
anOption<u16>
is cleaner.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitly, thanks!