pub fn poll(&mut self, cx: &mut Context) -> Poll<DiscoveryEvent> {
        if !self.started {
            return Poll::Pending;
        }

        // Process the query queue
        self.process_queue();

        // Drive the queries and return any results from completed queries
        if let Some((min_ttl, result)) = self.poll_queries(cx) {
            // cache the found ENR's
            for enr in result.iter().cloned() {
                self.cached_enrs.put(enr.peer_id(), enr);
            }
            // return the result to the peer manager
            return Poll::Ready(DiscoveryEvent::QueryResult(min_ttl, result));
        }

        // Process the server event stream
        match self.event_stream {
            EventStream::Awaiting(ref mut fut) => {
                // Still awaiting the event stream, poll it
                if let Poll::Ready(event_stream) = fut.poll_unpin(cx) {
                    match event_stream {
                        Ok(stream) => self.event_stream = EventStream::Present(stream),
                        Err(e) => {
                            slog::crit!(self.log, "Discv5 event stream failed"; "error" => e.to_string());
                            self.event_stream = EventStream::InActive;
                        }
                    }
                }
            }
            EventStream::InActive => {} // ignore checking the stream
            EventStream::Present(ref mut stream) => {
                while let Ok(event) = stream.try_recv() {
                    match event {
                        // We filter out unwanted discv5 events here and only propagate useful results to
                        // the peer manager.
                        Discv5Event::Discovered(_enr) => {
                            // Peers that get discovered during a query but are not contactable or
                            // don't match a predicate can end up here. For debugging purposes we
                            // log these to see if we are unnecessarily dropping discovered peers
                            /*
                            if enr.eth2() == self.local_enr().eth2() {
                                trace!(self.log, "Peer found in process of query"; "peer_id" => format!("{}", enr.peer_id()), "tcp_socket" => enr.tcp_socket());
                            } else {
                                // this is temporary warning for debugging the DHT
                                warn!(self.log, "Found peer during discovery not on correct fork"; "peer_id" => format!("{}", enr.peer_id()), "tcp_socket" => enr.tcp_socket());
                            }
                            */
                        }
                        Discv5Event::SocketUpdated(socket) => {
                            info!(self.log, "Address updated"; "ip" => format!("{}",socket.ip()), "udp_port" => format!("{}", socket.port()));
                            // Discv5 will have updated our local ENR. We save the updated version
                            // to disk.
                            let enr = self.discv5.local_enr();
                            enr::save_enr_to_disk(Path::new(&self.enr_dir), &enr, &self.log);
                            return Poll::Ready(DiscoveryEvent::SocketUpdated(socket));
                        }
                        _ => {} // Ignore all other discv5 server events
                    }
                }
            }
        }
        Poll::Pending
    }
// Main execution loop to be driven by the peer manager.
Main execution loop to be driven by the peer manager.
Main execution loop to be driven by the peer manager .
pub fn poll(&mut self, cx: &mut Context) -> Poll<DiscoveryEvent> {
        if !self.started {
            return Poll::Pending;
        }
        // Process the query queue
        self.process_queue();
        // Drive the queries and return any results from completed queries
        if let Some((min_ttl, result)) = self.poll_queries(cx) {
            // cache the found ENR's
            for enr in result.iter().cloned() {
                self.cached_enrs.put(enr.peer_id(), enr);
            }
            // return the result to the peer manager
            return Poll::Ready(DiscoveryEvent::QueryResult(min_ttl, result));
        }
        // Process the server event stream
        match self.event_stream {
            EventStream::Awaiting(ref mut fut) => {
                // Still awaiting the event stream, poll it
                if let Poll::Ready(event_stream) = fut.poll_unpin(cx) {
                    match event_stream {
                        Ok(stream) => self.event_stream = EventStream::Present(stream),
                        Err(e) => {
                            slog::crit!(self.log, "Discv5 event stream failed"; "error" => e.to_string());
                            self.event_stream = EventStream::InActive;
                        }
                    }
                }
            }
            EventStream::InActive => {} // ignore checking the stream
            EventStream::Present(ref mut stream) => {
                while let Ok(event) = stream.try_recv() {
                    match event {
                        // We filter out unwanted discv5 events here and only propagate useful results to
                        // the peer manager.
                        Discv5Event::Discovered(_enr) => {
                            // Peers that get discovered during a query but are not contactable or
                            // don't match a predicate can end up here. For debugging purposes we
                            // log these to see if we are unnecessarily dropping discovered peers
                            /*
                            if enr.eth2() == self.local_enr().eth2() {
                                trace!(self.log, "Peer found in process of query"; "peer_id" => format!("{}", enr.peer_id()), "tcp_socket" => enr.tcp_socket());
                            } else {
                                // this is temporary warning for debugging the DHT
                                warn!(self.log, "Found peer during discovery not on correct fork"; "peer_id" => format!("{}", enr.peer_id()), "tcp_socket" => enr.tcp_socket());
                            }
                            */
                        }
                        Discv5Event::SocketUpdated(socket) => {
                            info!(self.log, "Address updated"; "ip" => format!("{}",socket.ip()), "udp_port" => format!("{}", socket.port()));
                            // Discv5 will have updated our local ENR. We save the updated version
                            // to disk.
                            let enr = self.discv5.local_enr();
                            enr::save_enr_to_disk(Path::new(&self.enr_dir), &enr, &self.log);
                            return Poll::Ready(DiscoveryEvent::SocketUpdated(socket));
                        }
                        _ => {} // Ignore all other discv5 server events
                    }
                }
            }
        }
        Poll::Pending
    }
pub fn poll ( & mut self , cx : & mut Context ) -> Poll < DiscoveryEvent > { if ! self . started { return Poll :: Pending ; } // Process the query queue self . process_queue ( ) ; // Drive the queries and return any results from completed queries if let Some ( ( min_ttl , result ) ) = self . poll_queries ( cx ) { // cache the found ENR's for enr in result . iter ( ) . cloned ( ) { self . cached_enrs . put ( enr . peer_id ( ) , enr ) ; } // return the result to the peer manager return Poll :: Ready ( DiscoveryEvent :: QueryResult ( min_ttl , result ) ) ; } // Process the server event stream match self . event_stream { EventStream :: Awaiting ( ref mut fut ) => { // Still awaiting the event stream, poll it if let Poll :: Ready ( event_stream ) = fut . poll_unpin ( cx ) { match event_stream { Ok ( stream ) => self . event_stream = EventStream :: Present ( stream ) , Err ( e ) => { slog :: crit ! ( self log " " " " e to_string ( ) ) ; self . event_stream = EventStream :: InActive ; } } } } EventStream :: InActive => { } // ignore checking the stream EventStream :: Present ( ref mut stream ) => { while let Ok ( event ) = stream . try_recv ( ) { match event { // We filter out unwanted discv5 events here and only propagate useful results to // the peer manager. Discv5Event :: Discovered ( _enr ) => { // Peers that get discovered during a query but are not contactable or // don't match a predicate can end up here. For debugging purposes we // log these to see if we are unnecessarily dropping discovered peers /*
                            if enr.eth2() == self.local_enr().eth2() {
                                trace!(self.log, "Peer found in process of query"; "peer_id" => format!("{}", enr.peer_id()), "tcp_socket" => enr.tcp_socket());
                            } else {
                                // this is temporary warning for debugging the DHT
                                warn!(self.log, "Found peer during discovery not on correct fork"; "peer_id" => format!("{}", enr.peer_id()), "tcp_socket" => enr.tcp_socket());
                            }
                            */ } Discv5Event :: SocketUpdated ( socket ) => { info ! ( self log " " " " format ( " " socket ip ( ) ) " " format ( " " socket port ( ) ) ) ; // Discv5 will have updated our local ENR. We save the updated version // to disk. let enr = self . discv5 . local_enr ( ) ; enr :: save_enr_to_disk ( Path :: new ( & self . enr_dir ) , & enr , & self . log ) ; return Poll :: Ready ( DiscoveryEvent :: SocketUpdated ( socket ) ) ; } _ => { } // Ignore all other discv5 server events } } } } Poll :: Pending }
Main execution loop to be driven by the peer manager.
Main execution loop to be driven by the peer manager .
&mut self
cx
&mut Context
&mut self
cx
&mut Context