Skip to content

Commit

Permalink
fix for the sake of leanring
Browse files Browse the repository at this point in the history
  • Loading branch information
divagant-martian committed Jun 17, 2022
1 parent 3676a7e commit ee45d46
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ impl Service {
event = Service::bucket_maintenance_poll(&self.kbuckets) => {
self.send_event(event);
}
Some(event) = Service::bucket_maintenance_poll_topics(self.topics_kbuckets.iter_mut()) => {
Some(event) = Service::bucket_maintenance_poll_topics(KBuckets::Topic (self.topics_kbuckets.iter_mut())) => {
self.send_event(event);
}
query_event = Service::query_event_poll(&mut self.queries) => {
Expand Down Expand Up @@ -2308,34 +2308,13 @@ impl Service {

/// A future that maintains the topic kbuckets and inserts nodes when required. This returns the
/// `Discv5Event::NodeInsertedTopics` variants.
async fn bucket_maintenance_poll_topics(
kbuckets: KBuckets<'_, Box<dyn Iterator<Item = (&TopicHash, &mut KBucketsTable<NodeId, Enr>)> + '_>>,
async fn bucket_maintenance_poll_topics<
'a,
T: Iterator<Item = (&'a TopicHash, &'a mut KBucketsTable<NodeId, Enr>)>,
>(
kbuckets: KBuckets<'a, T>,
) -> Option<Discv5Event> {
// Drain applied pending entries from the routing table.
let mut update_kbuckets_futures = Vec::new();
for entry in kbuckets {
let (topic_hash, kbuckets) = match kbuckets {
KBuckets::Topic => (Some(entry.0), entry.1),
KBuckets::Local => (None, entry)
};
update_kbuckets_futures.push(future::poll_fn(move |_cx| {
if let Some(entry) = (*topic_kbuckets).take_applied_pending() {
let event = Discv5Event::NodeInsertedTopic {
node_id: entry.inserted.into_preimage(),
replaced: entry.evicted.map(|n| n.key.into_preimage()),
topic_hash: *topic_hash,
};
return Poll::Ready(event);
}
Poll::Pending
}));
}
if update_kbuckets_futures.is_empty() {
None
} else {
let (event, _, _) = select_all(update_kbuckets_futures).await;
Some(event)
}
None
}

/// A future the maintains active queries. This returns completed and timed out queries, as
Expand Down Expand Up @@ -2397,8 +2376,9 @@ enum ConnectionStatus {
}

/// The separate layers of kbuckets that exist.
enum KBuckets<'a, T: Iterator<Item = (&'a TopicHash, &'a mut KBucketsTable<NodeId, Enr>)>> {
enum KBuckets<'a, T: Iterator<Item = (&'a TopicHash, &'a mut KBucketsTable<NodeId, Enr>)>> {
/// One set of local kbuckets exist, this is the local routing table.
Local(Arc<RwLock<KBucketsTable<NodeId, Enr>>>),
Local(&'a Arc<RwLock<KBucketsTable<NodeId, Enr>>>),
Topic(T),
}
}

0 comments on commit ee45d46

Please sign in to comment.